Search in sources :

Example 21 with ClientAccessToken

use of org.apache.cxf.rs.security.oauth2.common.ClientAccessToken in project cxf by apache.

the class BigQueryServer method main.

public static void main(String[] args) throws Exception {
    final String pc12File = args[0];
    final String keySecret = args[1];
    final String issuer = args[2];
    final String projectId = args[3];
    PrivateKey privateKey = loadPrivateKey(pc12File, keySecret);
    ClientAccessToken accessToken = getAccessToken(privateKey, issuer);
    WebClient bigQueryClient = WebClient.create("https://www.googleapis.com/bigquery/v2/projects/" + projectId + "/queries", Collections.singletonList(new JsonMapObjectProvider()));
    bigQueryClient.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON);
    List<ShakespeareText> texts = BigQueryService.getMatchingTexts(bigQueryClient, accessToken, "brave", "10");
    System.out.println("Matching texts:");
    for (ShakespeareText text : texts) {
        System.out.println(text.getText() + ":" + text.getDate());
    }
}
Also used : PrivateKey(java.security.PrivateKey) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) JsonMapObjectProvider(org.apache.cxf.jaxrs.provider.json.JsonMapObjectProvider) WebClient(org.apache.cxf.jaxrs.client.WebClient)

Example 22 with ClientAccessToken

use of org.apache.cxf.rs.security.oauth2.common.ClientAccessToken in project cxf by apache.

the class BigQueryServer method getAccessToken.

private static ClientAccessToken getAccessToken(PrivateKey privateKey, String issuer) {
    JwsHeaders headers = new JwsHeaders(JoseType.JWT, SignatureAlgorithm.RS256);
    JwtClaims claims = new JwtClaims();
    claims.setIssuer(issuer);
    claims.setAudience("https://www.googleapis.com/oauth2/v3/token");
    long issuedAt = OAuthUtils.getIssuedAt();
    claims.setIssuedAt(issuedAt);
    claims.setExpiryTime(issuedAt + 60 * 60);
    claims.setProperty("scope", "https://www.googleapis.com/auth/bigquery.readonly");
    JwtToken token = new JwtToken(headers, claims);
    JwsJwtCompactProducer p = new JwsJwtCompactProducer(token);
    String base64UrlAssertion = p.signWith(privateKey);
    JwtBearerGrant grant = new JwtBearerGrant(base64UrlAssertion);
    WebClient accessTokenService = WebClient.create("https://www.googleapis.com/oauth2/v3/token", Arrays.asList(new OAuthJSONProvider(), new AccessTokenGrantWriter()));
    WebClient.getConfig(accessTokenService).getInInterceptors().add(new LoggingInInterceptor());
    accessTokenService.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.APPLICATION_JSON);
    return accessTokenService.post(grant, ClientAccessToken.class);
}
Also used : JwtToken(org.apache.cxf.rs.security.jose.jwt.JwtToken) JwsHeaders(org.apache.cxf.rs.security.jose.jws.JwsHeaders) JwsJwtCompactProducer(org.apache.cxf.rs.security.jose.jws.JwsJwtCompactProducer) JwtClaims(org.apache.cxf.rs.security.jose.jwt.JwtClaims) JwtBearerGrant(org.apache.cxf.rs.security.oauth2.grants.jwt.JwtBearerGrant) AccessTokenGrantWriter(org.apache.cxf.rs.security.oauth2.client.AccessTokenGrantWriter) OAuthJSONProvider(org.apache.cxf.rs.security.oauth2.provider.OAuthJSONProvider) LoggingInInterceptor(org.apache.cxf.ext.logging.LoggingInInterceptor) WebClient(org.apache.cxf.jaxrs.client.WebClient)

Example 23 with ClientAccessToken

use of org.apache.cxf.rs.security.oauth2.common.ClientAccessToken in project cxf by apache.

the class BigQueryService method completeBigQuerySearch.

@POST
@Path("/complete")
@Consumes("application/x-www-form-urlencoded")
@Produces("text/html")
public BigQueryResponse completeBigQuerySearch(@FormParam("word") String searchWord, @FormParam("maxResults") String maxResults) {
    ClientAccessToken accessToken = oidcContext.getToken();
    BigQueryResponse bigQueryResponse = new BigQueryResponse(getUserInfo(), searchWord);
    bigQueryResponse.setTexts(getMatchingTexts(bigQueryClient, accessToken, searchWord, maxResults));
    return bigQueryResponse;
}
Also used : ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 24 with ClientAccessToken

use of org.apache.cxf.rs.security.oauth2.common.ClientAccessToken in project cxf by apache.

the class AccessTokenService method handleTokenRequest.

/**
 * Processes an access token request
 * @param params the form parameters representing the access token grant
 * @return Access Token or the error
 */
