Search in sources :

Example 1 with Tokens

use of com.nimbusds.oauth2.sdk.token.Tokens in project product-is by wso2.

the class AdaptiveScriptTemporaryClaimPersistenceTestCase method makeTokenRequest.

/**
 * Makes a token request with specified grant.
 *
 * @param authorizationGrant    Relevant authorization grant.
 * @return                      OIDC tokens coming from request.
 * @throws Exception            Exception.
 */
private OIDCTokens makeTokenRequest(AuthorizationGrant authorizationGrant, String uriString, String scopeString) throws Exception {
    ClientID clientID = new ClientID(consumerKey);
    Secret clientSecret = new Secret(consumerSecret);
    ClientAuthentication clientAuth = new ClientSecretBasic(clientID, clientSecret);
    URI uri = new URI(uriString);
    Scope scope = null;
    if (StringUtils.isNotBlank(scopeString)) {
        scope = new Scope(scopeString);
    }
    TokenRequest request = new TokenRequest(uri, clientAuth, authorizationGrant, scope);
    HTTPResponse tokenHTTPResp = request.toHTTPRequest().send();
    Assert.assertNotNull(tokenHTTPResp, "Token http response is null.");
    TokenResponse tokenResponse = OIDCTokenResponseParser.parse(tokenHTTPResp);
    Assert.assertNotNull(tokenResponse, "Token response of access token response is null.");
    Assert.assertFalse(tokenResponse instanceof TokenErrorResponse, "JWT access token response contains errors.");
    OIDCTokenResponse oidcTokenResponse = (OIDCTokenResponse) tokenResponse;
    OIDCTokens oidcTokens = oidcTokenResponse.getOIDCTokens();
    Assert.assertNotNull(oidcTokens, "OIDC Tokens object is null.");
    return oidcTokens;
}
Also used : Secret(com.nimbusds.oauth2.sdk.auth.Secret) TokenErrorResponse(com.nimbusds.oauth2.sdk.TokenErrorResponse) Scope(com.nimbusds.oauth2.sdk.Scope) OIDCTokenResponse(com.nimbusds.openid.connect.sdk.OIDCTokenResponse) TokenResponse(com.nimbusds.oauth2.sdk.TokenResponse) HTTPResponse(com.nimbusds.oauth2.sdk.http.HTTPResponse) OIDCTokenResponse(com.nimbusds.openid.connect.sdk.OIDCTokenResponse) OIDCTokens(com.nimbusds.openid.connect.sdk.token.OIDCTokens) TokenRequest(com.nimbusds.oauth2.sdk.TokenRequest) ClientID(com.nimbusds.oauth2.sdk.id.ClientID) ClientAuthentication(com.nimbusds.oauth2.sdk.auth.ClientAuthentication) URI(java.net.URI) ClientSecretBasic(com.nimbusds.oauth2.sdk.auth.ClientSecretBasic)

Example 2 with Tokens

use of com.nimbusds.oauth2.sdk.token.Tokens in project product-is by wso2.

the class ExtendSessionEndpointAuthCodeGrantTestCase method makeTokenRequest.

/**
 * To make a token request with specified grant.
 *
 * @param authorizationGrant    Relevant authorization grant.
 * @return                      OIDC tokens coming from request.
 * @throws Exception            Exception.
 */
private OIDCTokens makeTokenRequest(AuthorizationGrant authorizationGrant, String uriString, String scopeString) throws Exception {
    ClientID clientID = new ClientID(consumerKey);
    Secret clientSecret = new Secret(consumerSecret);
    ClientAuthentication clientAuth = new ClientSecretBasic(clientID, clientSecret);
    URI uri = new URI(uriString);
    Scope scope = null;
    if (StringUtils.isNotBlank(scopeString)) {
        scope = new Scope(scopeString);
    }
    TokenRequest request = new TokenRequest(uri, clientAuth, authorizationGrant, scope);
    HTTPResponse tokenHTTPResp = request.toHTTPRequest().send();
    Assert.assertNotNull(tokenHTTPResp, "Token http response is null.");
    TokenResponse tokenResponse = OIDCTokenResponseParser.parse(tokenHTTPResp);
    Assert.assertNotNull(tokenResponse, "Token response of access token response is null.");
    Assert.assertFalse(tokenResponse instanceof TokenErrorResponse, "JWT access token response contains errors.");
    OIDCTokenResponse oidcTokenResponse = (OIDCTokenResponse) tokenResponse;
    OIDCTokens oidcTokens = oidcTokenResponse.getOIDCTokens();
    Assert.assertNotNull(oidcTokens, "OIDC Tokens object is null.");
    return oidcTokens;
}
Also used : Secret(com.nimbusds.oauth2.sdk.auth.Secret) TokenErrorResponse(com.nimbusds.oauth2.sdk.TokenErrorResponse) Scope(com.nimbusds.oauth2.sdk.Scope) OIDCTokenResponse(com.nimbusds.openid.connect.sdk.OIDCTokenResponse) TokenResponse(com.nimbusds.oauth2.sdk.TokenResponse) HTTPResponse(com.nimbusds.oauth2.sdk.http.HTTPResponse) OIDCTokenResponse(com.nimbusds.openid.connect.sdk.OIDCTokenResponse) OIDCTokens(com.nimbusds.openid.connect.sdk.token.OIDCTokens) TokenRequest(com.nimbusds.oauth2.sdk.TokenRequest) ClientID(com.nimbusds.oauth2.sdk.id.ClientID) ClientAuthentication(com.nimbusds.oauth2.sdk.auth.ClientAuthentication) URI(java.net.URI) OAUTH2_REDIRECT_URI(org.wso2.identity.integration.test.utils.OAuth2Constant.OAUTH2_REDIRECT_URI) ClientSecretBasic(com.nimbusds.oauth2.sdk.auth.ClientSecretBasic)

