use of org.apache.cxf.rs.security.oauth2.client.Consumer in project cxf by apache.
the class JAXRSOAuth2Test method testSAML2BearerGrant.
@Test
public void testSAML2BearerGrant() throws Exception {
String address = "https://localhost:" + PORT + "/oauth2/token";
WebClient wc = createWebClient(address);
Crypto crypto = new CryptoLoader().loadCrypto(CRYPTO_RESOURCE_PROPERTIES);
SelfSignInfo signInfo = new SelfSignInfo(crypto, "alice", "password");
SamlCallbackHandler samlCallbackHandler = new SamlCallbackHandler(false);
String audienceURI = "https://localhost:" + PORT + "/oauth2/token";
samlCallbackHandler.setAudience(audienceURI);
SamlAssertionWrapper assertionWrapper = SAMLUtils.createAssertion(samlCallbackHandler, signInfo);
Document doc = DOMUtils.newDocument();
Element assertionElement = assertionWrapper.toDOM(doc);
String assertion = DOM2Writer.nodeToString(assertionElement);
Saml2BearerGrant grant = new Saml2BearerGrant(assertion);
ClientAccessToken at = OAuthClientUtils.getAccessToken(wc, new Consumer("alice", "alice"), grant, false);
assertNotNull(at.getTokenKey());
}
use of org.apache.cxf.rs.security.oauth2.client.Consumer in project cxf by apache.
the class JAXRSOAuth2Test method testJWTBearerGrant.
@Test
public void testJWTBearerGrant() throws Exception {
String address = "https://localhost:" + PORT + "/oauth2/token";
WebClient wc = createWebClient(address);
// Create the JWT Token
String token = OAuth2TestUtils.createToken("resourceOwner", "alice", address, true, true);
JwtBearerGrant grant = new JwtBearerGrant(token);
ClientAccessToken at = OAuthClientUtils.getAccessToken(wc, new Consumer("alice", "alice"), grant, false);
assertNotNull(at.getTokenKey());
}
use of org.apache.cxf.rs.security.oauth2.client.Consumer in project cxf by apache.
the class OIDCFlowTest method testImplicitFlow.
@org.junit.Test
public void testImplicitFlow() throws Exception {
URL busFile = OIDCFlowTest.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 Access Token
client.type("application/json").accept("application/json");
client.query("client_id", "consumer-id");
client.query("redirect_uri", "http://www.blah.apache.org");
client.query("scope", "openid");
client.query("response_type", "id_token token");
client.query("nonce", "123456789");
client.path("authorize-implicit/");
Response response = client.get();
OAuthAuthorizationData authzData = response.readEntity(OAuthAuthorizationData.class);
// Now call "decision" to get the access token
client.path("decision");
client.type("application/x-www-form-urlencoded");
Form form = new Form();
form.param("session_authenticity_token", authzData.getAuthenticityToken());
form.param("client_id", authzData.getClientId());
form.param("redirect_uri", authzData.getRedirectUri());
form.param("scope", authzData.getProposedScope());
if (authzData.getResponseType() != null) {
form.param("response_type", authzData.getResponseType());
}
if (authzData.getNonce() != null) {
form.param("nonce", authzData.getNonce());
}
form.param("oauthDecision", "allow");
response = client.post(form);
String location = response.getHeaderString("Location");
// Check Access Token
String accessToken = OAuth2TestUtils.getSubstring(location, "access_token");
assertNotNull(accessToken);
// Check IdToken
String idToken = OAuth2TestUtils.getSubstring(location, "id_token");
assertNotNull(idToken);
validateIdToken(idToken, null);
JwsJwtCompactConsumer jwtConsumer = new JwsJwtCompactConsumer(idToken);
JwtToken jwt = jwtConsumer.getJwtToken();
Assert.assertNotNull(jwt.getClaims().getClaim(IdToken.ACCESS_TOKEN_HASH_CLAIM));
Assert.assertNotNull(jwt.getClaims().getClaim(IdToken.NONCE_CLAIM));
OidcUtils.validateAccessTokenHash(accessToken, jwt, true);
}
use of org.apache.cxf.rs.security.oauth2.client.Consumer in project cxf by apache.
the class OIDCFlowTest method testImplicitFlowNoAccessToken.
@org.junit.Test
public void testImplicitFlowNoAccessToken() throws Exception {
URL busFile = OIDCFlowTest.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 Access Token
client.type("application/json").accept("application/json");
client.query("client_id", "consumer-id");
client.query("redirect_uri", "http://www.blah.apache.org");
client.query("scope", "openid");
client.query("response_type", "id_token");
client.query("nonce", "123456789");
client.path("authorize-implicit/");
Response response = client.get();
OAuthAuthorizationData authzData = response.readEntity(OAuthAuthorizationData.class);
// Now call "decision" to get the access token
client.path("decision");
client.type("application/x-www-form-urlencoded");
Form form = new Form();
form.param("session_authenticity_token", authzData.getAuthenticityToken());
form.param("client_id", authzData.getClientId());
form.param("redirect_uri", authzData.getRedirectUri());
form.param("scope", authzData.getProposedScope());
if (authzData.getResponseType() != null) {
form.param("response_type", authzData.getResponseType());
}
if (authzData.getNonce() != null) {
form.param("nonce", authzData.getNonce());
}
form.param("oauthDecision", "allow");
response = client.post(form);
String location = response.getHeaderString("Location");
// Check Access Token - it should not be present
String accessToken = OAuth2TestUtils.getSubstring(location, "access_token");
assertNull(accessToken);
// Check IdToken
String idToken = OAuth2TestUtils.getSubstring(location, "id_token");
assertNotNull(idToken);
validateIdToken(idToken, null);
JwsJwtCompactConsumer jwtConsumer = new JwsJwtCompactConsumer(idToken);
JwtToken jwt = jwtConsumer.getJwtToken();
Assert.assertNull(jwt.getClaims().getClaim(IdToken.ACCESS_TOKEN_HASH_CLAIM));
Assert.assertNotNull(jwt.getClaims().getClaim(IdToken.NONCE_CLAIM));
}
use of org.apache.cxf.rs.security.oauth2.client.Consumer in project cxf by apache.
the class OIDCFlowTest method testAuthorizationCodeFlowWithScope.
@org.junit.Test
public void testAuthorizationCodeFlowWithScope() throws Exception {
URL busFile = OIDCFlowTest.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, "openid read_balance");
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);
ClientAccessToken accessToken = OAuth2TestUtils.getAccessTokenWithAuthorizationCode(client, code);
assertNotNull(accessToken.getTokenKey());
assertTrue(accessToken.getApprovedScope().contains("openid"));
assertTrue(accessToken.getApprovedScope().contains("read_balance"));
String idToken = accessToken.getParameters().get("id_token");
assertNotNull(idToken);
validateIdToken(idToken, null);
}
Aggregations