Search in sources :

Example 11 with PASSWORD

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

the class UserInfoTest method testEncryptedUserInfo.

@org.junit.Test
public void testEncryptedUserInfo() throws Exception {
    URL busFile = UserInfoTest.class.getResource("client.xml");
    String address = "https://localhost:" + PORT + "/services/oidc";
    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");
    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"));
    String idToken = accessToken.getParameters().get("id_token");
    assertNotNull(idToken);
    validateIdToken(idToken, null);
    // Now invoke on the UserInfo service with the access token
    String userInfoAddress = "https://localhost:" + PORT + "/services/encrypted/userinfo";
    WebClient userInfoClient = WebClient.create(userInfoAddress, OAuth2TestUtils.setupProviders(), busFile.toString());
    userInfoClient.accept("application/jwt");
    userInfoClient.header("Authorization", "Bearer " + accessToken.getTokenKey());
    Response serviceResponse = userInfoClient.get();
    assertEquals(200, serviceResponse.getStatus());
    String token = serviceResponse.readEntity(String.class);
    assertNotNull(token);
    KeyStore keystore = KeyStore.getInstance("JKS");
    keystore.load(ClassLoaderUtils.getResourceAsStream("keys/bob.jks", this.getClass()), "password".toCharArray());
    JweJwtCompactConsumer jwtConsumer = new JweJwtCompactConsumer(token);
    PrivateKey privateKey = (PrivateKey) keystore.getKey("bob", "password".toCharArray());
    JwtToken jwt = jwtConsumer.decryptWith(privateKey);
    assertEquals("alice", jwt.getClaim(JwtConstants.CLAIM_SUBJECT));
    assertEquals("consumer-id", jwt.getClaim(JwtConstants.CLAIM_AUDIENCE));
}
Also used : Response(javax.ws.rs.core.Response) JwtToken(org.apache.cxf.rs.security.jose.jwt.JwtToken) PrivateKey(java.security.PrivateKey) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) JweJwtCompactConsumer(org.apache.cxf.rs.security.jose.jwe.JweJwtCompactConsumer) WebClient(org.apache.cxf.jaxrs.client.WebClient) KeyStore(java.security.KeyStore) URL(java.net.URL)

Aggregations

ClientAccessToken (org.apache.cxf.rs.security.oauth2.common.ClientAccessToken)9 WebClient (org.apache.cxf.jaxrs.client.WebClient)7 Test (org.junit.Test)6 URL (java.net.URL)3 Form (javax.ws.rs.core.Form)3 Response (javax.ws.rs.core.Response)3 Consumer (org.apache.cxf.rs.security.oauth2.client.Consumer)3 KeyStore (java.security.KeyStore)2 HashMap (java.util.HashMap)2 Client (javax.ws.rs.client.Client)2 JwtToken (org.apache.cxf.rs.security.jose.jwt.JwtToken)2 OAuthServiceException (org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Principal (java.security.Principal)1 PrivateKey (java.security.PrivateKey)1 Certificate (java.security.cert.Certificate)1 X509Certificate (java.security.cert.X509Certificate)1 ArrayList (java.util.ArrayList)1