List of usage examples for com.google.gson JsonElement getAsString
public String getAsString()
From source file:com.googlesource.gerrit.plugins.oauth.BitbucketOAuthService.java
License:Apache License
@Override public OAuthUserInfo getUserInfo(OAuthToken token) throws IOException { OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL); Token t = new Token(token.getToken(), token.getSecret(), token.getRaw()); service.signRequest(t, request);/*from ww w . j a v a 2 s.co m*/ Response response = request.send(); if (response.getCode() != SC_OK) { throw new IOException(String.format("Status %s (%s) for request %s", response.getCode(), response.getBody(), request.getUrl())); } JsonElement 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(); JsonObject userObject = jsonObject.getAsJsonObject("user"); if (userObject == null || userObject.isJsonNull()) { throw new IOException("Response doesn't contain 'user' field"); } JsonElement usernameElement = userObject.get("username"); String username = usernameElement.getAsString(); JsonElement displayName = jsonObject.get("display_name"); return new OAuthUserInfo(username, username, null, displayName == null || displayName.isJsonNull() ? null : displayName.getAsString(), null); } else { throw new IOException(String.format("Invalid JSON '%s': not a JSON Object", userJson)); } }
From source file:com.googlesource.gerrit.plugins.oauth.CasOAuthService.java
License:Apache License
@Override public OAuthUserInfo getUserInfo(OAuthToken token) throws IOException { final String protectedResourceUrl = String.format(PROTECTED_RESOURCE_URL, rootUrl); OAuthRequest request = new OAuthRequest(Verb.GET, protectedResourceUrl); Token t = new Token(token.getToken(), token.getSecret(), token.getRaw()); service.signRequest(t, request);/* www. j a va 2 s .co m*/ Response response = request.send(); if (response.getCode() != HttpServletResponse.SC_OK) { throw new IOException(String.format("Status %s (%s) for request %s", response.getCode(), response.getBody(), request.getUrl())); } if (log.isDebugEnabled()) { log.debug("User info response: {}", response.getBody()); } JsonElement userJson = JSON.newGson().fromJson(response.getBody(), JsonElement.class); if (!userJson.isJsonObject()) { throw new IOException(String.format("Invalid JSON '%s': not a JSON Object", userJson)); } JsonObject jsonObject = userJson.getAsJsonObject(); JsonElement id = jsonObject.get("id"); if (id == null || id.isJsonNull()) { throw new IOException(String.format("CAS response missing id: %s", response.getBody())); } JsonElement attrListJson = jsonObject.get("attributes"); if (attrListJson == null) { throw new IOException(String.format("CAS response missing attributes: %s", response.getBody())); } String email = null, name = null, login = null; if (attrListJson.isJsonArray()) { // It is possible for CAS to be configured to not return any attributes (email, name, login), // in which case, // CAS returns an empty JSON object "attributes":{}, rather than "null" or an empty JSON array // "attributes": [] JsonArray attrJson = attrListJson.getAsJsonArray(); for (JsonElement elem : attrJson) { if (elem == null || !elem.isJsonObject()) { throw new IOException(String.format("Invalid JSON '%s': not a JSON Object", elem)); } JsonObject obj = elem.getAsJsonObject(); String property = getStringElement(obj, "email"); if (property != null) email = property; property = getStringElement(obj, "name"); if (property != null) name = property; property = getStringElement(obj, "login"); if (property != null) login = property; } } return new OAuthUserInfo(CAS_PROVIDER_PREFIX + id.getAsString(), login, email, name, fixLegacyUserId ? id.getAsString() : null); }
From source file:com.googlesource.gerrit.plugins.oauth.CasOAuthService.java
License:Apache License
private String getStringElement(JsonObject o, String name) { JsonElement elem = o.get(name); if (elem == null || elem.isJsonNull()) return null; return elem.getAsString(); }
From source file:com.googlesource.gerrit.plugins.oauth.DexOAuthService.java
License:Apache License
@Override public OAuthUserInfo getUserInfo(OAuthToken token) throws IOException { JsonElement tokenJson = JSON.newGson().fromJson(token.getRaw(), JsonElement.class); JsonObject tokenObject = tokenJson.getAsJsonObject(); JsonElement id_token = tokenObject.get("id_token"); JsonElement claimJson = JSON.newGson().fromJson(parseJwt(id_token.getAsString()), JsonElement.class); // Dex does not support basic profile currently (2017-09), extracting info // from access token claim JsonObject claimObject = claimJson.getAsJsonObject(); JsonElement emailElement = claimObject.get("email"); JsonElement nameElement = claimObject.get("name"); if (emailElement == null || emailElement.isJsonNull()) { throw new IOException("Response doesn't contain email field"); }//from w w w. ja v a 2 s . c o m if (nameElement == null || nameElement.isJsonNull()) { throw new IOException("Response doesn't contain name field"); } String email = emailElement.getAsString(); String name = nameElement.getAsString(); String username = email; if (domain != null && domain.length() > 0) { username = email.replace("@" + domain, ""); } return new OAuthUserInfo(DEX_PROVIDER_PREFIX + email /*externalId*/, username /*username*/, email /*email*/, name /*displayName*/, null /*claimedIdentity*/); }
From source file:com.googlesource.gerrit.plugins.oauth.FacebookOAuthService.java
License:Apache License
@Override public OAuthUserInfo getUserInfo(OAuthToken token) throws IOException { OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL); Token t = new Token(token.getToken(), token.getSecret(), token.getRaw()); request.addQuerystringParameter(FIELDS_QUERY, FIELDS); service.signRequest(t, request);//from www. j a v a 2 s . c om Response response = request.send(); if (response.getCode() != HttpServletResponse.SC_OK) { throw new IOException(String.format("Status %s (%s) for request %s", response.getCode(), response.getBody(), request.getUrl())); } JsonElement 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 id = jsonObject.get("id"); if (id == null || id.isJsonNull()) { throw new IOException("Response doesn't contain id field"); } JsonElement email = jsonObject.get("email"); JsonElement name = jsonObject.get("name"); // Heads up! // Lets keep `login` equal to `email`, since `username` field is // deprecated for Facebook API versions v2.0 and higher JsonElement login = jsonObject.get("email"); return new OAuthUserInfo(FACEBOOK_PROVIDER_PREFIX + id.getAsString(), login == null || login.isJsonNull() ? null : login.getAsString(), email == null || email.isJsonNull() ? null : email.getAsString(), name == null || name.isJsonNull() ? null : name.getAsString(), null); } throw new IOException(String.format("Invalid JSON '%s': not a JSON Object", userJson)); }
From source file:com.googlesource.gerrit.plugins.oauth.GitCafeOAuthService.java
License:Apache License
@Override public OAuthUserInfo getUserInfo(OAuthToken token) throws IOException { OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL); request.addHeader("Authorization", "Bearer " + token.getToken()); Token t = new Token(token.getToken(), token.getSecret(), token.getRaw()); service.signRequest(t, request);//w w w . ja va2 s . c o m Response response = request.send(); if (response.getCode() != HttpServletResponse.SC_OK) { throw new IOException(String.format("Status %s (%s) for request %s", response.getCode(), response.getBody(), request.getUrl())); } JsonElement userJson = OutputFormat.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 id = jsonObject.get("id"); if (id == null || id.isJsonNull()) { throw new IOException(String.format("Response doesn't contain id field")); } JsonElement email = jsonObject.get("email"); JsonElement fullname = jsonObject.get("fullname"); JsonElement username = jsonObject.get("username"); return new OAuthUserInfo(id.getAsString(), username == null || username.isJsonNull() ? null : username.getAsString(), email == null || email.isJsonNull() ? null : email.getAsString(), fullname == null || fullname.isJsonNull() ? null : fullname.getAsString(), null); } else { throw new IOException(String.format("Invalid JSON '%s': not a JSON Object", userJson)); } }
From source file:com.googlesource.gerrit.plugins.oauth.GitHubOAuthService.java
License:Apache License
@Override public OAuthUserInfo getUserInfo(OAuthToken token) throws IOException { OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL); Token t = new Token(token.getToken(), token.getSecret(), token.getRaw()); service.signRequest(t, request);//from www. j a v a 2 s . c om Response response = request.send(); if (response.getCode() != HttpServletResponse.SC_OK) { throw new IOException(String.format("Status %s (%s) for request %s", response.getCode(), response.getBody(), request.getUrl())); } JsonElement userJson = OutputFormat.JSON.newGson().fromJson(response.getBody(), JsonElement.class); if (userJson.isJsonObject()) { JsonObject jsonObject = userJson.getAsJsonObject(); JsonElement email = jsonObject.get("email"); JsonElement name = jsonObject.get("name"); JsonElement id = jsonObject.get("id"); JsonElement login = jsonObject.get("login"); return new OAuthUserInfo(id.getAsString(), login.isJsonNull() ? null : login.getAsString(), email.isJsonNull() ? null : email.getAsString(), name.isJsonNull() ? null : name.getAsString(), null); } else { throw new IOException(String.format("Invalid JSON '%s': not a JSON Object", userJson)); } }
From source file:com.googlesource.gerrit.plugins.oauth.GitLabOAuthService.java
License:Apache License
@Override public OAuthUserInfo getUserInfo(OAuthToken token) throws IOException { final String protectedResourceUrl = String.format(PROTECTED_RESOURCE_URL, rootUrl); OAuthRequest request = new OAuthRequest(Verb.GET, protectedResourceUrl); Token t = new Token(token.getToken(), token.getSecret(), token.getRaw()); service.signRequest(t, request);/*w w w .j ava2s . co m*/ Response response = request.send(); if (response.getCode() != SC_OK) { throw new IOException(String.format("Status %s (%s) for request %s", response.getCode(), response.getBody(), request.getUrl())); } JsonElement userJson = JSON.newGson().fromJson(response.getBody(), JsonElement.class); if (log.isDebugEnabled()) { log.debug("User info response: {}", response.getBody()); } JsonObject jsonObject = userJson.getAsJsonObject(); if (jsonObject == null || jsonObject.isJsonNull()) { throw new IOException("Response doesn't contain 'user' field" + jsonObject); } JsonElement id = jsonObject.get("id"); JsonElement username = jsonObject.get("username"); JsonElement email = jsonObject.get("email"); JsonElement name = jsonObject.get("name"); return new OAuthUserInfo(GITLAB_PROVIDER_PREFIX + id.getAsString(), username == null || username.isJsonNull() ? null : username.getAsString(), email == null || email.isJsonNull() ? null : email.getAsString(), name == null || name.isJsonNull() ? null : name.getAsString(), null); }
From source file:com.googlesource.gerrit.plugins.oauth.GoogleOAuthService.java
License:Apache License
@Override public OAuthUserInfo getUserInfo(OAuthToken token) throws IOException { OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL); Token t = new Token(token.getToken(), token.getSecret(), token.getRaw()); service.signRequest(t, request);/* w w w .ja va2s . c o m*/ Response response = request.send(); if (response.getCode() != HttpServletResponse.SC_OK) { throw new IOException(String.format("Status %s (%s) for request %s", response.getCode(), response.getBody(), request.getUrl())); } JsonElement userJson = OutputFormat.JSON.newGson().fromJson(response.getBody(), JsonElement.class); if (userJson.isJsonObject()) { JsonObject jsonObject = userJson.getAsJsonObject(); JsonElement id = jsonObject.get("id"); if (id.isJsonNull()) { throw new IOException(String.format("Response doesn't contain id field")); } JsonElement email = jsonObject.get("email"); JsonElement name = jsonObject.get("name"); String claimedIdentifier = null; if (linkToExistingOpenIDAccounts) { claimedIdentifier = lookupClaimedIdentity(token); } return new OAuthUserInfo(id.getAsString() /*externalId*/, null /*username*/, email.isJsonNull() ? null : email.getAsString() /*email*/, name.isJsonNull() ? null : name.getAsString() /*displayName*/, claimedIdentifier /*claimedIdentity*/); } else { throw new IOException(String.format("Invalid JSON '%s': not a JSON Object", userJson)); } }
From source file:com.googlesource.gerrit.plugins.oauth.GoogleOAuthService.java
License:Apache License
/** * @param token// w w w . j a v a2 s .co m * @return OpenID id token, when contained in id_token, null otherwise */ private static String lookupClaimedIdentity(OAuthToken token) { JsonElement idToken = OutputFormat.JSON.newGson().fromJson(token.getRaw(), JsonElement.class); if (idToken.isJsonObject()) { JsonObject idTokenObj = idToken.getAsJsonObject(); JsonElement idTokenElement = idTokenObj.get("id_token"); if (!idTokenElement.isJsonNull()) { String payload = decodePayload(idTokenElement.getAsString()); if (!Strings.isNullOrEmpty(payload)) { JsonElement openidIdToken = OutputFormat.JSON.newGson().fromJson(payload, JsonElement.class); if (openidIdToken.isJsonObject()) { JsonObject openidIdObj = openidIdToken.getAsJsonObject(); JsonElement openidIdElement = openidIdObj.get("openid_id"); if (!openidIdElement.isJsonNull()) { String openIdId = openidIdElement.getAsString(); log.debug("OAuth2: openid_id={}", openIdId); return openIdId; } log.debug("OAuth2: JWT doesn't contain openid_id element"); } } } } return null; }