use of org.apache.cxf.rs.security.oauth2.client.Consumer in project cxf by apache.
the class OAuth2FiltersTest method testServiceWithTokenUsingIncorrectAudience.
@org.junit.Test
public void testServiceWithTokenUsingIncorrectAudience() throws Exception {
URL busFile = OAuth2FiltersTest.class.getResource("client.xml");
// Get Authorization Code
String oauthService = "https://localhost:" + OAUTH_PORT + "/services/";
WebClient oauthClient = WebClient.create(oauthService, OAuth2TestUtils.setupProviders(), "alice", "security", busFile.toString());
// Save the Cookie for the second request...
WebClient.getConfig(oauthClient).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
String code = OAuth2TestUtils.getAuthorizationCode(oauthClient, null, "consumer-id-aud2");
assertNotNull(code);
// Now get the access token
oauthClient = WebClient.create(oauthService, OAuth2TestUtils.setupProviders(), "consumer-id-aud2", "this-is-a-secret", busFile.toString());
// Save the Cookie for the second request...
WebClient.getConfig(oauthClient).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
String address = "https://localhost:" + PORT + "/securedxyz/bookstore/books";
ClientAccessToken accessToken = OAuth2TestUtils.getAccessTokenWithAuthorizationCode(oauthClient, code, "consumer-id-aud2", address);
assertNotNull(accessToken.getTokenKey());
// Now invoke on the service with the access token
WebClient client = WebClient.create(address, OAuth2TestUtils.setupProviders(), busFile.toString());
client.header("Authorization", "Bearer " + accessToken.getTokenKey());
Response response = client.post(new Book("book", 123L));
assertNotEquals(response.getStatus(), 200);
}
use of org.apache.cxf.rs.security.oauth2.client.Consumer in project cxf by apache.
the class OAuth2FiltersTest method testServiceWithTokenAndMultipleScopes.
@org.junit.Test
public void testServiceWithTokenAndMultipleScopes() throws Exception {
URL busFile = OAuth2FiltersTest.class.getResource("client.xml");
// Get Authorization Code
String oauthService = "https://localhost:" + OAUTH_PORT + "/services/";
WebClient oauthClient = WebClient.create(oauthService, OAuth2TestUtils.setupProviders(), "alice", "security", busFile.toString());
// Save the Cookie for the second request...
WebClient.getConfig(oauthClient).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
String code = OAuth2TestUtils.getAuthorizationCode(oauthClient, "read_book create_image create_book");
assertNotNull(code);
// Now get the access token
oauthClient = WebClient.create(oauthService, OAuth2TestUtils.setupProviders(), "consumer-id", "this-is-a-secret", busFile.toString());
// Save the Cookie for the second request...
WebClient.getConfig(oauthClient).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
ClientAccessToken accessToken = OAuth2TestUtils.getAccessTokenWithAuthorizationCode(oauthClient, code);
assertNotNull(accessToken.getTokenKey());
// Now invoke on the service with the access token
String address = "https://localhost:" + PORT + "/secured/bookstore/books";
WebClient client = WebClient.create(address, OAuth2TestUtils.setupProviders(), busFile.toString());
client.header("Authorization", "Bearer " + accessToken.getTokenKey());
Response response = client.type("application/xml").post(new Book("book", 123L));
assertEquals(response.getStatus(), 200);
Book returnedBook = response.readEntity(Book.class);
assertEquals(returnedBook.getName(), "book");
assertEquals(returnedBook.getId(), 123L);
}
use of org.apache.cxf.rs.security.oauth2.client.Consumer in project cxf by apache.
the class OAuth2JwtFiltersTest method doTestServiceWithJwtTokenAndScope.
private void doTestServiceWithJwtTokenAndScope(String oauthService, String rsAddress) throws Exception {
URL busFile = OAuth2JwtFiltersTest.class.getResource("client.xml");
// Get Authorization Code
WebClient oauthClient = WebClient.create(oauthService, OAuth2TestUtils.setupProviders(), "alice", "security", busFile.toString());
// Save the Cookie for the second request...
WebClient.getConfig(oauthClient).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
String code = OAuth2TestUtils.getAuthorizationCode(oauthClient, "create_book");
assertNotNull(code);
// Now get the access token
oauthClient = WebClient.create(oauthService, OAuth2TestUtils.setupProviders(), "consumer-id", "this-is-a-secret", busFile.toString());
// Save the Cookie for the second request...
WebClient.getConfig(oauthClient).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
ClientAccessToken accessToken = OAuth2TestUtils.getAccessTokenWithAuthorizationCode(oauthClient, code);
assertNotNull(accessToken.getTokenKey());
JwsJwtCompactConsumer jwtConsumer = new JwsJwtCompactConsumer(accessToken.getTokenKey());
JwsSignatureVerifier verifier = JwsUtils.loadSignatureVerifier("org/apache/cxf/systest/jaxrs/security/alice.rs.properties", null);
assertTrue(jwtConsumer.verifySignatureWith(verifier));
JwtClaims claims = jwtConsumer.getJwtClaims();
assertEquals("consumer-id", claims.getStringProperty(OAuthConstants.CLIENT_ID));
assertEquals("alice", claims.getStringProperty("username"));
// Now invoke on the service with the access token
WebClient client = WebClient.create(rsAddress, OAuth2TestUtils.setupProviders(), busFile.toString());
client.header("Authorization", "Bearer " + accessToken.getTokenKey());
Response response = client.type("application/xml").post(new Book("book", 123L));
assertEquals(200, response.getStatus());
Book returnedBook = response.readEntity(Book.class);
assertEquals(returnedBook.getName(), "book");
assertEquals(returnedBook.getId(), 123L);
}
use of org.apache.cxf.rs.security.oauth2.client.Consumer in project cxf by apache.
the class AuthorizationGrantNegativeTest method testRepeatAuthorizationCode.
// Send the authorization code twice to get an access token
@org.junit.Test
public void testRepeatAuthorizationCode() throws Exception {
URL busFile = AuthorizationGrantTest.class.getResource("client.xml");
String address = "https://localhost:" + PORT + "/services/";
WebClient client = WebClient.create(address, OAuth2TestUtils.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 = OAuth2TestUtils.getAuthorizationCode(client);
assertNotNull(code);
// Now get the access token
client = WebClient.create(address, OAuth2TestUtils.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);
client.type("application/x-www-form-urlencoded").accept("application/json");
client.path("token");
// First invocation
Form form = new Form();
form.param("grant_type", "authorization_code");
form.param("code", code);
form.param("client_id", "consumer-id");
Response response = client.post(form);
ClientAccessToken token = response.readEntity(ClientAccessToken.class);
assertNotNull(token.getTokenKey());
// Now try to get a second token
response = client.post(form);
try {
response.readEntity(ClientAccessToken.class);
fail("Failure expected on trying to get a second access token");
} catch (ResponseProcessingException ex) {
// expected
}
}
use of org.apache.cxf.rs.security.oauth2.client.Consumer in project cxf by apache.
the class AuthorizationGrantTest method testJWTAuthorizationGrant.
@org.junit.Test
public void testJWTAuthorizationGrant() throws Exception {
URL busFile = AuthorizationGrantTest.class.getResource("client.xml");
String address = "https://localhost:" + PORT + "/services/";
WebClient client = WebClient.create(address, OAuth2TestUtils.setupProviders(), "alice", "security", busFile.toString());
// Create the JWT Token
String token = OAuth2TestUtils.createToken("DoubleItSTSIssuer", "consumer-id", "https://localhost:" + PORT + "/services/token", true, true);
// Get Access Token
client.type("application/x-www-form-urlencoded").accept("application/json");
client.path("token");
Form form = new Form();
form.param("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer");
form.param("assertion", token);
form.param("client_id", "consumer-id");
Response response = client.post(form);
ClientAccessToken accessToken = response.readEntity(ClientAccessToken.class);
assertNotNull(accessToken.getTokenKey());
assertNotNull(accessToken.getRefreshToken());
}
Aggregations