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());
}
}
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);
}
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;
}
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();
}
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());
}
Aggregations