Search in sources :

Example 6 with OAuth2AccessTokenRespDTO

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

the class RefreshGrantHandler method handleError.

private OAuth2AccessTokenRespDTO handleError(String errorCode, String errorMsg, OAuth2AccessTokenReqDTO tokenReqDTO) {
    if (log.isDebugEnabled()) {
        log.debug("OAuth-Error-Code=" + errorCode + " client-id=" + tokenReqDTO.getClientId() + " grant-type=" + tokenReqDTO.getGrantType() + " scope=" + OAuth2Util.buildScopeString(tokenReqDTO.getScope()));
    }
    OAuth2AccessTokenRespDTO tokenRespDTO;
    tokenRespDTO = new OAuth2AccessTokenRespDTO();
    tokenRespDTO.setError(true);
    tokenRespDTO.setErrorCode(errorCode);
    tokenRespDTO.setErrorMsg(errorMsg);
    return tokenRespDTO;
}
Also used : OAuth2AccessTokenRespDTO(org.wso2.carbon.identity.oauth2.dto.OAuth2AccessTokenRespDTO)

Example 7 with OAuth2AccessTokenRespDTO

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

the class RefreshGrantHandler method buildTokenResponse.

private OAuth2AccessTokenRespDTO buildTokenResponse(OAuthTokenReqMessageContext tokReqMsgCtx, AccessTokenDO accessTokenBean) {
    String scope = OAuth2Util.buildScopeString(tokReqMsgCtx.getScope());
    OAuth2AccessTokenRespDTO tokenResp = new OAuth2AccessTokenRespDTO();
    tokenResp.setAccessToken(accessTokenBean.getAccessToken());
    tokenResp.setTokenId(accessTokenBean.getTokenId());
    tokenResp.setRefreshToken(accessTokenBean.getRefreshToken());
    if (accessTokenBean.getValidityPeriodInMillis() > 0) {
        tokenResp.setExpiresIn(accessTokenBean.getValidityPeriod());
        tokenResp.setExpiresInMillis(accessTokenBean.getValidityPeriodInMillis());
    } else {
        tokenResp.setExpiresIn(Long.MAX_VALUE);
        tokenResp.setExpiresInMillis(Long.MAX_VALUE);
    }
    tokenResp.setAuthorizedScopes(scope);
    return tokenResp;
}
Also used : OAuth2AccessTokenRespDTO(org.wso2.carbon.identity.oauth2.dto.OAuth2AccessTokenRespDTO)

Example 8 with OAuth2AccessTokenRespDTO

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

the class AccessTokenIssuerTest method testIssueValidateGrantError.

@Test(dataProvider = "invalidGrantErrorDataProvider")
public void testIssueValidateGrantError(boolean throwException, String exceptionMsg) throws Exception {
    AuthorizationGrantHandler dummyGrantHandler = mock(AuthorizationGrantHandler.class);
    when(dummyGrantHandler.isConfidentialClient()).thenReturn(false);
    // Not a confidential client
    when(dummyGrantHandler.isOfTypeApplicationUser()).thenReturn(true);
    when(dummyGrantHandler.isAuthorizedClient(any(OAuthTokenReqMessageContext.class))).thenReturn(true);
    if (throwException) {
        // validate grant will throw an exception
        when(dummyGrantHandler.validateGrant(any(OAuthTokenReqMessageContext.class))).thenThrow(new IdentityOAuth2Exception(exceptionMsg));
    } else {
        // validate grant will return false
        when(dummyGrantHandler.validateGrant(any(OAuthTokenReqMessageContext.class))).thenReturn(false);
    }
    HashMap<String, AuthorizationGrantHandler> authorizationGrantHandlers = new HashMap<>();
    authorizationGrantHandlers.put(DUMMY_GRANT_TYPE, dummyGrantHandler);
    mockOAuth2ServerConfiguration(authorizationGrantHandlers);
    OAuth2AccessTokenReqDTO reqDTO = new OAuth2AccessTokenReqDTO();
    reqDTO.setGrantType(DUMMY_GRANT_TYPE);
    OAuthClientAuthnContext oAuthClientAuthnContext = new OAuthClientAuthnContext();
    oAuthClientAuthnContext.setClientId(SOME_CLIENT_ID);
    reqDTO.setoAuthClientAuthnContext(oAuthClientAuthnContext);
    OAuth2AccessTokenRespDTO tokenRespDTO = AccessTokenIssuer.getInstance().issue(reqDTO);
    assertNotNull(tokenRespDTO);
    assertTrue(tokenRespDTO.isError());
    assertEquals(tokenRespDTO.getErrorCode(), OAuthError.TokenResponse.INVALID_GRANT);
    assertEquals(tokenRespDTO.getErrorMsg(), exceptionMsg);
}
Also used : IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) OAuth2AccessTokenRespDTO(org.wso2.carbon.identity.oauth2.dto.OAuth2AccessTokenRespDTO) AuthorizationGrantHandler(org.wso2.carbon.identity.oauth2.token.handlers.grant.AuthorizationGrantHandler) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) OAuth2AccessTokenReqDTO(org.wso2.carbon.identity.oauth2.dto.OAuth2AccessTokenReqDTO) OAuthClientAuthnContext(org.wso2.carbon.identity.oauth2.bean.OAuthClientAuthnContext) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 9 with OAuth2AccessTokenRespDTO

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

