org.wso2.carbon.identity.oauth2.stub.dto.OAuth2TokenValidationResponseDTO Java Examples

The following examples show how to use org.wso2.carbon.identity.oauth2.stub.dto.OAuth2TokenValidationResponseDTO. 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 6 votes vote down vote up
/**
 * Validates the OAuth 2.0 request
 *
 * @param accessTokenIdentifier The accessToken from the authorization header
 * @return OAuth2TokenValidationResponseDTO
 * @throws Exception
 */
public OAuth2TokenValidationResponseDTO validateAuthenticationRequest(String accessTokenIdentifier,
                                                                      List<OAuth2TokenValidationRequestDTO_TokenValidationContextParam> params) throws Exception {

    OAuth2TokenValidationRequestDTO oauthReq = new OAuth2TokenValidationRequestDTO();
    OAuth2TokenValidationRequestDTO_OAuth2AccessToken accessToken =
            new org.wso2.carbon.identity.oauth2.stub.dto.OAuth2TokenValidationRequestDTO_OAuth2AccessToken();
    accessToken.setTokenType(OAuthConstants.BEARER_TOKEN_TYPE);
    accessToken.setIdentifier(accessTokenIdentifier);
    oauthReq.setAccessToken(accessToken);
    oauthReq.setContext(params.toArray(new OAuth2TokenValidationRequestDTO_TokenValidationContextParam[params.size()]));
    try {
        return stub.validate(oauthReq);
    } catch (RemoteException e) {
        throw new Exception("Error while validating OAuth2 request", e);
    }
}
 
Example #2
Source File: OAuthServiceClient.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * Validates the OAuth 2.0 request
 *
 * @param accessTokenIdentifier
 * @return
 * @throws Exception
 */
public OAuth2TokenValidationResponseDTO validateAccessToken(String accessTokenIdentifier)
        throws Exception {
    OAuth2TokenValidationRequestDTO oauthReq = new OAuth2TokenValidationRequestDTO();
    OAuth2TokenValidationRequestDTO_OAuth2AccessToken accessToken =
            new OAuth2TokenValidationRequestDTO_OAuth2AccessToken();
    accessToken.setTokenType(BEARER_TOKEN_TYPE);
    accessToken.setIdentifier(accessTokenIdentifier);
    oauthReq.setAccessToken(accessToken);
    try {
        return stub.validate(oauthReq);
    } catch (RemoteException e) {
        log.error("Error while validating OAuth2 request");
        throw new Exception("Error while validating OAuth2 request", e);
    }
}
 
Example #3
Source File: OAuthHandler.java    From attic-stratos with Apache License 2.0 6 votes vote down vote up
public Response handle(Message message, ClassResourceInfo classResourceInfo) {
    try {
        OAuth2TokenValidationResponseDTO respDTO;
        ValidationServiceClient validationServiceClient = new
                ValidationServiceClient(oauthValidationEndpoint, username, password);
        HttpHeaders httpHeaders = new HttpHeadersImpl(message);
        String header = httpHeaders.getRequestHeaders().getFirst("Authorization");
        // if the authorization token has Bearer..
        if (header.startsWith("Bearer ")) {
            String accessToken = header.substring(7).trim();
            respDTO = validationServiceClient.validateAuthenticationRequest(accessToken); //TODO : send scope params
            boolean valid = respDTO.getValid();
            if (!valid) {
                // authorization failure..
                return Response.status(Response.Status.FORBIDDEN).build();
            }
        }
    } catch (Exception e) {
        log.error("Error while validating access token", e);
        return Response.status(Response.Status.FORBIDDEN).build();
    }
    AuthenticationContext.setAuthenticated(true);
    return null;
}
 
Example #4
Source File: OAuthHandler.java    From product-private-paas with Apache License 2.0 6 votes vote down vote up
public Response handle(Message message, ClassResourceInfo classResourceInfo) {
    try {
        OAuth2TokenValidationResponseDTO respDTO;
        ValidationServiceClient validationServiceClient = new ValidationServiceClient(oauthValidationEndpoint,
                username, password);
        HttpHeaders httpHeaders = new HttpHeadersImpl(message);
        String header = httpHeaders.getRequestHeaders().getFirst("Authorization");
        // if the authorization token has Bearer..
        if (header.startsWith("Bearer ")) {
            String accessToken = header.substring(7).trim();
            respDTO = validationServiceClient.validateAuthenticationRequest(accessToken); //TODO : send scope params
            boolean valid = respDTO.getValid();
            if (!valid) {
                // authorization failure..
                return Response.status(Response.Status.FORBIDDEN).build();
            }
        }
    } catch (Exception e) {
        log.error("Error while validating access token", e);
        return Response.status(Response.Status.FORBIDDEN).build();
    }
    AuthenticationContext.setAuthenticated(true);
    return null;
}
 
Example #5
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 #6
Source File: ValidationServiceClient.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
public OAuth2TokenValidationResponseDTO validateAuthenticationRequest(String accessToken) throws Exception {
    OAuth2TokenValidationRequestDTO oauthReq = new OAuth2TokenValidationRequestDTO();
    OAuth2TokenValidationRequestDTO_OAuth2AccessToken oAuth2AccessToken
            = new OAuth2TokenValidationRequestDTO_OAuth2AccessToken();
    oAuth2AccessToken.setIdentifier(accessToken);
    oAuth2AccessToken.setTokenType(OAuthConstants.BEARER_TOKEN_TYPE);
    oauthReq.setAccessToken(oAuth2AccessToken);
    try {
        return stub.validate(oauthReq);
    } catch (RemoteException e) {
        log.error("Error while validating OAuth2 request");
        throw new Exception("Error while validating OAuth2 request", e);
    }
}
 
Example #7
Source File: ValidationServiceClient.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
public OAuth2TokenValidationResponseDTO validateAuthenticationRequest(String accessToken) throws Exception {
    OAuth2TokenValidationRequestDTO oauthReq = new OAuth2TokenValidationRequestDTO();
    OAuth2TokenValidationRequestDTO_OAuth2AccessToken oAuth2AccessToken
            = new OAuth2TokenValidationRequestDTO_OAuth2AccessToken();
    oAuth2AccessToken.setIdentifier(accessToken);
    oAuth2AccessToken.setTokenType(OAuthConstants.BEARER_TOKEN_TYPE);
    oauthReq.setAccessToken(oAuth2AccessToken);
    try {
        return stub.validate(oauthReq);
    } catch (RemoteException e) {
        log.error("Error while validating OAuth2 request");
        throw new Exception("Error while validating OAuth2 request", e);
    }
}
 
Example #8
Source File: ValidationServiceClient.java    From product-private-paas with Apache License 2.0 5 votes vote down vote up
public OAuth2TokenValidationResponseDTO validateAuthenticationRequest(String accessToken) throws Exception {
    OAuth2TokenValidationRequestDTO oauthReq = new OAuth2TokenValidationRequestDTO();
    oauthReq.setAccessToken(accessToken);
    oauthReq.setTokenType(OAuthConstants.BEARER_TOKEN_TYPE);
    try {
        return stub.validate(oauthReq);
    } catch (RemoteException e) {
        log.error("Error while validating OAuth2 request");
        throw new Exception("Error while validating OAuth2 request", e);
    }
}
 
Example #9
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");

}