Search in sources :

Example 26 with OAuthClientAuthnContext

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

the class AccessTokenIssuerTest method testCrossTenantTokenRequestError.

/**
 * Tests whether cross tenant token requests fail in tenant qualified URL mode.
 *
 * @throws Exception
 */
@Test(dataProvider = "tenantDataProvider", expectedExceptions = InvalidOAuthClientException.class)
public void testCrossTenantTokenRequestError(String tenantInContext) throws Exception {
    OAuth2AccessTokenReqDTO reqDTO = new OAuth2AccessTokenReqDTO();
    reqDTO.setGrantType("password");
    OAuthClientAuthnContext oAuthClientAuthnContext = new OAuthClientAuthnContext();
    oAuthClientAuthnContext.setAuthenticated(true);
    reqDTO.setoAuthClientAuthnContext(oAuthClientAuthnContext);
    mockStatic(IdentityTenantUtil.class);
    when(IdentityTenantUtil.isTenantQualifiedUrlsEnabled()).thenReturn(true);
    when(IdentityTenantUtil.getTenantDomainFromContext()).thenReturn(tenantInContext);
    when(OAuth2Util.class, "validateRequestTenantDomain", anyString()).thenCallRealMethod();
    mockPasswordGrantHandler(true, true, true, true);
    AccessTokenIssuer.getInstance().issue(reqDTO);
}
Also used : OAuth2Util(org.wso2.carbon.identity.oauth2.util.OAuth2Util) 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 27 with OAuthClientAuthnContext

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

the class AccessTokenIssuerTest method testIssueNoAuthorizationGrantHandler.

/**
 * No authorization grant handler found for the given grant type.
 *
 * @throws Exception
 */
@Test
public void testIssueNoAuthorizationGrantHandler() throws Exception {
    when(oAuthServerConfiguration.getSupportedGrantTypes()).thenReturn(new HashMap<String, AuthorizationGrantHandler>());
    OAuth2AccessTokenReqDTO reqDTO = new OAuth2AccessTokenReqDTO();
    reqDTO.setGrantType(DUMMY_GRANT_TYPE);
    OAuthClientAuthnContext oAuthClientAuthnContext = new OAuthClientAuthnContext();
    oAuthClientAuthnContext.setAuthenticated(true);
    reqDTO.setoAuthClientAuthnContext(oAuthClientAuthnContext);
    OAuth2AccessTokenRespDTO tokenRespDTO = AccessTokenIssuer.getInstance().issue(reqDTO);
    assertNotNull(tokenRespDTO);
    assertTrue(tokenRespDTO.isError());
    assertEquals(tokenRespDTO.getErrorCode(), OAuthError.TokenResponse.UNSUPPORTED_GRANT_TYPE);
}
Also used : OAuth2AccessTokenRespDTO(org.wso2.carbon.identity.oauth2.dto.OAuth2AccessTokenRespDTO) AuthorizationGrantHandler(org.wso2.carbon.identity.oauth2.token.handlers.grant.AuthorizationGrantHandler) 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 28 with OAuthClientAuthnContext

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

the class BasicAuthClientAuthenticatorTest method testAuthenticateClientExeption.

@Test(dataProvider = "testClientAuthnDataErrorScenario", expectedExceptions = OAuthClientAuthnException.class)
public void testAuthenticateClientExeption(String headerName, String headerValue, HashMap<String, List> bodyContent, Object oAuthClientAuthnContextObj, Object exception) throws Exception {
    OAuthClientAuthnContext oAuthClientAuthnContext = (OAuthClientAuthnContext) oAuthClientAuthnContextObj;
    HttpServletRequest httpServletRequest = PowerMockito.mock(HttpServletRequest.class);
    PowerMockito.mockStatic(OAuth2Util.class);
    if (exception instanceof IdentityOAuthAdminException) {
        PowerMockito.when(OAuth2Util.authenticateClient(Matchers.anyString(), Matchers.anyString())).thenThrow((IdentityOAuthAdminException) exception);
    } else if (exception instanceof IdentityOAuth2Exception) {
        PowerMockito.when(OAuth2Util.authenticateClient(Matchers.anyString(), Matchers.anyString())).thenThrow((IdentityOAuth2Exception) exception);
    }
    PowerMockito.when(httpServletRequest.getHeader(headerName)).thenReturn(headerValue);
    basicAuthClientAuthenticator.authenticateClient(httpServletRequest, bodyContent, oAuthClientAuthnContext);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) IdentityOAuthAdminException(org.wso2.carbon.identity.oauth.IdentityOAuthAdminException) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) OAuthClientAuthnContext(org.wso2.carbon.identity.oauth2.bean.OAuthClientAuthnContext) Test(org.testng.annotations.Test) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 29 with OAuthClientAuthnContext

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

the class BasicAuthClientAuthenticatorTest method buildOAuthClientAuthnContext.

private OAuthClientAuthnContext buildOAuthClientAuthnContext(String clientId, String clientSecret) {
    OAuthClientAuthnContext oAuthClientAuthnContext = new OAuthClientAuthnContext();
    oAuthClientAuthnContext.setClientId(clientId);
    if (StringUtils.isNotEmpty(clientSecret)) {
        oAuthClientAuthnContext.addParameter(OAuth.OAUTH_CLIENT_SECRET, clientSecret);
    }
    return oAuthClientAuthnContext;
}
Also used : OAuthClientAuthnContext(org.wso2.carbon.identity.oauth2.bean.OAuthClientAuthnContext)

Example 30 with OAuthClientAuthnContext

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

the class BasicAuthClientAuthenticatorTest method testGetClientIdErrorScenario.

@Test(dataProvider = "testGetClientIdDataErrorScenario", expectedExceptions = OAuthClientAuthnException.class)
public void testGetClientIdErrorScenario(String headerName, String headerValue, HashMap<String, List> bodyContent) throws Exception {
    HttpServletRequest httpServletRequest = PowerMockito.mock(HttpServletRequest.class);
    PowerMockito.when(httpServletRequest.getHeader(headerName)).thenReturn(headerValue);
    basicAuthClientAuthenticator.getClientId(httpServletRequest, bodyContent, new OAuthClientAuthnContext());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) OAuthClientAuthnContext(org.wso2.carbon.identity.oauth2.bean.OAuthClientAuthnContext) Test(org.testng.annotations.Test) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

OAuthClientAuthnContext (org.wso2.carbon.identity.oauth2.bean.OAuthClientAuthnContext)39 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)24 Test (org.testng.annotations.Test)24 PowerMockIdentityBaseTest (org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)21 OAuth2AccessTokenReqDTO (org.wso2.carbon.identity.oauth2.dto.OAuth2AccessTokenReqDTO)12 OAuth2AccessTokenRespDTO (org.wso2.carbon.identity.oauth2.dto.OAuth2AccessTokenRespDTO)10 Matchers.anyString (org.mockito.Matchers.anyString)9 HashMap (java.util.HashMap)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 OAuthRevocationRequestDTO (org.wso2.carbon.identity.oauth2.dto.OAuthRevocationRequestDTO)7 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)6 AuthorizationGrantHandler (org.wso2.carbon.identity.oauth2.token.handlers.grant.AuthorizationGrantHandler)6 Map (java.util.Map)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)3 Response (javax.ws.rs.core.Response)3 BeforeTest (org.testng.annotations.BeforeTest)3 Consumes (javax.ws.rs.Consumes)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2