org.openid4java.message.ax.FetchResponse Java Examples

The following examples show how to use org.openid4java.message.ax.FetchResponse. 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: ConsumerServlet.java    From openid4java with Apache License 2.0 6 votes vote down vote up
/**
 * @param httpReq
 * @param authSuccess
 * @throws MessageException 
 */
private void receiveAttributeExchange(HttpServletRequest httpReq,
		AuthSuccess authSuccess) throws MessageException {
	if (authSuccess.hasExtension(AxMessage.OPENID_NS_AX)) {
		FetchResponse fetchResp = (FetchResponse) authSuccess
				.getExtension(AxMessage.OPENID_NS_AX);

		// List emails = fetchResp.getAttributeValues("email");
		// String email = (String) emails.get(0);

		List aliases = fetchResp.getAttributeAliases();
		Map attributes = new LinkedHashMap();
		for (Iterator iter = aliases.iterator(); iter.hasNext();) {
			String alias = (String) iter.next();
			List values = fetchResp.getAttributeValues(alias);
			if (values.size() > 0) {
				String[] arr = new String[values.size()];
				values.toArray(arr);
				attributes.put(alias, StringUtils.join(arr));
			}
		}
		httpReq.setAttribute("attributes", attributes);
	}
}
 
Example #2
Source File: OpenIDAttributeExchange.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * Populate the response with claim values. If we can't find the required values with us, we
 * simply avoid sending them. An Identity Provider MAY return any subset of the following fields
 * in response to the query.
 *
 * @param claimValues Claim values.
 * @throws MessageException
 */
protected void setAttributeExchangeValues(FetchResponse response,
                                          Map<String, OpenIDClaimDTO> claimValues) throws MessageException {

    Iterator<Entry<String, OpenIDClaimDTO>> iterator = null;
    Entry<String, OpenIDClaimDTO> entry = null;
    OpenIDClaimDTO claim = null;

    iterator = claimValues.entrySet().iterator();

    while (iterator.hasNext()) {
        entry = iterator.next();
        claim = entry.getValue();
        response.addAttribute(claim.getClaimUri(), claim.getClaimValue());
    }
}
 
Example #3
Source File: CustomOpenIdProcessor.java    From OpenID-Attacker with GNU General Public License v2.0 5 votes vote down vote up
private Message processAxExtension(Message token, final AuthRequest authRequest) throws MessageException {
    if (authRequest.hasExtension(AxMessage.OPENID_NS_AX)) {
        MessageExtension extension = authRequest.getExtension(AxMessage.OPENID_NS_AX);
        if (extension instanceof FetchRequest) {
            final FetchRequest fetchRequest = (FetchRequest) extension;
            final Map userDataMap = getValidUser().getUserDataMap();
            final FetchResponse fetchResponse = FetchResponse.createFetchResponse(fetchRequest, userDataMap);
            token.addExtension(fetchResponse, "ax");
        } else {
            throw new UnsupportedOperationException("TODO: if (ext instanceof StoreRequest)");
        }
    }
    return token;
}
 
Example #4
Source File: SampleConsumer.java    From openid4java with Apache License 2.0 4 votes vote down vote up
public Identifier verifyResponse(HttpServletRequest httpReq)
{
    try
    {
        // extract the parameters from the authentication response
        // (which comes in as a HTTP request from the OpenID provider)
        ParameterList response =
                new ParameterList(httpReq.getParameterMap());

        // retrieve the previously stored discovery information
        DiscoveryInformation discovered = (DiscoveryInformation)
                httpReq.getSession().getAttribute("openid-disc");

        // extract the receiving URL from the HTTP request
        StringBuffer receivingURL = httpReq.getRequestURL();
        String queryString = httpReq.getQueryString();
        if (queryString != null && queryString.length() > 0)
            receivingURL.append("?").append(httpReq.getQueryString());

        // verify the response; ConsumerManager needs to be the same
        // (static) instance used to place the authentication request
        VerificationResult verification = manager.verify(
                receivingURL.toString(),
                response, discovered);

        // examine the verification result and extract the verified identifier
        Identifier verified = verification.getVerifiedId();
        if (verified != null)
        {
            AuthSuccess authSuccess = (AuthSuccess) verification.getAuthResponse();

            HttpSession session = httpReq.getSession(true);
            session.setAttribute("openid_identifier", authSuccess.getIdentity());

            if (authSuccess.hasExtension(AxMessage.OPENID_NS_AX))
            {
                FetchResponse fetchResp = (FetchResponse) authSuccess.getExtension(AxMessage.OPENID_NS_AX);
                session.setAttribute("emailFromFetch", fetchResp.getAttributeValues("email").get(0));
            }
            if (authSuccess.hasExtension(SRegMessage.OPENID_NS_SREG))
            {
                SRegResponse sregResp = (SRegResponse) authSuccess.getExtension(SRegMessage.OPENID_NS_SREG);
                session.setAttribute("emailFromSReg", sregResp.getAttributeValue("email"));
            }
            return verified;  // success
        }
    }
    catch (OpenIDException e)
    {
        // present error to the user
        throw new RuntimeException("wrap:" + e.getMessage(), e);
    }

    return null;
}
 
