Skip to content
This repository was archived by the owner on May 12, 2026. It is now read-only.

Commit 4504fc1

Browse files
authored
Merge pull request #123 from google/shinfan-dev
Switch OAuth2 HTTP surface to use builder pattern
2 parents 65b6bea + 0f6bb99 commit 4504fc1

18 files changed

Lines changed: 1117 additions & 167 deletions

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public static ClientId fromStream(InputStream stream) throws IOException {
123123
* @param clientId Text identifier of the Client ID.
124124
* @param clientSecret Secret to associated with the Client ID.
125125
*/
126+
@Deprecated
126127
public ClientId(String clientId, String clientSecret) {
127128
this.clientId = Preconditions.checkNotNull(clientId);
128129
this.clientSecret = clientSecret;
@@ -141,4 +142,44 @@ public final String getClientId() {
141142
public final String getClientSecret() {
142143
return clientSecret;
143144
}
145+
146+
public static Builder newBuilder() {
147+
return new Builder();
148+
}
149+
150+
public Builder toBuilder() {
151+
return new Builder(this);
152+
}
153+
154+
public static class Builder {
155+
156+
private String clientId;
157+
158+
private String clientSecret;
159+
160+
protected Builder() {}
161+
162+
protected Builder(ClientId clientId) {
163+
this.clientId = clientId.getClientId();
164+
this.clientSecret = clientId.getClientSecret();
165+
}
166+
167+
public Builder setClientId(String clientId) {
168+
this.clientId = clientId;
169+
return this;
170+
}
171+
172+
public Builder setClientSecret(String clientSecret) {
173+
this.clientSecret = clientSecret;
174+
return this;
175+
}
176+
177+
public String getClientSecret() {
178+
return clientSecret;
179+
}
180+
181+
public ClientId build() {
182+
return new ClientId(clientId, clientSecret);
183+
}
184+
}
144185
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public class CloudShellCredentials extends GoogleCredentials {
6363

6464
private final int authPort;
6565

66+
public static CloudShellCredentials of(int authPort) {
67+
return CloudShellCredentials.newBuilder().setAuthPort(authPort).build();
68+
}
69+
70+
@Deprecated
6671
public CloudShellCredentials(int authPort) {
6772
this.authPort = authPort;
6873
}
@@ -112,4 +117,35 @@ public boolean equals(Object obj) {
112117
CloudShellCredentials other = (CloudShellCredentials) obj;
113118
return this.authPort == other.authPort;
114119
}
120+
121+
public Builder toBuilder() {
122+
return new Builder(this);
123+
}
124+
125+
public static Builder newBuilder() {
126+
return new Builder();
127+
}
128+
129+
public static class Builder extends GoogleCredentials.Builder {
130+
private int authPort;
131+
132+
protected Builder() {}
133+
134+
protected Builder(CloudShellCredentials credentials) {
135+
this.authPort = credentials.authPort;
136+
}
137+
138+
public Builder setAuthPort(int authPort) {
139+
this.authPort = authPort;
140+
return this;
141+
}
142+
143+
public int getAuthPort() {
144+
return authPort;
145+
}
146+
147+
public CloudShellCredentials build() {
148+
return new CloudShellCredentials(authPort);
149+
}
150+
}
115151
}

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,20 @@ public class ComputeEngineCredentials extends GoogleCredentials {
8282

8383
private transient HttpTransportFactory transportFactory;
8484

85+
/**
86+
* Returns a credentials instance from the given transport factory
87+
*
88+
* @param transportFactory The Http transport factory
89+
* @return the credential instance
90+
*/
91+
public static ComputeEngineCredentials of(HttpTransportFactory transportFactory) {
92+
return ComputeEngineCredentials.newBuilder().setHttpTransportFactory(transportFactory).build();
93+
}
94+
8595
/**
8696
* Constructor with minimum information and default behavior.
8797
*/
98+
@Deprecated
8899
public ComputeEngineCredentials() {
89100
this(null);
90101
}
@@ -95,6 +106,7 @@ public ComputeEngineCredentials() {
95106
* @param transportFactory HTTP transport factory, creates the transport used to get access
96107
* tokens.
97108
*/
109+
@Deprecated
98110
public ComputeEngineCredentials(HttpTransportFactory transportFactory) {
99111
this.transportFactory = firstNonNull(transportFactory,
100112
getFromServiceLoader(HttpTransportFactory.class, OAuth2Utils.HTTP_TRANSPORT_FACTORY));
@@ -225,4 +237,35 @@ private void readObject(ObjectInputStream input) throws IOException, ClassNotFou
225237
input.defaultReadObject();
226238
transportFactory = newInstance(transportFactoryClassName);
227239
}
240+
241+
public Builder toBuilder() {
242+
return new Builder(this);
243+
}
244+
245+
public static Builder newBuilder() {
246+
return new Builder();
247+
}
248+
249+
public static class Builder extends GoogleCredentials.Builder {
250+
private HttpTransportFactory transportFactory;
251+
252+
protected Builder() {}
253+
254+
protected Builder(ComputeEngineCredentials credentials) {
255+
this.transportFactory = credentials.transportFactory;
256+
}
257+
258+
public Builder setHttpTransportFactory(HttpTransportFactory transportFactory) {
259+
this.transportFactory = transportFactory;
260+
return this;
261+
}
262+
263+
public HttpTransportFactory getHttpTransportFactory() {
264+
return transportFactory;
265+
}
266+
267+
public ComputeEngineCredentials build() {
268+
return new ComputeEngineCredentials(transportFactory);
269+
}
270+
}
228271
}

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ public class GoogleCredentials extends OAuth2Credentials {
5353
private static final DefaultCredentialsProvider defaultCredentialsProvider =
5454
new DefaultCredentialsProvider();
5555

56+
/**
57+
* Returns the credentials instance from the given access token.
58+
*
59+
* @param accessToken the access token
60+
* @return the credentials instance
61+
*/
62+
public static GoogleCredentials of(AccessToken accessToken) {
63+
return GoogleCredentials.newBuilder().setAccessToken(accessToken).build();
64+
}
65+
5666
/**
5767
* Returns the Application Default Credentials.
5868
*
@@ -167,10 +177,19 @@ protected GoogleCredentials() {
167177
*
168178
* @param accessToken Initial or temporary access token.
169179
**/
180+
@Deprecated
170181
public GoogleCredentials(AccessToken accessToken) {
171182
super(accessToken);
172183
}
173184

185+
public static Builder newBuilder() {
186+
return new Builder();
187+
}
188+
189+
public Builder toBuilder() {
190+
return new Builder(this);
191+
}
192+
174193
/**
175194
* Indicates whether the credentials require scopes to be specified via a call to
176195
* {link GoogleCredentials#createScoped} before use.
@@ -195,4 +214,22 @@ public GoogleCredentials createScoped(Collection<String> scopes) {
195214
public GoogleCredentials createDelegated(String user) {
196215
return this;
197216
}
217+
218+
public static class Builder extends OAuth2Credentials.Builder {
219+
protected Builder() {}
220+
221+
protected Builder(GoogleCredentials credentials) {
222+
setAccessToken(credentials.getAccessToken());
223+
}
224+
225+
public GoogleCredentials build() {
226+
return new GoogleCredentials(getAccessToken());
227+
}
228+
229+
@Override
230+
public Builder setAccessToken(AccessToken token) {
231+
super.setAccessToken(token);
232+
return this;
233+
}
234+
}
198235
}

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ public class OAuth2Credentials extends Credentials {
7272
@VisibleForTesting
7373
transient Clock clock = Clock.SYSTEM;
7474

75+
/**
76+
* Returns the credentials instance from the given access token.
77+
*
78+
* @param accessToken the access token
79+
* @return the credentials instance
80+
*/
81+
public static OAuth2Credentials of(AccessToken accessToken) {
82+
return OAuth2Credentials.newBuilder().setAccessToken(accessToken).build();
83+
}
84+
7585
/**
7686
* Default constructor.
7787
**/
@@ -84,6 +94,7 @@ protected OAuth2Credentials() {
8494
*
8595
* @param accessToken Initial or temporary access token.
8696
**/
97+
@Deprecated
8798
public OAuth2Credentials(AccessToken accessToken) {
8899
if (accessToken != null) {
89100
useAccessToken(accessToken);
@@ -279,4 +290,36 @@ protected static <T> T newInstance(String className) throws IOException, ClassNo
279290
protected static <T> T getFromServiceLoader(Class<? extends T> clazz, T defaultInstance) {
280291
return Iterables.getFirst(ServiceLoader.load(clazz), defaultInstance);
281292
}
293+
294+
public static Builder newBuilder() {
295+
return new Builder();
296+
}
297+
298+
public Builder toBuilder() {
299+
return new Builder(this);
300+
}
301+
302+
public static class Builder {
303+
304+
private AccessToken accessToken;
305+
306+
protected Builder() {}
307+
308+
protected Builder(OAuth2Credentials credentials) {
309+
this.accessToken = credentials.getAccessToken();
310+
}
311+
312+
public Builder setAccessToken(AccessToken token) {
313+
this.accessToken = token;
314+
return this;
315+
}
316+
317+
public AccessToken getAccessToken() {
318+
return accessToken;
319+
}
320+
321+
public OAuth2Credentials build() {
322+
return new OAuth2Credentials(accessToken);
323+
}
324+
}
282325
}

0 commit comments

Comments
 (0)