Form params = getQuery();
String sessionId = getCookies().getFirstValue(ClientCookieID);
getLogger().info("sessionId = " + sessionId);
ConcurrentMap<String, Object> attribs = getContext().getAttributes();
AuthSession session = (sessionId == null) ? null
: (AuthSession) attribs.get(sessionId);
// check owner:
String scopeOwner = null;
if (getRequest().getClientInfo().getUser() != null)
scopeOwner = getRequest().getClientInfo().getUser().getIdentifier();
if (scopeOwner == null && session != null)
scopeOwner = session.getScopeOwner();
getLogger().info("OWNER - " + scopeOwner);
if (scopeOwner == null) {
sendError(sessionId, OAuthError.INVALID_REQUEST,
params.getFirstValue(STATE), "No Scope Owner", null);
return getResponseEntity();
}
// check clientId:
String clientId = params.getFirstValue(CLIENT_ID);
if (clientId == null || clientId.length() < 1) {
sendError(sessionId, OAuthError.INVALID_REQUEST,
params.getFirstValue(STATE),
"No client_id parameter found.", null);
getLogger().info("Could not find client ID");
return getResponseEntity();
}
Client client = clients.findById(clientId);
getLogger().info("Client = " + client);
if (client == null) {
// client = clients.createClient(clientId, redirUri);
sendError(sessionId, OAuthError.INVALID_CLIENT,
params.getFirstValue(STATE),
"Need to register the client : " + clientId, null);
getLogger().info("Need to register the client : " + clientId);
return getResponseEntity();
}
getLogger().info("CLIENT ID - " + clientId);
// check redir:
String redirUri = params.getFirstValue(REDIR_URI);
if (redirUri == null || redirUri.length() == 0) {
sendError(sessionId, OAuthError.INVALID_REQUEST,
params.getFirstValue(STATE),
"No redirect_uri parameter found.", null);
getLogger().info("No mandatory redirect URI provided");
return getResponseEntity();
}
if (!redirUri.startsWith(client.getRedirectUri())) {
sendError(sessionId, OAuthError.REDIRECT_URI_MISMATCH,
params.getFirstValue(STATE),
"Callback URI does not match.", null);
getLogger().info("Callback URI does not match.");
return getResponseEntity();
}
getLogger().info("CLIENT ID - " + clientId);
// check response type:
String typeString = params.getFirstValue(RESPONSE_TYPE);
ResponseType type = null;
try {
type = Enum.valueOf(ResponseType.class, typeString);
getLogger().info("Found flow - " + type);
if (!Method.GET.equals(getMethod()))
setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED);
} catch (IllegalArgumentException iae) {
sendError(sessionId, OAuthError.UNSUPPORTED_RESPONSE_TYPE,
params.getFirstValue(STATE), "Unsupported flow", null);
getLogger().log(Level.WARNING, "Error in execution.", iae);
} catch (NullPointerException npe) {
sendError(sessionId, OAuthError.INVALID_REQUEST,
params.getFirstValue(STATE),
"No response_type parameter found.", null);
}
getLogger().info("RESPONSE TYPE - " + type);
// setup session if needed:
if (session != null)
getLogger().info("client = " + session.getClient());
else { // cleanup old cookie...and setup session
getCookieSettings().removeAll(ClientCookieID);
}
if (session == null) {
getLogger().info("Setting ClientCookieID");
session = new AuthSession(getContext().getAttributes(),
new ScheduledThreadPoolExecutor(5));
CookieSetting cs = new CookieSetting(ClientCookieID,
session.getId());
// TODO create a secure mode setting, update all cookies
// cs.setAccessRestricted(true);
// cs.setSecure(true);
getCookieSettings().add(cs);
getLogger().info("Setting cookie - " + session.getId());
}
setupSession(session, client, type, redirUri, params);
session.setScopeOwner(scopeOwner);
return doPostAuthenticate(session, client);
}