-
Notifications
You must be signed in to change notification settings - Fork 300
feat: add FPNV #1170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
boikoa-gl
wants to merge
9
commits into
firebase:main
Choose a base branch
from
boikoa-gl:fpnv
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: add FPNV #1170
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c52dbe1
feat: add fpnv branch
boikoa-gl 68b5377
feat: update pom file
boikoa-gl c23059c
chore: resolve comments
boikoa-gl 95003a5
chore: resolve comments
boikoa-gl d9df9a9
feat: update code and tests
boikoa-gl 18f5ecb
chore: resolve comments
boikoa-gl c76b960
feat: update FirebasePnvException
boikoa-gl 58445ed
Update src/main/java/com/google/firebase/fpnv/FirebasePnvToken.java
boikoa-gl edea209
chore: resolve robot comments
boikoa-gl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.firebase.fpnv; | ||
|
|
||
| import com.google.firebase.FirebaseApp; | ||
| import com.google.firebase.ImplFirebaseTrampolines; | ||
| import com.google.firebase.fpnv.internal.FirebasePnvTokenVerifier; | ||
| import com.google.firebase.internal.FirebaseService; | ||
|
|
||
| /** | ||
| * This class is the entry point for the Firebase Phone Number Verification (FPNV) service. | ||
| * | ||
| * <p>You can get an instance of {@link FirebasePnv} via {@link #getInstance()}, | ||
| * or {@link #getInstance(FirebaseApp)} and then use it. | ||
| */ | ||
| public final class FirebasePnv { | ||
| private static final String SERVICE_ID = FirebasePnv.class.getName(); | ||
| private final FirebasePnvTokenVerifier tokenVerifier; | ||
|
|
||
| private FirebasePnv(FirebaseApp app) { | ||
| this.tokenVerifier = new FirebasePnvTokenVerifier(app); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the {@link FirebasePnv} instance for the default {@link FirebaseApp}. | ||
| * | ||
| * @return The {@link FirebasePnv} instance for the default {@link FirebaseApp}. | ||
| */ | ||
| public static FirebasePnv getInstance() { | ||
| return getInstance(FirebaseApp.getInstance()); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the {@link FirebasePnv} instance for the specified {@link FirebaseApp}. | ||
| * | ||
| * @return The {@link FirebasePnv} instance for the specified {@link FirebaseApp}. | ||
| */ | ||
| public static synchronized FirebasePnv getInstance(FirebaseApp app) { | ||
| FirebaseFpnvService service = ImplFirebaseTrampolines.getService(app, SERVICE_ID, | ||
| FirebaseFpnvService.class); | ||
| if (service == null) { | ||
| service = ImplFirebaseTrampolines.addService(app, new FirebaseFpnvService(app)); | ||
| } | ||
| return service.getInstance(); | ||
| } | ||
|
|
||
| /** | ||
| * Verifies a Firebase Phone Number Verification token (FPNV JWT). | ||
| * | ||
| * @param fpnvJwt The FPNV JWT string to verify. | ||
| * @return A verified {@link FirebasePnvToken}. | ||
| * @throws FirebasePnvException If verification fails. | ||
| */ | ||
| public FirebasePnvToken verifyToken(String fpnvJwt) throws FirebasePnvException { | ||
| return this.tokenVerifier.verifyToken(fpnvJwt); | ||
| } | ||
|
|
||
| private static class FirebaseFpnvService extends FirebaseService<FirebasePnv> { | ||
| FirebaseFpnvService(FirebaseApp app) { | ||
| super(SERVICE_ID, new FirebasePnv(app)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
28 changes: 28 additions & 0 deletions
28
src/main/java/com/google/firebase/fpnv/FirebasePnvErrorCode.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.firebase.fpnv; | ||
|
|
||
| /** | ||
| * Error codes that are used in {@link FirebasePnv}. | ||
| */ | ||
| public enum FirebasePnvErrorCode { | ||
| INVALID_ARGUMENT, | ||
| INVALID_TOKEN, | ||
| TOKEN_EXPIRED, | ||
| INTERNAL_ERROR, | ||
| SERVICE_ERROR, | ||
| } |
82 changes: 82 additions & 0 deletions
82
src/main/java/com/google/firebase/fpnv/FirebasePnvException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.firebase.fpnv; | ||
|
|
||
| import com.google.firebase.ErrorCode; | ||
| import com.google.firebase.FirebaseException; | ||
|
|
||
| /** | ||
| * Generic exception related to Firebase Phone Number Verification. | ||
| * Check the error code and message for more | ||
| * details. | ||
| */ | ||
| public class FirebasePnvException extends FirebaseException { | ||
| private final FirebasePnvErrorCode errorCode; | ||
|
|
||
| /** | ||
| * Exception that created from {@link FirebasePnvErrorCode}, | ||
| * {@link String} message and {@link Throwable} cause. | ||
| * | ||
| * @param errorCode {@link FirebasePnvErrorCode} | ||
| * @param message {@link String} | ||
| * @param cause {@link Throwable} | ||
| */ | ||
| public FirebasePnvException( | ||
| FirebasePnvErrorCode errorCode, | ||
| String message, | ||
| Throwable cause | ||
| ) { | ||
| super(mapToFirebaseError(errorCode), message, cause); | ||
| this.errorCode = errorCode; | ||
| } | ||
|
|
||
| /** | ||
| * Exception that created from {@link FirebasePnvErrorCode} and {@link String} message. | ||
| * | ||
| * @param errorCode {@link FirebasePnvErrorCode} | ||
| * @param message {@link String} | ||
| */ | ||
| public FirebasePnvException( | ||
| FirebasePnvErrorCode errorCode, | ||
| String message | ||
| ) { | ||
| this(errorCode, message, null); | ||
| } | ||
|
|
||
| public FirebasePnvErrorCode getFpnvErrorCode() { | ||
| return errorCode; | ||
| } | ||
|
|
||
| private static ErrorCode mapToFirebaseError(FirebasePnvErrorCode code) { | ||
| if (code == null) { | ||
| return ErrorCode.INTERNAL; | ||
| } | ||
| switch (code) { | ||
| case INVALID_ARGUMENT: | ||
| return ErrorCode.INVALID_ARGUMENT; | ||
| case TOKEN_EXPIRED: | ||
| case INVALID_TOKEN: | ||
| return ErrorCode.UNAUTHENTICATED; | ||
| case SERVICE_ERROR: | ||
| return ErrorCode.UNAVAILABLE; | ||
| case INTERNAL_ERROR: | ||
| default: | ||
| return ErrorCode.INTERNAL; | ||
| } | ||
| } | ||
| } | ||
|
|
96 changes: 96 additions & 0 deletions
96
src/main/java/com/google/firebase/fpnv/FirebasePnvToken.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.firebase.fpnv; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkArgument; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import com.nimbusds.jwt.JWTClaimsSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Represents a verified Firebase Phone Number Verification token. | ||
| */ | ||
| public class FirebasePnvToken { | ||
| private final Map<String, Object> claims; | ||
|
|
||
| /** | ||
| * Create an instance of {@link FirebasePnvToken} from a map of JWT claims. | ||
| * | ||
| * @param claims A map of JWT claims. | ||
| */ | ||
| public FirebasePnvToken(Map<String, Object> claims) { | ||
| checkArgument(claims != null && claims.containsKey("sub"), | ||
| "Claims map must at least contain sub"); | ||
| this.claims = ImmutableMap.copyOf(claims); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the issuer identifier for the issuer of the response. | ||
| */ | ||
| public String getIssuer() { | ||
| return (String) claims.get("iss"); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the phone number of the user. | ||
| * This corresponds to the 'sub' claim in the JWT. | ||
| */ | ||
| public String getPhoneNumber() { | ||
| return (String) claims.get("sub"); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the audience for which this token is intended. | ||
| */ | ||
| public List<String> getAudience() { | ||
| Object audience = claims.get("aud"); | ||
| if (audience instanceof String) { | ||
| return ImmutableList.of((String) audience); | ||
| } else if (audience instanceof List) { | ||
| // The nimbus-jose-jwt library should provide a List<String>, but we copy it | ||
| // to an immutable list for safety and to prevent modification. | ||
| @SuppressWarnings("unchecked") | ||
| List<String> audienceList = (List<String>) audience; | ||
| return ImmutableList.copyOf(audienceList); | ||
| } | ||
| return ImmutableList.of(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the expiration time in seconds since the Unix epoch. | ||
| */ | ||
| public long getExpirationTime() { | ||
| return ((java.util.Date) claims.get("exp")).getTime() / 1000L; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the issued-at time in seconds since the Unix epoch. | ||
| */ | ||
| public long getIssuedAt() { | ||
| return ((java.util.Date) claims.get("iat")).getTime() / 1000L; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the entire map of claims. | ||
| */ | ||
| public Map<String, Object> getClaims() { | ||
| return claims; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.