示例#1
0
  public boolean verify() {
    try {
      // Normalize
      String claimedId = normalize(id);
      String server = null;
      String delegate = null;

      // Discover
      HttpResponse response = WS.url(claimedId).get();

      // Try HTML (I know it's bad)
      String html = response.getString();
      server = discoverServer(html);

      if (server == null) {

        // Try YADIS
        Document xrds = null;

        if (response.getContentType().contains("application/xrds+xml")) {
          xrds = getXml(html, response.getEncoding());
        } else if (response.getHeader("X-XRDS-Location") != null) {
          xrds = WS.url(response.getHeader("X-XRDS-Location")).get().getXml();
        } else {
          return false;
        }

        // Ok we have the XRDS file
        server =
            XPath.selectText(
                "//Type[text()='http://specs.openid.net/auth/2.0/server']/following-sibling::URI/text()",
                xrds);
        claimedId =
            XPath.selectText(
                "//Type[text()='http://specs.openid.net/auth/2.0/signon']/following-sibling::LocalID/text()",
                xrds);
        if (claimedId == null) {
          claimedId = "http://specs.openid.net/auth/2.0/identifier_select";
        } else {
          server =
              XPath.selectText(
                  "//Type[text()='http://specs.openid.net/auth/2.0/signon']/following-sibling::URI/text()",
                  xrds);
        }

        if (server == null) {
          return false;
        }

      } else {

        // Delegate
        Matcher openid2Localid =
            Pattern.compile("<link[^>]+openid2[.]local_id[^>]+>", Pattern.CASE_INSENSITIVE)
                .matcher(html);
        Matcher openidDelegate =
            Pattern.compile("<link[^>]+openid[.]delegate[^>]+>", Pattern.CASE_INSENSITIVE)
                .matcher(html);
        if (openid2Localid.find()) {
          delegate = extractHref(openid2Localid.group());
        } else if (openidDelegate.find()) {
          delegate = extractHref(openidDelegate.group());
        }
      }

      // Redirect
      String url = server;
      if (!server.contains("?")) {
        url += "?";
      }
      if (!url.endsWith("?") && !url.endsWith("&")) {
        url += "&";
      }

      url += "openid.ns=" + URLEncoder.encode("http://specs.openid.net/auth/2.0", "UTF-8");
      url += "&openid.mode=checkid_setup";
      url += "&openid.claimed_id=" + URLEncoder.encode(claimedId, "utf8");
      url +=
          "&openid.identity=" + URLEncoder.encode(delegate == null ? claimedId : delegate, "utf8");

      if (returnAction != null
          && (returnAction.startsWith("http://") || returnAction.startsWith("https://"))) {
        url += "&openid.return_to=" + URLEncoder.encode(returnAction, "utf8");
      } else {
        url +=
            "&openid.return_to="
                + URLEncoder.encode(
                    Request.current().getBase() + Router.reverse(returnAction), "utf8");
      }
      if (realmAction != null
          && (realmAction.startsWith("http://") || realmAction.startsWith("https://"))) {
        url += "&openid.realm=" + URLEncoder.encode(realmAction, "utf8");
      } else {
        url +=
            "&openid.realm="
                + URLEncoder.encode(
                    Request.current().getBase() + Router.reverse(realmAction), "utf8");
      }

      if (!sregOptional.isEmpty() || !sregRequired.isEmpty()) {
        url +=
            "&openid.ns.sreg="
                + URLEncoder.encode("http://openid.net/extensions/sreg/1.1", "UTF-8");
      }
      String sregO = "";
      for (String a : sregOptional) {
        sregO += URLEncoder.encode(a, "UTF-8") + ",";
      }
      if (!StringUtils.isEmpty(sregO)) {
        url += "&openid.sreg.optional=" + sregO.substring(0, sregO.length() - 1);
      }
      String sregR = "";
      for (String a : sregRequired) {
        sregR += URLEncoder.encode(a, "UTF-8") + ",";
      }
      if (!StringUtils.isEmpty(sregR)) {
        url += "&openid.sreg.required=" + sregR.substring(0, sregR.length() - 1);
      }

      if (!axRequired.isEmpty() || !axOptional.isEmpty()) {
        url += "&openid.ns.ax=http%3A%2F%2Fopenid.net%2Fsrv%2Fax%2F1.0";
        url += "&openid.ax.mode=fetch_request";
        for (String a : axOptional.keySet()) {
          url += "&openid.ax.type." + a + "=" + URLEncoder.encode(axOptional.get(a), "UTF-8");
        }
        for (String a : axRequired.keySet()) {
          url += "&openid.ax.type." + a + "=" + URLEncoder.encode(axRequired.get(a), "UTF-8");
        }
        if (!axRequired.isEmpty()) {
          String r = "";
          for (String a : axRequired.keySet()) {
            r += "," + a;
          }
          r = r.substring(1);
          url += "&openid.ax.required=" + r;
        }
        if (!axOptional.isEmpty()) {
          String r = "";
          for (String a : axOptional.keySet()) {
            r += "," + a;
          }
          r = r.substring(1);
          url += "&openid.ax.if_available=" + r;
        }
      }

      if (Logger.isTraceEnabled()) {
        // Debug
        Logger.trace("Send request %s", url);
      }

      throw new Redirect(url);
    } catch (Redirect e) {
      throw e;
    } catch (PlayException e) {
      throw e;
    } catch (Exception e) {
      return false;
    }
  }