Example #5
Source File: OpenIdImpl.java    From socialauth with MIT License 4 votes vote down vote up
/**
 * Verifies the user when the external provider redirects back to our
 * application.
 * 
 * 
 * @param requestParams
 *            request parameters, received from the provider
 * @return Profile object containing the profile information
 * @throws Exception
 */

@Override
public Profile verifyResponse(final Map<String, String> requestParams)
		throws Exception {
	if (!providerState) {
		throw new ProviderStateException();
	}
	try {
		// extract the parameters from the authentication response
		// (which comes in as a HTTP request from the OpenID provider)
		ParameterList response = new ParameterList(requestParams);

		// extract the receiving URL from the HTTP request
		StringBuffer receivingURL = new StringBuffer();
		receivingURL.append(successUrl);
		StringBuffer sb = new StringBuffer();
		for (Map.Entry<String, String> entry : requestParams.entrySet()) {
			String key = entry.getKey();
			String value = entry.getValue();
			if (sb.length() > 0) {
				sb.append("&");
			}
			sb.append(key).append("=").append(value);
		}
		receivingURL.append("?").append(sb.toString());

		// verify the response; ConsumerManager needs to be the same
		// (static) instance used to place the authentication request
		VerificationResult verification = manager.verify(
				receivingURL.toString(), response, discovered);

		// examine the verification result and extract the verified
		// identifier
		Identifier verified = verification.getVerifiedId();
		if (verified != null) {
			LOG.debug("Verified Id : " + verified.getIdentifier());
			Profile p = new Profile();
			p.setValidatedId(verified.getIdentifier());
			AuthSuccess authSuccess = (AuthSuccess) verification
					.getAuthResponse();

			if (authSuccess.hasExtension(AxMessage.OPENID_NS_AX)) {
				FetchResponse fetchResp = (FetchResponse) authSuccess
						.getExtension(AxMessage.OPENID_NS_AX);

				p.setEmail(fetchResp.getAttributeValue("email"));
				p.setFirstName(fetchResp.getAttributeValue("firstname"));
				p.setLastName(fetchResp.getAttributeValue("lastname"));
				p.setFullName(fetchResp.getAttributeValue("fullname"));

				// also use the ax namespace for compatibility
				if (p.getEmail() == null) {
					p.setEmail(fetchResp.getAttributeValue("emailax"));
				}
				if (p.getFirstName() == null) {
					p.setFirstName(fetchResp
							.getAttributeValue("firstnameax"));
				}
				if (p.getLastName() == null) {
					p.setLastName(fetchResp.getAttributeValue("lastnameax"));
				}
				if (p.getFullName() == null) {
					p.setFullName(fetchResp.getAttributeValue("fullnameax"));
				}

			}
			userProfile = p;
			return p;
		}
	} catch (OpenIDException e) {
		throw e;
	}

	return null;
}
 
Example #6
Source File: OpenIdConsumer.java    From jerseyoauth2 with MIT License 4 votes vote down vote up
public Identifier verifyResponse(HttpServletRequest httpReq) {
	try {
		// extract the parameters from the authentication response
		// (which comes in as a HTTP request from the OpenID provider)
		ParameterList response = new ParameterList(httpReq.getParameterMap());

		// retrieve the previously stored discovery information
		DiscoveryInformation discovered = (DiscoveryInformation) httpReq.getSession().getAttribute(OpenIdConstants.OPENID_DISC);

		// extract the receiving URL from the HTTP request
		StringBuffer receivingURL = httpReq.getRequestURL();
		String queryString = httpReq.getQueryString();
		if (queryString != null && queryString.length() > 0)
			receivingURL.append("?").append(httpReq.getQueryString());

		// verify the response; ConsumerManager needs to be the same
		// (static) instance used to place the authentication request
		VerificationResult verification = manager.verify(receivingURL.toString(), response, discovered);

		// examine the verification result and extract the verified
		// identifier
		Identifier verified = verification.getVerifiedId();
		if (verified != null) {
			AuthSuccess authSuccess = (AuthSuccess) verification.getAuthResponse();

			if (authSuccess.hasExtension(AxMessage.OPENID_NS_AX)) {
				FetchResponse fetchResp = (FetchResponse) authSuccess.getExtension(AxMessage.OPENID_NS_AX);

				List<?> emails = fetchResp.getAttributeValues("email");
				String email = (String) emails.get(0);
				
				httpReq.getSession().setAttribute(OpenIdConstants.OPENID_SESSION_VAR, new OpenIDUser(email));
			}

			return verified; // success
		}
	} catch (OpenIDException e) {
		// present error to the user
	}

	return null;
}