Skip to content

Commit 7bfc2ff

Browse files
Endpoints Auth Between Services (GoogleCloudPlatform#1330)
added endpoints auth between services sample
1 parent 8ef463d commit 7bfc2ff

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.example</groupId>
6+
<artifactId>example</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
11+
<!--
12+
The parent pom defines common style checks and testing strategies for our samples.
13+
Removing or replacing it should not affect the execution of the samples in anyway.
14+
-->
15+
<parent>
16+
<groupId>com.google.cloud.samples</groupId>
17+
<artifactId>shared-configuration</artifactId>
18+
<version>1.0.10</version>
19+
</parent>
20+
21+
22+
<properties>
23+
<maven.compiler.source>1.8</maven.compiler.source>
24+
<maven.compiler.target>1.8</maven.compiler.target>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>com.google.api-client</groupId>
30+
<artifactId>google-api-client</artifactId>
31+
<version>1.28.0</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>com.auth0</groupId>
35+
<artifactId>java-jwt</artifactId>
36+
<version>3.7.0</version>
37+
</dependency>
38+
</dependencies>
39+
<build>
40+
<sourceDirectory>src</sourceDirectory>
41+
<plugins>
42+
<plugin>
43+
<artifactId>maven-compiler-plugin</artifactId>
44+
<version>3.3</version>
45+
<configuration>
46+
<source>1.8</source>
47+
<target>1.8</target>
48+
</configuration>
49+
</plugin>
50+
<plugin>
51+
<groupId>org.codehaus.mojo</groupId>
52+
<artifactId>exec-maven-plugin</artifactId>
53+
<version>1.2.1</version>
54+
<configuration>
55+
<mainClass>com.example.app.GoogleJwtClient</mainClass>
56+
</configuration>
57+
</plugin>
58+
</plugins>
59+
60+
</build>
61+
62+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.app;
18+
19+
import com.auth0.jwt.JWT;
20+
import com.auth0.jwt.JWTCreator;
21+
import com.auth0.jwt.algorithms.Algorithm;
22+
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
23+
24+
import java.io.BufferedReader;
25+
import java.io.FileInputStream;
26+
import java.io.FileNotFoundException;
27+
import java.io.IOException;
28+
import java.io.InputStreamReader;
29+
import java.net.HttpURLConnection;
30+
import java.net.ProtocolException;
31+
import java.net.URL;
32+
import java.security.interfaces.RSAPrivateKey;
33+
import java.util.Date;
34+
import java.util.concurrent.TimeUnit;
35+
36+
37+
38+
/**
39+
* JWTClient shows how a client can authenticate with a Cloud Endpoints service
40+
*/
41+
public class GoogleJwtClient {
42+
43+
// [START endpoints_generate_jwt_sa]
44+
/**
45+
* Generates a signed JSON Web Token using a Google API Service Account
46+
* utilizes com.auth0.jwt.
47+
*/
48+
public static String generateJwt(final String saKeyfile, final String saEmail,
49+
final String audience, final int expiryLength)
50+
throws FileNotFoundException, IOException {
51+
52+
Date now = new Date();
53+
Date expTime = new Date(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(expiryLength));
54+
55+
// Build the JWT payload
56+
JWTCreator.Builder token = JWT.create()
57+
.withIssuedAt(now)
58+
// Expires after 'expiraryLength' seconds
59+
.withExpiresAt(expTime)
60+
// Must match 'issuer' in the security configuration in your
61+
// swagger spec (e.g. service account email)
62+
.withIssuer(saEmail)
63+
// Must be either your Endpoints service name, or match the value
64+
// specified as the 'x-google-audience' in the OpenAPI document
65+
.withAudience(audience)
66+
// Subject and email should match the service account's email
67+
.withSubject(saEmail)
68+
.withClaim("email", saEmail);
69+
70+
// Sign the JWT with a service account
71+
FileInputStream stream = new FileInputStream(saKeyfile);
72+
GoogleCredential cred = GoogleCredential.fromStream(stream);
73+
RSAPrivateKey key = (RSAPrivateKey) cred.getServiceAccountPrivateKey();
74+
Algorithm algorithm = Algorithm.RSA256(null, key);
75+
return token.sign(algorithm);
76+
}
77+
// [END endpoints_generate_jwt_sa]
78+
79+
80+
// [START endpoints_jwt_request]
81+
/**
82+
* Makes an authorized request to the endpoint.
83+
*/
84+
public static String makeJwtRequest(final String singedJwt, final URL url)
85+
throws IOException, ProtocolException {
86+
87+
HttpURLConnection con = (HttpURLConnection) url.openConnection();
88+
con.setRequestMethod("GET");
89+
con.setRequestProperty("Content-Type", "application/json");
90+
con.setRequestProperty("Authorization", "Bearer " + singedJwt);
91+
92+
InputStreamReader reader = new InputStreamReader(con.getInputStream());
93+
BufferedReader buffReader = new BufferedReader(reader);
94+
95+
String line;
96+
StringBuilder result = new StringBuilder();
97+
while ((line = buffReader.readLine()) != null) {
98+
result.append(line);
99+
}
100+
buffReader.close();
101+
return result.toString();
102+
}
103+
// [END endpoints_jwt_request]
104+
}

endpoints/getting-started/src/main/java/com/example/endpoints/AuthInfoServlet.java

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import javax.servlet.http.HttpServletRequest;
2929
import javax.servlet.http.HttpServletResponse;
3030

31+
// [START endpoints_auth_info_backend]
3132
/**
3233
* A servlet that returns authentication information.
3334
* See openapi.yaml for authentication mechanisms (e.g. JWT tokens, Google ID token).
@@ -57,3 +58,4 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOExc
5758
}
5859
}
5960
}
61+
// [END endpoints_auth_info_backend]

0 commit comments

Comments
 (0)