|
| 1 | +/* |
| 2 | + * Copyright 2026 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.google.firebase.phonenumberverification; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkArgument; |
| 20 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 21 | + |
| 22 | +import com.google.common.collect.ImmutableList; |
| 23 | +import com.google.common.collect.ImmutableMap; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | + |
| 27 | +/** |
| 28 | + * Represents a verified Firebase Phone Number Verification token. |
| 29 | + */ |
| 30 | +public class FirebasePhoneNumberVerificationToken { |
| 31 | + private final Map<String, Object> claims; |
| 32 | + |
| 33 | + /** |
| 34 | + * Create an instance of {@link FirebasePhoneNumberVerificationToken} from a map of JWT claims. |
| 35 | + * |
| 36 | + * @param claims A map of JWT claims. |
| 37 | + */ |
| 38 | + public FirebasePhoneNumberVerificationToken(Map<String, Object> claims) { |
| 39 | + checkNotNull(claims, "Claims map must not be null"); |
| 40 | + checkArgument(claims.containsKey("sub"), "Claims map must contain sub"); |
| 41 | + this.claims = ImmutableMap.copyOf(claims); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Returns the issuer identifier for the issuer of the response. |
| 46 | + */ |
| 47 | + public String getIssuer() { |
| 48 | + return (String) claims.get("iss"); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Returns the phone number of the user. |
| 53 | + * This corresponds to the 'sub' claim in the JWT. |
| 54 | + */ |
| 55 | + public String getPhoneNumber() { |
| 56 | + return (String) claims.get("sub"); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Returns the audience for which this token is intended. |
| 61 | + */ |
| 62 | + public List<String> getAudience() { |
| 63 | + Object audience = claims.get("aud"); |
| 64 | + if (audience instanceof String) { |
| 65 | + return ImmutableList.of((String) audience); |
| 66 | + } else if (audience instanceof List) { |
| 67 | + @SuppressWarnings("unchecked") |
| 68 | + List<String> audienceList = (List<String>) audience; |
| 69 | + return ImmutableList.copyOf(audienceList); |
| 70 | + } |
| 71 | + return ImmutableList.of(); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Returns the expiration time in seconds since the Unix epoch. |
| 76 | + */ |
| 77 | + public long getExpirationTime() { |
| 78 | + Object exp = claims.get("exp"); |
| 79 | + if (exp instanceof java.util.Date) { |
| 80 | + return ((java.util.Date) exp).getTime() / 1000L; |
| 81 | + } |
| 82 | + return exp instanceof Number ? ((Number) exp).longValue() : 0L; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Returns the issued-at time in seconds since the Unix epoch. |
| 87 | + */ |
| 88 | + public long getIssuedAt() { |
| 89 | + Object iat = claims.get("iat"); |
| 90 | + if (iat instanceof java.util.Date) { |
| 91 | + return ((java.util.Date) iat).getTime() / 1000L; |
| 92 | + } |
| 93 | + return iat instanceof Number ? ((Number) iat).longValue() : 0L; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Returns the entire map of claims. |
| 98 | + */ |
| 99 | + public Map<String, Object> getClaims() { |
| 100 | + return claims; |
| 101 | + } |
| 102 | +} |
0 commit comments