|
| 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 com.google.firebase.FirebaseApp; |
| 20 | +import com.google.firebase.ImplFirebaseTrampolines; |
| 21 | +import com.google.firebase.internal.FirebaseService; |
| 22 | +import com.google.firebase.phonenumberverification.internal.FirebasePhoneNumberVerificationTokenVerifier; |
| 23 | + |
| 24 | +/** |
| 25 | + * This class is the entry point for the Firebase Phone Number Verification service. |
| 26 | + * |
| 27 | + * <p>You can get an instance of {@link FirebasePhoneNumberVerification} via {@link #getInstance()}, |
| 28 | + * or {@link #getInstance(FirebaseApp)} and then use it. |
| 29 | + */ |
| 30 | +public final class FirebasePhoneNumberVerification { |
| 31 | + private static final String SERVICE_ID = FirebasePhoneNumberVerification.class.getName(); |
| 32 | + private final FirebasePhoneNumberVerificationTokenVerifier tokenVerifier; |
| 33 | + |
| 34 | + private FirebasePhoneNumberVerification(FirebaseApp app) { |
| 35 | + this.tokenVerifier = new FirebasePhoneNumberVerificationTokenVerifier(app); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Gets the {@link FirebasePhoneNumberVerification} instance for the default {@link FirebaseApp}. |
| 40 | + * |
| 41 | + * @return The {@link FirebasePhoneNumberVerification} instance for the default {@link FirebaseApp}. |
| 42 | + */ |
| 43 | + public static FirebasePhoneNumberVerification getInstance() { |
| 44 | + return getInstance(FirebaseApp.getInstance()); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Gets the {@link FirebasePhoneNumberVerification} instance for the specified {@link FirebaseApp}. |
| 49 | + * |
| 50 | + * @return The {@link FirebasePhoneNumberVerification} instance for the specified {@link FirebaseApp}. |
| 51 | + */ |
| 52 | + public static synchronized FirebasePhoneNumberVerification getInstance(FirebaseApp app) { |
| 53 | + FirebasePhoneNumberVerificationService service = ImplFirebaseTrampolines.getService(app, SERVICE_ID, |
| 54 | + FirebasePhoneNumberVerificationService.class); |
| 55 | + if (service == null) { |
| 56 | + service = ImplFirebaseTrampolines.addService(app, new FirebasePhoneNumberVerificationService(app)); |
| 57 | + } |
| 58 | + return service.getInstance(); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Verifies a Firebase Phone Number Verification token (JWT). |
| 63 | + * |
| 64 | + * @param phoneNumberVerificationJwt The JWT string to verify. |
| 65 | + * @return A verified {@link FirebasePhoneNumberVerificationToken}. |
| 66 | + * @throws FirebasePhoneNumberVerificationException If verification fails. |
| 67 | + */ |
| 68 | + public FirebasePhoneNumberVerificationToken verifyToken(String phoneNumberVerificationJwt) |
| 69 | + throws FirebasePhoneNumberVerificationException { |
| 70 | + return this.tokenVerifier.verifyToken(phoneNumberVerificationJwt); |
| 71 | + } |
| 72 | + |
| 73 | + private static class FirebasePhoneNumberVerificationService extends FirebaseService<FirebasePhoneNumberVerification> { |
| 74 | + FirebasePhoneNumberVerificationService(FirebaseApp app) { |
| 75 | + super(SERVICE_ID, new FirebasePhoneNumberVerification(app)); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments