|
22 | 22 | import com.stormpath.sdk.impl.authc.DefaultBasicApiAuthenticationRequest; |
23 | 23 | import com.stormpath.sdk.impl.authc.DefaultHttpServletRequestWrapper; |
24 | 24 | import com.stormpath.sdk.impl.error.DefaultError; |
| 25 | +import com.stormpath.sdk.impl.oauth.DefaultIdSiteAuthenticationRequest; |
25 | 26 | import com.stormpath.sdk.impl.oauth.DefaultOAuthStormpathSocialGrantRequestAuthentication; |
26 | 27 | import com.stormpath.sdk.lang.Assert; |
27 | 28 | import com.stormpath.sdk.oauth.AccessTokenResult; |
28 | 29 | import com.stormpath.sdk.oauth.Authenticators; |
| 30 | +import com.stormpath.sdk.oauth.IdSiteAuthenticationRequest; |
29 | 31 | import com.stormpath.sdk.oauth.OAuthClientCredentialsGrantRequestAuthentication; |
30 | 32 | import com.stormpath.sdk.oauth.OAuthGrantRequestAuthenticationResult; |
31 | 33 | import com.stormpath.sdk.oauth.OAuthPasswordGrantRequestAuthentication; |
@@ -67,6 +69,7 @@ public class AccessTokenController extends AbstractController { |
67 | 69 | private static final String CLIENT_CREDENTIALS_GRANT_TYPE = "client_credentials"; |
68 | 70 | private static final String PASSWORD_GRANT_TYPE = "password"; |
69 | 71 | private static final String STORMPATH_SOCIAL_GRANT_TYPE = "stormpath_social"; |
| 72 | + private static final String STORMPATH_TOKEN_GRANT_TYPE = "stormpath_token"; |
70 | 73 | private static final String REFRESH_TOKEN_GRANT_TYPE = "refresh_token"; |
71 | 74 | private static final String GRANT_TYPE_PARAM_NAME = "grant_type"; |
72 | 75 |
|
@@ -307,6 +310,29 @@ private OAuthException convertToOAuthException(ResourceException e, OAuthErrorCo |
307 | 310 | return new OAuthException(oauthError, message); |
308 | 311 | } |
309 | 312 |
|
| 313 | + private AccessTokenResult stormpathTokenAuthenticationRequest(HttpServletRequest request, HttpServletResponse response) { |
| 314 | + OAuthGrantRequestAuthenticationResult authenticationResult; |
| 315 | + |
| 316 | + try { |
| 317 | + Application app = getApplication(request); |
| 318 | + String token = request.getParameter("token"); |
| 319 | + |
| 320 | + IdSiteAuthenticationRequest authenticationRequest = new DefaultIdSiteAuthenticationRequest(token); |
| 321 | + |
| 322 | + authenticationResult = Authenticators.ID_SITE_AUTHENTICATOR |
| 323 | + .forApplication(app) |
| 324 | + .authenticate(authenticationRequest); |
| 325 | + } catch (ResourceException e) { |
| 326 | + log.debug("Unable to authenticate stormpath token grant request: {}", e.getMessage(), e); |
| 327 | + throw convertToOAuthException(e, OAuthErrorCode.INVALID_CLIENT); |
| 328 | + } catch (IllegalArgumentException ex) { |
| 329 | + throw new OAuthException(OAuthErrorCode.INVALID_REQUEST); |
| 330 | + } |
| 331 | + |
| 332 | + return createAccessTokenResult(request, response, authenticationResult); |
| 333 | + } |
| 334 | + |
| 335 | + |
310 | 336 | @Override |
311 | 337 | protected ViewModel doPost(HttpServletRequest request, HttpServletResponse response) throws Exception { |
312 | 338 |
|
@@ -352,6 +378,14 @@ protected ViewModel doPost(HttpServletRequest request, HttpServletResponse respo |
352 | 378 | throw new OAuthException(OAuthErrorCode.INVALID_CLIENT); |
353 | 379 | } |
354 | 380 | break; |
| 381 | + case STORMPATH_TOKEN_GRANT_TYPE: |
| 382 | + try { |
| 383 | + result = this.stormpathTokenAuthenticationRequest(request, response); |
| 384 | + } catch (HttpAuthenticationException ex) { |
| 385 | + log.warn("Unable to authenticate client", ex); |
| 386 | + throw new OAuthException(OAuthErrorCode.INVALID_CLIENT); |
| 387 | + } |
| 388 | + break; |
355 | 389 | default: |
356 | 390 | throw new OAuthException(OAuthErrorCode.UNSUPPORTED_GRANT_TYPE, "'" + grantType + "' is an unsupported grant type."); |
357 | 391 | } |
|
0 commit comments