org.wso2.carbon.identity.oauth2.stub.OAuth2TokenValidationServiceStub Java Examples

The following examples show how to use org.wso2.carbon.identity.oauth2.stub.OAuth2TokenValidationServiceStub. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OAuth2TokenValidationServiceClient.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * OAuth2TokenValidationService Admin Service Client
 *
 * @param backendServerURL The server URL of the WSO2 Identity Server
 * @param username The user name to be used to log into the WSO2 Identity Server with admin privileges
 * @param password The password used to log into the WSO2 Identity Server with admin privileges
 * @param configCtx The configuration context instance
 * @throws Exception
 */
public OAuth2TokenValidationServiceClient(String backendServerURL, String username, String password,
                                          ConfigurationContext configCtx) throws Exception {
    String serviceURL = backendServerURL + "OAuth2TokenValidationService";
    try {
        stub = new OAuth2TokenValidationServiceStub(configCtx, serviceURL);
        MicroIntegratorBaseUtils.setBasicAccessSecurityHeaders(username, password, true, stub._getServiceClient());
    } catch (AxisFault e) {
        throw new Exception("Error initializing OAuth Client", e);
    }
}
 
Example #2
Source File: OAuthTokenValidationStubFactory.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
@Override
public void passivateObject(Object o) throws Exception {
    if (o instanceof OAuth2TokenValidationServiceStub) {
        OAuth2TokenValidationServiceStub stub = (OAuth2TokenValidationServiceStub) o;
        stub._getServiceClient().cleanupTransport();
    }
}
 
Example #3
Source File: ExternalOAuthValidator.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
/**
 * This method gets a string accessToken and validates it and generate the OAuth2ClientApplicationDTO
 * containing the validity and user details if valid.
 *
 * @param token which need to be validated.
 * @return OAuthValidationResponse with the validated results.
 */
public OAuthValidationResponse validateToken(String token) throws RemoteException {
    OAuth2TokenValidationRequestDTO validationRequest = new OAuth2TokenValidationRequestDTO();
    OAuth2TokenValidationRequestDTO_OAuth2AccessToken accessToken =
            new OAuth2TokenValidationRequestDTO_OAuth2AccessToken();
    accessToken.setTokenType(OauthAuthenticatorConstants.BEARER_TOKEN_TYPE);
    accessToken.setIdentifier(token);
    validationRequest.setAccessToken(accessToken);
    OAuth2TokenValidationServiceStub tokenValidationService =
            new OAuth2TokenValidationServiceStub(hostURL);
    ServiceClient client = tokenValidationService._getServiceClient();
    Options options = client.getOptions();
    List<Header> headerList = new ArrayList<>();
    Header header = new Header();
    header.setName(HTTPConstants.HEADER_AUTHORIZATION);
    header.setValue(OauthAuthenticatorConstants.AUTHORIZATION_HEADER_PREFIX_BASIC + " " + getBasicAuthCredentials());
    headerList.add(header);
    options.setProperty(org.apache.axis2.transport.http.HTTPConstants.HTTP_HEADERS, headerList);
    client.setOptions(options);
    OAuth2TokenValidationResponseDTO tokenValidationResponse = tokenValidationService.
            findOAuthConsumerIfTokenIsValid(validationRequest).getAccessTokenValidationResponse();
    boolean isValid = tokenValidationResponse.getValid();
    String userName = null;
    String tenantDomain = null;
    if (isValid) {
        userName = MultitenantUtils.getTenantAwareUsername(
                tokenValidationResponse.getAuthorizedUser());
        tenantDomain = MultitenantUtils.
                getTenantDomain(tokenValidationResponse.getAuthorizedUser());
    }
    return new OAuthValidationResponse(userName,tenantDomain,isValid);
}
 
Example #4
Source File: OAuthServiceClient.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * OAuth2TokenValidationService Admin Service Client
 *
 * @param backendServerURL
 * @param username
 * @param password
 * @param configCtx
 * @throws Exception
 */
public OAuthServiceClient(String backendServerURL, String username, String password,
                          ConfigurationContext configCtx) throws Exception {
    String serviceURL = backendServerURL + "OAuth2TokenValidationService";
    try {
        stub = new OAuth2TokenValidationServiceStub(configCtx, serviceURL);
        CarbonUtils.setBasicAccessSecurityHeaders(username, password, true, stub._getServiceClient());
    } catch (AxisFault e) {
        log.error("Error initializing OAuth2 Client");
        throw new Exception("Error initializing OAuth Client", e);
    }
}
 
