Skip to content

Commit 0fab63c

Browse files
authored
Merge pull request #125 from google/shinfan-dev
Add builder pattern to AppEngine credentials
2 parents 4504fc1 + 0831e15 commit 0fab63c

2 files changed

Lines changed: 97 additions & 13 deletions

File tree

appengine/java/com/google/auth/appengine/AppEngineCredentials.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,12 @@ public class AppEngineCredentials extends GoogleCredentials implements ServiceAc
6262

6363
private transient AppIdentityService appIdentityService;
6464

65+
@Deprecated
6566
public AppEngineCredentials(Collection<String> scopes) {
6667
this(scopes, null);
6768
}
6869

70+
@Deprecated
6971
public AppEngineCredentials(Collection<String> scopes, AppIdentityService appIdentityService) {
7072
this.scopes = scopes == null ? ImmutableSet.<String>of() : ImmutableList.copyOf(scopes);
7173
this.appIdentityService = appIdentityService != null ? appIdentityService
@@ -137,4 +139,47 @@ private void readObject(ObjectInputStream input) throws IOException, ClassNotFou
137139
input.defaultReadObject();
138140
appIdentityService = newInstance(appIdentityServiceClassName);
139141
}
142+
143+
public static Builder newBuilder() {
144+
return new Builder();
145+
}
146+
147+
public Builder toBuilder() {
148+
return new Builder(this);
149+
}
150+
151+
public static class Builder extends GoogleCredentials.Builder {
152+
153+
private Collection<String> scopes;
154+
private AppIdentityService appIdentityService;
155+
156+
protected Builder() {}
157+
158+
protected Builder(AppEngineCredentials credentials) {
159+
this.scopes = credentials.scopes;
160+
this.appIdentityService = credentials.appIdentityService;
161+
}
162+
163+
public Builder setScopes(Collection<String> scopes) {
164+
this.scopes = scopes;
165+
return this;
166+
}
167+
168+
public Builder setAppIdentityService(AppIdentityService appIdentityService) {
169+
this.appIdentityService = appIdentityService;
170+
return this;
171+
}
172+
173+
public Collection<String> getScopes() {
174+
return scopes;
175+
}
176+
177+
public AppIdentityService getAppIdentityService() {
178+
return appIdentityService;
179+
}
180+
181+
public AppEngineCredentials build() {
182+
return new AppEngineCredentials(scopes, appIdentityService);
183+
}
184+
}
140185
}

