@Override public Object wrap(final Req req, final HandlerInvocation invocation) throws Exception { TokenAuthData auth = HttpUtils.getAuth(req); String username = auth != null ? auth.user : null; if (U.isEmpty(username)) { HttpUtils.clearUserData(req); } Set<String> roles = userRoles(req, username); Set<String> scope = auth != null ? auth.scope : null; if (U.notEmpty(requiredRoles) && !Secure.hasAnyRole(username, roles, requiredRoles)) { throw new SecurityException("The user doesn't have the required roles!"); } Ctx ctx = Ctxs.required(); ctx.setUser(new UserInfo(username, roles, scope)); return invocation.invoke(); }
public UserInfo(String username, Set<String> roles, Set<String> scope) { this.username = username; this.roles = Collections.unmodifiableSet(U.safe(roles)); this.is = rolesMap(roles); this.scope = Collections.unmodifiableSet(U.safe(scope)); }
@Override public Object handle(HttpExchange x) { x.accessDeniedIf(!Conf.dev()); String username = x.param("user"); U.must(username.matches("\\w+")); username += "@debug"; String email = username; String name = U.capitalized(username); UserInfo user = new UserInfo(username, email, name); Ctxs.ctx().setUser(user); user.saveTo(x.cookiepack()); throw x.goBack(0); } });
public synchronized UserInfo user() { return cookiepack != null ? UserInfo.from(cookiepack()) : null; }
public static UserInfo login(Req req, String username, String password) { LoginProvider loginProvider = Customization.of(req).loginProvider(); U.must(loginProvider != null, "A login provider wasn't set!"); RolesProvider rolesProvider = Customization.of(req).rolesProvider(); U.must(rolesProvider != null, "A roles provider wasn't set!"); try { boolean success = loginProvider.login(req, username, password); if (success) { Set<String> roles = rolesProvider.getRolesForUser(req, username); return new UserInfo(username, roles); } } catch (Throwable e) { throw U.rte("Login error!", e); } return null; }
public UserInfo(String username, String email, String name, String oauthId, String oauthProvider, Set<String> roles) { this.username = username; this.email = email; this.name = name; this.oauthId = oauthId; this.oauthProvider = oauthProvider; this.roles = roles; this.is = rolesMap(roles); }
Set<String> roles = customization.rolesProvider().getRolesForUser(req, username); UserInfo user = new UserInfo(username, roles); user.name = name; user.email = email;
public static UserInfo from(Map<String, ?> scope) { String username = (String) scope.get(USERNAME); String email = (String) scope.get(EMAIL); String name = (String) scope.get(NAME); return username != null ? new UserInfo(username, email, name) : ANONYMOUS; }
@Override public Object wrap(final Req req, final HandlerInvocation invocation) throws Exception { TokenAuthData auth = HttpUtils.getAuth(req); String username = auth != null ? auth.user : null; if (U.isEmpty(username)) { HttpUtils.clearUserData(req); } Set<String> roles = userRoles(req, username); Set<String> scope = auth != null ? auth.scope : null; if (U.notEmpty(requiredRoles) && !Secure.hasAnyRole(username, roles, requiredRoles)) { throw new SecurityException("The user doesn't have the required roles!"); } Ctx ctx = Ctxs.required(); ctx.setUser(new UserInfo(username, roles, scope)); return invocation.invoke(); }
@Override public boolean login(String username, String password) { LoginProvider loginProvider = Customization.of(req).loginProvider(); U.must(loginProvider != null, "A login provider wasn't set!"); RolesProvider rolesProvider = Customization.of(req).rolesProvider(); U.must(rolesProvider != null, "A roles provider wasn't set!"); req.tokenChanged.set(true); boolean success; try { success = loginProvider.login(req, username, password); if (success) { Set<String> roles = rolesProvider.getRolesForUser(req, username); long ttl = Conf.TOKEN.entry("ttl").or(0); long expiresOn = ttl > 0 ? U.time() + ttl : Long.MAX_VALUE; UserInfo user = new UserInfo(username, roles, null); Ctxs.required().setUser(user); request().token().put(Tokens._USER, username); request().token().put(Tokens._EXPIRES, expiresOn); } } catch (Throwable e) { throw U.rte("Login error!", e); } return success; }
public static synchronized <T> T executeInCtx(CtxData cd, Callable<T> action) { Ctx ctx = Ctxs.open("call"); ctx.setApp(cd.app()); ctx.setExchange(null); ctx.setUser(new UserInfo(cd.username(), cd.roles())); U.assign(ctx.extras(), cd.extras()); try { return Lmbd.call(action); } finally { Ctxs.close(); } }