Skip to content
This repository was archived by the owner on Dec 12, 2018. It is now read-only.

Commit 185d3dc

Browse files
author
Jose Luis Barrueta
authored
Merge pull request #1133 from stormpath/Issue-1127
issue 1127 - added support for grant_type of stormpath_token to AccessTokenController
2 parents bedafdd + b92007a commit 185d3dc

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

extensions/servlet/src/main/java/com/stormpath/sdk/servlet/mvc/AccessTokenController.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
import com.stormpath.sdk.impl.authc.DefaultBasicApiAuthenticationRequest;
2323
import com.stormpath.sdk.impl.authc.DefaultHttpServletRequestWrapper;
2424
import com.stormpath.sdk.impl.error.DefaultError;
25+
import com.stormpath.sdk.impl.oauth.DefaultIdSiteAuthenticationRequest;
2526
import com.stormpath.sdk.impl.oauth.DefaultOAuthStormpathSocialGrantRequestAuthentication;
2627
import com.stormpath.sdk.lang.Assert;
2728
import com.stormpath.sdk.oauth.AccessTokenResult;
2829
import com.stormpath.sdk.oauth.Authenticators;
30+
import com.stormpath.sdk.oauth.IdSiteAuthenticationRequest;
2931
import com.stormpath.sdk.oauth.OAuthClientCredentialsGrantRequestAuthentication;
3032
import com.stormpath.sdk.oauth.OAuthGrantRequestAuthenticationResult;
3133
import com.stormpath.sdk.oauth.OAuthPasswordGrantRequestAuthentication;
@@ -67,6 +69,7 @@ public class AccessTokenController extends AbstractController {
6769
private static final String CLIENT_CREDENTIALS_GRANT_TYPE = "client_credentials";
6870
private static final String PASSWORD_GRANT_TYPE = "password";
6971
private static final String STORMPATH_SOCIAL_GRANT_TYPE = "stormpath_social";
72+
private static final String STORMPATH_TOKEN_GRANT_TYPE = "stormpath_token";
7073
private static final String REFRESH_TOKEN_GRANT_TYPE = "refresh_token";
7174
private static final String GRANT_TYPE_PARAM_NAME = "grant_type";
7275

@@ -307,6 +310,29 @@ private OAuthException convertToOAuthException(ResourceException e, OAuthErrorCo
307310
return new OAuthException(oauthError, message);
308311
}
309312

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+
310336
@Override
311337
protected ViewModel doPost(HttpServletRequest request, HttpServletResponse response) throws Exception {
312338

@@ -352,6 +378,14 @@ protected ViewModel doPost(HttpServletRequest request, HttpServletResponse respo
352378
throw new OAuthException(OAuthErrorCode.INVALID_CLIENT);
353379
}
354380
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;
355389
default:
356390
throw new OAuthException(OAuthErrorCode.UNSUPPORTED_GRANT_TYPE, "'" + grantType + "' is an unsupported grant type.");
357391
}

0 commit comments

Comments
 (0)