Skip to content

Instantly share code, notes, and snippets.

@jeremychone
Last active December 20, 2015 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremychone/6050084 to your computer and use it in GitHub Desktop.
Save jeremychone/6050084 to your computer and use it in GitHub Desktop.
@Singleton
public class AppAuthRequest implements AuthRequest<User>{
@Inject
private UserDao userDao;
@Override
public AuthToken<User> authRequest(RequestContext rc) {
AuthToken<User> authToken = null;
//TODO: here get the various user tokens (here just get the username)
String username = rc.getCookie("username");
WebRequestType wrt = rc.getWebRequestType();
switch(wrt){
// we always allow any static files and generated assets
case GENERATED_ASSET:
case STATIC_FILE:
break;
// We authenticate for template and rest calls
case WEB_TEMPLATE:
case WEB_REST:
if (!Strings.isNullOrEmpty(username)){
User user = userDao.getByUsername(username);
//TODO: here do token validation. Right now, just passthrough
if (user != null){
authToken = new AuthToken<User>();
authToken.setUser(user);
return authToken;
}
}
break;
default:
break;
}
// We block "unauthenticated" calls for any rest API calls except login
if (authToken == null && wrt == WebRequestType.WEB_REST && !"/login".equals(rc.getResourcePath()) ){
throw new AbortWithHttpStatusException(HttpStatus.FORBIDDEN);
}
return authToken;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment