-
Notifications
You must be signed in to change notification settings - Fork 259
docs(samples): added auth samples and tests #927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
3bd604d
docs(samples): added client code for idtoken, adc and metadata server
Sita04 bd1bc55
docs(samples): added authexplicit and copyright
Sita04 ece1c56
docs(samples): add auth with metadata server
Sita04 0c26c33
docs(samples): minor refactoring and added tests
Sita04 45449b2
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 7ef36df
Merge branch 'main' into auth-samples
Shabirmean b2f367e
refactored acc to review comments
Sita04 01b57ad
Merge remote-tracking branch 'origin/auth-samples' into auth-samples
Sita04 b9dee60
Merge branch 'main' into auth-samples
Sita04 3ed6be0
refactored acc to review comments
Sita04 dd39fd4
Merge remote-tracking branch 'origin/auth-samples' into auth-samples
Sita04 a46ccc4
Merge branch 'main' into auth-samples
Sita04 5eb6506
refactored acc to review comments
Sita04 cc6a5a5
Merge remote-tracking branch 'origin/auth-samples' into auth-samples
Sita04 7c68634
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 1baeebe
minor comment update
Sita04 9fc3a7a
Merge remote-tracking branch 'origin/auth-samples' into auth-samples
Sita04 b8c5f3f
Merge branch 'main' into auth-samples
Sita04 680cfdd
modified google id token verification and removed third party dependency
Sita04 1944e5d
Merge remote-tracking branch 'origin/auth-samples' into auth-samples
Sita04 a943290
removed third party deps from pom
Sita04 d7d6257
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] cf11754
Merge branch 'auth-samples' of https://github.com/googleapis/google-a…
gcf-owl-bot[bot] b910be5
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] e23fe35
Merge branch 'auth-samples' of https://github.com/googleapis/google-a…
gcf-owl-bot[bot] ada91fb
Merge branch 'main' into auth-samples
Sita04 8573bb3
Merge branch 'main' into auth-samples
Sita04 591d0e6
included comment about verifying Google ID tokens
Sita04 82d7350
Merge remote-tracking branch 'origin/auth-samples' into auth-samples
Sita04 54013b5
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
docs(samples): added authexplicit and copyright
- Loading branch information
commit bd1bc554a1c1334b8547c72fc5cefb5caaf4b14d
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| * Copyright 2022 Google Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import com.google.api.gax.paging.Page; | ||
| import com.google.auth.oauth2.GoogleCredentials; | ||
| import com.google.cloud.storage.Bucket; | ||
| import com.google.cloud.storage.Storage; | ||
| import com.google.cloud.storage.StorageOptions; | ||
| import com.google.common.collect.Lists; | ||
| import java.io.FileInputStream; | ||
| import java.io.IOException; | ||
| import java.security.GeneralSecurityException; | ||
|
|
||
| public class AuthenticateExplicit { | ||
|
|
||
| public static void main(String[] args) throws IOException, GeneralSecurityException { | ||
| // TODO(Developer): | ||
| // 1. Replace the below variable. | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // 2. Make sure you have the necessary permission to list storage buckets "storage.buckets.list" | ||
| String projectId = "your-google-cloud-project-id"; | ||
|
|
||
| // Path to the service account json credential file. | ||
| String jsonCredentialPath = "path-to-json-credential-file"; | ||
|
|
||
| // Provide the scopes that you might need to request to access Google APIs, | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // depending on the level of access you need. | ||
| // Example: The following scope lets you view and manage Pub/Sub topics and subscriptions. | ||
| // For more information, see: https://developers.google.com/identity/protocols/oauth2/scopes | ||
| String scope = "https://www.googleapis.com/auth/devstorage.full_control"; | ||
Sita04 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| authenticateExplicit(projectId, jsonCredentialPath, scope); | ||
| } | ||
|
|
||
| // Authenticating using Client libraries can be done in one of the following ways: | ||
| // 1. Implicit authentication with ADC (Application Default Credentials) | ||
| // 2. Explicit authentication by specifying the service account | ||
| // 3. Bring your own (BYO) access token | ||
| // 4. Using API keys (for libraries that support) | ||
| // | ||
| // In this snippet, we demonstrate "Explicit authentication by specifying the service account". | ||
| public static void authenticateExplicit(String project, String jsonCredentialPath, String scope) | ||
| throws IOException { | ||
|
|
||
| // This snippet demonstrates how to initialize Cloud Storage and list buckets. | ||
| // Note that the credentials are explicitly specified when constructing the client. | ||
| Storage storage = initService(project, jsonCredentialPath, scope); | ||
|
|
||
| System.out.println("Buckets:"); | ||
| Page<Bucket> buckets = storage.list(); | ||
| for (Bucket bucket : buckets.iterateAll()) { | ||
| System.out.println(bucket.toString()); | ||
| } | ||
| } | ||
|
|
||
| // Initialize the Storage client by explicitly setting the Service account to use. | ||
| public static Storage initService(String projectId, String jsonCredentialPath, String scope) | ||
| throws IOException { | ||
| // Construct the GoogleCredentials object which accepts the service account json file and | ||
| // scope as the input parameters. | ||
| GoogleCredentials credentials = GoogleCredentials | ||
| .fromStream(new FileInputStream(jsonCredentialPath)) | ||
| .createScoped(Lists.newArrayList(scope)); | ||
|
|
||
| // Construct the Storage client. | ||
| // Note that, here we explicitly specify the service account to use. | ||
| return StorageOptions.newBuilder() | ||
| .setCredentials(credentials) | ||
| .setProjectId(projectId) | ||
| .build() | ||
| .getService(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
samples/snippets/src/main/java/IdTokenFromImpersonatedCredentials.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
samples/snippets/src/main/java/IdTokenFromImpersonatedCredentialsREST.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
samples/snippets/src/main/java/IdTokenFromMetadataServer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
samples/snippets/src/main/java/IdTokenFromServiceAccountREST.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
samples/snippets/src/main/java/VerifyNonGoogleIdToken.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.