Skip to content

Commit f55d41f

Browse files
authored
fix: accessToken scopes clean serialization and default as empty list (#1125)
* fix: acessToken scopes clean serialization and default as empty list * cleanup and more tests * 🦉 Updates from OwlBot post-processor
1 parent 240c26b commit f55d41f

File tree

6 files changed

+133
-62
lines changed

6 files changed

+133
-62
lines changed

oauth2_http/java/com/google/auth/oauth2/AccessToken.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import com.google.common.base.MoreObjects;
3535
import java.io.Serializable;
36+
import java.util.ArrayList;
3637
import java.util.Arrays;
3738
import java.util.Date;
3839
import java.util.List;
@@ -54,7 +55,7 @@ public class AccessToken implements Serializable {
5455
public AccessToken(String tokenValue, Date expirationTime) {
5556
this.tokenValue = tokenValue;
5657
this.expirationTimeMillis = (expirationTime == null) ? null : expirationTime.getTime();
57-
this.scopes = null;
58+
this.scopes = new ArrayList<>();
5859
}
5960

6061
private AccessToken(Builder builder) {
@@ -135,7 +136,7 @@ public boolean equals(Object obj) {
135136
public static class Builder {
136137
private String tokenValue;
137138
private Date expirationTime;
138-
private List<String> scopes;
139+
private List<String> scopes = new ArrayList<>();
139140

140141
protected Builder() {}
141142

@@ -163,9 +164,18 @@ public Builder setTokenValue(String tokenValue) {
163164
}
164165

165166
public Builder setScopes(String scopes) {
166-
if (scopes != null) {
167+
if (scopes != null && scopes.trim().length() > 0) {
167168
this.scopes = Arrays.asList(scopes.split(" "));
168169
}
170+
return this;
171+
}
172+
173+
public Builder setScopes(List<String> scopes) {
174+
if (scopes == null) {
175+
this.scopes = new ArrayList<>();
176+
} else {
177+
this.scopes = scopes;
178+
}
169179

170180
return this;
171181
}

oauth2_http/java/com/google/auth/oauth2/DefaultCredentialsProvider.java

-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
* overriding the state and environment for testing purposes.
5454
*/
5555
class DefaultCredentialsProvider {
56-
5756
static final DefaultCredentialsProvider DEFAULT = new DefaultCredentialsProvider();
5857
static final String CREDENTIAL_ENV_VAR = "GOOGLE_APPLICATION_CREDENTIALS";
5958
static final String QUOTA_PROJECT_ENV_VAR = "GOOGLE_CLOUD_QUOTA_PROJECT";

oauth2_http/java/com/google/auth/oauth2/OAuth2Utils.java

+15
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import java.util.Arrays;
6464
import java.util.Collection;
6565
import java.util.HashSet;
66+
import java.util.List;
6667
import java.util.Map;
6768
import java.util.Set;
6869

@@ -168,6 +169,20 @@ static String validateOptionalString(Map<String, Object> map, String key, String
168169
return (String) value;
169170
}
170171

172+
/** Return the specified list of strings from JSON or throw a helpful error message. */
173+
static List<String> validateOptionalListString(
174+
Map<String, Object> map, String key, String errorPrefix) throws IOException {
175+
Object value = map.get(key);
176+
if (value == null) {
177+
return null;
178+
}
179+
if (!(value instanceof List)) {
180+
throw new IOException(
181+
String.format(VALUE_WRONG_TYPE_MESSAGE, errorPrefix, "List<String>", key));
182+
}
183+
return (List<String>) value;
184+
}
185+
171186
/** Return the specified integer from JSON or throw a helpful error message. */
172187
static int validateInt32(Map<String, Object> map, String key, String errorPrefix)
173188
throws IOException {

oauth2_http/java/com/google/auth/oauth2/UserAuthorizer.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import java.io.IOException;
4747
import java.net.URI;
4848
import java.net.URL;
49+
import java.util.ArrayList;
4950
import java.util.Collection;
5051
import java.util.Date;
5152
import java.util.List;
@@ -205,8 +206,8 @@ public UserCredentials getCredentials(String userId) throws IOException {
205206
Long expirationMillis =
206207
OAuth2Utils.validateLong(tokenJson, "expiration_time_millis", TOKEN_STORE_ERROR);
207208
Date expirationTime = new Date(expirationMillis);
208-
String scopes =
209-
OAuth2Utils.validateOptionalString(
209+
List<String> scopes =
210+
OAuth2Utils.validateOptionalListString(
210211
tokenJson, OAuth2Utils.TOKEN_RESPONSE_SCOPE, FETCH_TOKEN_ERROR);
211212
AccessToken accessToken =
212213
AccessToken.newBuilder()
@@ -362,20 +363,18 @@ public void storeCredentials(String userId, UserCredentials credentials) throws
362363
String acessTokenValue = null;
363364
String scopes = null;
364365
Date expiresBy = null;
366+
List<String> grantedScopes = new ArrayList<>();
367+
365368
if (accessToken != null) {
366369
acessTokenValue = accessToken.getTokenValue();
367370
expiresBy = accessToken.getExpirationTime();
368-
List<String> grantedScopes = accessToken.getScopes();
369-
370-
if (grantedScopes != null) {
371-
scopes = String.join(" ", grantedScopes);
372-
}
371+
grantedScopes = accessToken.getScopes();
373372
}
374373
String refreshToken = credentials.getRefreshToken();
375374
GenericJson tokenStateJson = new GenericJson();
376375
tokenStateJson.setFactory(OAuth2Utils.JSON_FACTORY);
377376
tokenStateJson.put("access_token", acessTokenValue);
378-
tokenStateJson.put(OAuth2Utils.TOKEN_RESPONSE_SCOPE, scopes);
377+
tokenStateJson.put(OAuth2Utils.TOKEN_RESPONSE_SCOPE, grantedScopes);
379378
tokenStateJson.put("expiration_time_millis", expiresBy.getTime());
380379
if (refreshToken != null) {
381380
tokenStateJson.put("refresh_token", refreshToken);

oauth2_http/javatests/com/google/auth/oauth2/AccessTokenTest.java

+55-8
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,17 @@
3131

3232
package com.google.auth.oauth2;
3333

34-
import static org.junit.Assert.assertArrayEquals;
3534
import static org.junit.Assert.assertEquals;
3635
import static org.junit.Assert.assertFalse;
36+
import static org.junit.Assert.assertNotSame;
37+
import static org.junit.Assert.assertSame;
3738
import static org.junit.Assert.assertTrue;
3839

3940
import java.io.IOException;
41+
import java.util.ArrayList;
4042
import java.util.Arrays;
4143
import java.util.Date;
44+
import java.util.List;
4245
import org.junit.Test;
4346
import org.junit.runner.RunWith;
4447
import org.junit.runners.JUnit4;
@@ -49,15 +52,16 @@ public class AccessTokenTest extends BaseSerializationTest {
4952

5053
private static final String TOKEN = "AccessToken";
5154
private static final Date EXPIRATION_DATE = new Date();
52-
private static final String SCOPES = "scope1 scope2";
55+
private static final List<String> SCOPES = Arrays.asList("scope1", "scope2");
56+
private static final String SCOPES_STRING = "scope1 scope2";
5357

5458
@Test
5559
public void constructor() {
5660
AccessToken accessToken = new AccessToken(TOKEN, EXPIRATION_DATE);
5761
assertEquals(TOKEN, accessToken.getTokenValue());
5862
assertEquals(EXPIRATION_DATE, accessToken.getExpirationTime());
5963
assertEquals(EXPIRATION_DATE.getTime(), (long) accessToken.getExpirationTimeMillis());
60-
assertEquals(null, accessToken.getScopes());
64+
assertEquals(new ArrayList<>(), accessToken.getScopes());
6165
}
6266

6367
@Test
@@ -66,12 +70,41 @@ public void builder() {
6670
AccessToken.newBuilder()
6771
.setExpirationTime(EXPIRATION_DATE)
6872
.setTokenValue(TOKEN)
69-
.setScopes(SCOPES)
73+
.setScopes(SCOPES_STRING)
7074
.build();
7175
assertEquals(TOKEN, accessToken.getTokenValue());
7276
assertEquals(EXPIRATION_DATE, accessToken.getExpirationTime());
7377
assertEquals(EXPIRATION_DATE.getTime(), (long) accessToken.getExpirationTimeMillis());
74-
assertArrayEquals(SCOPES.split(" "), accessToken.getScopes().toArray());
78+
assertEquals(SCOPES, accessToken.getScopes());
79+
assertNotSame(SCOPES, accessToken.getScopes());
80+
81+
// scopes list
82+
accessToken =
83+
AccessToken.newBuilder()
84+
.setExpirationTime(EXPIRATION_DATE)
85+
.setTokenValue(TOKEN)
86+
.setScopes(SCOPES)
87+
.build();
88+
assertEquals(SCOPES, accessToken.getScopes());
89+
assertSame(SCOPES, accessToken.getScopes());
90+
91+
// single scope
92+
accessToken =
93+
AccessToken.newBuilder()
94+
.setExpirationTime(EXPIRATION_DATE)
95+
.setTokenValue(TOKEN)
96+
.setScopes("dummy")
97+
.build();
98+
assertEquals(Arrays.asList("dummy"), accessToken.getScopes());
99+
100+
// empty scope
101+
accessToken =
102+
AccessToken.newBuilder()
103+
.setExpirationTime(EXPIRATION_DATE)
104+
.setTokenValue(TOKEN)
105+
.setScopes(" ")
106+
.build();
107+
assertEquals(new ArrayList<>(), accessToken.getScopes());
75108
}
76109

77110
@Test
@@ -87,6 +120,7 @@ public void equals_true() throws IOException {
87120
AccessToken.newBuilder()
88121
.setExpirationTime(EXPIRATION_DATE)
89122
.setTokenValue(TOKEN)
123+
.setTokenValue(TOKEN)
90124
.setScopes(SCOPES)
91125
.build();
92126

@@ -107,7 +141,7 @@ public void equals_false_scopes() throws IOException {
107141
AccessToken.newBuilder()
108142
.setExpirationTime(EXPIRATION_DATE)
109143
.setTokenValue(TOKEN)
110-
.setScopes("scope1")
144+
.setScopes(Arrays.asList("scope1"))
111145
.build();
112146

113147
assertFalse(accessToken.equals(otherAccessToken));
@@ -165,7 +199,7 @@ public void toString_containsFields() {
165199
String expectedToString =
166200
String.format(
167201
"AccessToken{tokenValue=%s, expirationTimeMillis=%d, scopes=%s}",
168-
TOKEN, EXPIRATION_DATE.getTime(), Arrays.asList(SCOPES.split(" ")));
202+
TOKEN, EXPIRATION_DATE.getTime(), SCOPES);
169203
assertEquals(expectedToString, accessToken.toString());
170204
}
171205

@@ -190,14 +224,27 @@ public void hashCode_equals() throws IOException {
190224

191225
@Test
192226
public void serialize() throws IOException, ClassNotFoundException {
227+
AccessToken emptyScopes =
228+
AccessToken.newBuilder()
229+
.setExpirationTime(EXPIRATION_DATE)
230+
.setTokenValue(TOKEN)
231+
.setScopes("")
232+
.build();
233+
234+
AccessToken deserializedAccessToken = serializeAndDeserialize(emptyScopes);
235+
assertEquals(emptyScopes, deserializedAccessToken);
236+
assertEquals(emptyScopes.hashCode(), deserializedAccessToken.hashCode());
237+
assertEquals(emptyScopes.toString(), deserializedAccessToken.toString());
238+
assertEquals(new ArrayList<>(), deserializedAccessToken.getScopes());
239+
193240
AccessToken accessToken =
194241
AccessToken.newBuilder()
195242
.setExpirationTime(EXPIRATION_DATE)
196243
.setTokenValue(TOKEN)
197244
.setScopes(SCOPES)
198245
.build();
199246

200-
AccessToken deserializedAccessToken = serializeAndDeserialize(accessToken);
247+
deserializedAccessToken = serializeAndDeserialize(accessToken);
201248
assertEquals(accessToken, deserializedAccessToken);
202249
assertEquals(accessToken.hashCode(), deserializedAccessToken.hashCode());
203250
assertEquals(accessToken.toString(), deserializedAccessToken.toString());

0 commit comments

Comments
 (0)