Skip to content

Commit dee0645

Browse files
authored
Merge pull request spotify#1160 from spotify/dxia/patch1
Fix bug: use scoped GoogleCredentials
2 parents 990e734 + 2b023b6 commit dee0645

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

helios-client/src/main/java/com/spotify/helios/client/GoogleCredentialsAccessTokenProvider.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import com.google.auth.oauth2.AccessToken;
2727
import com.google.auth.oauth2.GoogleCredentials;
28+
import com.google.common.annotations.VisibleForTesting;
2829
import java.io.File;
2930
import java.io.FileInputStream;
3031
import java.io.IOException;
@@ -50,7 +51,7 @@ class GoogleCredentialsAccessTokenProvider {
5051
*
5152
* @return Return an AccessToken or null
5253
*/
53-
static AccessToken getAccessToken(List<String> scopes) throws IOException {
54+
static AccessToken getAccessToken(final List<String> scopes) throws IOException {
5455
GoogleCredentials credentials = null;
5556

5657
// first check whether the environment variable is set
@@ -70,16 +71,21 @@ static AccessToken getAccessToken(List<String> scopes) throws IOException {
7071
log.debug("Using Google Application Default Credentials");
7172
}
7273

74+
return getAccessToken(credentials, scopes);
75+
}
76+
77+
@VisibleForTesting
78+
static AccessToken getAccessToken(final GoogleCredentials credentials,
79+
final List<String> scopes) throws IOException {
7380
if (credentials == null) {
7481
return null;
7582
}
7683

77-
if (!scopes.isEmpty()) {
78-
credentials.createScoped(scopes);
79-
}
80-
credentials.refresh();
84+
final GoogleCredentials newCredentials =
85+
scopes.isEmpty() ? credentials : credentials.createScoped(scopes);
86+
newCredentials.refresh();
8187

82-
return credentials.getAccessToken();
88+
return newCredentials.getAccessToken();
8389
}
8490

8591
static AccessToken getAccessToken() throws IOException {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*-
2+
* -\-\-
3+
* Helios Client
4+
* --
5+
* Copyright (C) 2016 - 2017 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.helios.client;
22+
23+
import static java.util.Collections.singletonList;
24+
import static org.mockito.Matchers.anyCollectionOf;
25+
import static org.mockito.Mockito.mock;
26+
import static org.mockito.Mockito.verify;
27+
import static org.mockito.Mockito.when;
28+
29+
import com.google.auth.oauth2.GoogleCredentials;
30+
import java.util.List;
31+
import org.junit.Test;
32+
33+
public class GoogleCredentialsAccessTokenProviderTest {
34+
35+
@Test
36+
public void getAccessToken() throws Exception {
37+
final GoogleCredentials credentials = mock(GoogleCredentials.class);
38+
final GoogleCredentials scopedCredentials = mock(GoogleCredentials.class);
39+
when(credentials.createScoped(anyCollectionOf(String.class))).thenReturn(scopedCredentials);
40+
final List<String> scopes = singletonList("somescope");
41+
GoogleCredentialsAccessTokenProvider.getAccessToken(credentials, scopes);
42+
verify(credentials).createScoped(scopes);
43+
verify(scopedCredentials).refresh();
44+
}
45+
46+
}

0 commit comments

Comments
 (0)