Extract OAuth2 access token json extractor in its own class
Change-Id: I896cdaf15ea2a54e89b1ea1934f36be1a970e8be
diff --git a/src/main/java/com/googlesource/gerrit/plugins/oauth/GitLabApi.java b/src/main/java/com/googlesource/gerrit/plugins/oauth/GitLabApi.java
index 2c27f35..db0851f 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/oauth/GitLabApi.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/oauth/GitLabApi.java
@@ -14,16 +14,11 @@
package com.googlesource.gerrit.plugins.oauth;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
import org.scribe.builder.api.DefaultApi20;
-import org.scribe.exceptions.OAuthException;
import org.scribe.extractors.AccessTokenExtractor;
import org.scribe.model.OAuthConfig;
-import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.oauth.OAuthService;
-import org.scribe.utils.Preconditions;
public class GitLabApi extends DefaultApi20 {
private static final String AUTHORIZE_URL =
@@ -57,21 +52,6 @@
@Override
public AccessTokenExtractor getAccessTokenExtractor() {
- return new GitLabJsonTokenExtractor();
- }
-
- private static final class GitLabJsonTokenExtractor implements AccessTokenExtractor {
- private Pattern accessTokenPattern = Pattern.compile("\"access_token\"\\s*:\\s*\"(\\S*?)\"");
-
- @Override
- public Token extract(String response) {
- Preconditions.checkEmptyString(
- response, "Cannot extract a token from a null or empty String");
- Matcher matcher = accessTokenPattern.matcher(response);
- if (matcher.find()) {
- return new Token(matcher.group(1), "", response);
- }
- throw new OAuthException("Cannot extract an acces token. Response was: " + response);
- }
+ return OAuth2AccessTokenJsonExtractor.instance();
}
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/oauth/Google2Api.java b/src/main/java/com/googlesource/gerrit/plugins/oauth/Google2Api.java
index ed48549..88c640d 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/oauth/Google2Api.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/oauth/Google2Api.java
@@ -16,13 +16,9 @@
import static org.scribe.utils.OAuthEncoder.encode;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
import org.scribe.builder.api.DefaultApi20;
-import org.scribe.exceptions.OAuthException;
import org.scribe.extractors.AccessTokenExtractor;
import org.scribe.model.OAuthConfig;
-import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.oauth.OAuthService;
import org.scribe.utils.Preconditions;
@@ -62,22 +58,6 @@
@Override
public AccessTokenExtractor getAccessTokenExtractor() {
- return new GoogleJsonTokenExtractor();
- }
-
- private static final class GoogleJsonTokenExtractor implements AccessTokenExtractor {
- private Pattern accessTokenPattern = Pattern.compile("\"access_token\"\\s*:\\s*\"(\\S*?)\"");
-
- @Override
- public Token extract(String response) {
- Preconditions.checkEmptyString(
- response, "Cannot extract a token from a null or empty String");
- Matcher matcher = accessTokenPattern.matcher(response);
- if (matcher.find()) {
- return new Token(matcher.group(1), "", response);
- }
-
- throw new OAuthException("Cannot extract an acces token. Response was: " + response);
- }
+ return OAuth2AccessTokenJsonExtractor.instance();
}
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/oauth/OAuth2AccessTokenJsonExtractor.java b/src/main/java/com/googlesource/gerrit/plugins/oauth/OAuth2AccessTokenJsonExtractor.java
new file mode 100644
index 0000000..3fad710
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/oauth/OAuth2AccessTokenJsonExtractor.java
@@ -0,0 +1,47 @@
+// Copyright (C) 2018 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 static org.scribe.model.OAuthConstants.ACCESS_TOKEN;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import org.scribe.exceptions.OAuthException;
+import org.scribe.extractors.AccessTokenExtractor;
+import org.scribe.model.Token;
+import org.scribe.utils.Preconditions;
+
+class OAuth2AccessTokenJsonExtractor implements AccessTokenExtractor {
+ private static final Pattern ACCESS_TOKEN_REGEX_PATTERN =
+ Pattern.compile("\"" + ACCESS_TOKEN + "\"\\s*:\\s*\"(\\S*?)\"");
+
+ private OAuth2AccessTokenJsonExtractor() {}
+
+ private static final AccessTokenExtractor INSTANCE = new OAuth2AccessTokenJsonExtractor();
+
+ static AccessTokenExtractor instance() {
+ return INSTANCE;
+ }
+
+ @Override
+ public Token extract(String response) {
+ Preconditions.checkEmptyString(response, "Cannot extract a token from a null or empty String");
+ Matcher matcher = ACCESS_TOKEN_REGEX_PATTERN.matcher(response);
+ if (matcher.find()) {
+ return new Token(matcher.group(1), "", response);
+ }
+ throw new OAuthException("Cannot extract an access token. Response was: " + response);
+ }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/oauth/Office365Api.java b/src/main/java/com/googlesource/gerrit/plugins/oauth/Office365Api.java
index 12dd12f..8a28520 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/oauth/Office365Api.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/oauth/Office365Api.java
@@ -16,13 +16,9 @@
import static org.scribe.utils.OAuthEncoder.encode;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
import org.scribe.builder.api.DefaultApi20;
-import org.scribe.exceptions.OAuthException;
import org.scribe.extractors.AccessTokenExtractor;
import org.scribe.model.OAuthConfig;
-import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.oauth.OAuthService;
import org.scribe.utils.Preconditions;
@@ -61,22 +57,6 @@
@Override
public AccessTokenExtractor getAccessTokenExtractor() {
- return new Office365JsonTokenExtractor();
- }
-
- private static final class Office365JsonTokenExtractor implements AccessTokenExtractor {
- private Pattern accessTokenPattern = Pattern.compile("\"access_token\"\\s*:\\s*\"(\\S*?)\"");
-
- @Override
- public Token extract(String response) {
- Preconditions.checkEmptyString(
- response, "Cannot extract a token from a null or empty String");
- Matcher matcher = accessTokenPattern.matcher(response);
- if (matcher.find()) {
- return new Token(matcher.group(1), "", response);
- }
-
- throw new OAuthException("Cannot extract an acces token. Response was: " + response);
- }
+ return OAuth2AccessTokenJsonExtractor.instance();
}
}