Search in sources :

Example 31 with AuthzCodeDO

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

the class AuthorizationCodeGrantHandlerTest method buildErrorTokenRequestMessageContext.

@DataProvider(name = "buildErrorTokenRequestMessageContext")
public Object[][] buildErrorTokenRequestMessageContext() {
    OAuthTokenReqMessageContext oAuthTokenReqMessageContext1 = new OAuthTokenReqMessageContext(new OAuth2AccessTokenReqDTO());
    oAuthTokenReqMessageContext1.getOauth2AccessTokenReqDTO().setAuthorizationCode("123456");
    oAuthTokenReqMessageContext1.getOauth2AccessTokenReqDTO().setCallbackURI("callBackUrl2");
    OAuthTokenReqMessageContext oAuthTokenReqMessageContext2 = new OAuthTokenReqMessageContext(new OAuth2AccessTokenReqDTO());
    oAuthTokenReqMessageContext2.getOauth2AccessTokenReqDTO().setAuthorizationCode("123456");
    oAuthTokenReqMessageContext2.getOauth2AccessTokenReqDTO().setCallbackURI("callBackUrl");
    AuthzCodeDO authzCodeDO1 = new AuthzCodeDO();
    authzCodeDO1.setState(OAuthConstants.AuthorizationCodeState.INACTIVE);
    AuthenticatedUser authenticatedUser = new AuthenticatedUser();
    authenticatedUser.setUserName("user");
    WhiteboxImpl.setInternalState(authzCodeDO1, "authorizedUser", authenticatedUser);
    WhiteboxImpl.setInternalState(authzCodeDO1, "callbackUrl", "callBackUrl");
    WhiteboxImpl.setInternalState(authzCodeDO1, "state", "INACTIVE");
    AuthzCodeDO authzCodeDO2 = new AuthzCodeDO();
    WhiteboxImpl.setInternalState(authzCodeDO2, "authorizedUser", authenticatedUser);
    WhiteboxImpl.setInternalState(authzCodeDO2, "callbackUrl", "callBackUrl");
    WhiteboxImpl.setInternalState(authzCodeDO2, "validityPeriod", 3000000L);
    return new Object[][] { { oAuthTokenReqMessageContext1, null, CLIENT_ID_VALUE, true, 1000L, "Invalid authorization code" }, { oAuthTokenReqMessageContext1, authzCodeDO1, CLIENT_ID_VALUE, true, 1000L, "Inactive authorization code" }, { oAuthTokenReqMessageContext1, authzCodeDO2, CLIENT_ID_VALUE, true, 1000L, "Expired authorization code" }, { oAuthTokenReqMessageContext1, authzCodeDO2, CLIENT_ID_VALUE, true, System.currentTimeMillis(), "Callback url mismatch" }, { oAuthTokenReqMessageContext2, authzCodeDO2, CLIENT_ID_VALUE, false, System.currentTimeMillis(), "PKCE validation failed" }, { oAuthTokenReqMessageContext2, authzCodeDO2, INVALID_CLIENT, true, System.currentTimeMillis(), "Error while retrieving app information" } };
}
Also used : OAuthTokenReqMessageContext(org.wso2.carbon.identity.oauth2.token.OAuthTokenReqMessageContext) AuthzCodeDO(org.wso2.carbon.identity.oauth2.model.AuthzCodeDO) OAuth2AccessTokenReqDTO(org.wso2.carbon.identity.oauth2.dto.OAuth2AccessTokenReqDTO) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser) DataProvider(org.testng.annotations.DataProvider)

Example 32 with AuthzCodeDO

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

the class AuthorizationCodeDAOImplTest method testInsertAuthorizationCode.

@Test
public void testInsertAuthorizationCode() throws Exception {
    String consumerKey = UUID.randomUUID().toString();
    String authzCodeID = UUID.randomUUID().toString();
    String authzCode = UUID.randomUUID().toString();
    mockStatic(OAuth2Util.class);
    when(OAuth2Util.getTenantId(anyString())).thenReturn(DEFAULT_TENANT_ID);
    AuthzCodeDO authzCodeDO = persistAuthorizationCode(consumerKey, authzCodeID, authzCode, OAuthConstants.AuthorizationCodeState.ACTIVE);
    Assert.assertEquals(authorizationCodeDAO.getCodeIdByAuthorizationCode(authzCode), authzCodeDO.getAuthzCodeId());
    Assert.assertNull(authorizationCodeDAO.getCodeIdByAuthorizationCode(UUID.randomUUID().toString()));
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) AuthzCodeDO(org.wso2.carbon.identity.oauth2.model.AuthzCodeDO) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 33 with AuthzCodeDO

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

the class AuthorizationCodeDAOImplTest method persistAuthorizationCodeWithModifiedScope.

private AuthzCodeDO persistAuthorizationCodeWithModifiedScope(String consumerKey, String authzCodeId, String authzCode, String status, String[] scope) throws Exception {
    createApplication(consumerKey, UUID.randomUUID().toString(), DEFAULT_TENANT_ID);
    AuthzCodeDO authzCodeDO = new AuthzCodeDO(authenticatedUser, scope, new Timestamp(System.currentTimeMillis()), 3600000L, CALLBACK, consumerKey, authzCode, authzCodeId, status, null, null);
    authorizationCodeDAO.insertAuthorizationCode(authzCode, consumerKey, CALLBACK, authzCodeDO);
    return authzCodeDO;
}
Also used : AuthzCodeDO(org.wso2.carbon.identity.oauth2.model.AuthzCodeDO) Timestamp(java.sql.Timestamp)