@POST
@Consumes("application/x-www-form-urlencoded")
@Produces("application/json")
public Response handleTokenRequest(MultivaluedMap<String, String> params) {
    // Make sure the client is authenticated
    Client client = authenticateClientIfNeeded(params);
    if (!OAuthUtils.isGrantSupportedForClient(client, isCanSupportPublicClients(), params.getFirst(OAuthConstants.GRANT_TYPE))) {
        LOG.log(Level.FINE, "The grant type {} is not supported for the client", params.getFirst(OAuthConstants.GRANT_TYPE));
        return createErrorResponse(params, OAuthConstants.UNAUTHORIZED_CLIENT);
    }
    try {
        checkAudience(client, params);
    } catch (OAuthServiceException ex) {
        return super.createErrorResponseFromBean(ex.getError());
    }
    // Find the grant handler
    AccessTokenGrantHandler handler = findGrantHandler(params);
    if (handler == null) {
        LOG.fine("No Grant Handler found");
        return createErrorResponse(params, OAuthConstants.UNSUPPORTED_GRANT_TYPE);
    }
    // Create the access token
    final ServerAccessToken serverToken;
    try {
        serverToken = handler.createAccessToken(client, params);
    } catch (WebApplicationException ex) {
        throw ex;
    } catch (RuntimeException ex) {
        LOG.log(Level.FINE, "Error creating the access token", ex);
        // This is done to bypass a Check-Style
        // restriction on a number of return statements
        OAuthServiceException oauthEx = ex instanceof OAuthServiceException ? (OAuthServiceException) ex : new OAuthServiceException(ex);
        return handleException(oauthEx, OAuthConstants.INVALID_GRANT);
    }
    if (serverToken == null) {
        LOG.fine("No access token was created");
        return createErrorResponse(params, OAuthConstants.INVALID_GRANT);
    }
    // Extract the information to be of use for the client
    ClientAccessToken clientToken = OAuthUtils.toClientAccessToken(serverToken, isWriteOptionalParameters());
    processClientAccessToken(clientToken, serverToken);
    // Return it to the client
    return Response.ok(clientToken).header(HttpHeaders.CACHE_CONTROL, "no-store").header("Pragma", "no-cache").build();
}
Also used : ServerAccessToken(org.apache.cxf.rs.security.oauth2.common.ServerAccessToken) WebApplicationException(javax.ws.rs.WebApplicationException) OAuthServiceException(org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException) AccessTokenGrantHandler(org.apache.cxf.rs.security.oauth2.provider.AccessTokenGrantHandler) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) Client(org.apache.cxf.rs.security.oauth2.common.Client) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 25 with ClientAccessToken

use of org.apache.cxf.rs.security.oauth2.common.ClientAccessToken in project cxf by apache.

the class OAuthClientUtilsTest method fromMapToClientToken.

@Test
public void fromMapToClientToken() {
    final Map<String, String> map = new HashMap<>();
    final String accessToken = "SlAV32hkKG";
    map.put(OAuthConstants.ACCESS_TOKEN, accessToken);
    final String tokenType = "Bearer";
    map.put(OAuthConstants.ACCESS_TOKEN_TYPE, tokenType);
    final String refreshToken = "8xLOxBtZp8";
    map.put(OAuthConstants.REFRESH_TOKEN, refreshToken);
    final String expiresIn = "3600";
    map.put(OAuthConstants.ACCESS_TOKEN_EXPIRES_IN, expiresIn);
    final ClientAccessToken token = OAuthClientUtils.fromMapToClientToken(map);
    assertEquals(accessToken, token.getTokenKey());
    assertEquals(tokenType, token.getTokenType());
    assertEquals(refreshToken, token.getRefreshToken());
    assertEquals(Long.parseLong(expiresIn), token.getExpiresIn());
}
Also used : HashMap(java.util.HashMap) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) Test(org.junit.Test)

Aggregations

ClientAccessToken (org.apache.cxf.rs.security.oauth2.common.ClientAccessToken)134 WebClient (org.apache.cxf.jaxrs.client.WebClient)116 URL (java.net.URL)53 Response (javax.ws.rs.core.Response)51 Form (javax.ws.rs.core.Form)41 Test (org.junit.Test)21 Consumer (org.apache.cxf.rs.security.oauth2.client.Consumer)16 Book (org.apache.cxf.systest.jaxrs.security.Book)12 JwsJwtCompactConsumer (org.apache.cxf.rs.security.jose.jws.JwsJwtCompactConsumer)11 TokenIntrospection (org.apache.cxf.rs.security.oauth2.common.TokenIntrospection)11 OAuthServiceException (org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException)8 JsonMapObjectProvider (org.apache.cxf.jaxrs.provider.json.JsonMapObjectProvider)7 OAuthJSONProvider (org.apache.cxf.rs.security.oauth2.provider.OAuthJSONProvider)7 ClientRegistration (org.apache.cxf.rs.security.oauth2.services.ClientRegistration)7 ClientRegistrationResponse (org.apache.cxf.rs.security.oauth2.services.ClientRegistrationResponse)7 ResponseProcessingException (javax.ws.rs.client.ResponseProcessingException)6 JwtToken (org.apache.cxf.rs.security.jose.jwt.JwtToken)6 AuthorizationCodeGrant (org.apache.cxf.rs.security.oauth2.grants.code.AuthorizationCodeGrant)6 HashMap (java.util.HashMap)4 Produces (javax.ws.rs.Produces)4