the class AccessTokenIssuerTest method testIssueWithOpenIdScopeFailure.

@Test
public void testIssueWithOpenIdScopeFailure() throws Exception {
    OAuth2AccessTokenReqDTO reqDTO = new OAuth2AccessTokenReqDTO();
    reqDTO.setGrantType(DUMMY_GRANT_TYPE);
    reqDTO.setScope(SCOPES_WITH_OPENID);
    setupOIDCScopeTest(DUMMY_GRANT_TYPE, false);
    OAuthClientAuthnContext oAuthClientAuthnContext = new OAuthClientAuthnContext();
    oAuthClientAuthnContext.setClientId(SOME_CLIENT_ID);
    reqDTO.setoAuthClientAuthnContext(oAuthClientAuthnContext);
    OAuth2AccessTokenRespDTO tokenRespDTO = AccessTokenIssuer.getInstance().issue(reqDTO);
    assertNotNull(tokenRespDTO);
    assertTrue(tokenRespDTO.isError());
    assertEquals(tokenRespDTO.getErrorCode(), OAuth2ErrorCodes.SERVER_ERROR);
    // ID Token should not be set
    assertNull(tokenRespDTO.getIDToken());
}
Also used : OAuth2AccessTokenRespDTO(org.wso2.carbon.identity.oauth2.dto.OAuth2AccessTokenRespDTO) OAuth2AccessTokenReqDTO(org.wso2.carbon.identity.oauth2.dto.OAuth2AccessTokenReqDTO) OAuthClientAuthnContext(org.wso2.carbon.identity.oauth2.bean.OAuthClientAuthnContext) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 10 with OAuth2AccessTokenRespDTO

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

the class AccessTokenIssuerTest method testIssueFailedMultipleClientAuthentication.

/**
 * Multiple Client Authentication mechanisms used to authenticate the request.
 *
 * @throws Exception
 */
@Test
public void testIssueFailedMultipleClientAuthentication() throws Exception {
    OAuth2AccessTokenReqDTO reqDTO = new OAuth2AccessTokenReqDTO();
    reqDTO.setGrantType(DUMMY_GRANT_TYPE);
    OAuthClientAuthnContext oAuthClientAuthnContext = new OAuthClientAuthnContext();
    oAuthClientAuthnContext.addAuthenticator("ClientAuthenticator1");
    oAuthClientAuthnContext.addAuthenticator("ClientAuthenticator2");
    reqDTO.setoAuthClientAuthnContext(oAuthClientAuthnContext);
    OAuth2AccessTokenRespDTO tokenRespDTO = AccessTokenIssuer.getInstance().issue(reqDTO);
    assertNotNull(tokenRespDTO);
    assertTrue(tokenRespDTO.isError());
    assertEquals(tokenRespDTO.getErrorCode(), OAuthError.TokenResponse.INVALID_REQUEST, "Error Code has been " + "changed. Previously it was: " + OAuthError.TokenResponse.INVALID_REQUEST);
}
Also used : OAuth2AccessTokenRespDTO(org.wso2.carbon.identity.oauth2.dto.OAuth2AccessTokenRespDTO) OAuth2AccessTokenReqDTO(org.wso2.carbon.identity.oauth2.dto.OAuth2AccessTokenReqDTO) OAuthClientAuthnContext(org.wso2.carbon.identity.oauth2.bean.OAuthClientAuthnContext) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Aggregations

OAuth2AccessTokenRespDTO (org.wso2.carbon.identity.oauth2.dto.OAuth2AccessTokenRespDTO)30 OAuth2AccessTokenReqDTO (org.wso2.carbon.identity.oauth2.dto.OAuth2AccessTokenReqDTO)19 Test (org.testng.annotations.Test)18 HashMap (java.util.HashMap)16 Matchers.anyString (org.mockito.Matchers.anyString)15 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)15 PowerMockIdentityBaseTest (org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)12 OAuthClientAuthnContext (org.wso2.carbon.identity.oauth2.bean.OAuthClientAuthnContext)10 AuthorizationGrantHandler (org.wso2.carbon.identity.oauth2.token.handlers.grant.AuthorizationGrantHandler)10 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)9 ArrayList (java.util.ArrayList)7 ResponseHeader (org.wso2.carbon.identity.oauth2.ResponseHeader)6 AuthorizationGrantCacheEntry (org.wso2.carbon.identity.oauth.cache.AuthorizationGrantCacheEntry)5 OAuthAppDO (org.wso2.carbon.identity.oauth.dao.OAuthAppDO)5 OAuthTokenReqMessageContext (org.wso2.carbon.identity.oauth2.token.OAuthTokenReqMessageContext)5 Hashtable (java.util.Hashtable)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 AuthenticatedUser (org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)4 Date (java.util.Date)3 Map (java.util.Map)3