use of org.xdi.oxauth.client.UserInfoResponse in project oxTrust by GluuFederation.
the class OpenIdClient method getUserProfile.
/**
* {@InheritDoc}
*/
@Override
public UserProfile getUserProfile(final OpenIdCredentials credential, final WebContext context) {
init();
try {
final String accessToken = getAccessToken(credential);
final UserInfoResponse userInfoResponse = getUserInfo(accessToken);
final UserProfile profile = retrieveUserProfileFromUserInfoResponse(context, userInfoResponse);
logger.debug("User profile: '{}'", profile);
return profile;
} catch (final Exception ex) {
throw new CommunicationException(ex);
}
}
use of org.xdi.oxauth.client.UserInfoResponse in project oxTrust by GluuFederation.
the class OpenIdClient method getUserInfo.
private UserInfoResponse getUserInfo(final String accessToken) {
logger.debug("Session validation successful. Getting user information");
final UserInfoClient userInfoClient = new UserInfoClient(this.openIdConfiguration.getUserInfoEndpoint());
final UserInfoResponse userInfoResponse = userInfoClient.execUserInfo(accessToken);
logger.trace("userInfoResponse.getStatus(): '{}'", userInfoResponse.getStatus());
logger.trace("userInfoResponse.getErrorType(): '{}'", userInfoResponse.getErrorType());
logger.debug("userInfoResponse.getClaims(): '{}'", userInfoResponse.getClaims());
return userInfoResponse;
}
use of org.xdi.oxauth.client.UserInfoResponse in project oxAuth by GluuFederation.
the class RpDemoServlet method doGet.
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
resp.setContentType("text/html;charset=utf-8");
PrintWriter pw = resp.getWriter();
pw.println("<h1>RP Demo</h1>");
pw.println("<br/><br/>");
String accessToken = (String) req.getSession().getAttribute("access_token");
String userInfoEndpoint = (String) req.getSession().getAttribute("userinfo_endpoint");
LOG.trace("access_token: " + accessToken + ", userinfo_endpoint: " + userInfoEndpoint);
UserInfoClient userInfoClient = new UserInfoClient(userInfoEndpoint);
userInfoClient.setExecutor(Utils.createTrustAllExecutor());
UserInfoResponse response = userInfoClient.execUserInfo(accessToken);
LOG.trace("UserInfo response: " + response);
if (response.getStatus() != 200) {
pw.print("Failed to fetch user info claims");
return;
}
pw.println("<h2>User Info Claims:</h2>");
pw.println("<br/>");
for (Map.Entry<String, List<String>> entry : response.getClaims().entrySet()) {
pw.print("Name: " + entry.getKey() + " Value: " + entry.getValue());
pw.println("<br/>");
}
} catch (Exception e) {
LOG.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
use of org.xdi.oxauth.client.UserInfoResponse in project oxTrust by GluuFederation.
the class OAuthValidationFilter method getOAuthData.
private OAuthData getOAuthData(HttpSession session, HttpServletRequest request, String authorizationCode) throws Exception {
// Check state
String authorizationState = request.getParameter(Configuration.OAUTH_STATE);
final String stateSession = session != null ? (String) session.getAttribute(Configuration.SESSION_AUTH_STATE) : null;
if (!StringHelper.equals(stateSession, authorizationState)) {
log.error("Login failed, oxTrust wasn't allow to access user data");
return null;
}
String oAuthAuthorizeUrl = getPropertyFromInitParams(null, Configuration.OAUTH_PROPERTY_AUTHORIZE_URL, null);
String oAuthHost = getOAuthHost(oAuthAuthorizeUrl);
String oAuthTokenUrl = getPropertyFromInitParams(null, Configuration.OAUTH_PROPERTY_TOKEN_URL, null);
String oAuthUserInfoUrl = getPropertyFromInitParams(null, Configuration.OAUTH_PROPERTY_USERINFO_URL, null);
String oAuthClientId = getPropertyFromInitParams(null, Configuration.OAUTH_PROPERTY_CLIENT_ID, null);
String oAuthClientPassword = getPropertyFromInitParams(null, Configuration.OAUTH_PROPERTY_CLIENT_PASSWORD, null);
if (oAuthClientPassword != null) {
try {
oAuthClientPassword = StringEncrypter.defaultInstance().decrypt(oAuthClientPassword, Configuration.instance().getCryptoPropertyValue());
} catch (EncryptionException ex) {
log.error("Failed to decrypt property: " + Configuration.OAUTH_PROPERTY_CLIENT_PASSWORD, ex);
}
}
String scopes = getParameter(request, Configuration.OAUTH_SCOPE);
log.trace("scopes : " + scopes);
// 1. Request access token using the authorization code
log.trace("Getting access token");
TokenClient tokenClient1 = new TokenClient(oAuthTokenUrl);
String redirectURL = constructRedirectUrl(request);
TokenResponse tokenResponse = tokenClient1.execAuthorizationCode(authorizationCode, redirectURL, oAuthClientId, oAuthClientPassword);
if (tokenResponse == null) {
log.error("Get empty token response. User can't log into application");
return null;
}
log.trace("tokenResponse : " + tokenResponse);
log.trace("tokenResponse.getErrorType() : " + tokenResponse.getErrorType());
String accessToken = tokenResponse.getAccessToken();
String idToken = tokenResponse.getIdToken();
log.trace("accessToken : " + accessToken);
log.trace("idToken : " + idToken);
// Parse JWT
Jwt jwt;
try {
jwt = Jwt.parse(idToken);
} catch (InvalidJwtException ex) {
log.error("Failed to parse id_token");
return null;
}
// Check nonce
String nonceResponse = (String) jwt.getClaims().getClaim(JwtClaimName.NONCE);
final String nonceSession = session != null ? (String) session.getAttribute(Configuration.SESSION_AUTH_NONCE) : null;
if (!StringHelper.equals(nonceSession, nonceResponse)) {
log.error("User info response : nonce is not matching.");
return null;
}
log.info("Session validation successful. User is logged in");
UserInfoClient userInfoClient = new UserInfoClient(oAuthUserInfoUrl);
UserInfoResponse userInfoResponse = userInfoClient.execUserInfo(accessToken);
if (userInfoResponse == null) {
log.error("Get empty user info response. User can't log into application");
return null;
}
OAuthData oAuthData = new OAuthData();
oAuthData.setHost(oAuthHost);
// Determine uid
List<String> uidValues = userInfoResponse.getClaims().get(JwtClaimName.USER_NAME);
if ((uidValues == null) || (uidValues.size() == 0)) {
log.error("User infor response doesn't contains uid claim");
return null;
}
oAuthData.setUserUid(uidValues.get(0));
oAuthData.setAccessToken(accessToken);
oAuthData.setAccessTokenExpirationInSeconds(tokenResponse.getExpiresIn());
oAuthData.setScopes(scopes);
oAuthData.setIdToken(idToken);
log.trace("User uid: " + oAuthData.getUserUid());
return oAuthData;
}
Aggregations