Package org.openid4java.discovery

Examples of org.openid4java.discovery.DiscoveryInformation


            // perform discovery on the user-supplied identifier
            List discoveries = manager.discover(userSuppliedString);

            // attempt to associate with the OpenID provider
            // and retrieve one service endpoint for authentication
            DiscoveryInformation discovered = manager.associate(discoveries);

            // store the discovery information in the user's session
            ctx.getSession().setAttribute("openid-disc", discovered);

            // obtain a AuthRequest message to be sent to the OpenID provider
            AuthRequest authReq = manager.authenticate(discovered, returnToUrl);

            // Attribute Exchange example: fetching the 'email' attribute
            FetchRequest fetch = FetchRequest.createFetchRequest();
            fetch.addAttribute("email",
                    // attribute alias
                    "http://schema.openid.net/contact/email",   // type URI
                    true);                                      // required

            // attach the extension to the authentication request
            authReq.addExtension(fetch);


            if (! discovered.isVersion2() )
            {
                // Option 1: GET HTTP-redirect to the OpenID Provider endpoint
                // The only method supported in OpenID 1.x
                // redirect-URL usually limited ~2048 bytes
                try {
View Full Code Here


            // (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();
View Full Code Here

                            providerOpenIDConsumerManager = openIDStep2ConsumerManager;
                        }
                    }
                   
                    // OpenID discovery
                    DiscoveryInformation discovered = null;
                    try
                    {
                        if (userSuppliedDiscoveryString != null)
                        {
                            List discoveries = null;
                            if (providerConsumer.equals(STEP2_CONSUMER_INIT_PARAM_NAME_VALUE))
                            {
                                // verify discovery string is likely a host name
                                if ((userSuppliedDiscoveryString.indexOf("://") == -1) && (userSuppliedDiscoveryString.indexOf('@') == -1) && (userSuppliedDiscoveryString.indexOf('=') == -1))
                                {
                                    // Step2 OpenId discovery
                                    IdpIdentifier providerIdentifier = new IdpIdentifier(userSuppliedDiscoveryString);
                                    discoveries = providerOpenIDConsumerManager.getDiscovery().discover(providerIdentifier);                                   
                                }
                            }
                            else
                            {
                                // standard OpenId discovery
                                discoveries = providerOpenIDConsumerManager.discover(userSuppliedDiscoveryString);
                            }
                            if ((discoveries != null) && !discoveries.isEmpty())
                            {
                                discovered = providerOpenIDConsumerManager.associate(discoveries);
                            }
                        }
                    }
                    catch (OpenIDException oide)
                    {
                        throw new RuntimeException("Unexpected OpenID discovery exception: "+oide, oide);
                    }
                    if (discovered == null)
                    {
                        throw new RuntimeException("No OpenID provider discovered for: "+userSuppliedDiscoveryString);                   
                    }
                    discoveredProvider = true;

                    // log OpenID provider
                    if (log.isDebugEnabled())
                    {
                        log.debug("Discovered OpenID provider endpoint: "+discovered.getOPEndpoint()+", ["+discovered.getClass().getSimpleName()+"]");
                    }

                    // save login state
                    httpSession.setAttribute(OPEN_ID_LOGIN_LOCALE_ATTR_NAME, request.getLocale());
                    httpSession.setAttribute(OPEN_ID_LOGIN_SERVER_NAME_ATTR_NAME, request.getServerName());

                    // save OpenID provider in session
                    httpSession.setAttribute(OPEN_ID_PROVIDER_ATTR_NAME, provider);
                    httpSession.setAttribute(OPEN_ID_DISCOVERY_INFO_ATTR_NAME, discovered);

                    // create OpenID authentication request and redirect
                    String authReturnToURL = openIDRealmURL+"/"+OpenIDConstants.OPEN_ID_AUTHENTICATED_REQUEST+"?"+OpenIDConstants.OPEN_ID_RETURN+"="+returnPath;
                    String authRedirectURL = null;
                    try
                    {
                        // authentication request
                        AuthRequest authRequest = providerOpenIDConsumerManager.authenticate(discovered, authReturnToURL, openIDRealmURL);
                        // request attribute exchange data
                        FetchRequest axRequest = FetchRequest.createFetchRequest();
                        axRequest.addAttribute("email", "http://axschema.org/contact/email", true);
                        axRequest.addAttribute("fullname", "http://axschema.org/namePerson", true);
                        axRequest.addAttribute("lastname", "http://axschema.org/namePerson/last", true);
                        axRequest.addAttribute("firstname", "http://axschema.org/namePerson/first", true);
                        axRequest.addAttribute("nickname", "http://axschema.org/namePerson/friendly", true);
                        authRequest.addExtension(axRequest);
                        // request simple registration data
                        SRegRequest sregRequest = SRegRequest.createFetchRequest();
                        sregRequest.addAttribute("email", true);
                        sregRequest.addAttribute("fullname", true);
                        sregRequest.addAttribute("nickname", true);
                        authRequest.addExtension(sregRequest);
                        // authentication redirect
                        authRedirectURL = authRequest.getDestinationUrl(true);
                    }
                    catch (OpenIDException oide)
                    {
                        throw new RuntimeException("Unexpected OpenID authentication request exception: "+oide, oide);
                    }
                    response.sendRedirect(authRedirectURL);

                    // log authentication redirect
                    if (log.isDebugEnabled())
                    {
                        log.debug("OpenID authentication redirect: "+authRedirectURL);
                    }
                }
                catch (Exception e)
                {
                    // log error and redirect back to portal with error
                    // set as session attribute
                    log.error("OpenID login error: "+e, e);
                    httpSession.setAttribute(OpenIDConstants.OPEN_ID_ERROR, (!discoveredProvider ? OpenIDConstants.OPEN_ID_ERROR_NO_PROVIDER : OpenIDConstants.OPEN_ID_ERROR_CANNOT_AUTH));
                    response.sendRedirect(returnPath);
                }
            }
            else if (requestPath.equals(OpenIDConstants.OPEN_ID_AUTHENTICATED_REQUEST))
            {
                // request parameters
                String returnPath = request.getParameter(OpenIDConstants.OPEN_ID_RETURN);
                if ((returnPath == null) || (returnPath.length() == 0))
                {
                    returnPath = request.getContextPath()+"/";
                }
               
                // session parameters
                Locale loginLocale = (Locale)httpSession.getAttribute(OPEN_ID_LOGIN_LOCALE_ATTR_NAME);
                if (loginLocale != null)
                {
                    httpSession.removeAttribute(OPEN_ID_LOGIN_LOCALE_ATTR_NAME);
                }
                String loginServerName = (String)httpSession.getAttribute(OPEN_ID_LOGIN_SERVER_NAME_ATTR_NAME);
                if (loginServerName != null)
                {
                    httpSession.removeAttribute(OPEN_ID_LOGIN_SERVER_NAME_ATTR_NAME);
                }
                OpenIDRegistrationConfiguration portalRegistrationConfiguration = (OpenIDRegistrationConfiguration)httpSession.getAttribute(OpenIDConstants.OPEN_ID_REGISTRATION_CONFIGURATION);
                if (portalRegistrationConfiguration != null)
                {
                    httpSession.removeAttribute(OpenIDConstants.OPEN_ID_REGISTRATION_CONFIGURATION);
                }

                boolean authenticatedByProvider = false;
                boolean portalUserExists = false;
                try
                {
                    // request parameters
                    ParameterList authParams = new ParameterList(request.getParameterMap());

                    // retrieve OpenID provider from session
                    String provider = (String)httpSession.getAttribute(OPEN_ID_PROVIDER_ATTR_NAME);
                    DiscoveryInformation discovered = (DiscoveryInformation)httpSession.getAttribute(OPEN_ID_DISCOVERY_INFO_ATTR_NAME);

                    // reconstruct the authenticated request URL
                    StringBuffer authRequestURLBuffer = request.getRequestURL();
                    String authRequestQueryString = request.getQueryString();
                    if ((authRequestQueryString != null) && (authRequestQueryString.length() > 0))
View Full Code Here

      @SuppressWarnings("rawtypes")
      List discoveries = manager.discover(userSuppliedString);

      // attempt to associate with the OpenID provider
      // and retrieve one service endpoint for authentication
      DiscoveryInformation discovered = manager.associate(discoveries);

      // store the discovery information in the user's session
      httpReq.getSession().setAttribute("openid-disc", discovered);

      // obtain a AuthRequest message to be sent to the OpenID provider
View Full Code Here

      // 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();
View Full Code Here

    replay(resp);
    return resp;
  }
 
  private DiscoveryInformation createMockInfo() {
    DiscoveryInformation info = createMock(DiscoveryInformation.class);
    replay(info);
    return info;
  }
View Full Code Here

    return id;
  }

  @Test
  public void testAuthRequest() throws IOException, MessageException, DiscoveryException, ConsumerException {
    DiscoveryInformation info = createMockInfo();
    HttpSession session = createMockSession(info, false, false, true);
    HttpServletRequest req = createMockRequest(session);
    HttpServletResponse resp = createMockResponse();

    AuthRequest authRequest = createMockAuthRequest();
View Full Code Here

    assertFalse(consumer.authRequest("discover", req, resp));
  }
 
  @Test(expected = RuntimeException.class)
  public void testAuthRequestIOException() throws IOException, MessageException, DiscoveryException, ConsumerException {
    DiscoveryInformation info = createMockInfo();
    HttpSession session = createMockSession(info, false, false, true);
    HttpServletRequest req = createMockRequest(session);
    HttpServletResponse resp = createMockResponse();

    AuthRequest authRequest = createMockAuthRequest();
View Full Code Here

    assertFalse(consumer.authRequest("discover", req, resp));
  }

  @Test
  public void testVerifyResponse() throws IOException, MessageException, DiscoveryException, ConsumerException, AssociationException {
    DiscoveryInformation info = createMockInfo();
    HttpSession session = createMockSession(info, true, true, true);
    HttpServletRequest req = createMockRequest(session);

    AuthRequest authRequest = createMockAuthRequest();
    AuthSuccess authSuccess = createMockAuthSuccess(createMockFetchResponse(), createRegResponse(),
View Full Code Here

    assertEquals(id, consumer.verifyResponse(req));
  }
 
  @Test
  public void testVerifyResponseNullIdentifier() throws IOException, MessageException, DiscoveryException, ConsumerException, AssociationException {
    DiscoveryInformation info = createMockInfo();
    HttpSession session = createMockSession(info, false, false, true);
    HttpServletRequest req = createMockRequest(session);

    AuthRequest authRequest = createMockAuthRequest();
    AuthSuccess authSuccess = createMockAuthSuccess(createMockFetchResponse(), createRegResponse(),
View Full Code Here

TOP

Related Classes of org.openid4java.discovery.DiscoveryInformation

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.