org.openid4java.message.sreg.SRegMessage Java Examples

The following examples show how to use org.openid4java.message.sreg.SRegMessage. 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 receiveSimpleRegistration(HttpServletRequest httpReq,
		AuthSuccess authSuccess) throws MessageException {
	if (authSuccess.hasExtension(SRegMessage.OPENID_NS_SREG)) {
		MessageExtension ext = authSuccess
				.getExtension(SRegMessage.OPENID_NS_SREG);
		if (ext instanceof SRegResponse) {
			SRegResponse sregResp = (SRegResponse) ext;
			for (Iterator iter = sregResp.getAttributeNames()
					.iterator(); iter.hasNext();) {
				String name = (String) iter.next();
				String value = sregResp.getParameterValue(name);
				httpReq.setAttribute(name, value);
			}
		}
	}
}
 
Example #2
Source File: OpenIDExtensionFactory.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * Create an instance of the OpenIDExtension based on the OpenID extension type
 *
 * @param request  OpenIDAuthenticationRequest instance
 * @return Appropriate OpenIDExtension instance
 * @throws IdentityException
 */
public OpenIDExtension getExtension(OpenIDAuthenticationRequest request)
        throws IdentityException {

    if (request == null) {
        return null;
    }

    String alias = request.getExtensionAlias();

    if (AxMessage.OPENID_NS_AX.equals(alias) || ExchangeAttributes.NS_AX.equals(alias)) {
        return new OpenIDAttributeExchange(request);
    } else if (SimpleRegAttributes.NS_SREG.equals(alias) || SRegMessage.OPENID_NS_SREG.equals(alias) ||
               SimpleRegAttributes.NS_SREG_1.equals(alias)) {
        return new OpenIDSimpleReg(request);
    } else if (PapeMessage.OPENID_NS_PAPE.equals(alias)) {
        return new OpenIDPape(request);
    }

    return null;
}
 
Example #3
Source File: Message.java    From openid4java with Apache License 2.0 5 votes vote down vote up
protected Message (ParameterList params)
{
    this();
    this._params = params;

    //build the extension list when creating a message from a param list
    Iterator iter = _params.getParameters().iterator();

    // simple registration is a special case; we support only:
    // SREG1.0 (no namespace, "sreg" alias hardcoded) in :
    //   - OpenID1 messages
    //   - OpenID2 messages (against the 2.0 spec),
    //     to accomodate Yahoo's non-2.0-compliant implementation
    // SREG1.1 (namespace, any possible alias) in OpenID2 messages
    boolean hasOpenidDotSreg = false;

    while (iter.hasNext())
    {
        String key = ((Parameter) iter.next()).getKey();
        if (key.startsWith("openid.ns.") && key.length() > 10)
            _extAliases.put(_params.getParameter(key).getValue(),
                    key.substring(10));

        if (key.startsWith("openid.sreg."))
            hasOpenidDotSreg = true;
    }

    // only do the workaround for OpenID1 messages
    if ( hasOpenidDotSreg && ! _extAliases.values().contains("sreg")
         /*! todo: revert this: hasParameter("openid.ns")*/ )
        _extAliases.put(SRegMessage.OPENID_NS_SREG, "sreg");

    _extCounter = _extAliases.size();
}
 
Example #4
Source File: CustomOpenIdProcessor.java    From OpenID-Attacker with GNU General Public License v2.0 5 votes vote down vote up
private String detectSRegVersion(final AuthRequest authRequest) {
    String sregNamespace = null;
    if (authRequest.hasExtension(SRegMessage.OPENID_NS_SREG)) {
        sregNamespace = SRegMessage.OPENID_NS_SREG;
    } else if (authRequest.hasExtension(SRegMessage.OPENID_NS_SREG11)) {
        sregNamespace = SRegMessage.OPENID_NS_SREG11;
    }
    return sregNamespace;
}
 
Example #5
Source File: Message.java    From openid4java with Apache License 2.0 4 votes vote down vote up
/**
 * Adds a set of extension-specific parameters to a message,
 * trying to use the supplied preferredAlias
 * if the supplied alias is not already taken.
 *
 * Allows adding pseude-extensions to v1 messages
 * but only if the supplied preferredAlias can be used;
 * MessageException is thrown otherwise.
 *
 * @param extension the extension to be added to this message
 * @param preferredAlias the preferred alias to use for the extension's parameters
 * @return the actual alias that was used for the extension
 * @throws MessageException if the preferredAlias could not be used
 * and this is a v1 message
 */
public String addExtension(MessageExtension extension, String preferredAlias) throws MessageException
{
    String typeUri = extension.getTypeUri();

    if (hasExtension(typeUri))
        throw new MessageException("Extension already present: " + typeUri);

    String alias = preferredAlias != null && ! _extAliases.containsValue(preferredAlias) ?
            preferredAlias : "ext" + Integer.toString(++ _extCounter);

    if (! hasParameter("openid.ns") && preferredAlias != null && ! alias.equals(preferredAlias))
        throw new MessageException("Cannot add (pseudo) extension to v1 message for alias: " + preferredAlias);

    // use the hardcoded "sreg" alias for SREG, for seamless interoperation
    // between SREG10/OpenID1 and SREG11/OpenID2
    if (SRegMessage.OPENID_NS_SREG.equals(typeUri))
        alias = "sreg";

    _extAliases.put(typeUri, alias);

    if (DEBUG) _log.debug("Adding extension; type URI: "
                          + typeUri + " alias: " +alias);

    //if (hasParameter("openid.ns"))
        set("openid.ns." + alias, typeUri);

    Iterator iter = extension.getParameters().getParameters().iterator();
    while (iter.hasNext())
    {
        Parameter param = (Parameter) iter.next();

        String paramName = param.getKey().length() > 0 ?
                "openid." + alias + "." + param.getKey() :
                "openid." + alias;

        set(paramName, param.getValue());
    }


    if (this instanceof AuthSuccess)
    {
        if (extension.signRequired())
            ((AuthSuccess)this).addSignExtension(typeUri);

        if ( ((AuthSuccess)this).getSignExtensions().contains(typeUri) )
            ((AuthSuccess)this).buildSignedList();
    }

    return alias;
}
 
Example #6
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;
}