Factor out some wide spread boilerplate code

Change-Id: I9cf7e674d6499a09f8a15a3d8f55ec55157b5695
diff --git a/src/main/java/com/googlesource/gerrit/plugins/oauth/AirVantageOAuthService.java b/src/main/java/com/googlesource/gerrit/plugins/oauth/AirVantageOAuthService.java
index 0af9316..2bd7b3e 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/oauth/AirVantageOAuthService.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/oauth/AirVantageOAuthService.java
@@ -15,6 +15,7 @@
 package com.googlesource.gerrit.plugins.oauth;
 
 import static com.google.gerrit.json.OutputFormat.JSON;
+import static com.googlesource.gerrit.plugins.oauth.JsonUtil.isNull;
 import static javax.servlet.http.HttpServletResponse.SC_OK;
 import static org.slf4j.LoggerFactory.getLogger;
 
@@ -87,7 +88,7 @@
       if (userJson.isJsonObject()) {
         JsonObject jsonObject = userJson.getAsJsonObject();
         JsonElement id = jsonObject.get("uid");
-        if (id == null || id.isJsonNull()) {
+        if (isNull(jsonObject)) {
           throw new IOException("Response doesn't contain uid field");
         }
         JsonElement email = jsonObject.get("email");
diff --git a/src/main/java/com/googlesource/gerrit/plugins/oauth/Auth0OAuthService.java b/src/main/java/com/googlesource/gerrit/plugins/oauth/Auth0OAuthService.java
index 065120f..b2fc206 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/oauth/Auth0OAuthService.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/oauth/Auth0OAuthService.java
@@ -15,6 +15,8 @@
 package com.googlesource.gerrit.plugins.oauth;
 
 import static com.google.gerrit.json.OutputFormat.JSON;
+import static com.googlesource.gerrit.plugins.oauth.JsonUtil.asString;
+import static com.googlesource.gerrit.plugins.oauth.JsonUtil.isNull;
 
 import com.github.scribejava.core.builder.ServiceBuilder;
 import com.github.scribejava.core.model.OAuth2AccessToken;
@@ -93,7 +95,7 @@
         log.debug("User info response: {}", response.getBody());
       }
       JsonObject jsonObject = userJson.getAsJsonObject();
-      if (jsonObject == null || jsonObject.isJsonNull()) {
+      if (isNull(jsonObject)) {
         throw new IOException("Response doesn't contain 'user' field" + jsonObject);
       }
       JsonElement id = jsonObject.get("sub");
@@ -102,9 +104,9 @@
       JsonElement name = jsonObject.get("name");
       return new OAuthUserInfo(
           AUTH0_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(),
+          asString(username),
+          asString(email),
+          asString(name),
           id.getAsString());
     } catch (ExecutionException | InterruptedException e) {
       throw new RuntimeException("Cannot retrieve user info resource", e);
diff --git a/src/main/java/com/googlesource/gerrit/plugins/oauth/AuthentikOAuthService.java b/src/main/java/com/googlesource/gerrit/plugins/oauth/AuthentikOAuthService.java
index df810a1..7c4662a 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/oauth/AuthentikOAuthService.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/oauth/AuthentikOAuthService.java
@@ -15,6 +15,8 @@
 package com.googlesource.gerrit.plugins.oauth;
 
 import static com.google.gerrit.json.OutputFormat.JSON;
+import static com.googlesource.gerrit.plugins.oauth.JsonUtil.asString;
+import static com.googlesource.gerrit.plugins.oauth.JsonUtil.isNull;
 
 import com.github.scribejava.core.builder.ServiceBuilder;
 import com.github.scribejava.core.model.OAuth2AccessToken;
@@ -95,7 +97,7 @@
         log.debug("User info response: {}", response.getBody());
       }
       JsonObject jsonObject = userJson.getAsJsonObject();
-      if (jsonObject == null || jsonObject.isJsonNull()) {
+      if (isNull(jsonObject)) {
         throw new IOException("Response doesn't contain 'user' field" + jsonObject);
       }
       JsonElement id = jsonObject.get("sub");
@@ -103,11 +105,11 @@
       JsonElement email = jsonObject.get("email");
       JsonElement name = jsonObject.get("name");
       return new OAuthUserInfo(
-          AUTHENTIK_PROVIDER_PREFIX + id.getAsString() /*externalId*/,
-          username == null || username.isJsonNull() ? null : username.getAsString() /*username*/,
-          email == null || email.isJsonNull() ? null : email.getAsString() /*email*/,
-          name == null || name.isJsonNull() ? null : name.getAsString() /*displayName*/,
-          linkExistingGerrit ? "gerrit:" + username.getAsString() : null /*claimedIdentity*/);
+          AUTHENTIK_PROVIDER_PREFIX + id.getAsString(),
+          asString(username),
+          asString(email),
+          asString(name),
+          linkExistingGerrit ? "gerrit:" + username.getAsString() : null);
     } catch (ExecutionException | InterruptedException e) {
       throw new RuntimeException("Cannot retrieve user info resource", e);
     }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/oauth/AzureActiveDirectoryService.java b/src/main/java/com/googlesource/gerrit/plugins/oauth/AzureActiveDirectoryService.java