Example #5
Source File: ValidationServiceClient.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
public ValidationServiceClient(String backendServerURL, String username, String password) throws Exception {
    String serviceURL = backendServerURL + "OAuth2TokenValidationService";
    try {
        stub = new OAuth2TokenValidationServiceStub(serviceURL);
        CarbonUtils.setBasicAccessSecurityHeaders(username, password, true, stub._getServiceClient());
    } catch (AxisFault e) {
        log.error("Error initializing OAuth2 Client");
        throw new Exception("Error initializing OAuth Client", e);
    }
}
 
Example #6
Source File: ValidationServiceClient.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
public ValidationServiceClient(String backendServerURL, String username, String password) throws Exception {
    String serviceURL = backendServerURL + "OAuth2TokenValidationService";
    try {
        stub = new OAuth2TokenValidationServiceStub(serviceURL);
        CarbonUtils.setBasicAccessSecurityHeaders(username, password, true, stub._getServiceClient());
    } catch (AxisFault e) {
        log.error("Error initializing OAuth2 Client");
        throw new Exception("Error initializing OAuth Client", e);
    }
}
 
Example #7
Source File: ValidationServiceClient.java    From product-private-paas with Apache License 2.0 5 votes vote down vote up
public ValidationServiceClient(String backendServerURL, String username, String password) throws Exception {
    String serviceURL = backendServerURL + "OAuth2TokenValidationService";
    try {
        stub = new OAuth2TokenValidationServiceStub(serviceURL);
        CarbonUtils.setBasicAccessSecurityHeaders(username, password, true, stub._getServiceClient());
    } catch (AxisFault e) {
        log.error("Error initializing OAuth2 Client");
        throw new Exception("Error initializing OAuth Client", e);
    }
}
 
Example #8
Source File: OauthAuthenticatorTest.java    From carbon-device-mgt with Apache License 2.0 4 votes vote down vote up
@Test(description = "This method tests the authenticate under different parameters",
        dependsOnMethods = {"testInit"})
public void testAuthenticate() throws Exception {
    Request request = createOauthRequest(BEARER_HEADER);
    Assert.assertEquals(oAuthAuthenticator.authenticate(request, null).getStatus(),
            WebappAuthenticator.Status.CONTINUE, "Authentication status mismatched");
    request = createOauthRequest(BEARER_HEADER + "abc");
    org.apache.coyote.Request coyoteRequest = request.getCoyoteRequest();
    Field uriMB = org.apache.coyote.Request.class.getDeclaredField("uriMB");
    uriMB.setAccessible(true);
    MessageBytes bytes = MessageBytes.newInstance();
    bytes.setString("test");
    uriMB.set(coyoteRequest, bytes);
    request.setCoyoteRequest(coyoteRequest);
    Field tokenValidator = OAuthAuthenticator.class.getDeclaredField("tokenValidator");
    tokenValidator.setAccessible(true);

    GenericObjectPool genericObjectPool = Mockito.mock(GenericObjectPool.class, Mockito.CALLS_REAL_METHODS);
    RemoteOAuthValidator remoteOAuthValidator = Mockito
            .mock(RemoteOAuthValidator.class, Mockito.CALLS_REAL_METHODS);
    tokenValidator.set(oAuthAuthenticator, remoteOAuthValidator);
    Field stubs = RemoteOAuthValidator.class.getDeclaredField("stubs");
    stubs.setAccessible(true);
    stubs.set(remoteOAuthValidator, genericObjectPool);
    OAuth2TokenValidationResponseDTO oAuth2TokenValidationResponseDTO = new OAuth2TokenValidationResponseDTO();
    oAuth2TokenValidationResponseDTO.setValid(true);
    oAuth2TokenValidationResponseDTO.setAuthorizedUser("[email protected]");
    OAuth2ClientApplicationDTO oAuth2ClientApplicationDTO = Mockito
            .mock(OAuth2ClientApplicationDTO.class, Mockito.CALLS_REAL_METHODS);
    Mockito.doReturn(oAuth2TokenValidationResponseDTO).when(oAuth2ClientApplicationDTO)
            .getAccessTokenValidationResponse();
    OAuth2TokenValidationServiceStub oAuth2TokenValidationServiceStub = Mockito
            .mock(OAuth2TokenValidationServiceStub.class, Mockito.CALLS_REAL_METHODS);
    Mockito.doReturn(oAuth2ClientApplicationDTO).when(oAuth2TokenValidationServiceStub)
            .findOAuthConsumerIfTokenIsValid(Mockito.any());
    Mockito.doReturn(oAuth2TokenValidationServiceStub).when(genericObjectPool).borrowObject();
    oAuthAuthenticator.canHandle(request);
    AuthenticationInfo authenticationInfo = oAuthAuthenticator.authenticate(request, null);
    Assert.assertEquals(authenticationInfo.getUsername(), "admin");

}