|
19 | 19 | import static com.google.common.base.Preconditions.checkNotNull; |
20 | 20 |
|
21 | 21 | import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; |
| 22 | +import com.google.api.client.googleapis.extensions.appengine.auth.oauth2.AppIdentityCredential; |
22 | 23 | import com.google.api.client.http.HttpRequestInitializer; |
23 | 24 | import com.google.api.client.http.HttpTransport; |
24 | 25 | import com.google.api.client.json.jackson.JacksonFactory; |
|
38 | 39 | */ |
39 | 40 | public abstract class AuthCredentials implements Restorable<AuthCredentials> { |
40 | 41 |
|
| 42 | + private static class AppEngineAuthCredentials extends AuthCredentials { |
| 43 | + |
| 44 | + private static final AuthCredentials INSTANCE = new AppEngineAuthCredentials(); |
| 45 | + private static final AppEngineAuthCredentialsState STATE = |
| 46 | + new AppEngineAuthCredentialsState(); |
| 47 | + |
| 48 | + private static class AppEngineAuthCredentialsState |
| 49 | + implements RestorableState<AuthCredentials>, Serializable { |
| 50 | + |
| 51 | + private static final long serialVersionUID = 3558563960848658928L; |
| 52 | + |
| 53 | + @Override |
| 54 | + public AuthCredentials restore() { |
| 55 | + return INSTANCE; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public int hashCode() { |
| 60 | + return getClass().getName().hashCode(); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public boolean equals(Object obj) { |
| 65 | + return obj instanceof AppEngineAuthCredentialsState; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport, |
| 71 | + Set<String> scopes) { |
| 72 | + return new AppIdentityCredential(scopes); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public RestorableState<AuthCredentials> capture() { |
| 77 | + return STATE; |
| 78 | + } |
| 79 | + } |
| 80 | + |
41 | 81 | public static class ServiceAccountAuthCredentials extends AuthCredentials { |
42 | 82 |
|
43 | 83 | private final String account; |
@@ -181,12 +221,16 @@ public RestorableState<AuthCredentials> capture() { |
181 | 221 | protected abstract HttpRequestInitializer httpRequestInitializer(HttpTransport transport, |
182 | 222 | Set<String> scopes); |
183 | 223 |
|
| 224 | + public static AuthCredentials createForAppEngine() { |
| 225 | + return AppEngineAuthCredentials.INSTANCE; |
| 226 | + } |
| 227 | + |
184 | 228 | /** |
185 | 229 | * Returns the Application Default Credentials. |
186 | 230 | * |
187 | 231 | * <p>Returns the Application Default Credentials which are credentials that identify and |
188 | 232 | * authorize the whole application. This is the built-in service account if running on |
189 | | - * Google App/Compute Engine or the credentials file can be read from the path in the environment |
| 233 | + * Google Compute Engine or the credentials file can be read from the path in the environment |
190 | 234 | * variable GOOGLE_APPLICATION_CREDENTIALS. |
191 | 235 | * </p> |
192 | 236 | * |
|
0 commit comments