Example 3 with Tokens

use of com.nimbusds.oauth2.sdk.token.Tokens in project product-is by wso2.

the class OAuth2IDTokenEncryptionTestCase method testResourceOwnerGrantSendAuthRequestPost.

@Test(groups = "wso2.is", description = "Send authorize user request for resource owner grant type.", dependsOnMethods = "testImplicitGrantDecryptIDToken")
public void testResourceOwnerGrantSendAuthRequestPost() throws Exception {
    // Remove previous data from variables.
    sessionDataKey = null;
    sessionDataKeyConsent = null;
    idToken = null;
    // Reset client.
    client = HttpClientBuilder.create().disableRedirectHandling().build();
    String username = "admin";
    Secret password = new Secret("admin");
    AuthorizationGrant passwordGrant = new ResourceOwnerPasswordCredentialsGrant(username, password);
    ClientID clientID = new ClientID(consumerKey);
    Secret clientSecret = new Secret(consumerSecret);
    ClientAuthentication clientAuth = new ClientSecretBasic(clientID, clientSecret);
    Scope scope = new Scope(OAuth2Constant.OAUTH2_SCOPE_OPENID);
    URI tokenEndpoint = new URI(OAuth2Constant.ACCESS_TOKEN_ENDPOINT);
    TokenRequest request = new TokenRequest(tokenEndpoint, clientAuth, passwordGrant, scope);
    HTTPResponse tokenHTTPResp = request.toHTTPRequest().send();
    Assert.assertNotNull(tokenHTTPResp, "Access token http response is null.");
    TokenResponse tokenResponse = OIDCTokenResponseParser.parse(tokenHTTPResp);
    Assert.assertNotNull(tokenResponse, "Access token response is null.");
    Assert.assertFalse(tokenResponse instanceof TokenErrorResponse, "Access token response contains errors.");
    OIDCTokenResponse oidcTokenResponse = (OIDCTokenResponse) tokenResponse;
    OIDCTokens oidcTokens = oidcTokenResponse.getOIDCTokens();
    Assert.assertNotNull(oidcTokens, "OIDC Tokens object is null.");
    idToken = oidcTokens.getIDTokenString();
    Assert.assertNotNull(idToken, "ID token is null");
}
Also used : HTTPResponse(com.nimbusds.oauth2.sdk.http.HTTPResponse) OIDCTokenResponse(com.nimbusds.openid.connect.sdk.OIDCTokenResponse) ResourceOwnerPasswordCredentialsGrant(com.nimbusds.oauth2.sdk.ResourceOwnerPasswordCredentialsGrant) URI(java.net.URI) ClientSecretBasic(com.nimbusds.oauth2.sdk.auth.ClientSecretBasic) Secret(com.nimbusds.oauth2.sdk.auth.Secret) TokenErrorResponse(com.nimbusds.oauth2.sdk.TokenErrorResponse) Scope(com.nimbusds.oauth2.sdk.Scope) OIDCTokenResponse(com.nimbusds.openid.connect.sdk.OIDCTokenResponse) TokenResponse(com.nimbusds.oauth2.sdk.TokenResponse) OIDCTokens(com.nimbusds.openid.connect.sdk.token.OIDCTokens) TokenRequest(com.nimbusds.oauth2.sdk.TokenRequest) ClientID(com.nimbusds.oauth2.sdk.id.ClientID) AuthorizationGrant(com.nimbusds.oauth2.sdk.AuthorizationGrant) ClientAuthentication(com.nimbusds.oauth2.sdk.auth.ClientAuthentication) Test(org.testng.annotations.Test)

