use of org.apache.cxf.rs.security.oidc.common.UserInfo in project cxf by apache.
the class UserInfoClient method getUserInfo.
public UserInfo getUserInfo(ClientAccessToken at, IdToken idToken, Consumer client) {
if (!sendTokenAsFormParameter) {
OAuthClientUtils.setAuthorizationHeader(profileClient, at);
if (getUserInfoFromJwt) {
String jwt = profileClient.get(String.class);
return getUserInfoFromJwt(jwt, idToken, client);
}
UserInfo profile = profileClient.get(UserInfo.class);
validateUserInfo(profile, idToken, client);
return profile;
}
Form form = new Form().param("access_token", at.getTokenKey());
if (getUserInfoFromJwt) {
String jwt = profileClient.form(form).readEntity(String.class);
return getUserInfoFromJwt(jwt, idToken, client);
}
UserInfo profile = profileClient.form(form).readEntity(UserInfo.class);
validateUserInfo(profile, idToken, client);
return profile;
}
use of org.apache.cxf.rs.security.oidc.common.UserInfo in project cxf by apache.
the class UserInfoClient method getUserInfoFromJwt.
public UserInfo getUserInfoFromJwt(JwtToken jwt, IdToken idToken, Consumer client) {
UserInfo profile = new UserInfo(jwt.getClaims().asMap());
validateUserInfo(profile, idToken, client);
return profile;
}
use of org.apache.cxf.rs.security.oidc.common.UserInfo in project cxf by apache.
the class OidcUserInfoProvider method createContext.
@Override
public UserInfoContext createContext(Message m) {
final OidcClientTokenContext ctx = (OidcClientTokenContext) m.getContent(ClientTokenContext.class);
final UserInfo userInfo = ctx != null ? ctx.getUserInfo() : m.getContent(UserInfo.class);
if (userInfo != null) {
final IdToken idToken = ctx != null ? ctx.getIdToken() : m.getContent(IdToken.class);
return new UserInfoContext() {
@Override
public UserInfo getUserInfo() {
return userInfo;
}
@Override
public IdToken getIdToken() {
return idToken;
}
};
}
return null;
}
use of org.apache.cxf.rs.security.oidc.common.UserInfo in project cxf by apache.
the class OIDCNegativeTest method testUserInfoRefreshToken.
@org.junit.Test
public void testUserInfoRefreshToken() throws Exception {
URL busFile = UserInfoTest.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");
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());
String oldAccessToken = accessToken.getTokenKey();
assertTrue(accessToken.getApprovedScope().contains("openid"));
String idToken = accessToken.getParameters().get("id_token");
assertNotNull(idToken);
// 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");
form.param("scope", "openid");
Response response = client.post(form);
accessToken = response.readEntity(ClientAccessToken.class);
assertNotNull(accessToken.getTokenKey());
assertNotNull(accessToken.getRefreshToken());
accessToken.getParameters().get("id_token");
assertNotNull(idToken);
String newAccessToken = accessToken.getTokenKey();
// Now test the UserInfoService.
// The old Access Token should fail
String userInfoAddress = "https://localhost:" + port + "/ui/plain/userinfo";
WebClient userInfoClient = WebClient.create(userInfoAddress, OAuth2TestUtils.setupProviders(), busFile.toString());
userInfoClient.accept("application/json");
userInfoClient.header("Authorization", "Bearer " + oldAccessToken);
Response serviceResponse = userInfoClient.get();
assertEquals(serviceResponse.getStatus(), 401);
// The refreshed Access Token should work
userInfoClient.replaceHeader("Authorization", "Bearer " + newAccessToken);
serviceResponse = userInfoClient.get();
assertEquals(serviceResponse.getStatus(), 200);
UserInfo userInfo = serviceResponse.readEntity(UserInfo.class);
assertNotNull(userInfo);
assertEquals("alice", userInfo.getSubject());
assertEquals("consumer-id", userInfo.getAudience());
}
use of org.apache.cxf.rs.security.oidc.common.UserInfo in project cxf by apache.
the class UserInfoTest method testPlainUserInfo.
@org.junit.Test
public void testPlainUserInfo() 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/plain/userinfo";
WebClient userInfoClient = WebClient.create(userInfoAddress, OAuth2TestUtils.setupProviders(), busFile.toString());
userInfoClient.accept("application/json");
userInfoClient.header("Authorization", "Bearer " + accessToken.getTokenKey());
Response serviceResponse = userInfoClient.get();
assertEquals(serviceResponse.getStatus(), 200);
UserInfo userInfo = serviceResponse.readEntity(UserInfo.class);
assertNotNull(userInfo);
assertEquals("alice", userInfo.getSubject());
assertEquals("consumer-id", userInfo.getAudience());
}
Aggregations