Search in sources :

Example 11 with Token

use of org.gluu.oxauth.model.uma.wrapper.Token in project oxAuth by GluuFederation.

the class UmaClient method request.

@Deprecated
public static Token request(final String authorizeUrl, final String tokenUrl, final String umaUserId, final String umaUserSecret, final String umaClientId, final String umaClientSecret, final String umaRedirectUri, UmaScopeType p_type, String... scopeArray) throws Exception {
    // 1. Request authorization and receive the authorization code.
    List<ResponseType> responseTypes = new ArrayList<ResponseType>();
    responseTypes.add(ResponseType.CODE);
    responseTypes.add(ResponseType.ID_TOKEN);
    List<String> scopes = new ArrayList<String>();
    scopes.add(p_type.getValue());
    if (scopeArray != null && scopeArray.length > 0) {
        scopes.addAll(Arrays.asList(scopeArray));
    }
    String state = UUID.randomUUID().toString();
    AuthorizationRequest request = new AuthorizationRequest(responseTypes, umaClientId, scopes, umaRedirectUri, null);
    request.setState(state);
    request.setAuthUsername(umaUserId);
    request.setAuthPassword(umaUserSecret);
    request.getPrompts().add(Prompt.NONE);
    AuthorizeClient authorizeClient = new AuthorizeClient(authorizeUrl);
    authorizeClient.setRequest(request);
    AuthorizationResponse response1 = authorizeClient.exec();
    String scope = response1.getScope();
    String authorizationCode = response1.getCode();
    if (Util.allNotBlank(authorizationCode)) {
        // 2. Request access token using the authorization code.
        TokenRequest tokenRequest = new TokenRequest(GrantType.AUTHORIZATION_CODE);
        tokenRequest.setCode(authorizationCode);
        tokenRequest.setRedirectUri(umaRedirectUri);
        tokenRequest.setAuthUsername(umaClientId);
        tokenRequest.setAuthPassword(umaClientSecret);
        tokenRequest.setAuthenticationMethod(AuthenticationMethod.CLIENT_SECRET_BASIC);
        tokenRequest.setScope(scope);
        TokenClient tokenClient1 = new TokenClient(tokenUrl);
        tokenClient1.setRequest(tokenRequest);
        TokenResponse response2 = tokenClient1.exec();
        if (response2.getStatus() == 200) {
            final String patToken = response2.getAccessToken();
            final String patRefreshToken = response2.getRefreshToken();
            final Integer expiresIn = response2.getExpiresIn();
            if (Util.allNotBlank(patToken, patRefreshToken)) {
                return new Token(authorizationCode, patRefreshToken, patToken, scope, expiresIn);
            }
        }
    }
    return null;
}
Also used : AuthorizationRequest(org.gluu.oxauth.client.AuthorizationRequest) ArrayList(java.util.ArrayList) Token(org.gluu.oxauth.model.uma.wrapper.Token) ResponseType(org.gluu.oxauth.model.common.ResponseType) AuthorizationResponse(org.gluu.oxauth.client.AuthorizationResponse) TokenResponse(org.gluu.oxauth.client.TokenResponse) TokenRequest(org.gluu.oxauth.client.TokenRequest) AuthorizeClient(org.gluu.oxauth.client.AuthorizeClient) TokenClient(org.gluu.oxauth.client.TokenClient)

Example 12 with Token

use of org.gluu.oxauth.model.uma.wrapper.Token in project oxAuth by GluuFederation.

the class UmaClient method request.

public static Token request(final String tokenUrl, final String umaClientId, final String umaClientSecret, UmaScopeType scopeType, ClientHttpEngine engine, String... scopeArray) throws Exception {
    String scope = scopeType.getValue();
    if (scopeArray != null && scopeArray.length > 0) {
        for (String s : scopeArray) {
            scope = scope + " " + s;
        }
    }
    TokenClient tokenClient = new TokenClient(tokenUrl);
    if (engine != null) {
        tokenClient.setExecutor(engine);
    }
    TokenResponse response = tokenClient.execClientCredentialsGrant(scope, umaClientId, umaClientSecret);
    if (response.getStatus() == 200) {
        final String patToken = response.getAccessToken();
        final Integer expiresIn = response.getExpiresIn();
        if (Util.allNotBlank(patToken)) {
            return new Token(null, null, patToken, scopeType.getValue(), expiresIn);
        }
    }
    return null;
}
Also used : TokenResponse(org.gluu.oxauth.client.TokenResponse) Token(org.gluu.oxauth.model.uma.wrapper.Token) TokenClient(org.gluu.oxauth.client.TokenClient)

Aggregations

Token (org.gluu.oxauth.model.uma.wrapper.Token)12 BaseTest (org.gluu.oxauth.BaseTest)7 Parameters (org.testng.annotations.Parameters)7 Test (org.testng.annotations.Test)7 IntrospectionService (org.gluu.oxauth.client.service.IntrospectionService)4 IntrospectionResponse (org.gluu.oxauth.model.common.IntrospectionResponse)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Response (javax.ws.rs.core.Response)2 TokenClient (org.gluu.oxauth.client.TokenClient)2 TokenRequest (org.gluu.oxauth.client.TokenRequest)2 TokenResponse (org.gluu.oxauth.client.TokenResponse)2 StatService (org.gluu.oxauth.client.service.StatService)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Builder (javax.ws.rs.client.Invocation.Builder)1 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)1 AuthorizationRequest (org.gluu.oxauth.client.AuthorizationRequest)1 AuthorizationResponse (org.gluu.oxauth.client.AuthorizationResponse)1 AuthorizeClient (org.gluu.oxauth.client.AuthorizeClient)1 UmaException (org.gluu.oxauth.client.uma.exception.UmaException)1