Search in sources :

Example 11 with OAuth2AuthorizeRespDTO

use of org.wso2.carbon.identity.oauth2.dto.OAuth2AuthorizeRespDTO in project identity-inbound-auth-oauth by wso2-extensions.

the class AuthorizationHandlerManagerTest method testHandleAuthorizationTokenResponse.

@Test(dataProvider = "IdpIDColumnAvailabilityDataProvider")
public void testHandleAuthorizationTokenResponse(boolean isIDPIdColumnEnabled) throws Exception {
    OAuth2ServiceComponentHolder.setIDPIdColumnEnabled(isIDPIdColumnEnabled);
    authorizationHandlerManager = AuthorizationHandlerManager.getInstance();
    authzReqDTO.setResponseType(TestConstants.AUTHORIZATION_HANDLER_RESPONSE_TYPE_TOKEN);
    authzReqDTO.setConsumerKey(TestConstants.CLIENT_ID);
    authzReqDTO.setScopes(TestConstants.SCOPE_STRING.split(" "));
    AuthenticatedUser user = new AuthenticatedUser();
    user.setUserName(TestConstants.USER_NAME);
    user.setUserId("4b4414e1-916b-4475-aaee-6b0751c29ff6");
    user.setTenantDomain(TestConstants.TENANT_DOMAIN);
    user.setUserStoreDomain(TestConstants.USER_DOMAIN_PRIMARY);
    user.setFederatedIdPName(TestConstants.LOCAL_IDP);
    authzReqDTO.setUser(user);
    OAuth2AuthorizeRespDTO respDTO = authorizationHandlerManager.handleAuthorization(authzReqDTO);
    Assert.assertNotNull(respDTO, "Response is null");
    Assert.assertNotNull(respDTO.getAccessToken(), "Access token returned is null");
}
Also used : OAuth2AuthorizeRespDTO(org.wso2.carbon.identity.oauth2.dto.OAuth2AuthorizeRespDTO) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser) Test(org.testng.annotations.Test) IdentityBaseTest(org.wso2.carbon.identity.testutil.IdentityBaseTest)

Example 12 with OAuth2AuthorizeRespDTO

use of org.wso2.carbon.identity.oauth2.dto.OAuth2AuthorizeRespDTO in project identity-inbound-auth-oauth by wso2-extensions.

the class CodeResponseTypeHandlerTest method testIssue.

@Test(dataProvider = "IdpIDColumnAvailabilityDataProvider")
public void testIssue(boolean isIDPIdColumnEnabled) throws Exception {
    OAuth2ServiceComponentHolder.setIDPIdColumnEnabled(isIDPIdColumnEnabled);
    OAuthAppDO oAuthAppDO = new OAuthAppDO();
    oAuthAppDO.setGrantTypes("implicit");
    oAuthAppDO.setOauthConsumerKey(TEST_CONSUMER_KEY);
    oAuthAppDO.setState("active");
    AuthenticatedUser user = new AuthenticatedUser();
    user.setUserStoreDomain("PRIMARY");
    user.setUserName("testUser");
    user.setFederatedIdPName(TestConstants.LOCAL_IDP);
    oAuthAppDO.setUser(user);
    oAuthAppDO.setApplicationName("testApp");
    AppInfoCache appInfoCache = AppInfoCache.getInstance();
    appInfoCache.addToCache(TEST_CONSUMER_KEY, oAuthAppDO);
    CodeResponseTypeHandler codeResponseTypeHandler = new CodeResponseTypeHandler();
    codeResponseTypeHandler.init();
    OAuth2AuthorizeRespDTO oAuth2AuthorizeRespDTO = codeResponseTypeHandler.issue(authAuthzReqMessageContext);
    Assert.assertNotNull(oAuth2AuthorizeRespDTO.getAuthorizationCode(), "Access token not Authorization code");
    Assert.assertEquals(oAuth2AuthorizeRespDTO.getCallbackURI(), TEST_CALLBACK_URL, "Callback url not set");
}
Also used : AppInfoCache(org.wso2.carbon.identity.oauth.cache.AppInfoCache) OAuthAppDO(org.wso2.carbon.identity.oauth.dao.OAuthAppDO) OAuth2AuthorizeRespDTO(org.wso2.carbon.identity.oauth2.dto.OAuth2AuthorizeRespDTO) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser) Test(org.testng.annotations.Test)

Example 13 with OAuth2AuthorizeRespDTO

use of org.wso2.carbon.identity.oauth2.dto.OAuth2AuthorizeRespDTO in project identity-inbound-auth-oauth by wso2-extensions.

the class TokenResponseTypeHandlerTest method testIssue.

