|
| 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 | +} |
0 commit comments