public OAuthUserInfo getUserInfo()

in src/main/java/com/googlesource/gerrit/plugins/oauth/PhabricatorOAuthService.java [78:127]


  public OAuthUserInfo getUserInfo(OAuthToken token) throws IOException {
    OAuthRequest request =
        new OAuthRequest(Verb.GET, String.format(PROTECTED_RESOURCE_URL, rootUrl));
    OAuth2AccessToken t = new OAuth2AccessToken(token.getToken(), token.getRaw());
    service.signRequest(t, request);

    JsonElement userJson = null;
    try (Response response = service.execute(request)) {
      if (response.getCode() != HttpServletResponse.SC_OK) {
        throw new IOException(
            String.format(
                "Status %s (%s) for request %s",
                response.getCode(), response.getBody(), request.getUrl()));
      }
      userJson = JSON.newGson().fromJson(response.getBody(), JsonElement.class);
      if (log.isDebugEnabled()) {
        log.debug("User info response: {}", response.getBody());
      }
      if (userJson.isJsonObject()) {
        JsonObject jsonObject = userJson.getAsJsonObject();
        JsonElement jsonResult = jsonObject.get("result");
        if (jsonResult == null) {
          throw new IOException("Response doesn't contain result field");
        }
        JsonObject resultObject = jsonResult.getAsJsonObject();
        JsonElement id = resultObject.get("phid");
        if (id == null || id.isJsonNull()) {
          throw new IOException("Response doesn't contain id field");
        }
        JsonElement email = resultObject.get("primaryEmail");
        JsonElement name = resultObject.get("realName");
        JsonElement username = resultObject.get("userName");
        String login = null;

        if (!username.isJsonNull()) {
          login = username.getAsString();
        }
        return new OAuthUserInfo(
            PHABRICATOR_PROVIDER_PREFIX + id.getAsString() /*externalId*/,
            login /*username*/,
            email == null || email.isJsonNull() ? null : email.getAsString() /*email*/,
            name == null || name.isJsonNull() ? null : name.getAsString() /*displayName*/,
            null);
      }
    } catch (ExecutionException | InterruptedException e) {
      throw new RuntimeException("Cannot retrieve user info resource", e);
    }

    throw new IOException(String.format("Invalid JSON '%s': not a JSON Object", userJson));
  }