Search in sources :

Example 11 with OAuthClientAuthnContext

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

the class BasicAuthClientAuthenticatorTest method testAuthenticateClient.

@Test(dataProvider = "testClientAuthnData")
public void testAuthenticateClient(String headerName, String headerValue, HashMap<String, List> bodyContent, Object oAuthClientAuthnContextObj, boolean isAuthenticated, boolean authenticationResult) throws Exception {
    OAuthClientAuthnContext oAuthClientAuthnContext = (OAuthClientAuthnContext) oAuthClientAuthnContextObj;
    HttpServletRequest httpServletRequest = PowerMockito.mock(HttpServletRequest.class);
    PowerMockito.mockStatic(OAuth2Util.class);
    PowerMockito.when(OAuth2Util.authenticateClient(Matchers.anyString(), Matchers.anyString())).thenReturn(isAuthenticated);
    PowerMockito.when(httpServletRequest.getHeader(headerName)).thenReturn(headerValue);
    assertEquals(basicAuthClientAuthenticator.authenticateClient(httpServletRequest, bodyContent, oAuthClientAuthnContext), authenticationResult, "Expected client authentication result was not " + "received");
}
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)

Example 12 with OAuthClientAuthnContext

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

the class BasicAuthClientAuthenticatorTest method testCanAuthenticate.

@Test(dataProvider = "testCanAuthenticateData")
public void testCanAuthenticate(String headerName, String headerValue, HashMap<String, List> bodyContent, boolean canHandle) throws Exception {
    HttpServletRequest httpServletRequest = PowerMockito.mock(HttpServletRequest.class);
    PowerMockito.when(httpServletRequest.getHeader(headerName)).thenReturn(headerValue);
    assertEquals(basicAuthClientAuthenticator.canAuthenticate(httpServletRequest, bodyContent, new OAuthClientAuthnContext()), canHandle, "Expected can authenticate evaluation not received");
}
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)

Example 13 with OAuthClientAuthnContext

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

the class BasicAuthClientAuthenticatorTest method testGetClientId.

@Test(dataProvider = "testGetClientIdData")
public void testGetClientId(String headerName, String headerValue, HashMap<String, List> bodyContent, String clientId) throws Exception {
    HttpServletRequest httpServletRequest = PowerMockito.mock(HttpServletRequest.class);
    PowerMockito.when(httpServletRequest.getHeader(headerName)).thenReturn(headerValue);
    assertEquals(basicAuthClientAuthenticator.getClientId(httpServletRequest, bodyContent, new OAuthClientAuthnContext()), clientId);
}
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)

Example 14 with OAuthClientAuthnContext

use of org.wso2.carbon.identity.oauth2.bean.OAuthClientAuthnContext 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 15 with OAuthClientAuthnContext

use of org.wso2.carbon.identity.oauth2.bean.OAuthClientAuthnContext 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)

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