@Test(dataProvider = "CommonDataProvider")
public void testIssue(boolean isIDPIdColumnEnabled, String clientId) throws Exception {
    OAuth2ServiceComponentHolder.setIDPIdColumnEnabled(isIDPIdColumnEnabled);
    AccessTokenResponseTypeHandler tokenResponseTypeHandler = new AccessTokenResponseTypeHandler();
    tokenResponseTypeHandler.init();
    OAuth2AuthorizeReqDTO authorizationReqDTO = new OAuth2AuthorizeReqDTO();
    authorizationReqDTO.setCallbackUrl("https://localhost:8000/callback");
    authorizationReqDTO.setConsumerKey(clientId);
    authenticatedUser.setUserName(TEST_USER_ID);
    authenticatedUser.setUserId("4b4414e1-916b-4475-aaee-6b0751c29ff6");
    authenticatedUser.setTenantDomain("carbon.super");
    authenticatedUser.setUserStoreDomain("PTEST");
    authenticatedUser.setFederatedIdPName(TestConstants.LOCAL_IDP);
    authorizationReqDTO.setUser(authenticatedUser);
    authorizationReqDTO.setResponseType(OAuthConstants.GrantTypes.TOKEN);
    OAuthAuthzReqMessageContext authAuthzReqMessageContext = new OAuthAuthzReqMessageContext(authorizationReqDTO);
    authAuthzReqMessageContext.setApprovedScope(new String[] { "scope1", "scope2", OAuthConstants.Scope.OPENID });
    OAuthAppDO oAuthAppDO = new OAuthAppDO();
    oAuthAppDO.setGrantTypes("implicit");
    oAuthAppDO.setOauthConsumerKey(clientId);
    oAuthAppDO.setUser(authenticatedUser);
    oAuthAppDO.setOauthVersion(OAuthConstants.OAuthVersions.VERSION_2);
    AccessTokenDO accessTokenDO = new AccessTokenDO();
    accessTokenDO.setAccessToken("abcdefghijklmn");
    accessTokenDO.setAuthzUser(authenticatedUser);
    new OAuthAppDAO().addOAuthApplication(oAuthAppDO);
    OAuth2AuthorizeRespDTO auth2AuthorizeReqDTO = tokenResponseTypeHandler.issue(authAuthzReqMessageContext);
    Assert.assertNotNull(auth2AuthorizeReqDTO.getAccessToken());
    Assert.assertTrue(auth2AuthorizeReqDTO.getValidityPeriod() > 1, "Access Token should be valid, i.e. not expired.");
}
Also used : AccessTokenDO(org.wso2.carbon.identity.oauth2.model.AccessTokenDO) OAuthAppDAO(org.wso2.carbon.identity.oauth.dao.OAuthAppDAO) OAuthAppDO(org.wso2.carbon.identity.oauth.dao.OAuthAppDO) OAuth2AuthorizeRespDTO(org.wso2.carbon.identity.oauth2.dto.OAuth2AuthorizeRespDTO) OAuthAuthzReqMessageContext(org.wso2.carbon.identity.oauth2.authz.OAuthAuthzReqMessageContext) OAuth2AuthorizeReqDTO(org.wso2.carbon.identity.oauth2.dto.OAuth2AuthorizeReqDTO) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 14 with OAuth2AuthorizeRespDTO

use of org.wso2.carbon.identity.oauth2.dto.OAuth2AuthorizeRespDTO in project identity-inbound-auth-oauth by wso2-extensions.

the class DefaultIDTokenBuilderTest method testBuildIDTokenForAuthorization.

@Test
public void testBuildIDTokenForAuthorization() throws Exception {
    String clientId = "dabfba9390aa423f8b04332794d83614";
    OAuth2AuthorizeRespDTO oAuth2AuthorizeRespDTO = new OAuth2AuthorizeRespDTO();
    AuthenticatedUser user = getDefaultAuthenticatedUserFederatedUser();
    OAuthAuthzReqMessageContext oAuthAuthzReqMessageContext = getOAuthAuthzReqMessageContextForUser(user, clientId);
    oAuth2AuthorizeRespDTO.setAccessToken("2sa9a678f890877856y66e75f605d456");
    mockRealmService();
    String idToken = defaultIDTokenBuilder.buildIDToken(oAuthAuthzReqMessageContext, oAuth2AuthorizeRespDTO);
    JWTClaimsSet claims = SignedJWT.parse(idToken).getJWTClaimsSet();
    Assert.assertEquals(claims.getAudience().get(0), clientId);
    Assert.assertEquals(claims.getIssuer(), "https://localhost:9443/oauth2/token");
    Assert.assertEquals(claims.getSubject(), "user1");
    Assert.assertEquals(claims.getClaim("isk"), "wso2.is.com");
    Long expirationTime = ((Date) claims.getClaim("exp")).getTime();
    Assert.assertTrue(expirationTime > (new Date()).getTime());
    Long issueTime = ((Date) claims.getClaim("iat")).getTime();
    Assert.assertTrue(issueTime <= (new Date()).getTime());
}
Also used : OAuth2AuthorizeRespDTO(org.wso2.carbon.identity.oauth2.dto.OAuth2AuthorizeRespDTO) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) OAuthAuthzReqMessageContext(org.wso2.carbon.identity.oauth2.authz.OAuthAuthzReqMessageContext) Matchers.anyString(org.mockito.Matchers.anyString) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser) Date(java.util.Date) SAML2BearerGrantHandlerTest(org.wso2.carbon.identity.oauth2.token.handlers.grant.saml.SAML2BearerGrantHandlerTest) Test(org.testng.annotations.Test)

