Skip to content
This repository was archived by the owner on Dec 12, 2018. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ protected Application getApplication(HttpServletRequest request) {
/**
* @since 1.0.RC8.3
*/
private AccessTokenResult tokenAuthenticationRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
protected AccessTokenResult tokenAuthenticationRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

OAuthGrantRequestAuthenticationResult authenticationResult;

Expand All @@ -224,7 +224,7 @@ private AccessTokenResult tokenAuthenticationRequest(HttpServletRequest request,
/**
* @since 1.0.RC8.3
*/
private AccessTokenResult refreshTokenAuthenticationRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
protected AccessTokenResult refreshTokenAuthenticationRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

OAuthGrantRequestAuthenticationResult authenticationResult;

Expand All @@ -246,7 +246,7 @@ private AccessTokenResult refreshTokenAuthenticationRequest(HttpServletRequest r
/**
* @since 1.0.0
*/
private AccessTokenResult clientCredentialsAuthenticationRequest(HttpServletRequest request, HttpServletResponse response) {
protected AccessTokenResult clientCredentialsAuthenticationRequest(HttpServletRequest request, HttpServletResponse response) {
OAuthGrantRequestAuthenticationResult authenticationResult;

try {
Expand All @@ -273,7 +273,7 @@ private AccessTokenResult clientCredentialsAuthenticationRequest(HttpServletRequ
/**
* @since 1.1.0
*/
private AccessTokenResult stormpathSocialAuthenticationRequest(HttpServletRequest request, HttpServletResponse response) {
protected AccessTokenResult stormpathSocialAuthenticationRequest(HttpServletRequest request, HttpServletResponse response) {
OAuthGrantRequestAuthenticationResult authenticationResult;

try {
Expand Down Expand Up @@ -310,7 +310,7 @@ private OAuthException convertToOAuthException(ResourceException e, OAuthErrorCo
return new OAuthException(oauthError, message);
}

private AccessTokenResult stormpathTokenAuthenticationRequest(HttpServletRequest request, HttpServletResponse response) {
protected AccessTokenResult stormpathTokenAuthenticationRequest(HttpServletRequest request, HttpServletResponse response) {
OAuthGrantRequestAuthenticationResult authenticationResult;

try {
Expand Down Expand Up @@ -338,7 +338,6 @@ protected ViewModel doPost(HttpServletRequest request, HttpServletResponse respo

String json;

AuthenticationRequest authcRequest = null;
AccessTokenResult result;

try {
Expand All @@ -355,48 +354,15 @@ protected ViewModel doPost(HttpServletRequest request, HttpServletResponse respo

grantTypeValidator.validate(grantType);

switch (grantType) {
case PASSWORD_GRANT_TYPE:
result = this.tokenAuthenticationRequest(request, response);
break;
case REFRESH_TOKEN_GRANT_TYPE:
result = this.refreshTokenAuthenticationRequest(request, response);
break;
case CLIENT_CREDENTIALS_GRANT_TYPE:
try {
result = this.clientCredentialsAuthenticationRequest(request, response);
} catch (HttpAuthenticationException e) {
log.warn("Unable to authenticate client", e);
throw new OAuthException(OAuthErrorCode.INVALID_CLIENT);
}
break;
case STORMPATH_SOCIAL_GRANT_TYPE:
try {
result = this.stormpathSocialAuthenticationRequest(request, response);
} catch (HttpAuthenticationException e) {
log.warn("Unable to authenticate client", e);
throw new OAuthException(OAuthErrorCode.INVALID_CLIENT);
}
break;
case STORMPATH_TOKEN_GRANT_TYPE:
try {
result = this.stormpathTokenAuthenticationRequest(request, response);
} catch (HttpAuthenticationException ex) {
log.warn("Unable to authenticate client", ex);
throw new OAuthException(OAuthErrorCode.INVALID_CLIENT);
}
break;
default:
throw new OAuthException(OAuthErrorCode.UNSUPPORTED_GRANT_TYPE, "'" + grantType + "' is an unsupported grant type.");
}
result = getAccessTokenResult(grantType, request, response);

saveResult(request, response, result);

json = result.getTokenResponse().toJson();

response.setStatus(HttpServletResponse.SC_OK);

SuccessfulAuthenticationRequestEvent e = createSuccessEvent(request, response, authcRequest, result);
SuccessfulAuthenticationRequestEvent e = createSuccessEvent(request, response, null, result);
publish(e);

} catch (OAuthException e) {
Expand All @@ -413,7 +379,7 @@ protected ViewModel doPost(HttpServletRequest request, HttpServletResponse respo

try {
FailedAuthenticationRequestEvent evt =
new DefaultFailedAuthenticationRequestEvent(request, response, authcRequest, e);
new DefaultFailedAuthenticationRequestEvent(request, response, null, e);
publish(evt);
} catch (Throwable t) {
log.warn(
Expand All @@ -434,6 +400,51 @@ protected ViewModel doPost(HttpServletRequest request, HttpServletResponse respo
return null;
}

/**
* Get the AccessTokenResult given the specified grantType. The request authorization and support for the grantType
* will already have been validated.
*
* @since 1.3.0
*/
protected AccessTokenResult getAccessTokenResult(String grantType, HttpServletRequest request, HttpServletResponse response) throws Exception {
AccessTokenResult result;
switch (grantType) {
case PASSWORD_GRANT_TYPE:
result = this.tokenAuthenticationRequest(request, response);
break;
case REFRESH_TOKEN_GRANT_TYPE:
result = this.refreshTokenAuthenticationRequest(request, response);
break;
case CLIENT_CREDENTIALS_GRANT_TYPE:
try {
result = this.clientCredentialsAuthenticationRequest(request, response);
} catch (HttpAuthenticationException e) {
log.warn("Unable to authenticate client", e);
throw new OAuthException(OAuthErrorCode.INVALID_CLIENT);
}
break;
case STORMPATH_SOCIAL_GRANT_TYPE:
try {
result = this.stormpathSocialAuthenticationRequest(request, response);
} catch (HttpAuthenticationException e) {
log.warn("Unable to authenticate client", e);
throw new OAuthException(OAuthErrorCode.INVALID_CLIENT);
}
break;
case STORMPATH_TOKEN_GRANT_TYPE:
try {
result = this.stormpathTokenAuthenticationRequest(request, response);
} catch (HttpAuthenticationException ex) {
log.warn("Unable to authenticate client", ex);
throw new OAuthException(OAuthErrorCode.INVALID_CLIENT);
}
break;
default:
throw new OAuthException(OAuthErrorCode.UNSUPPORTED_GRANT_TYPE, "'" + grantType + "' is an unsupported grant type.");
}
return result;
}

protected SuccessfulAuthenticationRequestEvent createSuccessEvent(HttpServletRequest request,
HttpServletResponse response,
AuthenticationRequest authcRequest,
Expand Down