appengine/javatests/com/google/auth/appengine/AppEngineCredentialsTest.java

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ public void refreshAccessToken_sameAs() throws IOException {
8989
MockAppIdentityService appIdentity = new MockAppIdentityService();
9090
appIdentity.setAccessTokenText(expectedAccessToken);
9191
appIdentity.setExpiration(new Date(System.currentTimeMillis() + 60L * 60L * 100L));
92-
AppEngineCredentials credentials = new AppEngineCredentials(SCOPES, appIdentity);
92+
AppEngineCredentials credentials = AppEngineCredentials.newBuilder()
93+
.setScopes(SCOPES)
94+
.setAppIdentityService(appIdentity)
95+
.build();
9396
AccessToken accessToken = credentials.refreshAccessToken();
9497
assertEquals(appIdentity.getAccessTokenText(), accessToken.getTokenValue());
9598
assertEquals(appIdentity.getExpiration(), accessToken.getExpirationTime());
@@ -99,7 +102,10 @@ public void refreshAccessToken_sameAs() throws IOException {
99102
public void getAccount_sameAs() throws IOException {
100103
MockAppIdentityService appIdentity = new MockAppIdentityService();
101104
appIdentity.setServiceAccountName(EXPECTED_ACCOUNT);
102-
AppEngineCredentials credentials = new AppEngineCredentials(SCOPES, appIdentity);
105+
AppEngineCredentials credentials = AppEngineCredentials.newBuilder()
106+
.setScopes(SCOPES)
107+
.setAppIdentityService(appIdentity)
108+
.build();
103109
assertEquals(EXPECTED_ACCOUNT, credentials.getAccount());
104110
}
105111

@@ -108,7 +114,10 @@ public void sign_sameAs() throws IOException {
108114
byte[] expectedSignature = {0xD, 0xE, 0xA, 0xD};
109115
MockAppIdentityService appIdentity = new MockAppIdentityService();
110116
appIdentity.setSignature(expectedSignature);
111-
AppEngineCredentials credentials = new AppEngineCredentials(SCOPES, appIdentity);
117+
AppEngineCredentials credentials = AppEngineCredentials.newBuilder()
118+
.setScopes(SCOPES)
119+
.setAppIdentityService(appIdentity)
120+
.build();
112121
assertArrayEquals(expectedSignature, credentials.sign(expectedSignature));
113122
}
114123

@@ -120,8 +129,10 @@ public void createScoped_clonesWithScopes() throws IOException {
120129
MockAppIdentityService appIdentity = new MockAppIdentityService();
121130
appIdentity.setAccessTokenText(expectedAccessToken);
122131

123-
GoogleCredentials credentials = new AppEngineCredentials(emptyScopes, appIdentity);
124-
132+
AppEngineCredentials credentials = AppEngineCredentials.newBuilder()
133+
.setScopes(emptyScopes)
134+
.setAppIdentityService(appIdentity)
135+
.build();
125136
assertTrue(credentials.createScopedRequired());
126137
try {
127138
credentials.getRequestMetadata(CALL_URI);
@@ -143,8 +154,15 @@ public void createScoped_clonesWithScopes() throws IOException {
143154
public void equals_true() throws IOException {
144155
final Collection<String> emptyScopes = Collections.emptyList();
145156
MockAppIdentityService appIdentity = new MockAppIdentityService();
146-
GoogleCredentials credentials = new AppEngineCredentials(emptyScopes, appIdentity);
147-
GoogleCredentials otherCredentials = new AppEngineCredentials(emptyScopes, appIdentity);
157+
158+
AppEngineCredentials credentials = AppEngineCredentials.newBuilder()
159+
.setScopes(emptyScopes)
160+
.setAppIdentityService(appIdentity)
161+
.build();
162+
AppEngineCredentials otherCredentials = AppEngineCredentials.newBuilder()
163+
.setScopes(emptyScopes)
164+
.setAppIdentityService(appIdentity)
165+
.build();
148166
assertTrue(credentials.equals(credentials));
149167
assertTrue(credentials.equals(otherCredentials));
150168
assertTrue(otherCredentials.equals(credentials));
@@ -155,8 +173,15 @@ public void equals_false_scopes() throws IOException {
155173
final Collection<String> emptyScopes = Collections.emptyList();
156174
final Collection<String> scopes = Collections.singleton("SomeScope");
157175
MockAppIdentityService appIdentity = new MockAppIdentityService();
158-
GoogleCredentials credentials = new AppEngineCredentials(emptyScopes, appIdentity);
159-
GoogleCredentials otherCredentials = new AppEngineCredentials(scopes, appIdentity);
176+
177+
AppEngineCredentials credentials = AppEngineCredentials.newBuilder()
178+
.setScopes(emptyScopes)
179+
.setAppIdentityService(appIdentity)
180+
.build();
181+
AppEngineCredentials otherCredentials = AppEngineCredentials.newBuilder()
182+
.setScopes(scopes)
183+
.setAppIdentityService(appIdentity)
184+
.build();
160185
assertFalse(credentials.equals(otherCredentials));
161186
assertFalse(otherCredentials.equals(credentials));
162187
}
@@ -170,24 +195,38 @@ public void toString_containsFields() throws IOException {
170195
MockAppIdentityService.class.getName());
171196
final Collection<String> scopes = Collections.singleton("SomeScope");
172197
MockAppIdentityService appIdentity = new MockAppIdentityService();
173-
GoogleCredentials credentials = new AppEngineCredentials(scopes, appIdentity);
198+
199+
AppEngineCredentials credentials = AppEngineCredentials.newBuilder()
200+
.setScopes(scopes)
201+
.setAppIdentityService(appIdentity)
202+
.build();
203+
174204
assertEquals(expectedToString, credentials.toString());
175205
}
176206

177207
@Test
178208
public void hashCode_equals() throws IOException {
179209
final Collection<String> emptyScopes = Collections.emptyList();
180210
MockAppIdentityService appIdentity = new MockAppIdentityService();
181-
GoogleCredentials credentials = new AppEngineCredentials(emptyScopes, appIdentity);
182-
GoogleCredentials otherCredentials = new AppEngineCredentials(emptyScopes, appIdentity);
211+
AppEngineCredentials credentials = AppEngineCredentials.newBuilder()
212+
.setScopes(emptyScopes)
213+
.setAppIdentityService(appIdentity)
214+
.build();
215+
AppEngineCredentials otherCredentials = AppEngineCredentials.newBuilder()
216+
.setScopes(emptyScopes)
217+
.setAppIdentityService(appIdentity)
218+
.build();
183219
assertEquals(credentials.hashCode(), otherCredentials.hashCode());
184220
}
185221

186222
@Test
187223
public void serialize() throws IOException, ClassNotFoundException {
188224
final Collection<String> scopes = Collections.singleton("SomeScope");
189225
MockAppIdentityService appIdentity = new MockAppIdentityService();
190-
GoogleCredentials credentials = new AppEngineCredentials(scopes, appIdentity);
226+
AppEngineCredentials credentials = AppEngineCredentials.newBuilder()
227+
.setScopes(scopes)
228+
.setAppIdentityService(appIdentity)
229+
.build();
191230
GoogleCredentials deserializedCredentials = serializeAndDeserialize(credentials);
192231
assertEquals(credentials, deserializedCredentials);
193232
assertEquals(credentials.hashCode(), deserializedCredentials.hashCode());

0 commit comments

Comments
 (0)