Example 15 with OAuth2AuthorizeRespDTO

use of org.wso2.carbon.identity.oauth2.dto.OAuth2AuthorizeRespDTO in project identity-inbound-auth-oauth by wso2-extensions.

the class DefaultIDTokenBuilderTest method testBuildEncryptedIDTokenForAuthorization.

@Test(dataProvider = "testBuildEncryptedIDTokenForSupportedAlgorithm")
public void testBuildEncryptedIDTokenForAuthorization(String algorithm) throws Exception {
    OAuthAuthzReqMessageContext oAuthAuthzReqMessageContext = getOAuthAuthzReqMessageContextForUser(getDefaultAuthenticatedLocalUser(), CLIENT_ID);
    OAuth2AuthorizeRespDTO oAuth2AuthorizeRespDTO = new OAuth2AuthorizeRespDTO();
    oAuth2AuthorizeRespDTO.setAccessToken(ACCESS_TOKEN);
    OAuthAppDO entry = getOAuthAppDO(algorithm);
    AppInfoCache.getInstance().addToCache(CLIENT_ID, entry);
    mockRealmService();
    String idToken = defaultIDTokenBuilder.buildIDToken(oAuthAuthzReqMessageContext, oAuth2AuthorizeRespDTO);
    EncryptedJWT encryptedJWT = decryptToken(idToken);
    JWTClaimsSet claims = encryptedJWT.getPayload().toSignedJWT().getJWTClaimsSet();
    Assert.assertEquals(claims.getAudience().get(0), CLIENT_ID);
    Assert.assertEquals(claims.getIssuer(), "https://localhost:9443/oauth2/token");
    Assert.assertEquals(claims.getSubject(), "user1");
    Assert.assertEquals(claims.getClaim("isk"), "wso2.is.com");
    Long expirationTime = ((Date) claims.getClaim("exp")).getTime();
    Assert.assertTrue(expirationTime < (new Date()).getTime());
    Long issueTime = ((Date) claims.getClaim("iat")).getTime();
    Assert.assertTrue(issueTime <= (new Date()).getTime());
}
Also used : OAuthAppDO(org.wso2.carbon.identity.oauth.dao.OAuthAppDO) OAuth2AuthorizeRespDTO(org.wso2.carbon.identity.oauth2.dto.OAuth2AuthorizeRespDTO) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) OAuthAuthzReqMessageContext(org.wso2.carbon.identity.oauth2.authz.OAuthAuthzReqMessageContext) Matchers.anyString(org.mockito.Matchers.anyString) EncryptedJWT(com.nimbusds.jwt.EncryptedJWT) Date(java.util.Date) SAML2BearerGrantHandlerTest(org.wso2.carbon.identity.oauth2.token.handlers.grant.saml.SAML2BearerGrantHandlerTest) Test(org.testng.annotations.Test)

Aggregations

OAuth2AuthorizeRespDTO (org.wso2.carbon.identity.oauth2.dto.OAuth2AuthorizeRespDTO)36 Test (org.testng.annotations.Test)22 AuthenticatedUser (org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)18 Matchers.anyString (org.mockito.Matchers.anyString)13 IdentityBaseTest (org.wso2.carbon.identity.testutil.IdentityBaseTest)12 OAuth2AuthorizeReqDTO (org.wso2.carbon.identity.oauth2.dto.OAuth2AuthorizeReqDTO)10 HashMap (java.util.HashMap)7 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)6 OAuthAuthzReqMessageContext (org.wso2.carbon.identity.oauth2.authz.OAuthAuthzReqMessageContext)6 AccessTokenDO (org.wso2.carbon.identity.oauth2.model.AccessTokenDO)6 OAuthAppDO (org.wso2.carbon.identity.oauth.dao.OAuthAppDO)5 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)4 Date (java.util.Date)4 OAuthResponse (org.apache.oltu.oauth2.common.message.OAuthResponse)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 BeforeTest (org.testng.annotations.BeforeTest)3 OAuthEventInterceptor (org.wso2.carbon.identity.oauth.event.OAuthEventInterceptor)3 OAuth2Parameters (org.wso2.carbon.identity.oauth2.model.OAuth2Parameters)3 RequestObject (org.wso2.carbon.identity.openidconnect.model.RequestObject)3