Search in sources :

Example 31 with Tokens

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

the class OAuth2ServiceJWTGrantTestCase method makeTokenRequest.

/**
 * To make a token request with specified grant.
 *
 * @param authorizationGrant Relevant authorization grant.
 * @return OIDC tokens coming from request.
 * @throws URISyntaxException URI Syntax Exception.
 * @throws IOException        IO Exception.
 * @throws ParseException     Parse Exception.
 */
private OIDCTokens makeTokenRequest(AuthorizationGrant authorizationGrant) throws URISyntaxException, IOException, ParseException {
    ClientID clientID = new ClientID(consumerKey);
    Secret clientSecret = new Secret(consumerSecret);
    ClientAuthentication clientAuth = new ClientSecretBasic(clientID, clientSecret);
    URI tokenEndpoint = new URI(OAuth2Constant.ACCESS_TOKEN_ENDPOINT);
    TokenRequest request = new TokenRequest(tokenEndpoint, clientAuth, authorizationGrant, new Scope(OAuth2Constant.OAUTH2_SCOPE_OPENID + " " + OAuth2Constant.OAUTH2_SCOPE_EMAIL));
    HTTPResponse tokenHTTPResp = request.toHTTPRequest().send();
    Assert.assertNotNull(tokenHTTPResp, "JWT access token http response is null.");
    TokenResponse tokenResponse = OIDCTokenResponseParser.parse(tokenHTTPResp);
    Assert.assertNotNull(tokenResponse, "Token response of JWT access token response is null.");
    Assert.assertFalse(tokenResponse instanceof TokenErrorResponse, "JWT access token response contains errors.");
    OIDCTokenResponse oidcTokenResponse = (OIDCTokenResponse) tokenResponse;
    JSONObject jsonObject = ((OIDCTokenResponse) tokenResponse).toJSONObject();
    refreshToken = String.valueOf(jsonObject.get("refresh_token"));
    OIDCTokens oidcTokens = oidcTokenResponse.getOIDCTokens();
    Assert.assertNotNull(oidcTokens, "OIDC Tokens object is null in JWT token");
    return oidcTokens;
}
Also used : HTTPResponse(com.nimbusds.oauth2.sdk.http.HTTPResponse) OIDCTokenResponse(com.nimbusds.openid.connect.sdk.OIDCTokenResponse) 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) JSONObject(net.minidev.json.JSONObject) 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)

Example 32 with Tokens

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

the class OIDCSubAttributeTestCase method testAuthCodeGrantSendGetTokensPost.

@Test(groups = "wso2.is", description = "Send get access token request.", dependsOnMethods = "testAuthCodeGrantSendApprovalPost")
public void testAuthCodeGrantSendGetTokensPost() throws Exception {
    ClientID clientID = new ClientID(consumerKey);
    Secret clientSecret = new Secret(consumerSecret);
    ClientSecretBasic clientSecretBasic = new ClientSecretBasic(clientID, clientSecret);
    URI callbackURI = new URI(CALLBACK_URL);
    AuthorizationCodeGrant authorizationCodeGrant = new AuthorizationCodeGrant(authorizationCode, callbackURI);
    TokenRequest tokenReq = new TokenRequest(new URI(OAuth2Constant.ACCESS_TOKEN_ENDPOINT), clientSecretBasic, authorizationCodeGrant);
    HTTPResponse tokenHTTPResp = tokenReq.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");
    accessToken = oidcTokens.getAccessToken().getValue();
}
Also used : Secret(com.nimbusds.oauth2.sdk.auth.Secret) TokenErrorResponse(com.nimbusds.oauth2.sdk.TokenErrorResponse) OIDCTokenResponse(com.nimbusds.openid.connect.sdk.OIDCTokenResponse) TokenResponse(com.nimbusds.oauth2.sdk.TokenResponse) AuthorizationCodeGrant(com.nimbusds.oauth2.sdk.AuthorizationCodeGrant) 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) URI(java.net.URI) ClientSecretBasic(com.nimbusds.oauth2.sdk.auth.ClientSecretBasic) Test(org.testng.annotations.Test) OAuth2ServiceAbstractIntegrationTest(org.wso2.identity.integration.test.oauth2.OAuth2ServiceAbstractIntegrationTest)

Example 33 with Tokens

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

the class OIDCSubAttributeTestCase method testResourceOwnerGrantSendAuthRequestPost.

@Test(groups = "wso2.is", description = "Send authorize user request for resource owner grant type.", dependsOnMethods = "testAuthCodeGrantValidateSub")
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 = userInfo.getUserName();
    Secret password = new Secret(userInfo.getPassword());
    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");
    accessToken = oidcTokens.getAccessToken().getValue();
}
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) OAuth2ServiceAbstractIntegrationTest(org.wso2.identity.integration.test.oauth2.OAuth2ServiceAbstractIntegrationTest)

Example 34 with Tokens

use of com.nimbusds.oauth2.sdk.token.Tokens in project microsoft-authentication-library-for-java by AzureAD.

the class TokenResponseTest method testConstructor.

@Test
public void testConstructor() throws ParseException {
    final TokenResponse response = new TokenResponse(new BearerAccessToken("access_token"), new RefreshToken("refresh_token"), idToken, null, null, expiresIn, extExpiresIn, null, refreshIn);
    Assert.assertNotNull(response);
    OIDCTokens tokens = response.getOIDCTokens();
    Assert.assertNotNull(tokens);
    final JWT jwt = tokens.getIDToken();
    Assert.assertTrue(jwt.getJWTClaimsSet().getClaims().size() >= 0);
}
Also used : RefreshToken(com.nimbusds.oauth2.sdk.token.RefreshToken) JWT(com.nimbusds.jwt.JWT) OIDCTokens(com.nimbusds.openid.connect.sdk.token.OIDCTokens) BearerAccessToken(com.nimbusds.oauth2.sdk.token.BearerAccessToken) Test(org.testng.annotations.Test)

Example 35 with Tokens

use of com.nimbusds.oauth2.sdk.token.Tokens in project microsoft-authentication-library-for-java by AzureAD.

the class TokenResponseTest method testEmptyIdToken.

@Test
public void testEmptyIdToken() {
    final TokenResponse response = new TokenResponse(new BearerAccessToken(idToken), new RefreshToken("refresh_token"), "", null, null, expiresIn, extExpiresIn, null, refreshIn);
    Assert.assertNotNull(response);
    OIDCTokens tokens = response.getOIDCTokens();
    Assert.assertNotNull(tokens);
    final AccessToken accessToken = tokens.getAccessToken();
    Assert.assertNotNull(accessToken);
}
Also used : RefreshToken(com.nimbusds.oauth2.sdk.token.RefreshToken) AccessToken(com.nimbusds.oauth2.sdk.token.AccessToken) BearerAccessToken(com.nimbusds.oauth2.sdk.token.BearerAccessToken) OIDCTokens(com.nimbusds.openid.connect.sdk.token.OIDCTokens) BearerAccessToken(com.nimbusds.oauth2.sdk.token.BearerAccessToken) Test(org.testng.annotations.Test)

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