Search in sources :

Example 1 with Token

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

the class IntrospectionWsHttpTest method basicAuthentication.

@Test
@Parameters({ "umaPatClientId", "umaPatClientSecret" })
public void basicAuthentication(final String umaPatClientId, final String umaPatClientSecret) throws Exception {
    final Token tokenToIntrospect = UmaClient.requestPat(tokenEndpoint, umaPatClientId, umaPatClientSecret, clientEngine(true));
    final IntrospectionService introspectionService = ClientFactory.instance().createIntrospectionService(introspectionEndpoint, clientEngine(true));
    final IntrospectionResponse introspectionResponse = introspectionService.introspectToken("Basic " + BaseRequest.getEncodedCredentials(umaPatClientId, umaPatClientSecret), tokenToIntrospect.getAccessToken());
    assertTrue(introspectionResponse != null && introspectionResponse.isActive());
}
Also used : IntrospectionResponse(org.gluu.oxauth.model.common.IntrospectionResponse) IntrospectionService(org.gluu.oxauth.client.service.IntrospectionService) Token(org.gluu.oxauth.model.uma.wrapper.Token) Parameters(org.testng.annotations.Parameters) BaseTest(org.gluu.oxauth.BaseTest) Test(org.testng.annotations.Test)

Example 2 with Token

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

the class UmaClient method request.

private static Token request(final String tokenUrl, final String clientKeyStoreFile, final String clientKeyStorePassword, final String clientId, final String keyId, TokenRequest tokenRequest) throws UmaException {
    OxAuthCryptoProvider cryptoProvider;
    try {
        cryptoProvider = new OxAuthCryptoProvider(clientKeyStoreFile, clientKeyStorePassword, null);
    } catch (Exception ex) {
        throw new UmaException("Failed to initialize crypto provider");
    }
    try {
        String tmpKeyId = keyId;
        if (StringHelper.isEmpty(tmpKeyId)) {
            // Get first key
            List<String> aliases = cryptoProvider.getKeys();
            if (aliases.size() > 0) {
                tmpKeyId = aliases.get(0);
            }
        }
        if (StringHelper.isEmpty(tmpKeyId)) {
            throw new UmaException("UMA keyId is empty");
        }
        SignatureAlgorithm algorithm = cryptoProvider.getSignatureAlgorithm(tmpKeyId);
        tokenRequest.setAuthenticationMethod(AuthenticationMethod.PRIVATE_KEY_JWT);
        tokenRequest.setAuthUsername(clientId);
        tokenRequest.setCryptoProvider(cryptoProvider);
        tokenRequest.setAlgorithm(algorithm);
        tokenRequest.setKeyId(tmpKeyId);
        tokenRequest.setAudience(tokenUrl);
        Token umaPat = UmaClient.request(tokenUrl, tokenRequest);
        return umaPat;
    } catch (Exception ex) {
        throw new UmaException("Failed to obtain valid UMA PAT token", ex);
    }
}
Also used : OxAuthCryptoProvider(org.gluu.oxauth.model.crypto.OxAuthCryptoProvider) UmaException(org.gluu.oxauth.client.uma.exception.UmaException) SignatureAlgorithm(org.gluu.oxauth.model.crypto.signature.SignatureAlgorithm) Token(org.gluu.oxauth.model.uma.wrapper.Token) UmaException(org.gluu.oxauth.client.uma.exception.UmaException)

Example 3 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 4 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)

Example 5 with Token

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

the class IntrospectionWsHttpTest method bearerWithResponseAsJwt.

@Test
@Parameters({ "umaPatClientId", "umaPatClientSecret" })
public void bearerWithResponseAsJwt(final String umaPatClientId, final String umaPatClientSecret) throws Exception {
    final ClientHttpEngine engine = clientEngine(true);
    final Token authorization = UmaClient.requestPat(tokenEndpoint, umaPatClientId, umaPatClientSecret, engine);
    final Token tokenToIntrospect = UmaClient.requestPat(tokenEndpoint, umaPatClientId, umaPatClientSecret, engine);
    final IntrospectionService introspectionService = ClientFactory.instance().createIntrospectionService(introspectionEndpoint, engine);
    final String jwtAsString = introspectionService.introspectTokenWithResponseAsJwt("Bearer " + authorization.getAccessToken(), tokenToIntrospect.getAccessToken(), true);
    final Jwt jwt = Jwt.parse(jwtAsString);
    assertTrue(Boolean.parseBoolean(jwt.getClaims().getClaimAsString("active")));
}
Also used : ClientHttpEngine(org.jboss.resteasy.client.jaxrs.ClientHttpEngine) Jwt(org.gluu.oxauth.model.jwt.Jwt) IntrospectionService(org.gluu.oxauth.client.service.IntrospectionService) Token(org.gluu.oxauth.model.uma.wrapper.Token) Parameters(org.testng.annotations.Parameters) BaseTest(org.gluu.oxauth.BaseTest) Test(org.testng.annotations.Test)

Aggregations

Token (org.gluu.oxauth.model.uma.wrapper.Token)11 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 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 Response (javax.ws.rs.core.Response)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