예제 #1
0
  private void _initialize() {
    try {
      if (_manager == null) {
        _manager = new ConsumerManager();

        _manager.setAssociations(new InMemoryConsumerAssociationStore());
        _manager.setNonceVerifier(new InMemoryNonceVerifier(5000));
      }
    } catch (ConsumerException ce) {
      _log.error(ce.getMessage());
    }
  }
  @Override
  public void sendRequest(
      ThemeDisplay themeDisplay, ActionRequest actionRequest, ActionResponse actionResponse)
      throws PortalException {

    HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);

    request = PortalUtil.getOriginalServletRequest(request);

    HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse);

    HttpSession session = request.getSession();

    LiferayPortletResponse liferayPortletResponse =
        PortalUtil.getLiferayPortletResponse(actionResponse);

    String openId = ParamUtil.getString(actionRequest, "openId");

    PortletURL portletURL = liferayPortletResponse.createActionURL();

    portletURL.setParameter(ActionRequest.ACTION_NAME, "/login/openid");
    portletURL.setParameter("saveLastPath", Boolean.FALSE.toString());
    portletURL.setParameter("mvcRenderCommandName", "/login/openid");
    portletURL.setParameter(Constants.CMD, Constants.READ);

    try {
      List<DiscoveryInformation> discoveryInformationList = _consumerManager.discover(openId);

      DiscoveryInformation discoveryInformation =
          _consumerManager.associate(discoveryInformationList);

      session.setAttribute(OpenIdWebKeys.OPEN_ID_DISCO, discoveryInformation);

      AuthRequest authRequest =
          _consumerManager.authenticate(
              discoveryInformation, portletURL.toString(), themeDisplay.getPortalURL());

      if (_userLocalService.fetchUserByOpenId(themeDisplay.getCompanyId(), openId) != null) {

        response.sendRedirect(authRequest.getDestinationUrl(true));

        return;
      }

      String screenName = getScreenName(openId);

      User user = _userLocalService.fetchUserByScreenName(themeDisplay.getCompanyId(), screenName);

      if (user != null) {
        _userLocalService.updateOpenId(user.getUserId(), openId);

        response.sendRedirect(authRequest.getDestinationUrl(true));

        return;
      }

      FetchRequest fetchRequest = FetchRequest.createFetchRequest();

      OpenIdProvider openIdProvider =
          _openIdProviderRegistry.getOpenIdProvider(discoveryInformation.getOPEndpoint());

      Map<String, String> openIdAXTypes = openIdProvider.getAxTypes();

      for (String openIdAXType : openIdAXTypes.keySet()) {
        fetchRequest.addAttribute(openIdAXType, openIdAXTypes.get(openIdAXType), true);
      }

      authRequest.addExtension(fetchRequest);

      SRegRequest sRegRequest = SRegRequest.createFetchRequest();

      sRegRequest.addAttribute(_OPEN_ID_SREG_ATTR_EMAIL, true);
      sRegRequest.addAttribute(_OPEN_ID_SREG_ATTR_FULLNAME, true);

      authRequest.addExtension(sRegRequest);

      response.sendRedirect(authRequest.getDestinationUrl(true));
    } catch (ConsumerException ce) {
      throw new OpenIdServiceException.ConsumerException(ce.getMessage(), ce);
    } catch (DiscoveryException de) {
      throw new OpenIdServiceException.DiscoveryException(de.getMessage(), de);
    } catch (MessageException me) {
      throw new OpenIdServiceException.MessageException(me.getMessage(), me);
    } catch (IOException ioe) {
      throw new SystemException("Unable to communicate with OpenId provider", ioe);
    }
  }
  /*
   * This method is called by the application when the user clicks on 'Sign In
   * with Intuit' button from the Login Page to get the OpenId.
   */
  @RequestMapping(value = "/initialize.htm", method = RequestMethod.GET)
  public void initialize(final HttpServletRequest request, final HttpServletResponse response)
      throws IOException {

    LOG.info("### OpenIdController -> initialize() - started ###");

    final List<DiscoveryInformation> discoveries = new ArrayList<DiscoveryInformation>();
    final ConsumerManager manager = new ConsumerManager();

    manager.setAssociations(new InMemoryConsumerAssociationStore());
    manager.setNonceVerifier(new InMemoryNonceVerifier(5000));
    manager.setMinAssocSessEnc(AssociationSessionType.DH_SHA256);

    DiscoveryInformation discovered = null;

    try {
      LOG.info("OpenID Provider URL = " + WebUtils.OPENID_PROVIDER_URL);
      discovered = new DiscoveryInformation(new URL(WebUtils.OPENID_PROVIDER_URL));
    } catch (DiscoveryException e) {
      LOG.error(e.getLocalizedMessage());
    } catch (MalformedURLException me) {
      LOG.error(me.getLocalizedMessage());
    }

    discoveries.add(discovered);

    final DiscoveryInformation discoveryInfo = manager.associate(discoveries);
    request.getSession().setAttribute("openid-disc", discoveryInfo);

    final FetchRequest fetch = FetchRequest.createFetchRequest();

    try {
      fetch.addAttribute("FirstName", "http://axschema.org/namePerson/first", true);
      fetch.addAttribute("LastName", "http://axschema.org/namePerson/last", true);
      fetch.addAttribute("Email", "http://axschema.org/contact/email", true);
      fetch.addAttribute("RealmId", "http://axschema.org/intuit/realmId", true);
    } catch (MessageException e) {
      LOG.error(e.getLocalizedMessage());
    }

    fetch.setCount("Email", 3);

    AuthRequest authReq = null;
    LOG.info("openIdReturnUrl = " + WebUtils.OPENID_RETURN_URL);
    try {
      authReq = manager.authenticate(discoveryInfo, WebUtils.OPENID_RETURN_URL);
      authReq.addExtension(fetch);
    } catch (MessageException e) {
      LOG.error(e.getLocalizedMessage());
    } catch (ConsumerException e) {
      LOG.error(e.getLocalizedMessage());
    }

    final HttpSession session = request.getSession();
    LOG.info("Session Id : " + session.getId());
    session.setAttribute("consumerManager", manager);
    LOG.info("authReq.getDestinationUrl: " + authReq.getDestinationUrl(true));
    LOG.info("### OpenIdController -> initialize() - completed ###");

    response.sendRedirect(authReq.getDestinationUrl(true));
  }