use of org.gluu.oxauth.client.exception.CommunicationException in project oxTrust by GluuFederation.
the class OpenIdClient method retrieveUserProfileFromUserInfoResponse.
protected CommonProfile retrieveUserProfileFromUserInfoResponse(final WebContext context, final UserInfoResponse userInfoResponse) {
final CommonProfile profile = new CommonProfile();
String nonceResponse = getFirstClaim(userInfoResponse, JwtClaimName.NONCE);
final String nonceSession = (String) context.getSessionAttribute(getName() + NONCE_PARAMETER);
logger.debug("Session nonce: '{}'", nonceSession);
if (!StringHelper.equals(nonceSession, nonceResponse)) {
logger.error("User info response: nonce is not matching.");
throw new CommunicationException("Nonce is not match");
}
String id = getFirstClaim(userInfoResponse, JwtClaimName.USER_NAME);
if (StringHelper.isEmpty(id)) {
id = getFirstClaim(userInfoResponse, JwtClaimName.SUBJECT_IDENTIFIER);
}
profile.setId(id);
List<ClaimToAttributeMapping> claimMappings = this.appConfiguration.getOpenIdClaimMapping();
if ((claimMappings == null) || (claimMappings.size() == 0)) {
logger.info("Using default claims to attributes mapping");
profile.setUserName(id);
profile.setEmail(getFirstClaim(userInfoResponse, JwtClaimName.EMAIL));
profile.setDisplayName(getFirstClaim(userInfoResponse, JwtClaimName.NAME));
profile.setFirstName(getFirstClaim(userInfoResponse, JwtClaimName.GIVEN_NAME));
profile.setFamilyName(getFirstClaim(userInfoResponse, JwtClaimName.FAMILY_NAME));
profile.setZone(getFirstClaim(userInfoResponse, JwtClaimName.ZONEINFO));
profile.setLocale(getFirstClaim(userInfoResponse, JwtClaimName.LOCALE));
} else {
for (ClaimToAttributeMapping mapping : claimMappings) {
String attribute = mapping.getAttribute();
String value = getFirstClaim(userInfoResponse, mapping.getClaim());
profile.addAttribute(attribute, value);
logger.trace("Adding attribute '{}' with value '{}'", attribute, value);
}
}
return profile;
}
use of org.gluu.oxauth.client.exception.CommunicationException in project oxTrust by GluuFederation.
the class OpenIdClient method retrieveUserProfileFromUserInfoResponse.
protected CommonProfile retrieveUserProfileFromUserInfoResponse(final WebContext context, final Jwt jwt, final UserInfoResponse userInfoResponse) {
final CommonProfile profile = new CommonProfile();
String nonceResponse = (String) jwt.getClaims().getClaim(JwtClaimName.NONCE);
final String nonceSession = (String) context.getSessionAttribute(getName() + SESSION_NONCE_PARAMETER);
logger.debug("Session nonce: '{}'", nonceSession);
if (!StringHelper.equals(nonceSession, nonceResponse)) {
logger.error("User info response: nonce is not matching.");
throw new CommunicationException("Nonce is not match" + nonceResponse + " : " + nonceSession);
}
String id = getFirstClaim(userInfoResponse, JwtClaimName.USER_NAME);
if (StringHelper.isEmpty(id)) {
id = getFirstClaim(userInfoResponse, JwtClaimName.SUBJECT_IDENTIFIER);
}
profile.setId(id);
String acrResponse = (String) jwt.getClaims().getClaim(JwtClaimName.AUTHENTICATION_CONTEXT_CLASS_REFERENCE);
logger.debug("Authentication ACR: '{}'", acrResponse);
profile.setUsedAcr(acrResponse);
List<ClaimToAttributeMapping> claimMappings = this.appConfiguration.getOpenIdClaimMapping();
if ((claimMappings == null) || (claimMappings.size() == 0)) {
logger.info("Using default claims to attributes mapping");
profile.setUserName(id);
profile.setEmail(getFirstClaim(userInfoResponse, JwtClaimName.EMAIL));
profile.setDisplayName(getFirstClaim(userInfoResponse, JwtClaimName.NAME));
profile.setFirstName(getFirstClaim(userInfoResponse, JwtClaimName.GIVEN_NAME));
profile.setFamilyName(getFirstClaim(userInfoResponse, JwtClaimName.FAMILY_NAME));
profile.setZone(getFirstClaim(userInfoResponse, JwtClaimName.ZONEINFO));
profile.setLocale(getFirstClaim(userInfoResponse, JwtClaimName.LOCALE));
} else {
for (ClaimToAttributeMapping mapping : claimMappings) {
String attribute = mapping.getAttribute();
String value = getFirstClaim(userInfoResponse, mapping.getClaim());
profile.addAttribute(attribute, value);
logger.trace("Adding attribute '{}' with value '{}'", attribute, value);
}
}
return profile;
}
use of org.gluu.oxauth.client.exception.CommunicationException in project oxTrust by GluuFederation.
the class OpenIdClient method getUserProfile.
@Override
public UserProfile getUserProfile(final OpenIdCredentials credential, final WebContext context) {
init();
try {
// Request access token using the authorization code
logger.debug("Getting access token");
final TokenClient tokenClient = new TokenClient(this.openIdConfiguration.getTokenEndpoint());
final TokenResponse tokenResponse = tokenClient.execAuthorizationCode(credential.getAuthorizationCode(), this.appConfiguration.getOpenIdRedirectUrl(), this.clientId, this.clientSecret);
logger.trace("tokenResponse.getStatus(): '{}'", tokenResponse.getStatus());
logger.trace("tokenResponse.getErrorType(): '{}'", tokenResponse.getErrorType());
final String accessToken = tokenResponse.getAccessToken();
logger.trace("accessToken : " + accessToken);
final String idToken = tokenResponse.getIdToken();
logger.trace("idToken : " + idToken);
// Store id_token in session
context.setSessionAttribute(getName() + SESSION_ID_TOKEN_PARAMETER, idToken);
// Parse JWT
Jwt jwt;
try {
jwt = Jwt.parse(idToken);
} catch (InvalidJwtException ex) {
logger.error("Failed to parse id_token: {}", idToken);
throw new CommunicationException("Failed to parse id_token");
}
final UserInfoResponse userInfoResponse = getUserInfo(accessToken);
final UserProfile profile = retrieveUserProfileFromUserInfoResponse(context, jwt, userInfoResponse);
logger.debug("User profile: '{}'", profile);
return profile;
} catch (final Exception ex) {
throw new CommunicationException(ex);
}
}
Aggregations