use of org.apache.cxf.rs.security.oauth2.common.ClientAccessToken in project tesb-rt-se by Talend.
the class RestaurantReservationService method completeReservation.
@GET
@Path("complete")
@Produces({ "text/html", "application/xml;q=0.9" })
public Response completeReservation(@QueryParam("code") String code, @QueryParam("state") String state) {
String userName = sc.getUserPrincipal().getName();
Map<String, ReservationRequest> userRequests = requests.get(userName);
if (userRequests == null) {
return redirectToFailureHandler(NO_REQUEST_USER);
}
if (state == null) {
return redirectToFailureHandler(NO_REQUEST_STATE);
}
ReservationRequest request = userRequests.remove(state);
if (request == null) {
return redirectToFailureHandler(NO_REQUEST_AVAILABLE);
}
if (code == null) {
return redirectToFailureHandler(NO_CODE_GRANT);
}
LOG.info("Completing the reservation request for a user: " + request.getReserveName());
AuthorizationCodeGrant codeGrant = new AuthorizationCodeGrant(code, getCallbackURI());
LOG.info("Requesting OAuth server to replace an authorized request token with an access token");
ClientAccessToken accessToken = manager.getAccessToken(codeGrant);
if (accessToken == null) {
return redirectToFailureHandler(NO_OAUTH_ACCESS_TOKEN);
}
Calendar c = null;
try {
String authHeader = manager.createAuthorizationHeader(accessToken);
socialService.replaceHeader("Authorization", authHeader);
c = socialService.get(Calendar.class);
} catch (RuntimeException ex) {
return redirectToFailureHandler(CALENDAR_ACCESS_PROBLEM);
}
CalendarEntry entry = c.getEntry(request.getHour());
if (entry.getEventDescription() == null || entry.getEventDescription().trim().isEmpty()) {
String address = restaurantService.post(new Form().param("name", request.getReserveName()).param("phone", request.getContactPhone()).param("hour", Integer.toString(request.getHour())), String.class);
if (address == null) {
return redirectToFailureHandler(NO_RESERVATION);
}
// update the user's calendar
String authHeader = manager.createAuthorizationHeader(accessToken);
socialService.replaceHeader("Authorization", authHeader);
Response response = socialService.form(new Form().param("hour", Integer.toString(request.getHour())).param("description", "Table reserved at " + address));
boolean calendarUpdated = response.getStatus() == 200 || response.getStatus() == 204;
return Response.ok(new ReservationConfirmation(address, request.getHour(), calendarUpdated)).build();
} else {
return redirectToFailureHandler(CALENDAR_BUSY);
}
}
use of org.apache.cxf.rs.security.oauth2.common.ClientAccessToken in project teiid by teiid.
the class OAuth20CredentialImpl method getAccessToken.
protected ClientAccessToken getAccessToken() {
if (getAccessTokenString() != null) {
// if we have access_token directly, use it
return new ClientAccessToken(OAuthConstants.ACCESS_TOKEN_TYPE, getAccessTokenString());
}
Consumer consumer = new Consumer(getClientId(), getClientSecret());
WebClient client = WebClient.create(getAccessTokenURI());
RefreshTokenGrant grant = new RefreshTokenGrant(getRefreshToken());
return OAuthClientUtils.getAccessToken(client, consumer, grant, null, "Bearer", false);
}
use of org.apache.cxf.rs.security.oauth2.common.ClientAccessToken in project testcases by coheigea.
the class AuthorizationGrantTest method testAuthorizationCodeGrant.
@org.junit.Test
public void testAuthorizationCodeGrant() throws Exception {
URL busFile = AuthorizationGrantTest.class.getResource("cxf-client.xml");
String address = "https://localhost:" + PORT + "/services/";
WebClient client = WebClient.create(address, setupProviders(), "alice", "security", busFile.toString());
// Save the Cookie for the second request...
WebClient.getConfig(client).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
// Get Authorization Code
String code = getAuthorizationCode(client);
assertNotNull(code);
// Now get the access token
client = WebClient.create(address, setupProviders(), "consumer-id", "this-is-a-secret", busFile.toString());
// Save the Cookie for the second request...
WebClient.getConfig(client).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
ClientAccessToken accessToken = getAccessTokenWithAuthorizationCode(client, code);
assertNotNull(accessToken.getTokenKey());
}
use of org.apache.cxf.rs.security.oauth2.common.ClientAccessToken in project testcases by coheigea.
the class AuthorizationGrantTest method testClientCredentialsGrant.
@org.junit.Test
public void testClientCredentialsGrant() throws Exception {
URL busFile = AuthorizationGrantTest.class.getResource("cxf-client.xml");
String address = "https://localhost:" + PORT + "/services/";
WebClient client = WebClient.create(address, setupProviders(), "consumer-id", "this-is-a-secret", busFile.toString());
// Get Access Token
client.type("application/x-www-form-urlencoded").accept("application/json");
client.path("token");
Form form = new Form();
form.param("grant_type", "client_credentials");
Response response = client.post(form);
ClientAccessToken accessToken = response.readEntity(ClientAccessToken.class);
assertNotNull(accessToken.getTokenKey());
assertNotNull(accessToken.getRefreshToken());
}
use of org.apache.cxf.rs.security.oauth2.common.ClientAccessToken in project testcases by coheigea.
the class AuthorizationGrantTest method testAuthorizationCodeGrantRefresh.
@org.junit.Test
public void testAuthorizationCodeGrantRefresh() throws Exception {
URL busFile = AuthorizationGrantTest.class.getResource("cxf-client.xml");
String address = "https://localhost:" + PORT + "/services/";
WebClient client = WebClient.create(address, setupProviders(), "alice", "security", busFile.toString());
// Save the Cookie for the second request...
WebClient.getConfig(client).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
// Get Authorization Code
String code = getAuthorizationCode(client);
assertNotNull(code);
// Now get the access token
client = WebClient.create(address, setupProviders(), "consumer-id", "this-is-a-secret", busFile.toString());
// Save the Cookie for the second request...
WebClient.getConfig(client).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
ClientAccessToken accessToken = getAccessTokenWithAuthorizationCode(client, code);
assertNotNull(accessToken.getTokenKey());
assertNotNull(accessToken.getRefreshToken());
// Refresh the access token
client.type("application/x-www-form-urlencoded").accept("application/json");
Form form = new Form();
form.param("grant_type", "refresh_token");
form.param("refresh_token", accessToken.getRefreshToken());
form.param("client_id", "consumer-id");
Response response = client.post(form);
accessToken = response.readEntity(ClientAccessToken.class);
assertNotNull(accessToken.getTokenKey());
assertNotNull(accessToken.getRefreshToken());
}
Aggregations