-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Endpoints Auth Between Services #1330
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
daniel-sanche
merged 15 commits into
GoogleCloudPlatform:master
from
daniel-sanche:endpoints-auth
Feb 8, 2019
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ac660f4
added jwt sample
daniel-sanche 789d4b0
added make request function
daniel-sanche baf8c87
removed * import
daniel-sanche 8fb81ca
use arguments for main function
daniel-sanche 62b972f
added region tag for auth info server
daniel-sanche a844bec
added comments
daniel-sanche 5441897
renames file
daniel-sanche b15d765
addressed checkstyle issues
daniel-sanche 38f53a8
added copyright header
daniel-sanche 4284fa4
Merge branch 'master' into endpoints-auth
daniel-sanche 0abb297
addressed PR comments
daniel-sanche 9d3f853
Merge branch 'endpoints-auth' of github.com:DanSanche/java-docs-sampl…
daniel-sanche 510d311
renamed class to be consistent with python
daniel-sanche 9ad50d4
Merge branch 'master' into endpoints-auth
daniel-sanche 545a9be
Merge branch 'master' into endpoints-auth
kurtisvg 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
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,62 @@ | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
|
|
||
| <modelVersion>4.0.0</modelVersion> | ||
| <groupId>com.example</groupId> | ||
| <artifactId>example</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
| <packaging>jar</packaging> | ||
|
|
||
|
|
||
| <!-- | ||
| The parent pom defines common style checks and testing strategies for our samples. | ||
| Removing or replacing it should not affect the execution of the samples in anyway. | ||
| --> | ||
| <parent> | ||
| <groupId>com.google.cloud.samples</groupId> | ||
| <artifactId>shared-configuration</artifactId> | ||
| <version>1.0.10</version> | ||
| </parent> | ||
|
|
||
|
|
||
| <properties> | ||
| <maven.compiler.source>1.8</maven.compiler.source> | ||
| <maven.compiler.target>1.8</maven.compiler.target> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.google.api-client</groupId> | ||
| <artifactId>google-api-client</artifactId> | ||
| <version>1.28.0</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.auth0</groupId> | ||
| <artifactId>java-jwt</artifactId> | ||
| <version>3.7.0</version> | ||
| </dependency> | ||
| </dependencies> | ||
| <build> | ||
| <sourceDirectory>src</sourceDirectory> | ||
| <plugins> | ||
| <plugin> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <version>3.3</version> | ||
| <configuration> | ||
| <source>1.8</source> | ||
| <target>1.8</target> | ||
| </configuration> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.codehaus.mojo</groupId> | ||
| <artifactId>exec-maven-plugin</artifactId> | ||
| <version>1.2.1</version> | ||
| <configuration> | ||
| <mainClass>com.example.app.GoogleJwtClient</mainClass> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
|
|
||
| </build> | ||
|
|
||
| </project> | ||
104 changes: 104 additions & 0 deletions
104
endpoints/getting-started/clients/src/main/java/com/example/app/GoogleJwtClient.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /* | ||
| * Copyright 2018 Google LLC | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| package com.example.app; | ||
|
|
||
| import com.auth0.jwt.JWT; | ||
| import com.auth0.jwt.JWTCreator; | ||
| import com.auth0.jwt.algorithms.Algorithm; | ||
| import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.FileInputStream; | ||
| import java.io.FileNotFoundException; | ||
| import java.io.IOException; | ||
| import java.io.InputStreamReader; | ||
| import java.net.HttpURLConnection; | ||
| import java.net.ProtocolException; | ||
| import java.net.URL; | ||
| import java.security.interfaces.RSAPrivateKey; | ||
| import java.util.Date; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * JWTClient shows how a client can authenticate with a Cloud Endpoints service | ||
| */ | ||
| public class GoogleJwtClient { | ||
|
|
||
| // [START endpoints_generate_jwt_sa] | ||
| /** | ||
| * Generates a signed JSON Web Token using a Google API Service Account | ||
| * utilizes com.auth0.jwt. | ||
| */ | ||
| public static String generateJwt(final String saKeyfile, final String saEmail, | ||
| final String audience, final int expiryLength) | ||
| throws FileNotFoundException, IOException { | ||
|
|
||
| Date now = new Date(); | ||
| Date expTime = new Date(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(expiryLength)); | ||
|
|
||
| // Build the JWT payload | ||
| JWTCreator.Builder token = JWT.create() | ||
| .withIssuedAt(now) | ||
| // Expires after 'expiraryLength' seconds | ||
| .withExpiresAt(expTime) | ||
| // Must match 'issuer' in the security configuration in your | ||
| // swagger spec (e.g. service account email) | ||
| .withIssuer(saEmail) | ||
| // Must be either your Endpoints service name, or match the value | ||
| // specified as the 'x-google-audience' in the OpenAPI document | ||
| .withAudience(audience) | ||
| // Subject and email should match the service account's email | ||
| .withSubject(saEmail) | ||
| .withClaim("email", saEmail); | ||
|
|
||
| // Sign the JWT with a service account | ||
| FileInputStream stream = new FileInputStream(saKeyfile); | ||
| GoogleCredential cred = GoogleCredential.fromStream(stream); | ||
| RSAPrivateKey key = (RSAPrivateKey) cred.getServiceAccountPrivateKey(); | ||
| Algorithm algorithm = Algorithm.RSA256(null, key); | ||
| return token.sign(algorithm); | ||
| } | ||
| // [END endpoints_generate_jwt_sa] | ||
|
|
||
|
|
||
| // [START endpoints_jwt_request] | ||
| /** | ||
| * Makes an authorized request to the endpoint. | ||
| */ | ||
| public static String makeJwtRequest(final String singedJwt, final URL url) | ||
| throws IOException, ProtocolException { | ||
|
|
||
| HttpURLConnection con = (HttpURLConnection) url.openConnection(); | ||
| con.setRequestMethod("GET"); | ||
| con.setRequestProperty("Content-Type", "application/json"); | ||
| con.setRequestProperty("Authorization", "Bearer " + singedJwt); | ||
|
|
||
| InputStreamReader reader = new InputStreamReader(con.getInputStream()); | ||
| BufferedReader buffReader = new BufferedReader(reader); | ||
|
|
||
| String line; | ||
| StringBuilder result = new StringBuilder(); | ||
| while ((line = buffReader.readLine()) != null) { | ||
| result.append(line); | ||
| } | ||
| buffReader.close(); | ||
| return result.toString(); | ||
| } | ||
| // [END endpoints_jwt_request] | ||
| } |
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.