index 49077fb..c221170 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/oauth/AzureActiveDirectoryService.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/oauth/AzureActiveDirectoryService.java
@@ -15,6 +15,8 @@
 package com.googlesource.gerrit.plugins.oauth;
 
 import static com.google.gerrit.json.OutputFormat.JSON;
+import static com.googlesource.gerrit.plugins.oauth.JsonUtil.asString;
+import static com.googlesource.gerrit.plugins.oauth.JsonUtil.isNull;
 
 import com.github.scribejava.apis.MicrosoftAzureActiveDirectory20Api;
 import com.github.scribejava.core.builder.ServiceBuilder;
@@ -172,7 +174,7 @@
       if (userJson.isJsonObject()) {
         JsonObject jsonObject = userJson.getAsJsonObject();
         JsonElement id = jsonObject.get("id");
-        if (id == null || id.isJsonNull()) {
+        if (isNull(id)) {
           throw new IOException("Response doesn't contain id field");
         }
         JsonElement email = jsonObject.get("mail");
@@ -184,10 +186,10 @@
         }
 
         return new OAuthUserInfo(
-            providerPrefix + id.getAsString() /*externalId*/,
-            login /*username*/,
-            email == null || email.isJsonNull() ? null : email.getAsString() /*email*/,
-            name == null || name.isJsonNull() ? null : name.getAsString() /*displayName*/,
+            providerPrefix + id.getAsString(),
+            login,
+            asString(email),
+            asString(name),
             linkOffice365Id ? OFFICE365_PROVIDER_PREFIX + id.getAsString() : null);
       }
     } catch (ExecutionException | InterruptedException e) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/oauth/BitbucketOAuthService.java b/src/main/java/com/googlesource/gerrit/plugins/oauth/BitbucketOAuthService.java
index accf586..181236c 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/oauth/BitbucketOAuthService.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/oauth/BitbucketOAuthService.java
@@ -15,6 +15,7 @@
 package com.googlesource.gerrit.plugins.oauth;
 
 import static com.google.gerrit.json.OutputFormat.JSON;
+import static com.googlesource.gerrit.plugins.oauth.JsonUtil.isNull;
 import static javax.servlet.http.HttpServletResponse.SC_OK;
 import static org.slf4j.LoggerFactory.getLogger;
 
