* @throws AuthenticationException
* @throws Base64DecoderException
*/
public AuthenticationToken autoPilot(HttpServletRequest req, HttpServletResponse resp, boolean passive)
throws IOException, AuthenticationException, GeneralSecurityException, Base64DecoderException {
UserService userService = UserServiceFactory.getUserService();
if (userService.getCurrentUser() != null) {
String userEmail = userService.getCurrentUser().getEmail();
AuthenticationToken authToken = store.getUserToken(userEmail);
//check token age and clear if token is too old
if (authToken != null && authToken.isExpired()) {
try {
AuthSubUtil.revokeToken(authToken.getToken(), key);
} catch (Exception x) { }
store.clearUserToken(authToken.getEmail());
authToken = null;
}
//check token validity to ensure that the token is still valid
if (authToken != null) {
try {
Map<String, String> info = AuthSubUtil.getTokenInfo(authToken.getToken(), key);
if (info == null || info.size() == 0) {
store.clearUserToken(authToken.getEmail());
authToken = null;
}
} catch (Exception x) {
store.clearUserToken(authToken.getEmail());
authToken = null;
}
}
if (authToken != null) {
authToken.setActivity(new Date());
return authToken;
} else {
String token = null, qs = req.getQueryString();
if (qs != null) {
token = AuthSubUtil.getTokenFromReply(qs);
}
if (token != null && !token.equals("")) {
token = URLDecoder.decode(token, "UTF-8");
token = AuthSubUtil.exchangeForSessionToken(token, key);
store.setUserToken(req.getUserPrincipal().getName(), token);
resp.sendRedirect(getFullUrl(req));
} else {
if (!passive) {
String authUrl = AuthSubUtil.getRequestUrl(
getFullUrl(req),
DocumentServiceImpl.AUTH_SCOPES, true, true);
resp.sendRedirect(authUrl);
}
}
}
} else {
if (!passive) {
String authUrl = userService.createLoginURL(getFullUrl(req));
resp.sendRedirect(authUrl);
}
}
return null;
}