Example usage for org.springframework.security.oauth2.provider TokenRequest getGrantType

List of usage examples for org.springframework.security.oauth2.provider TokenRequest getGrantType

Introduction

In this page you can find the example usage for org.springframework.security.oauth2.provider TokenRequest getGrantType.

Prototype

public String getGrantType() 

Source Link

Usage

From source file:org.joyrest.oauth2.endpoint.TokenEndpoint.java

@Override
protected void configure() {
    setControllerPath("oauth");

    post("token", (req, resp) -> {
        Authentication principal = basicAuthenticator.authenticate(req);

        String clientId = getClientId(principal);
        ClientDetails authenticatedClient = clientDetailsService.loadClientByClientId(clientId);

        Map<String, String> parameters = MapUtils.createOneDimMap(req.getQueryParams());
        TokenRequest tokenRequest = requestFactory.createTokenRequest(parameters, authenticatedClient);

        // Only validate the client details if a client authenticated during this request.
        if (!isEmpty(clientId) && !clientId.equals(tokenRequest.getClientId())) {
            throw new InvalidClientException("Given client ID does not match authenticated client");
        }//  ww w . ja va2s  . c o  m

        if (nonNull(authenticatedClient)) {
            requestValidator.validateScope(tokenRequest, authenticatedClient);
        }

        if (!isEmpty(tokenRequest.getGrantType())) {
            throw new InvalidRequestException("Missing grant type");
        }

        if (tokenRequest.getGrantType().equals("implicit")) {
            throw new InvalidGrantException("Implicit grant type not supported from token endpoint");
        }

        // The scope was requested or determined during the authorization step
        if (isAuthCodeRequest(parameters) && nonEmpty(tokenRequest.getScope())) {
            tokenRequest.setScope(emptySet());
        }

        // A refresh token has its own default scopes, so we should ignore any added by the factory here.
        if (isRefreshTokenRequest(parameters)) {
            tokenRequest.setScope(OAuth2Utils.parseParameterList(parameters.get(OAuth2Utils.SCOPE)));
        }

        OAuth2AccessToken token = tokenGranter.grant(tokenRequest.getGrantType(), tokenRequest);
        if (isNull(token)) {
            throw new UnsupportedGrantTypeException("Unsupported grant type: " + tokenRequest.getGrantType());
        }

        createResponse(resp, token);

    }, Resp(OAuth2AccessToken.class)).produces(JSON);
}

From source file:com.hundsun.sso.controller.OAuthRestController.java

@RequestMapping(value = "/oauth/rest_token", method = RequestMethod.POST)
@ResponseBody//from  ww  w .j av a 2  s  .  c om
public OAuth2AccessToken postAccessToken(@RequestBody Map<String, String> parameters) {

    String clientId = getClientId(parameters);
    ClientDetails authenticatedClient = clientDetailsService.loadClientByClientId(clientId);

    TokenRequest tokenRequest = oAuth2RequestFactory.createTokenRequest(parameters, authenticatedClient);

    if (clientId != null && !"".equals(clientId)) {
        // Only validate the client details if a client authenticated during this
        // request.
        if (!clientId.equals(tokenRequest.getClientId())) {
            // double check to make sure that the client ID in the token request is the same as that in the
            // authenticated client
            throw new InvalidClientException("Given client ID does not match authenticated client");
        }
    }

    if (authenticatedClient != null) {
        oAuth2RequestValidator.validateScope(tokenRequest, authenticatedClient);
    }

    final String grantType = tokenRequest.getGrantType();
    if (!StringUtils.hasText(grantType)) {
        throw new InvalidRequestException("Missing grant type");
    }
    if ("implicit".equals(grantType)) {
        throw new InvalidGrantException("Implicit grant type not supported from token endpoint");
    }

    if (isAuthCodeRequest(parameters)) {
        // The scope was requested or determined during the authorization step
        if (!tokenRequest.getScope().isEmpty()) {
            LOG.debug("Clearing scope of incoming token request");
            tokenRequest.setScope(Collections.<String>emptySet());
        }
    }

    if (isRefreshTokenRequest(parameters)) {
        // A refresh token has its own default scopes, so we should ignore any added by the factory here.
        tokenRequest.setScope(OAuth2Utils.parseParameterList(parameters.get(OAuth2Utils.SCOPE)));
    }

    OAuth2AccessToken token = getTokenGranter(grantType).grant(grantType, tokenRequest);
    if (token == null) {
        throw new UnsupportedGrantTypeException("Unsupported grant type: " + grantType);
    }

    return token;

}

From source file:com.monkeyk.sos.web.controller.OAuthRestController.java

@RequestMapping(value = "/oauth2/rest_token", method = RequestMethod.POST)
@ResponseBody//from  w w w  .  j  av a2 s  .  c  o  m
public OAuth2AccessToken postAccessToken(@RequestBody Map<String, String> parameters) {

    String clientId = getClientId(parameters);
    ClientDetails authenticatedClient = clientDetailsService.loadClientByClientId(clientId);

    TokenRequest tokenRequest = oAuth2RequestFactory.createTokenRequest(parameters, authenticatedClient);

    if (clientId != null && !"".equals(clientId)) {
        // Only validate the client details if a client authenticated during this
        // request.
        if (!clientId.equals(tokenRequest.getClientId())) {
            // double check to make sure that the client ID in the token request is the same as that in the
            // authenticated client
            throw new InvalidClientException("Given client ID does not match authenticated client");
        }
    }

    if (authenticatedClient != null) {
        oAuth2RequestValidator.validateScope(tokenRequest, authenticatedClient);
    }

    final String grantType = tokenRequest.getGrantType();
    if (!StringUtils.hasText(grantType)) {
        throw new InvalidRequestException("Missing grant type");
    }
    if ("implicit".equals(grantType)) {
        throw new InvalidGrantException("Implicit grant type not supported from token endpoint");
    }

    if (isAuthCodeRequest(parameters)) {
        // The scope was requested or determined during the authorization step
        if (!tokenRequest.getScope().isEmpty()) {
            LOG.debug("Clearing scope of incoming token request");
            tokenRequest.setScope(Collections.<String>emptySet());
        }
    }

    if (isRefreshTokenRequest(parameters)) {
        // A refresh token has its own default scopes, so we should ignore any added by the factory here.
        tokenRequest.setScope(OAuth2Utils.parseParameterList(parameters.get(OAuth2Utils.SCOPE)));
    }

    OAuth2AccessToken token = getTokenGranter(grantType).grant(grantType, tokenRequest);
    if (token == null) {
        throw new UnsupportedGrantTypeException("Unsupported grant type: " + grantType);
    }

    return token;

}

From source file:org.cloudfoundry.identity.uaa.oauth.token.Saml2TokenGranter.java

@Override
public OAuth2AccessToken grant(String grantType, TokenRequest tokenRequest) {
    TokenRequest adjusted = new TokenRequest(tokenRequest.getRequestParameters(), tokenRequest.getClientId(),
            tokenRequest.getScope(), tokenRequest.getGrantType());
    return super.grant(grantType, adjusted);
}