@@ -89,7 +90,7 @@
       if (userJson.isJsonObject()) {
         JsonObject jsonObject = userJson.getAsJsonObject();
         JsonObject userObject = jsonObject.getAsJsonObject("user");
-        if (userObject == null || userObject.isJsonNull()) {
+        if (isNull(userObject)) {
           throw new IOException("Response doesn't contain 'user' field");
         }
         JsonElement usernameElement = userObject.get("username");
diff --git a/src/main/java/com/googlesource/gerrit/plugins/oauth/CasOAuthService.java b/src/main/java/com/googlesource/gerrit/plugins/oauth/CasOAuthService.java
index 447b145..f47e096 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/oauth/CasOAuthService.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/oauth/CasOAuthService.java
@@ -16,6 +16,7 @@
 
 import static com.google.common.base.Strings.nullToEmpty;
 import static com.google.gerrit.json.OutputFormat.JSON;
+import static com.googlesource.gerrit.plugins.oauth.JsonUtil.isNull;
 
 import com.github.scribejava.core.builder.ServiceBuilder;
 import com.github.scribejava.core.model.OAuth2AccessToken;
@@ -104,7 +105,7 @@
       JsonObject jsonObject = userJson.getAsJsonObject();
 
       JsonElement id = jsonObject.get("id");
-      if (id == null || id.isJsonNull()) {
+      if (isNull(id)) {
         throw new IOException(String.format("CAS response missing id: %s", response.getBody()));
       }
 
@@ -158,7 +159,7 @@
 
   private String getStringElement(JsonObject o, String name) {
     JsonElement elem = o.get(name);
-    if (elem == null || elem.isJsonNull()) {
+    if (isNull(elem)) {
       return null;
     }
 
diff --git a/src/main/java/com/googlesource/gerrit/plugins/oauth/CognitoOAuthService.java b/src/main/java/com/googlesource/gerrit/plugins/oauth/CognitoOAuthService.java
index 3daef77..eee5a94 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/oauth/CognitoOAuthService.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/oauth/CognitoOAuthService.java
@@ -15,6 +15,8 @@
 package com.googlesource.gerrit.plugins.oauth;
 
 import static com.google.gerrit.json.OutputFormat.JSON;
+import static com.googlesource.gerrit.plugins.oauth.JsonUtil.asString;
+import static com.googlesource.gerrit.plugins.oauth.JsonUtil.isNull;
 
 import com.github.scribejava.core.builder.ServiceBuilder;
 import com.github.scribejava.core.model.OAuth2AccessToken;
@@ -96,7 +98,7 @@
       }
 
       JsonObject jsonObject = userJson.getAsJsonObject();
-      if (jsonObject == null || jsonObject.isJsonNull()) {
+      if (isNull(jsonObject)) {
         throw new IOException("Response doesn't contain 'user' field" + jsonObject);
       }
       JsonElement id = jsonObject.get("sub");
@@ -104,11 +106,11 @@
       JsonElement email = jsonObject.get("email");
       JsonElement name = jsonObject.get("name");
       return new OAuthUserInfo(
-          COGNITO_PROVIDER_PREFIX + id.getAsString() /*externalId*/,
-          username == null || username.isJsonNull() ? null : username.getAsString() /*username*/,
-          email == null || email.isJsonNull() ? null : email.getAsString() /*email*/,
-          name == null || name.isJsonNull() ? null : name.getAsString() /*displayName*/,
-          null /*claimedIdentity*/);
+          COGNITO_PROVIDER_PREFIX + id.getAsString(),
+          asString(username),
+          asString(email),
+          asString(name),
+          null);
     } catch (ExecutionException | InterruptedException e) {
       throw new RuntimeException("Cannot retrieve user info resource", e);
     }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/oauth/DexOAuthService.java b/src/main/java/com/googlesource/gerrit/plugins/oauth/DexOAuthService.java
index f3841d7..abf3689 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/oauth/DexOAuthService.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/oauth/DexOAuthService.java
@@ -15,6 +15,7 @@
 package com.googlesource.gerrit.plugins.oauth;
 
 import static com.google.gerrit.json.OutputFormat.JSON;
+import static com.googlesource.gerrit.plugins.oauth.JsonUtil.isNull;
 
 import com.github.scribejava.core.builder.ServiceBuilder;
 import com.github.scribejava.core.model.OAuth2AccessToken;
@@ -109,7 +110,7 @@
     JsonObject claimObject = claimJson.getAsJsonObject();
     JsonElement emailElement = claimObject.get("email");
     JsonElement nameElement = claimObject.get("name");
-    if (emailElement == null || emailElement.isJsonNull()) {
+    if (isNull(emailElement)) {
       throw new IOException("Response doesn't contain email field");
     }
     if (nameElement == null || nameElement.isJsonNull()) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/oauth/FacebookOAuthService.java b/src/main/java/com/googlesource/gerrit/plugins/oauth/FacebookOAuthService.java
index 7682410..c2a2419 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/oauth/FacebookOAuthService.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/oauth/FacebookOAuthService.java
@@ -15,6 +15,8 @@
 package com.googlesource.gerrit.plugins.oauth;
 
 import static com.google.gerrit.json.OutputFormat.JSON;
+import static com.googlesource.gerrit.plugins.oauth.JsonUtil.asString;
+import static com.googlesource.gerrit.plugins.oauth.JsonUtil.isNull;
 
 import com.github.scribejava.core.builder.ServiceBuilder;
 import com.github.scribejava.core.model.OAuth2AccessToken;
@@ -94,7 +96,7 @@
       if (userJson.isJsonObject()) {
         JsonObject jsonObject = userJson.getAsJsonObject();
         JsonElement id = jsonObject.get("id");
-        if (id == null || id.isJsonNull()) {
+        if (isNull(id)) {
           throw new IOException("Response doesn't contain id field");
         }
         JsonElement email = jsonObject.get("email");
@@ -106,9 +108,9 @@
 
         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(),
+            asString(login),
+            asString(email),
+            asString(name),
             null);
       }
     } catch (ExecutionException | InterruptedException e) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/oauth/GitHubOAuthService.java b/src/main/java/com/googlesource/gerrit/plugins/oauth/GitHubOAuthService.java
index 12643b2..e62b3f5 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/oauth/GitHubOAuthService.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/oauth/GitHubOAuthService.java
@@ -15,6 +15,8 @@
 package com.googlesource.gerrit.plugins.oauth;
 
 import static com.google.gerrit.json.OutputFormat.JSON;
+import static com.googlesource.gerrit.plugins.oauth.JsonUtil.asString;
+import static com.googlesource.gerrit.plugins.oauth.JsonUtil.isNull;
 
 import com.github.scribejava.core.builder.ServiceBuilder;
 import com.github.scribejava.core.model.OAuth2AccessToken;
@@ -107,7 +109,7 @@
       if (userJson.isJsonObject()) {
         JsonObject jsonObject = userJson.getAsJsonObject();
         JsonElement id = jsonObject.get("id");
-        if (id == null || id.isJsonNull()) {
+        if (isNull(id)) {
           throw new IOException("Response doesn't contain id field");
         }
         JsonElement email = jsonObject.get("email");
@@ -115,9 +117,9 @@
         JsonElement login = jsonObject.get("login");
         return new OAuthUserInfo(
             GITHUB_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(),
+            asString(login),
+            asString(email),
+            asString(name),
             fixLegacyUserId ? id.getAsString() : null);
       }
     } catch (ExecutionException | InterruptedException e) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/oauth/GitLabOAuthService.java b/src/main/java/com/googlesource/gerrit/plugins/oauth/GitLabOAuthService.java
index 57eb94b..767b551 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/oauth/GitLabOAuthService.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/oauth/GitLabOAuthService.java
@@ -15,6 +15,8 @@
 package com.googlesource.gerrit.plugins.oauth;
 
 import static com.google.gerrit.json.OutputFormat.JSON;
+import static com.googlesource.gerrit.plugins.oauth.JsonUtil.asString;
+import static com.googlesource.gerrit.plugins.oauth.JsonUtil.isNull;
 import static javax.servlet.http.HttpServletResponse.SC_OK;
 import static org.slf4j.LoggerFactory.getLogger;
 
@@ -90,7 +92,7 @@
         log.debug("User info response: {}", response.getBody());
       }
       JsonObject jsonObject = userJson.getAsJsonObject();
-      if (jsonObject == null || jsonObject.isJsonNull()) {
+      if (isNull(jsonObject)) {
         throw new IOException("Response doesn't contain 'user' field" + jsonObject);
       }
       JsonElement id = jsonObject.get("id");
@@ -99,9 +101,9 @@
       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(),
+          asString(username),
+          asString(email),
+          asString(name),
           null);
     } catch (ExecutionException | InterruptedException e) {
       throw new RuntimeException("Cannot retrieve user info resource", e);
diff --git a/src/main/java/com/googlesource/gerrit/plugins/oauth/GoogleOAuthService.java b/src/main/java/com/googlesource/gerrit/plugins/oauth/GoogleOAuthService.java
index 0542326..98ce6f7 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/oauth/GoogleOAuthService.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/oauth/GoogleOAuthService.java
@@ -15,6 +15,8 @@
 package com.googlesource.gerrit.plugins.oauth;
 
 import static com.google.gerrit.json.OutputFormat.JSON;
+import static com.googlesource.gerrit.plugins.oauth.JsonUtil.asString;
+import static com.googlesource.gerrit.plugins.oauth.JsonUtil.isNull;
 
 import com.github.scribejava.core.builder.ServiceBuilder;
 import com.github.scribejava.core.model.OAuth2AccessToken;
@@ -114,7 +116,7 @@
       if (userJson.isJsonObject()) {
         JsonObject jsonObject = userJson.getAsJsonObject();
         JsonElement id = jsonObject.get("id");
-        if (id == null || id.isJsonNull()) {
+        if (isNull(id)) {
           throw new IOException("Response doesn't contain id field");
         }
         JsonElement email = jsonObject.get("email");
@@ -141,10 +143,10 @@
           login = email.getAsString().split("@")[0];
         }
         return new OAuthUserInfo(
-            GOOGLE_PROVIDER_PREFIX + id.getAsString() /*externalId*/,
-            login /*username*/,
-            email == null || email.isJsonNull() ? null : email.getAsString() /*email*/,
-            name == null || name.isJsonNull() ? null : name.getAsString() /*displayName*/,
+            GOOGLE_PROVIDER_PREFIX + id.getAsString(),
+            login,
+            asString(email),
+            asString(name),
             fixLegacyUserId ? id.getAsString() : null /*claimedIdentity*/);
       }
     } catch (ExecutionException | InterruptedException e) {
@@ -182,7 +184,7 @@
 
   private static String retrieveHostedDomain(JsonObject jwtToken) {
     JsonElement hdClaim = jwtToken.get("hd");
-    if (hdClaim != null && !hdClaim.isJsonNull()) {
+    if (!isNull(hdClaim)) {
       String hd = hdClaim.getAsString();
       log.debug("OAuth2: hd={}", hd);
       return hd;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/oauth/JsonUtil.java b/src/main/java/com/googlesource/gerrit/plugins/oauth/JsonUtil.java
new file mode 100644
index 0000000..db7aaff
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/oauth/JsonUtil.java
@@ -0,0 +1,30 @@
+// Copyright (C) 2025 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.oauth;
+
+import com.google.gerrit.common.Nullable;
+import com.google.gson.JsonElement;
+
+public class JsonUtil {
+
+  public static boolean isNull(JsonElement e) {
+    return e == null || e.isJsonNull();
+  }
+
+  @Nullable
+  public static String asString(JsonElement e) {
+    return isNull(e) ? null : e.getAsString();
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/oauth/KeycloakOAuthService.java b/src/main/java/com/googlesource/gerrit/plugins/oauth/KeycloakOAuthService.java
index 228dff3..fba25a8 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/oauth/KeycloakOAuthService.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/oauth/KeycloakOAuthService.java
@@ -15,6 +15,7 @@
 package com.googlesource.gerrit.plugins.oauth;
 
 import static com.google.gerrit.json.OutputFormat.JSON;
+import static com.googlesource.gerrit.plugins.oauth.JsonUtil.isNull;
 
 import com.github.scribejava.core.builder.ServiceBuilder;
 import com.github.scribejava.core.model.OAuth2AccessToken;
@@ -108,13 +109,13 @@
     JsonElement usernameElement = claimObject.get("preferred_username");
     JsonElement emailElement = claimObject.get("email");
     JsonElement nameElement = claimObject.get("name");
-    if (usernameElement == null || usernameElement.isJsonNull()) {
+    if (isNull(usernameElement)) {
       throw new IOException("Response doesn't contain preferred_username field");
     }
-    if (emailElement == null || emailElement.isJsonNull()) {
+    if (isNull(emailElement)) {
       throw new IOException("Response doesn't contain email field");
     }
-    if (nameElement == null || nameElement.isJsonNull()) {
+    if (isNull(nameElement)) {
       throw new IOException("Response doesn't contain name field");
     }
     String usernameAsString = usernameElement.getAsString();
diff --git a/src/main/java/com/googlesource/gerrit/plugins/oauth/LemonLDAPOAuthService.java b/src/main/java/com/googlesource/gerrit/plugins/oauth/LemonLDAPOAuthService.java
index e213cc3..ce19d97 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/oauth/LemonLDAPOAuthService.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/oauth/LemonLDAPOAuthService.java
@@ -15,6 +15,8 @@
 package com.googlesource.gerrit.plugins.oauth;
 
 import static com.google.gerrit.json.OutputFormat.JSON;
+import static com.googlesource.gerrit.plugins.oauth.JsonUtil.asString;
+import static com.googlesource.gerrit.plugins.oauth.JsonUtil.isNull;
 import static org.slf4j.LoggerFactory.getLogger;
 
 import com.github.scribejava.core.builder.ServiceBuilder;
@@ -86,7 +88,7 @@
         log.debug("User info response: {}", response.getBody());
       }
       JsonObject jsonObject = userJson.getAsJsonObject();
-      if (jsonObject == null || jsonObject.isJsonNull()) {
+      if (isNull(jsonObject)) {
         throw new IOException("Response doesn't contain 'user' field" + jsonObject);
       }
       JsonElement id = jsonObject.get("sub");
@@ -95,9 +97,9 @@
       JsonElement name = jsonObject.get("name");
       return new OAuthUserInfo(
           LEMONLDAP_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(),
+          asString(username),
+          asString(email),
+          asString(name),
           null);
     } catch (ExecutionException | InterruptedException e) {
       throw new RuntimeException("Cannot retrieve user info resource", e);
diff --git a/src/main/java/com/googlesource/gerrit/plugins/oauth/PhabricatorOAuthService.java b/src/main/java/com/googlesource/gerrit/plugins/oauth/PhabricatorOAuthService.java
index 18086ec..017fa5b 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/oauth/PhabricatorOAuthService.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/oauth/PhabricatorOAuthService.java
@@ -15,6 +15,8 @@
 package com.googlesource.gerrit.plugins.oauth;
 
 import static com.google.gerrit.json.OutputFormat.JSON;
+import static com.googlesource.gerrit.plugins.oauth.JsonUtil.asString;
+import static com.googlesource.gerrit.plugins.oauth.JsonUtil.isNull;
 
 import com.github.scribejava.core.builder.ServiceBuilder;
 import com.github.scribejava.core.model.OAuth2AccessToken;
@@ -101,7 +103,7 @@
         }
         JsonObject resultObject = jsonResult.getAsJsonObject();
         JsonElement id = resultObject.get("phid");
-        if (id == null || id.isJsonNull()) {
+        if (isNull(id)) {
           throw new IOException("Response doesn't contain id field");
         }
         JsonElement email = resultObject.get("primaryEmail");
@@ -113,10 +115,10 @@
           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*/,
+            PHABRICATOR_PROVIDER_PREFIX + id.getAsString(),
+            login,
+            asString(email),
+            asString(name),
             null);
       }
     } catch (ExecutionException | InterruptedException e) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/oauth/TuleapOAuthService.java b/src/main/java/com/googlesource/gerrit/plugins/oauth/TuleapOAuthService.java
index ef84957..e10d36b 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/oauth/TuleapOAuthService.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/oauth/TuleapOAuthService.java
@@ -15,6 +15,8 @@
 package com.googlesource.gerrit.plugins.oauth;
 
 import static com.google.gerrit.json.OutputFormat.JSON;
+import static com.googlesource.gerrit.plugins.oauth.JsonUtil.asString;
+import static com.googlesource.gerrit.plugins.oauth.JsonUtil.isNull;
 
 import com.github.scribejava.core.builder.ServiceBuilder;
 import com.github.scribejava.core.model.OAuth2AccessToken;
@@ -93,7 +95,7 @@
         log.debug("User info response: {}", response.getBody());
       }
       JsonObject jsonObject = userJson.getAsJsonObject();
-      if (jsonObject == null || jsonObject.isJsonNull()) {
+      if (isNull(jsonObject)) {
         throw new IOException("Response doesn't contain 'user' field" + jsonObject);
       }
       JsonElement id = jsonObject.get("sub");
@@ -102,9 +104,9 @@
       JsonElement name = jsonObject.get("name");
       return new OAuthUserInfo(
           TULEAP_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(),
+          asString(username),
+          asString(email),
+          asString(name),
           id.getAsString());
     } catch (ExecutionException | InterruptedException e) {
       throw new RuntimeException("Cannot retrieve user info resource", e);