Search in sources :

Example 21 with AuthzCodeDO

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

the class AuthContextTokenDOTest method initTest.

@BeforeClass
public void initTest() throws SocketException {
    authzCodeDO = new AuthzCodeDO();
    authContextTokenDO = new AuthContextTokenDO(AUTHZ_CODE, CONSUMER_KEY, CALLBACK_URL, authzCodeDO);
}
Also used : AuthzCodeDO(org.wso2.carbon.identity.oauth2.model.AuthzCodeDO) BeforeClass(org.testng.annotations.BeforeClass)

Example 22 with AuthzCodeDO

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

the class AuthorizationCodeGrantHandlerTest method testValidateGrantException.

@Test(dataProvider = "buildErrorTokenRequestMessageContext")
public void testValidateGrantException(Object tokenRequestMessageContext, Object authzCode, String clientId, boolean pkceValid, long timestamp, String expectedError) throws Exception {
    AuthzCodeDO authzCodeDO = (AuthzCodeDO) authzCode;
    WhiteboxImpl.setInternalState(authorizationCodeGrantHandler, "cacheEnabled", true);
    OAuthCache oAuthCache = mock(OAuthCache.class);
    when(OAuthCache.getInstance()).thenReturn(oAuthCache);
    WhiteboxImpl.setInternalState(authorizationCodeGrantHandler, "oauthCache", oAuthCache);
    OAuthTokenReqMessageContext tokReqMsgCtx = (OAuthTokenReqMessageContext) tokenRequestMessageContext;
    oAuthServerConfiguration = mock(OAuthServerConfiguration.class);
    TokenPersistenceProcessor tokenPersistenceProcessor = mock(TokenPersistenceProcessor.class);
    when(OAuthServerConfiguration.getInstance()).thenReturn(oAuthServerConfiguration);
    when(oAuthServerConfiguration.getPersistenceProcessor()).thenReturn(tokenPersistenceProcessor);
    OAuthAppDAO oAuthAppDAO = mock(OAuthAppDAO.class);
    OAuthAppDO oAuthAppDO = new OAuthAppDO();
    whenNew(OAuthAppDAO.class).withNoArguments().thenReturn(oAuthAppDAO);
    when(oAuthAppDAO.getAppInformation(CLIENT_ID_VALUE)).thenReturn(oAuthAppDO);
    when(oAuthAppDAO.getAppInformation(INVALID_CLIENT)).thenThrow(new InvalidOAuthClientException("Error"));
    AppInfoCache appInfoCache = mock(AppInfoCache.class);
    when(AppInfoCache.getInstance()).thenReturn(appInfoCache);
    doNothing().when(appInfoCache).addToCache(anyString(), any(OAuthAppDO.class));
    spy(OAuth2Util.class);
    doReturn(pkceValid).when(OAuth2Util.class, "validatePKCE", anyString(), anyString(), anyString(), any(OAuthAppDO.class));
    try {
        authorizationCodeGrantHandler.validateGrant(tokReqMsgCtx);
        fail("Expected exception not thrown");
    } catch (IdentityOAuth2Exception e) {
        assertTrue(e.getMessage().contains(expectedError), "Expected error message with '" + expectedError + "'");
    }
}
Also used : OAuthAppDAO(org.wso2.carbon.identity.oauth.dao.OAuthAppDAO) AppInfoCache(org.wso2.carbon.identity.oauth.cache.AppInfoCache) OAuthAppDO(org.wso2.carbon.identity.oauth.dao.OAuthAppDO) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) OAuthCache(org.wso2.carbon.identity.oauth.cache.OAuthCache) OAuthTokenReqMessageContext(org.wso2.carbon.identity.oauth2.token.OAuthTokenReqMessageContext) OAuthServerConfiguration(org.wso2.carbon.identity.oauth.config.OAuthServerConfiguration) TokenPersistenceProcessor(org.wso2.carbon.identity.oauth.tokenprocessor.TokenPersistenceProcessor) AuthzCodeDO(org.wso2.carbon.identity.oauth2.model.AuthzCodeDO) InvalidOAuthClientException(org.wso2.carbon.identity.oauth.common.exception.InvalidOAuthClientException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 23 with AuthzCodeDO

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

the class AuthorizationCodeGrantHandlerTest method buildTokenRequestMessageContext.

@DataProvider(name = "BuildTokenRequestMessageContext")
public Object[][] buildTokenRequestMessageContext() {
    OAuthTokenReqMessageContext messageContext1 = new OAuthTokenReqMessageContext(new OAuth2AccessTokenReqDTO());
    messageContext1.getOauth2AccessTokenReqDTO().setAuthorizationCode("123456");
    OAuthTokenReqMessageContext messageContext2 = new OAuthTokenReqMessageContext(new OAuth2AccessTokenReqDTO());
    messageContext2.getOauth2AccessTokenReqDTO().setAuthorizationCode("123456");
    messageContext2.getOauth2AccessTokenReqDTO().setCallbackURI("callBackUrl");
    AuthenticatedUser authenticatedUser = new AuthenticatedUser();
    AuthzCodeDO authzCodeDO1 = new AuthzCodeDO();
    WhiteboxImpl.setInternalState(authzCodeDO1, "authorizedUser", authenticatedUser);
    WhiteboxImpl.setInternalState(authzCodeDO1, "callbackUrl", "callBackUrl");
    AuthzCodeDO authzCodeDO2 = new AuthzCodeDO();
    return new Object[][] { { messageContext1, authzCodeDO2, false, true, System.currentTimeMillis() + 250000L, true }, { messageContext2, authzCodeDO1, true, false, System.currentTimeMillis() + 250000L, true } };
}
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 24 with AuthzCodeDO

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

the class RequestObjectHandlerTest method revokeAccessToken.

@DataProvider(name = "requestObjectRevoke")
public Object[][] revokeAccessToken() {
    List<String> codeList = new ArrayList<>();
    codeList.add("code1");
    codeList.add("code2");
    AuthzCodeDO authzCodeDO = new AuthzCodeDO();
    authzCodeDO.setAuthorizationCode("code1");
    authzCodeDO.setAuthzCodeId("coded1");
    AuthzCodeDO authzCodeDO1 = new AuthzCodeDO();
    authzCodeDO1.setAuthzCodeId("codeId2");
    authzCodeDO1.setAuthorizationCode("code2");
    List<AuthzCodeDO> lstAuthzCode = new ArrayList<>();
    lstAuthzCode.add(authzCodeDO);
    lstAuthzCode.add(authzCodeDO1);
    return new Object[][] { { OIDCConstants.Event.POST_REVOKE_ACESS_TOKEN, codeList, null, OIDCConstants.Event.ACEESS_TOKENS, null }, { OIDCConstants.Event.POST_REVOKE_CODE, null, lstAuthzCode, OIDCConstants.Event.CODES, null }, { OIDCConstants.Event.POST_ISSUE_ACCESS_TOKEN, null, null, OIDCConstants.Event.TOKEN_ID, "token1" }, { OIDCConstants.Event.POST_ISSUE_CODE, null, null, OIDCConstants.Event.CODE_ID, "token1" } };
}
Also used : ArrayList(java.util.ArrayList) AuthzCodeDO(org.wso2.carbon.identity.oauth2.model.AuthzCodeDO) DataProvider(org.testng.annotations.DataProvider)

Example 25 with AuthzCodeDO

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

the class RequestObjectHandler method handlePostRevokeCode.

private void handlePostRevokeCode(Map<String, Object> eventProperties, String codeState) throws IdentityOAuth2Exception, IdentityOAuthAdminException {
    boolean isCodeRemove = isCodeRemoved(codeState);
    List<AuthzCodeDO> authzcodes = (List<AuthzCodeDO>) eventProperties.get(OIDCConstants.Event.CODES);
    for (AuthzCodeDO authzCodeDO : authzcodes) {
        String codeId = authzCodeDO.getAuthzCodeId();
        String tokenId = authzCodeDO.getOauthTokenId();
        if (isCodeRemove) {
            OAuthTokenPersistenceFactory.getInstance().getRequestObjectDAO().deleteRequestObjectReferenceByCode(codeId);
        } else if (StringUtils.isNotEmpty(tokenId) && OAuthConstants.AuthorizationCodeState.INACTIVE.equals(codeState)) {
            // update the token id  of request object reference identified by code id
            OAuthTokenPersistenceFactory.getInstance().getRequestObjectDAO().updateRequestObjectReferenceCodeToToken(codeId, tokenId);
        }
    }
}
Also used : List(java.util.List) AuthzCodeDO(org.wso2.carbon.identity.oauth2.model.AuthzCodeDO)

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