From 8ffddb80fa5a602a9928b6c8f4f215067c72d16e Mon Sep 17 00:00:00 2001 From: Jason Erickson Date: Thu, 5 Jan 2017 12:13:57 -0800 Subject: [PATCH 1/4] stormpath-sdk-java-1190 Refactor AccessTokenController to allow easier extension --- .../servlet/mvc/AccessTokenController.java | 95 +++++++++++-------- 1 file changed, 53 insertions(+), 42 deletions(-) diff --git a/extensions/servlet/src/main/java/com/stormpath/sdk/servlet/mvc/AccessTokenController.java b/extensions/servlet/src/main/java/com/stormpath/sdk/servlet/mvc/AccessTokenController.java index 4b2454f1ad..ce08b2c35b 100644 --- a/extensions/servlet/src/main/java/com/stormpath/sdk/servlet/mvc/AccessTokenController.java +++ b/extensions/servlet/src/main/java/com/stormpath/sdk/servlet/mvc/AccessTokenController.java @@ -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; @@ -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; @@ -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 { @@ -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 { @@ -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 { @@ -338,7 +338,6 @@ protected ViewModel doPost(HttpServletRequest request, HttpServletResponse respo String json; - AuthenticationRequest authcRequest = null; AccessTokenResult result; try { @@ -355,40 +354,7 @@ 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); @@ -396,7 +362,7 @@ protected ViewModel doPost(HttpServletRequest request, HttpServletResponse respo response.setStatus(HttpServletResponse.SC_OK); - SuccessfulAuthenticationRequestEvent e = createSuccessEvent(request, response, authcRequest, result); + SuccessfulAuthenticationRequestEvent e = createSuccessEvent(request, response, null, result); publish(e); } catch (OAuthException e) { @@ -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( @@ -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, From bb02d8daf9d7e84f1020e438876a79dcf6e0f941 Mon Sep 17 00:00:00 2001 From: Jason Erickson Date: Thu, 5 Jan 2017 14:23:53 -0800 Subject: [PATCH 2/4] Revert: stormpath-sdk-java-1190 Refactor AccessTokenController to allow easier extension --- .../servlet/mvc/AccessTokenController.java | 95 ++++++++----------- 1 file changed, 42 insertions(+), 53 deletions(-) diff --git a/extensions/servlet/src/main/java/com/stormpath/sdk/servlet/mvc/AccessTokenController.java b/extensions/servlet/src/main/java/com/stormpath/sdk/servlet/mvc/AccessTokenController.java index ce08b2c35b..4b2454f1ad 100644 --- a/extensions/servlet/src/main/java/com/stormpath/sdk/servlet/mvc/AccessTokenController.java +++ b/extensions/servlet/src/main/java/com/stormpath/sdk/servlet/mvc/AccessTokenController.java @@ -203,7 +203,7 @@ protected Application getApplication(HttpServletRequest request) { /** * @since 1.0.RC8.3 */ - protected AccessTokenResult tokenAuthenticationRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { + private AccessTokenResult tokenAuthenticationRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { OAuthGrantRequestAuthenticationResult authenticationResult; @@ -224,7 +224,7 @@ protected AccessTokenResult tokenAuthenticationRequest(HttpServletRequest reques /** * @since 1.0.RC8.3 */ - protected AccessTokenResult refreshTokenAuthenticationRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { + private AccessTokenResult refreshTokenAuthenticationRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { OAuthGrantRequestAuthenticationResult authenticationResult; @@ -246,7 +246,7 @@ protected AccessTokenResult refreshTokenAuthenticationRequest(HttpServletRequest /** * @since 1.0.0 */ - protected AccessTokenResult clientCredentialsAuthenticationRequest(HttpServletRequest request, HttpServletResponse response) { + private AccessTokenResult clientCredentialsAuthenticationRequest(HttpServletRequest request, HttpServletResponse response) { OAuthGrantRequestAuthenticationResult authenticationResult; try { @@ -273,7 +273,7 @@ protected AccessTokenResult clientCredentialsAuthenticationRequest(HttpServletRe /** * @since 1.1.0 */ - protected AccessTokenResult stormpathSocialAuthenticationRequest(HttpServletRequest request, HttpServletResponse response) { + private AccessTokenResult stormpathSocialAuthenticationRequest(HttpServletRequest request, HttpServletResponse response) { OAuthGrantRequestAuthenticationResult authenticationResult; try { @@ -310,7 +310,7 @@ private OAuthException convertToOAuthException(ResourceException e, OAuthErrorCo return new OAuthException(oauthError, message); } - protected AccessTokenResult stormpathTokenAuthenticationRequest(HttpServletRequest request, HttpServletResponse response) { + private AccessTokenResult stormpathTokenAuthenticationRequest(HttpServletRequest request, HttpServletResponse response) { OAuthGrantRequestAuthenticationResult authenticationResult; try { @@ -338,6 +338,7 @@ protected ViewModel doPost(HttpServletRequest request, HttpServletResponse respo String json; + AuthenticationRequest authcRequest = null; AccessTokenResult result; try { @@ -354,7 +355,40 @@ protected ViewModel doPost(HttpServletRequest request, HttpServletResponse respo grantTypeValidator.validate(grantType); - result = getAccessTokenResult(grantType, request, response); + 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."); + } saveResult(request, response, result); @@ -362,7 +396,7 @@ protected ViewModel doPost(HttpServletRequest request, HttpServletResponse respo response.setStatus(HttpServletResponse.SC_OK); - SuccessfulAuthenticationRequestEvent e = createSuccessEvent(request, response, null, result); + SuccessfulAuthenticationRequestEvent e = createSuccessEvent(request, response, authcRequest, result); publish(e); } catch (OAuthException e) { @@ -379,7 +413,7 @@ protected ViewModel doPost(HttpServletRequest request, HttpServletResponse respo try { FailedAuthenticationRequestEvent evt = - new DefaultFailedAuthenticationRequestEvent(request, response, null, e); + new DefaultFailedAuthenticationRequestEvent(request, response, authcRequest, e); publish(evt); } catch (Throwable t) { log.warn( @@ -400,51 +434,6 @@ 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, From 3943d147b2dfa4a802de00b01c85333d5fa299ff Mon Sep 17 00:00:00 2001 From: Jason Erickson Date: Thu, 5 Jan 2017 15:17:05 -0800 Subject: [PATCH 3/4] stormpath-sdk-java-1190 Refactor AccessTokenController to allow easier extension - debug printing to figure out problem in Travis CI --- .../sdk/impl/saml/RegisteredSamlServiceProviderIT.groovy | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/extensions/httpclient/src/test/groovy/com/stormpath/sdk/impl/saml/RegisteredSamlServiceProviderIT.groovy b/extensions/httpclient/src/test/groovy/com/stormpath/sdk/impl/saml/RegisteredSamlServiceProviderIT.groovy index 8a61e2b6f7..aa6a6d5c68 100644 --- a/extensions/httpclient/src/test/groovy/com/stormpath/sdk/impl/saml/RegisteredSamlServiceProviderIT.groovy +++ b/extensions/httpclient/src/test/groovy/com/stormpath/sdk/impl/saml/RegisteredSamlServiceProviderIT.groovy @@ -440,7 +440,8 @@ class RegisteredSamlServiceProviderIT extends AbstractSamlIT { def registration = client.instantiate(SamlServiceProviderRegistration) registration.setIdentityProvider(identityProvider).setServiceProvider(serviceProvider) createAndGetAndAssertNewRegistration(registration) - + // TODO Remove println + println("created registration: ${registration}") serviceProvider.delete() getRegisteredSAMLServiceProviderError(serviceProvider) @@ -450,6 +451,10 @@ class RegisteredSamlServiceProviderIT extends AbstractSamlIT { assertEquals(samlServiceProviderRegistrations.href, identityProvider.href + "/samlServiceProviderRegistrations") assertEquals(samlServiceProviderRegistrations.offset, 0) + // TODO Remove - debug only + if (samlServiceProviderRegistrations.size > 0) { + println("found registrations: ${samlServiceProviderRegistrations}") + } assertEquals(samlServiceProviderRegistrations.size, 0) assertEquals(samlServiceProviderRegistrations.limit, 25) From 3e81f23294ece7f225d70101e3863e97f21aa842 Mon Sep 17 00:00:00 2001 From: Jason Erickson Date: Thu, 5 Jan 2017 17:15:59 -0800 Subject: [PATCH 4/4] stormpath-sdk-java-1190 Refactor AccessTokenController to allow easier extension --- .../RegisteredSamlServiceProviderIT.groovy | 7 +- .../servlet/mvc/AccessTokenController.java | 95 +++++++++++-------- 2 files changed, 54 insertions(+), 48 deletions(-) diff --git a/extensions/httpclient/src/test/groovy/com/stormpath/sdk/impl/saml/RegisteredSamlServiceProviderIT.groovy b/extensions/httpclient/src/test/groovy/com/stormpath/sdk/impl/saml/RegisteredSamlServiceProviderIT.groovy index aa6a6d5c68..8a61e2b6f7 100644 --- a/extensions/httpclient/src/test/groovy/com/stormpath/sdk/impl/saml/RegisteredSamlServiceProviderIT.groovy +++ b/extensions/httpclient/src/test/groovy/com/stormpath/sdk/impl/saml/RegisteredSamlServiceProviderIT.groovy @@ -440,8 +440,7 @@ class RegisteredSamlServiceProviderIT extends AbstractSamlIT { def registration = client.instantiate(SamlServiceProviderRegistration) registration.setIdentityProvider(identityProvider).setServiceProvider(serviceProvider) createAndGetAndAssertNewRegistration(registration) - // TODO Remove println - println("created registration: ${registration}") + serviceProvider.delete() getRegisteredSAMLServiceProviderError(serviceProvider) @@ -451,10 +450,6 @@ class RegisteredSamlServiceProviderIT extends AbstractSamlIT { assertEquals(samlServiceProviderRegistrations.href, identityProvider.href + "/samlServiceProviderRegistrations") assertEquals(samlServiceProviderRegistrations.offset, 0) - // TODO Remove - debug only - if (samlServiceProviderRegistrations.size > 0) { - println("found registrations: ${samlServiceProviderRegistrations}") - } assertEquals(samlServiceProviderRegistrations.size, 0) assertEquals(samlServiceProviderRegistrations.limit, 25) diff --git a/extensions/servlet/src/main/java/com/stormpath/sdk/servlet/mvc/AccessTokenController.java b/extensions/servlet/src/main/java/com/stormpath/sdk/servlet/mvc/AccessTokenController.java index 4b2454f1ad..ce08b2c35b 100644 --- a/extensions/servlet/src/main/java/com/stormpath/sdk/servlet/mvc/AccessTokenController.java +++ b/extensions/servlet/src/main/java/com/stormpath/sdk/servlet/mvc/AccessTokenController.java @@ -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; @@ -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; @@ -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 { @@ -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 { @@ -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 { @@ -338,7 +338,6 @@ protected ViewModel doPost(HttpServletRequest request, HttpServletResponse respo String json; - AuthenticationRequest authcRequest = null; AccessTokenResult result; try { @@ -355,40 +354,7 @@ 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); @@ -396,7 +362,7 @@ protected ViewModel doPost(HttpServletRequest request, HttpServletResponse respo response.setStatus(HttpServletResponse.SC_OK); - SuccessfulAuthenticationRequestEvent e = createSuccessEvent(request, response, authcRequest, result); + SuccessfulAuthenticationRequestEvent e = createSuccessEvent(request, response, null, result); publish(e); } catch (OAuthException e) { @@ -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( @@ -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,