public void onPreAuthenticate(
     OpenIdUser user, HttpServletRequest request, UrlEncodedParameterMap params) {
   // the popup sign-in magic
   if ("true".equals(request.getParameter("popup"))) {
     String returnTo =
         params.get(Constants.OPENID_TRUST_ROOT)
             + request.getContextPath()
             + "/popup_verify.html";
     params.put(Constants.OPENID_RETURN_TO, returnTo);
     params.put(Constants.OPENID_REALM, returnTo);
     params.put("openid.ns.ui", "http://specs.openid.net/extensions/ui/1.0");
     params.put("openid.ui.mode", "popup");
   }
 }
  /**
   * Gets the UrlEncodedParameterMap filled with the openid parameters that is used to redirect the
   * user to his openid provider.
   */
  public static UrlEncodedParameterMap getAuthUrlMap(
      OpenIdUser user, String trustRoot, String realm, String returnTo) {
    if (!user.isAssociated())
      throw new IllegalArgumentException("claimed_id of user has not been verified.");

    UrlEncodedParameterMap map = new UrlEncodedParameterMap(user.getOpenIdServer());

    String identity = user.getOpenIdDelegate();
    if (identity == null) identity = user.getClaimedId();

    map.put(Constants.OPENID_NS, Constants.DEFAULT_NS);
    map.put(Constants.OPENID_MODE, Constants.Mode.CHECKID_SETUP);

    map.put(Constants.OPENID_TRUST_ROOT, trustRoot);
    map.put(Constants.OPENID_REALM, realm);
    map.put(Constants.OPENID_RETURN_TO, returnTo);
    map.put(Constants.OPENID_ASSOC_HANDLE, user.getAssocHandle());

    map.put(Constants.OPENID_IDENTITY, identity);
    map.put(Constants.OPENID_CLAIMED_ID, identity);

    return map;
  }
  /**
   * Gets the StringBuilder filled with the openid parameters that is used to redirect the user to
   * his openid provider.
   */
  public static StringBuilder getAuthUrlBuffer(
      OpenIdUser user, String trustRoot, String realm, String returnTo) {
    if (!user.isAssociated())
      throw new IllegalArgumentException("claimed_id of user has not been verified.");

    String identity = user.getOpenIdDelegate();
    if (identity == null) identity = user.getClaimedId();

    StringBuilder buffer = new StringBuilder().append(user.getOpenIdServer());
    char separator = user.getOpenIdServer().indexOf('?') == -1 ? '?' : '&';
    buffer.append(separator).append(Constants.OPENID_NS).append('=').append(Constants.DEFAULT_NS);

    buffer
        .append('&')
        .append(Constants.OPENID_MODE)
        .append('=')
        .append(Constants.Mode.CHECKID_SETUP);

    buffer
        .append('&')
        .append(Constants.OPENID_TRUST_ROOT)
        .append('=')
        .append(UrlEncodedParameterMap.encode(trustRoot));
    buffer
        .append('&')
        .append(Constants.OPENID_REALM)
        .append('=')
        .append(UrlEncodedParameterMap.encode(realm));
    buffer
        .append('&')
        .append(Constants.OPENID_RETURN_TO)
        .append('=')
        .append(UrlEncodedParameterMap.encode(returnTo));
    buffer
        .append('&')
        .append(Constants.OPENID_ASSOC_HANDLE)
        .append('=')
        .append(UrlEncodedParameterMap.encode(user.getAssocHandle()));

    buffer
        .append('&')
        .append(Constants.OPENID_IDENTITY)
        .append('=')
        .append(UrlEncodedParameterMap.encode(identity));
    buffer
        .append('&')
        .append(Constants.OPENID_CLAIMED_ID)
        .append('=')
        .append(UrlEncodedParameterMap.encode(identity));

    return buffer;
  }