-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'oauth/master'
Change-Id: I4b2e23d9d23f89fd134bb093eeb4657527d48c78
- Loading branch information
Showing
11 changed files
with
173 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/java/com/googlesource/gerrit/plugins/oauth/Facebook2Api.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// 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 org.scribe.builder.api.FacebookApi; | ||
import org.scribe.extractors.AccessTokenExtractor; | ||
|
||
public class Facebook2Api extends FacebookApi { | ||
@Override | ||
public AccessTokenExtractor getAccessTokenExtractor() { | ||
return OAuth2AccessTokenJsonExtractor.instance(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/com/googlesource/gerrit/plugins/oauth/OAuth2AccessTokenJsonExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// 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 com.google.common.annotations.VisibleForTesting; | ||
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; | ||
} | ||
|
||
@VisibleForTesting | ||
@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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/test/java/com/googlesource/gerrit/plugins/oauth/OAuth2AccessTokenJsonExtractorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// 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.junit.Assert.assertEquals; | ||
import static org.scribe.model.OAuthConstants.ACCESS_TOKEN; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
import org.scribe.exceptions.OAuthException; | ||
import org.scribe.extractors.AccessTokenExtractor; | ||
import org.scribe.model.Token; | ||
|
||
public class OAuth2AccessTokenJsonExtractorTest { | ||
private static final AccessTokenExtractor extractor = OAuth2AccessTokenJsonExtractor.instance(); | ||
private static final String TOKEN = "I0122HHJKLEM21F3WLPYHDKGKZULAUO4SGMV3ABKFTDT3T3X"; | ||
private static final String RESPONSE = "{\"" + ACCESS_TOKEN + "\":\"" + TOKEN + "\"}'"; | ||
private static final String RESPONSE_NON_JSON = ACCESS_TOKEN + "=" + TOKEN; | ||
private static final String RESPONSE_WITH_BLANKS = | ||
"{ \"" + ACCESS_TOKEN + "\" : \"" + TOKEN + "\"}'"; | ||
private static final String MESSAGE = "Cannot extract a token from a null or empty String"; | ||
|
||
@Rule public ExpectedException exception = ExpectedException.none(); | ||
|
||
@Test | ||
public void parseResponse() throws Exception { | ||
Token token = extractor.extract(RESPONSE); | ||
assertEquals(token.getToken(), TOKEN); | ||
} | ||
|
||
@Test | ||
public void parseResponseWithBlanks() throws Exception { | ||
Token token = extractor.extract(RESPONSE_WITH_BLANKS); | ||
assertEquals(token.getToken(), TOKEN); | ||
} | ||
|
||
@Test | ||
public void failParseNonJsonResponse() throws Exception { | ||
exception.expect(OAuthException.class); | ||
exception.expectMessage("Cannot extract an access token. Response was: " + RESPONSE_NON_JSON); | ||
extractor.extract(RESPONSE_NON_JSON); | ||
} | ||
|
||
@Test | ||
public void shouldThrowExceptionIfForNullParameter() throws Exception { | ||
exception.expect(IllegalArgumentException.class); | ||
exception.expectMessage(MESSAGE); | ||
extractor.extract(null); | ||
} | ||
|
||
@Test | ||
public void shouldThrowExceptionIfForEmptyString() throws Exception { | ||
exception.expect(IllegalArgumentException.class); | ||
exception.expectMessage(MESSAGE); | ||
extractor.extract(""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
load( | ||
"@com_googlesource_gerrit_bazlets//tools:junit.bzl", | ||
"junit_tests", | ||
) |