Skip to content

Commit

Permalink
Update Google Credentials (#3023)
Browse files Browse the repository at this point in the history
* Update credentials

* lint

* Update accesstoken

* lint

* remove <p>

* Use String.format

* lint

* Update test
  • Loading branch information
averikitsch authored Jun 3, 2020
1 parent daebde8 commit 76a8fa9
Show file tree
Hide file tree
Showing 132 changed files with 1,285 additions and 1,374 deletions.
12 changes: 8 additions & 4 deletions appengine-java8/firebase-tictactoe/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,20 @@
<type>jar</type>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
<version>0.20.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>com.googlecode.objectify</groupId>
<artifactId>objectify</artifactId>
<version>5.1.24</version>
<groupId>com.googlecode.objectify</groupId>
<artifactId>objectify</artifactId>
<version>5.1.24</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@

package com.example.appengine.firetactoe;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.appengine.http.UrlFetchTransport;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.ByteArrayContent;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.appengine.api.appidentity.AppIdentityService;
import com.google.appengine.api.appidentity.AppIdentityServiceFactory;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.common.io.BaseEncoding;
import com.google.common.io.CharStreams;
import com.google.gson.Gson;
Expand Down Expand Up @@ -56,7 +56,7 @@ public class FirebaseChannel {
"https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit";

private String firebaseDbUrl;
private GoogleCredential credential;
private GoogleCredentials credential;
// Keep this a package-private member variable, so that it can be mocked for unit tests
HttpTransport httpTransport;

Expand Down Expand Up @@ -91,7 +91,7 @@ private FirebaseChannel() {
CharStreams.toString(new InputStreamReader(firebaseConfigStream, StandardCharsets.UTF_8));
firebaseDbUrl = parseFirebaseUrl(firebaseSnippet);

credential = GoogleCredential.getApplicationDefault().createScoped(FIREBASE_SCOPES);
credential = GoogleCredentials.getApplicationDefault().createScoped(FIREBASE_SCOPES);
httpTransport = UrlFetchTransport.getDefaultInstance();
} catch (IOException e) {
throw new RuntimeException(e);
Expand All @@ -117,13 +117,15 @@ private static String parseFirebaseUrl(String firebaseSnippet) {

/**
* sendFirebaseMessage.
*
* @param channelKey .
* @param game .
* @throws IOException .
*/
public void sendFirebaseMessage(String channelKey, Game game) throws IOException {
// Make requests auth'ed using Application Default Credentials
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
HttpRequestFactory requestFactory =
httpTransport.createRequestFactory(new HttpCredentialsAdapter(credential));
GenericUrl url =
new GenericUrl(String.format("%s/channels/%s.json", firebaseDbUrl, channelKey));
HttpResponse response = null;
Expand Down Expand Up @@ -152,9 +154,7 @@ url, new ByteArrayContent("application/json", gameJson.getBytes()))
}
}

/**
* Create a secure JWT token for the given userId.
*/
/** Create a secure JWT token for the given userId. */
public String createFirebaseToken(Game game, String userId) {
final AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
final BaseEncoding base64 = BaseEncoding.base64();
Expand Down Expand Up @@ -186,15 +186,18 @@ public String createFirebaseToken(Game game, String userId) {

/**
* firebasePut.
*
* @param path .
* @param object .
* @return .
* @throws IOException .
*/
public HttpResponse firebasePut(String path, Object object) throws IOException {
// Make requests auth'ed using Application Default Credentials
Credential credential = GoogleCredential.getApplicationDefault().createScoped(FIREBASE_SCOPES);
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
GoogleCredentials credential =
GoogleCredentials.getApplicationDefault().createScoped(FIREBASE_SCOPES);
HttpRequestFactory requestFactory =
httpTransport.createRequestFactory(new HttpCredentialsAdapter(credential));

String json = new Gson().toJson(object);
GenericUrl url = new GenericUrl(path);
Expand All @@ -206,15 +209,18 @@ public HttpResponse firebasePut(String path, Object object) throws IOException {

/**
* firebasePatch.
*
* @param path .
* @param object .
* @return .
* @throws IOException .
*/
public HttpResponse firebasePatch(String path, Object object) throws IOException {
// Make requests auth'ed using Application Default Credentials
Credential credential = GoogleCredential.getApplicationDefault().createScoped(FIREBASE_SCOPES);
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
GoogleCredentials credential =
GoogleCredentials.getApplicationDefault().createScoped(FIREBASE_SCOPES);
HttpRequestFactory requestFactory =
httpTransport.createRequestFactory(new HttpCredentialsAdapter(credential));

String json = new Gson().toJson(object);
GenericUrl url = new GenericUrl(path);
Expand All @@ -226,15 +232,18 @@ public HttpResponse firebasePatch(String path, Object object) throws IOException

/**
* firebasePost.
*
* @param path .
* @param object .
* @return .
* @throws IOException .
*/
public HttpResponse firebasePost(String path, Object object) throws IOException {
// Make requests auth'ed using Application Default Credentials
Credential credential = GoogleCredential.getApplicationDefault().createScoped(FIREBASE_SCOPES);
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
GoogleCredentials credential =
GoogleCredentials.getApplicationDefault().createScoped(FIREBASE_SCOPES);
HttpRequestFactory requestFactory =
httpTransport.createRequestFactory(new HttpCredentialsAdapter(credential));

String json = new Gson().toJson(object);
GenericUrl url = new GenericUrl(path);
Expand All @@ -246,14 +255,17 @@ public HttpResponse firebasePost(String path, Object object) throws IOException

/**
* firebaseGet.
*
* @param path .
* @return .
* @throws IOException .
*/
public HttpResponse firebaseGet(String path) throws IOException {
// Make requests auth'ed using Application Default Credentials
Credential credential = GoogleCredential.getApplicationDefault().createScoped(FIREBASE_SCOPES);
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
GoogleCredentials credential =
GoogleCredentials.getApplicationDefault().createScoped(FIREBASE_SCOPES);
HttpRequestFactory requestFactory =
httpTransport.createRequestFactory(new HttpCredentialsAdapter(credential));

GenericUrl url = new GenericUrl(path);

Expand All @@ -262,14 +274,17 @@ public HttpResponse firebaseGet(String path) throws IOException {

/**
* firebaseDelete.
*
* @param path .
* @return .
* @throws IOException .
*/
public HttpResponse firebaseDelete(String path) throws IOException {
// Make requests auth'ed using Application Default Credentials
Credential credential = GoogleCredential.getApplicationDefault().createScoped(FIREBASE_SCOPES);
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
GoogleCredentials credential =
GoogleCredentials.getApplicationDefault().createScoped(FIREBASE_SCOPES);
HttpRequestFactory requestFactory =
httpTransport.createRequestFactory(new HttpCredentialsAdapter(credential));

GenericUrl url = new GenericUrl(path);

Expand Down
12 changes: 3 additions & 9 deletions bigquery/rest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,9 @@
</exclusions>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client</artifactId>
<version>1.30.6</version>
<exclusions>
<exclusion> <!-- exclude an old version of Guava -->
<groupId>com.google.guava</groupId>
<artifactId>guava-jdk5</artifactId>
</exclusion>
</exclusions>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
<version>0.20.0</version>
</dependency>

<!-- Test dependencies -->
Expand Down
29 changes: 13 additions & 16 deletions bigquery/rest/src/main/java/com/example/bigquery/LabelsSample.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.example.bigquery;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpContent;
import com.google.api.client.http.HttpHeaders;
Expand All @@ -29,6 +28,8 @@
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.Key;
import com.google.auth.oauth2.AccessToken;
import com.google.auth.oauth2.GoogleCredentials;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -60,21 +61,20 @@ public Dataset addLabel(String key, String value) {
/**
* Add or modify a label on a dataset.
*
* <p>See <a href="https://cloud.google.com/bigquery/docs/labeling-datasets">the BigQuery
* See <a href="https://cloud.google.com/bigquery/docs/labeling-datasets">the BigQuery
* documentation</a>.
*/
public static void labelDataset(
String projectId, String datasetId, String labelKey, String labelValue) throws IOException {

// Authenticate requests using Google Application Default credentials.
GoogleCredential credential = GoogleCredential.getApplicationDefault();
GoogleCredentials credential = GoogleCredentials.getApplicationDefault();
credential = credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/bigquery"));

// Get a new access token.
// Note that access tokens have an expiration. You can reuse a token rather than requesting a
// new one if it is not yet expired.
credential.refreshToken();
String accessToken = credential.getAccessToken();
AccessToken accessToken = credential.refreshAccessToken();

// Set the content of the request.
Dataset dataset = new Dataset();
Expand All @@ -85,7 +85,8 @@ public static void labelDataset(
String urlFormat =
"https://www.googleapis.com/bigquery/v2/projects/%s/datasets/%s"
+ "?fields=labels&access_token=%s";
GenericUrl url = new GenericUrl(String.format(urlFormat, projectId, datasetId, accessToken));
GenericUrl url =
new GenericUrl(String.format(urlFormat, projectId, datasetId, accessToken.getTokenValue()));
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory();
HttpRequest request = requestFactory.buildPostRequest(url, content);
request.setParser(JSON_FACTORY.createJsonObjectParser());
Expand Down Expand Up @@ -127,26 +128,21 @@ public Table addLabel(String key, String value) {
/**
* Add or modify a label on a table.
*
* <p>See <a href="https://cloud.google.com/bigquery/docs/labeling-datasets">the BigQuery
* See <a href="https://cloud.google.com/bigquery/docs/labeling-datasets">the BigQuery
* documentation</a>.
*/
public static void labelTable(
String projectId,
String datasetId,
String tableId,
String labelKey,
String labelValue)
String projectId, String datasetId, String tableId, String labelKey, String labelValue)
throws IOException {

// Authenticate requests using Google Application Default credentials.
GoogleCredential credential = GoogleCredential.getApplicationDefault();
GoogleCredentials credential = GoogleCredentials.getApplicationDefault();
credential = credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/bigquery"));

// Get a new access token.
// Note that access tokens have an expiration. You can reuse a token rather than requesting a
// new one if it is not yet expired.
credential.refreshToken();
String accessToken = credential.getAccessToken();
AccessToken accessToken = credential.refreshAccessToken();

// Set the content of the request.
Table table = new Table();
Expand All @@ -158,7 +154,8 @@ public static void labelTable(
"https://www.googleapis.com/bigquery/v2/projects/%s/datasets/%s/tables/%s"
+ "?fields=labels&access_token=%s";
GenericUrl url =
new GenericUrl(String.format(urlFormat, projectId, datasetId, tableId, accessToken));
new GenericUrl(
String.format(urlFormat, projectId, datasetId, tableId, accessToken.getTokenValue()));
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory();
HttpRequest request = requestFactory.buildPostRequest(url, content);
request.setParser(JSON_FACTORY.createJsonObjectParser());
Expand Down
10 changes: 5 additions & 5 deletions compute/cmdline/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ limitations under the License.
</parent>

<properties>
<maven.compiler.target>8</maven.compiler.target>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.compute.version>v1-rev20200501-1.30.9</project.compute.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.30.9</version>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
<version>0.20.0</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
Expand Down
Loading

0 comments on commit 76a8fa9

Please sign in to comment.