xref: /aosp_15_r20/external/tink/java_src/examples/jwt/JwtGeneratePublicJwkSet.java (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1*e7b1675dSTing-Kang Chang /**
2*e7b1675dSTing-Kang Chang  * Copyright 2021 Google LLC
3*e7b1675dSTing-Kang Chang  *
4*e7b1675dSTing-Kang Chang  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5*e7b1675dSTing-Kang Chang  * in compliance with the License. You may obtain a copy of the License at
6*e7b1675dSTing-Kang Chang  *
7*e7b1675dSTing-Kang Chang  * http://www.apache.org/licenses/LICENSE-2.0
8*e7b1675dSTing-Kang Chang  *
9*e7b1675dSTing-Kang Chang  * Unless required by applicable law or agreed to in writing, software distributed under the License
10*e7b1675dSTing-Kang Chang  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11*e7b1675dSTing-Kang Chang  * or implied. See the License for the specific language governing permissions and limitations under
12*e7b1675dSTing-Kang Chang  * the License.
13*e7b1675dSTing-Kang Chang  */
14*e7b1675dSTing-Kang Chang // [START java-jwt-generate-public-jwk-set-example]
15*e7b1675dSTing-Kang Chang package jwt;
16*e7b1675dSTing-Kang Chang 
17*e7b1675dSTing-Kang Chang import static java.nio.charset.StandardCharsets.UTF_8;
18*e7b1675dSTing-Kang Chang 
19*e7b1675dSTing-Kang Chang import com.google.crypto.tink.InsecureSecretKeyAccess;
20*e7b1675dSTing-Kang Chang import com.google.crypto.tink.KeysetHandle;
21*e7b1675dSTing-Kang Chang import com.google.crypto.tink.TinkJsonProtoKeysetFormat;
22*e7b1675dSTing-Kang Chang import com.google.crypto.tink.jwt.JwkSetConverter;
23*e7b1675dSTing-Kang Chang import com.google.crypto.tink.jwt.JwtSignatureConfig;
24*e7b1675dSTing-Kang Chang import java.nio.file.Files;
25*e7b1675dSTing-Kang Chang import java.nio.file.Path;
26*e7b1675dSTing-Kang Chang import java.nio.file.Paths;
27*e7b1675dSTing-Kang Chang 
28*e7b1675dSTing-Kang Chang /**
29*e7b1675dSTing-Kang Chang  * A command-line example for generating the public JWT keyset in JWK set format.
30*e7b1675dSTing-Kang Chang  *
31*e7b1675dSTing-Kang Chang  * <p>It loads cleartext private keys from disk - this is not recommended!
32*e7b1675dSTing-Kang Chang  *
33*e7b1675dSTing-Kang Chang  * <p>It requires the following arguments:
34*e7b1675dSTing-Kang Chang  *
35*e7b1675dSTing-Kang Chang  * <ul>
36*e7b1675dSTing-Kang Chang  *   <li>private-keyset-file: Name of the input file containing the private keyset.
37*e7b1675dSTing-Kang Chang  *   <li>public-jwkset-file: Name of the output file containing the public key in JWK set format.
38*e7b1675dSTing-Kang Chang  */
39*e7b1675dSTing-Kang Chang public final class JwtGeneratePublicJwkSet {
main(String[] args)40*e7b1675dSTing-Kang Chang   public static void main(String[] args) throws Exception {
41*e7b1675dSTing-Kang Chang     if (args.length != 2) {
42*e7b1675dSTing-Kang Chang       System.err.printf("Expected 2 parameters, got %d\n", args.length);
43*e7b1675dSTing-Kang Chang       System.err.println(
44*e7b1675dSTing-Kang Chang           "Usage: java JwtGeneratePublicJwkSet private-keyset-file public-jwk-set-file");
45*e7b1675dSTing-Kang Chang       System.exit(1);
46*e7b1675dSTing-Kang Chang     }
47*e7b1675dSTing-Kang Chang 
48*e7b1675dSTing-Kang Chang     Path privateKeysetFile = Paths.get(args[0]);
49*e7b1675dSTing-Kang Chang     Path publicJwkSetFile = Paths.get(args[1]);
50*e7b1675dSTing-Kang Chang 
51*e7b1675dSTing-Kang Chang     // Register all JWT signature key types with the Tink runtime.
52*e7b1675dSTing-Kang Chang     JwtSignatureConfig.register();
53*e7b1675dSTing-Kang Chang 
54*e7b1675dSTing-Kang Chang     // Read the keyset into a KeysetHandle.
55*e7b1675dSTing-Kang Chang     KeysetHandle privateKeysetHandle =
56*e7b1675dSTing-Kang Chang         TinkJsonProtoKeysetFormat.parseKeyset(
57*e7b1675dSTing-Kang Chang             new String(Files.readAllBytes(privateKeysetFile), UTF_8),
58*e7b1675dSTing-Kang Chang             InsecureSecretKeyAccess.get());
59*e7b1675dSTing-Kang Chang 
60*e7b1675dSTing-Kang Chang     // Export the public keyset as JWK set.
61*e7b1675dSTing-Kang Chang     String publicJwkSet =
62*e7b1675dSTing-Kang Chang         JwkSetConverter.fromPublicKeysetHandle(privateKeysetHandle.getPublicKeysetHandle());
63*e7b1675dSTing-Kang Chang     Files.write(publicJwkSetFile, publicJwkSet.getBytes(UTF_8));
64*e7b1675dSTing-Kang Chang   }
65*e7b1675dSTing-Kang Chang 
JwtGeneratePublicJwkSet()66*e7b1675dSTing-Kang Chang   private JwtGeneratePublicJwkSet() {}
67*e7b1675dSTing-Kang Chang }
68*e7b1675dSTing-Kang Chang // [END java-jwt-generate-public-jwk-set-example]
69