Example 4 with Tokens

use of com.nimbusds.oauth2.sdk.token.Tokens in project product-is by wso2.

the class OAuth2ServiceJWTGrantTestCase method makeJWTBearerGrantRequest.

/**
 * To make the JWT Bearer Grant request.
 *
 * @return OIDC Tokens.
 * @throws java.text.ParseException Parse Exception.
 * @throws URISyntaxException       URI Syntax Exception.
 * @throws IOException              IO Exception.
 * @throws ParseException           Parse Exception.
 */
private OIDCTokens makeJWTBearerGrantRequest() throws java.text.ParseException, URISyntaxException, IOException, ParseException {
    SignedJWT signedJWT = SignedJWT.parse(jwtAssertion);
    AuthorizationGrant jwtGrant = new JWTBearerGrant(signedJWT);
    return makeTokenRequest(jwtGrant);
}
Also used : JWTBearerGrant(com.nimbusds.oauth2.sdk.JWTBearerGrant) SignedJWT(com.nimbusds.jwt.SignedJWT) AuthorizationGrant(com.nimbusds.oauth2.sdk.AuthorizationGrant)

Example 5 with Tokens

use of com.nimbusds.oauth2.sdk.token.Tokens in project Application-Gateway by gianlucafrei.

the class GitHubDriver method loadUserInfo.

@Override
protected UserModel loadUserInfo(Tokens tokens) {
    AccessToken accessToken = tokens.getAccessToken();
    RefreshToken refreshToken = tokens.getRefreshToken();
    try {
        // Load data
        String email = loadUserEmail(accessToken);
        GitHubUserResponse profileResponse = makeGitHubApiRequest("https://api.github.com/user", accessToken.getValue(), GitHubUserResponse.class);
        // Create user model
        UserModel model = new UserModel(profileResponse.id);
        model.set("email", email);
        model.set("picture", profileResponse.avatar_url);
        model.set("preferred_username", profileResponse.login);
        model.set("email_verified", "true");
        model.set("sub", model.getId());
        model.set("name", profileResponse.name);
        model.set("profile", profileResponse.url);
        model.set("updated_at", profileResponse.updated_at);
        model.set("created_at", profileResponse.created_at);
        model.set("access-token", accessToken.toString());
        model.set("refreshToken", refreshToken != null ? refreshToken.toString() : null);
        return model;
    } catch (IOException | InterruptedException ex) {
        throw new ApplicationException("Could not load user profile data", ex);
    }
}
Also used : UserModel(org.owasp.oag.session.UserModel) RefreshToken(com.nimbusds.oauth2.sdk.token.RefreshToken) ApplicationException(org.owasp.oag.exception.ApplicationException) AccessToken(com.nimbusds.oauth2.sdk.token.AccessToken) IOException(java.io.IOException)

Aggregations

URI (java.net.URI)18 OIDCTokens (com.nimbusds.openid.connect.sdk.token.OIDCTokens)17 ClientSecretBasic (com.nimbusds.oauth2.sdk.auth.ClientSecretBasic)15 ClientID (com.nimbusds.oauth2.sdk.id.ClientID)15 OIDCTokenResponse (com.nimbusds.openid.connect.sdk.OIDCTokenResponse)15 TokenResponse (com.nimbusds.oauth2.sdk.TokenResponse)14 TokenRequest (com.nimbusds.oauth2.sdk.TokenRequest)13 BearerAccessToken (com.nimbusds.oauth2.sdk.token.BearerAccessToken)13 Secret (com.nimbusds.oauth2.sdk.auth.Secret)12 HTTPResponse (com.nimbusds.oauth2.sdk.http.HTTPResponse)12 Tokens (com.nimbusds.oauth2.sdk.token.Tokens)11 TokenErrorResponse (com.nimbusds.oauth2.sdk.TokenErrorResponse)10 ClientAuthentication (com.nimbusds.oauth2.sdk.auth.ClientAuthentication)10 AccessToken (com.nimbusds.oauth2.sdk.token.AccessToken)10 IOException (java.io.IOException)10 AccessTokenResponse (com.nimbusds.oauth2.sdk.AccessTokenResponse)8 Scope (com.nimbusds.oauth2.sdk.Scope)8 RefreshToken (com.nimbusds.oauth2.sdk.token.RefreshToken)8 HashMap (java.util.HashMap)8 Test (org.testng.annotations.Test)8