org.gluu.oxauth.client.UserInfoClient Java Examples

The following examples show how to use org.gluu.oxauth.client.UserInfoClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UserInfoAction.java    From oxAuth with MIT License 6 votes vote down vote up
public void exec() {
    try {
        UserInfoRequest request = new UserInfoRequest(accessToken);
        request.setAuthorizationMethod(authorizationMethod);

        UserInfoClient client = new UserInfoClient(userInfoEndpoint);
        client.setRequest(request);
        client.exec();

        showResults = true;
        requestString = client.getRequestAsString();
        responseString = client.getResponseAsString();
    } catch (Exception e) {
    	log.error(e.getMessage(), e);
    }
}
 
Example #2
Source File: GetUserInfoOperation.java    From oxd with Apache License 2.0 6 votes vote down vote up
@Override
public IOpResponse execute(GetUserInfoParams params) throws IOException {
    getValidationService().validate(params);

    UserInfoClient client = getOpClientFactory().createUserInfoClient(getDiscoveryService().getConnectDiscoveryResponseByOxdId(params.getOxdId()).getUserInfoEndpoint());
    client.setExecutor(getHttpService().getClientExecutor());
    client.setRequest(new UserInfoRequest(params.getAccessToken()));

    final UserInfoResponse response = client.exec();
    //validate subject identifier of successful response
    if (response.getStatus() == 200) {
        validateSubjectIdentifier(params.getIdToken(), response);
    }

    return new POJOResponse(Jackson2.createJsonMapper().readTree(response.getEntity()));
}
 
Example #3
Source File: OpenIdClient.java    From oxTrust with MIT License 5 votes vote down vote up
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;
}
 
Example #4
Source File: RpDemoServlet.java    From oxAuth with MIT License 5 votes vote down vote up
@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);
    }
}
 
Example #5
Source File: Authenticator.java    From oxTrust with MIT License 4 votes vote down vote up
private String requestAccessToken(String oxAuthHost, String authorizationCode, String sessionState, String scopes,
		String clientID, String clientPassword) {
	OpenIdConfigurationResponse openIdConfiguration = openIdService.getOpenIdConfiguration();
	// 1. Request access token using the authorization code.
	TokenClient tokenClient1 = new TokenClient(openIdConfiguration.getTokenEndpoint());

	log.info("Sending request to token endpoint");
	String redirectURL = appConfiguration.getLoginRedirectUrl();
	log.info("redirectURI : " + redirectURL);
	TokenResponse tokenResponse = tokenClient1.execAuthorizationCode(authorizationCode, redirectURL, clientID,
			clientPassword);

	log.debug(" tokenResponse : " + tokenResponse);
	if (tokenResponse == null) {
		log.error("Get empty token response. User rcan't log into application");
		return OxTrustConstants.RESULT_NO_PERMISSIONS;
	}

	log.debug(" tokenResponse.getErrorType() : " + tokenResponse.getErrorType());

	String accessToken = tokenResponse.getAccessToken();
	log.debug(" accessToken : " + accessToken);

	String idToken = tokenResponse.getIdToken();
	log.debug(" idToken : " + idToken);

	if (idToken == null) {
		log.error("Failed to get id_token");
		return OxTrustConstants.RESULT_NO_PERMISSIONS;
	}

	log.info("Session validation successful. User is logged in");
	UserInfoClient userInfoClient = new UserInfoClient(openIdConfiguration.getUserInfoEndpoint());
	UserInfoResponse userInfoResponse = userInfoClient.execUserInfo(accessToken);
	if (userInfoResponse == null) {
		log.error("Get empty token response. User can't log into application");
		return OxTrustConstants.RESULT_NO_PERMISSIONS;
	}

	// Parse JWT
	Jwt jwt;
	try {
		jwt = Jwt.parse(idToken);
	} catch (InvalidJwtException ex) {
		log.error("Failed to parse id_token");
		return OxTrustConstants.RESULT_NO_PERMISSIONS;
	}

	// Check nonce
	String nonceResponse = (String) jwt.getClaims().getClaim(JwtClaimName.NONCE);
	String nonceSession = (String) identity.getSessionMap().get(OxTrustConstants.OXAUTH_NONCE);
	if (!StringHelper.equals(nonceSession, nonceResponse)) {
		log.error("User info response :  nonce is not matching.");
		return OxTrustConstants.RESULT_NO_PERMISSIONS;
	}

	// Determine uid
	List<String> uidValues = userInfoResponse.getClaims().get(JwtClaimName.USER_NAME);
	if ((uidValues == null) || (uidValues.size() == 0)) {
		log.error("User info response doesn't contains uid claim");
		return OxTrustConstants.RESULT_NO_PERMISSIONS;
	}
	// Check requested authentication method
	if (identity.getSessionMap().containsKey(OxTrustConstants.OXAUTH_ACR_VALUES)) {
		String requestAcrValues = (String) identity.getSessionMap().get(OxTrustConstants.OXAUTH_ACR_VALUES);
		String issuer = openIdConfiguration.getIssuer();
		String responseIssuer = (String) jwt.getClaims().getClaim(JwtClaimName.ISSUER);
		if (issuer == null || responseIssuer == null || !issuer.equals(responseIssuer)) {
			log.error("User info response :  Issuer.");
			return OxTrustConstants.RESULT_NO_PERMISSIONS;
		}

		List<String> acrValues = jwt.getClaims()
				.getClaimAsStringList(JwtClaimName.AUTHENTICATION_CONTEXT_CLASS_REFERENCE);
		if ((acrValues == null) || (acrValues.size() == 0) || !acrValues.contains(requestAcrValues)) {
			log.error("User info response doesn't contains acr claim");
			return OxTrustConstants.RESULT_NO_PERMISSIONS;
		}
		if (!acrValues.contains(requestAcrValues)) {
			log.error("User info response contains acr='{}' claim but expected acr='{}'", acrValues,
					requestAcrValues);
			return OxTrustConstants.RESULT_NO_PERMISSIONS;
		}
	}
	OauthData oauthData = identity.getOauthData();
	oauthData.setHost(oxAuthHost);
	oauthData.setUserUid(uidValues.get(0));
	oauthData.setAccessToken(accessToken);
	oauthData.setAccessTokenExpirationInSeconds(tokenResponse.getExpiresIn());
	oauthData.setScopes(scopes);
	oauthData.setIdToken(idToken);
	oauthData.setSessionState(sessionState);
	identity.setWorkingParameter(OxTrustConstants.OXAUTH_SSO_SESSION_STATE, Boolean.FALSE);
	log.info("user uid:" + oauthData.getUserUid());

	String result = authenticate();

	return result;
}