|
| 1 | +// Copyright 2017 Google Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +package com.example.compute.signedmetadata; |
| 15 | + |
| 16 | +import com.auth0.jwt.exceptions.AlgorithmMismatchException; |
| 17 | +import com.auth0.jwt.exceptions.InvalidClaimException; |
| 18 | +import com.auth0.jwt.exceptions.JWTVerificationException; |
| 19 | +import com.auth0.jwt.exceptions.SignatureVerificationException; |
| 20 | +import com.auth0.jwt.exceptions.TokenExpiredException; |
| 21 | +import com.example.compute.signedmetadata.token.DecodedGoogleJWTWrapper; |
| 22 | +import com.example.compute.signedmetadata.token.TokenVerifier; |
| 23 | + |
| 24 | +class VerifyingInstance { |
| 25 | + |
| 26 | + private String audience; |
| 27 | + |
| 28 | + VerifyingInstance(String audience) { |
| 29 | + this.audience = audience; |
| 30 | + } |
| 31 | + |
| 32 | + void verifyToken(String token) { |
| 33 | + TokenVerifier gtv = new TokenVerifier(); |
| 34 | + // JWTVerificationException is runtime exception, we don't need to catch it if we want to exit |
| 35 | + // process in case of verification problem. However, to handle verification problems |
| 36 | + // programmatically we can can JWTVerificationException or specific subclass. |
| 37 | + // Following are examples how to handle verification failure. |
| 38 | + try { |
| 39 | + DecodedGoogleJWTWrapper decodedJWT = gtv.verifyWithAudience(audience, token); |
| 40 | + System.out.println("Project id : " + decodedJWT.getProjectId()); |
| 41 | + System.out.println("Project number : " + decodedJWT.getProjectNumber()); |
| 42 | + // This are examples how to handle exceptions, which indicate verification failure. |
| 43 | + } catch (AlgorithmMismatchException e) { |
| 44 | + // We assume that downloaded certs are RSA256, this exception will happen if this changes. |
| 45 | + throw e; |
| 46 | + } catch (SignatureVerificationException e) { |
| 47 | + // Could not verify signature of a token, possibly someone provided forged token. |
| 48 | + throw e; |
| 49 | + } catch (TokenExpiredException e) { |
| 50 | + // We encountered old token, possibly replay attack. |
| 51 | + throw e; |
| 52 | + } catch (InvalidClaimException e) { |
| 53 | + // Different Audience for token and for verification, possibly token for other verifier. |
| 54 | + throw e; |
| 55 | + } catch (JWTVerificationException e) { |
| 56 | + // Some other problem during verification |
| 57 | + // JWTVerificationException is super-class to: |
| 58 | + // - SignatureVerificationException |
| 59 | + // - TokenExpiredException |
| 60 | + // - InvalidClaimException |
| 61 | + throw e; |
| 62 | + } |
| 63 | + |
| 64 | + } |
| 65 | +} |
0 commit comments