Example 34 with AuthzCodeDO

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

the class AuthorizationCodeDAOImplTest method testGetAuthorizationCodesByUser.

@Test
public void testGetAuthorizationCodesByUser() throws Exception {
    String consumerKey = UUID.randomUUID().toString();
    String authzCodeID = UUID.randomUUID().toString();
    String authzCode = UUID.randomUUID().toString();
    AuthenticatedUser dummyAuthenticatedUser = new AuthenticatedUser();
    dummyAuthenticatedUser.setTenantDomain("super.wso2");
    dummyAuthenticatedUser.setUserName("MockedUser");
    dummyAuthenticatedUser.setUserStoreDomain(UserCoreConstants.PRIMARY_DEFAULT_DOMAIN_NAME);
    mockStatic(OAuth2Util.class);
    when(OAuth2Util.getTenantId(anyString())).thenReturn(DEFAULT_TENANT_ID);
    when(OAuth2Util.getUserStoreDomain(any())).thenReturn(UserCoreConstants.PRIMARY_DEFAULT_DOMAIN_NAME);
    AuthzCodeDO authzCodeDO = persistAuthorizationCode(consumerKey, authzCodeID, authzCode, OAuthConstants.AuthorizationCodeState.ACTIVE);
    mockStatic(OAuth2Util.class);
    mockStatic(IdentityUtil.class);
    when(OAuth2Util.getTenantId(anyString())).thenReturn(DEFAULT_TENANT_ID);
    when(IdentityUtil.isUserStoreInUsernameCaseSensitive(anyString())).thenReturn(true);
    when(OAuth2Util.isHashDisabled()).thenReturn(true);
    // Allow the method to pass the validation without wanting to traverse internally.
    when(OAuth2Util.calculateValidityInMillis(anyLong(), anyLong())).thenReturn(2000L);
    Assert.assertTrue((authorizationCodeDAO.getAuthorizationCodesByUser(authenticatedUser).contains(authzCodeDO.getAuthorizationCode())));
    Assert.assertTrue(authorizationCodeDAO.getAuthorizationCodesByUser(dummyAuthenticatedUser).isEmpty());
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) AuthzCodeDO(org.wso2.carbon.identity.oauth2.model.AuthzCodeDO) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 35 with AuthzCodeDO

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

the class AuthorizationCodeDAOImplTest method testDeactivateAuthorizationCodes.

@Test
public void testDeactivateAuthorizationCodes() throws Exception {
    String consumerKey1 = UUID.randomUUID().toString();
    String authzCodeID1 = UUID.randomUUID().toString();
    String authzCode1 = UUID.randomUUID().toString();
    String consumerKey2 = UUID.randomUUID().toString();
    String authzCodeID2 = UUID.randomUUID().toString();
    String authzCode2 = UUID.randomUUID().toString();
    AuthzCodeDO authzCodeDO1 = persistAuthorizationCode(consumerKey1, authzCodeID1, authzCode1, OAuthConstants.AuthorizationCodeState.ACTIVE);
    AuthzCodeDO authzCodeDO2 = persistAuthorizationCode(consumerKey2, authzCodeID2, authzCode2, OAuthConstants.AuthorizationCodeState.ACTIVE);
    mockStatic(OAuth2Util.class);
    mockStatic(IdentityUtil.class);
    authzCodeDO1.setOauthTokenId(UUID.randomUUID().toString());
    // If state is EXPIRED/INACTIVE needs to revoke token as well.
    mockStatic(OAuth2TokenUtil.class);
    doNothing().when(OAuth2TokenUtil.class, "postRevokeCode", anyString(), anyString(), anyString());
    List<AuthzCodeDO> revokeAuthzCodeList = new ArrayList<>();
    revokeAuthzCodeList.add(authzCodeDO1);
    revokeAuthzCodeList.add(authzCodeDO2);
    authorizationCodeDAO.deactivateAuthorizationCodes(revokeAuthzCodeList);
    Assert.assertTrue(authorizationCodeDAO.getActiveAuthorizationCodesByConsumerKey(authzCodeDO1.getConsumerKey()).isEmpty());
    Assert.assertTrue(authorizationCodeDAO.getActiveAuthorizationCodesByConsumerKey(authzCodeDO2.getConsumerKey()).isEmpty());
}
Also used : ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) AuthzCodeDO(org.wso2.carbon.identity.oauth2.model.AuthzCodeDO) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Aggregations

AuthzCodeDO (org.wso2.carbon.identity.oauth2.model.AuthzCodeDO)38 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)18 Test (org.testng.annotations.Test)11 Connection (java.sql.Connection)8 PreparedStatement (java.sql.PreparedStatement)8 SQLException (java.sql.SQLException)8 Timestamp (java.sql.Timestamp)8 Matchers.anyString (org.mockito.Matchers.anyString)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 AuthenticatedUser (org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)8 PowerMockIdentityBaseTest (org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)8 ArrayList (java.util.ArrayList)7 AccessTokenDO (org.wso2.carbon.identity.oauth2.model.AccessTokenDO)6 ResultSet (java.sql.ResultSet)5 HashMap (java.util.HashMap)5 OAuthCacheKey (org.wso2.carbon.identity.oauth.cache.OAuthCacheKey)5 HashSet (java.util.HashSet)4 OAuthTokenReqMessageContext (org.wso2.carbon.identity.oauth2.token.OAuthTokenReqMessageContext)4 Map (java.util.Map)3 DataProvider (org.testng.annotations.DataProvider)3