Skip to content

Commit c52dbe1

Browse files
committed
feat: add fpnv branch
1 parent 88d4b58 commit c52dbe1

File tree

8 files changed

+821
-0
lines changed

8 files changed

+821
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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.fpnv;
18+
19+
import com.google.firebase.FirebaseApp;
20+
import com.google.firebase.ImplFirebaseTrampolines;
21+
import com.google.firebase.fpnv.internal.FirebasePnvTokenVerifier;
22+
import com.google.firebase.internal.FirebaseService;
23+
24+
/**
25+
* This class is the entry point for the Firebase Phone Number Verification (FPNV) service.
26+
*
27+
* <p>You can get an instance of {@link FirebasePnv} via {@link #getInstance()},
28+
* or {@link #getInstance(FirebaseApp)} and then use it.
29+
*/
30+
public final class FirebasePnv {
31+
private static final String SERVICE_ID = FirebasePnv.class.getName();
32+
private final FirebasePnvTokenVerifier tokenVerifier;
33+
34+
private FirebasePnv(FirebaseApp app) {
35+
this.tokenVerifier = new FirebasePnvTokenVerifier(app);
36+
}
37+
38+
/**
39+
* Gets the {@link FirebasePnv} instance for the default {@link FirebaseApp}.
40+
*
41+
* @return The {@link FirebasePnv} instance for the default {@link FirebaseApp}.
42+
*/
43+
public static FirebasePnv getInstance() {
44+
return getInstance(FirebaseApp.getInstance());
45+
}
46+
47+
/**
48+
* Gets the {@link FirebasePnv} instance for the specified {@link FirebaseApp}.
49+
*
50+
* @return The {@link FirebasePnv} instance for the specified {@link FirebaseApp}.
51+
*/
52+
public static synchronized FirebasePnv getInstance(FirebaseApp app) {
53+
FirebaseFpnvService service = ImplFirebaseTrampolines.getService(app, SERVICE_ID,
54+
FirebaseFpnvService.class);
55+
if (service == null) {
56+
service = ImplFirebaseTrampolines.addService(app, new FirebaseFpnvService(app));
57+
}
58+
return service.getInstance();
59+
}
60+
61+
/**
62+
* Verifies a Firebase Phone Number Verification token (FPNV JWT).
63+
*
64+
* @param fpnvJwt The FPNV JWT string to verify.
65+
* @return A verified {@link FirebasePnvToken}.
66+
* @throws FirebasePnvException If verification fails.
67+
*/
68+
public FirebasePnvToken verifyToken(String fpnvJwt) throws FirebasePnvException {
69+
return this.tokenVerifier.verifyToken(fpnvJwt);
70+
}
71+
72+
private static class FirebaseFpnvService extends FirebaseService<FirebasePnv> {
73+
FirebaseFpnvService(FirebaseApp app) {
74+
super(SERVICE_ID, new FirebasePnv(app));
75+
}
76+
}
77+
}
78+
79+
80+
81+
82+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.fpnv;
18+
19+
/**
20+
* Error codes that are used in {@link FirebasePnv}.
21+
*/
22+
public enum FirebasePnvErrorCode {
23+
INVALID_ARGUMENT,
24+
INVALID_TOKEN,
25+
TOKEN_EXPIRED,
26+
INTERNAL_ERROR,
27+
SERVICE_ERROR,
28+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.fpnv;
18+
19+
/**
20+
* Generic exception related to Firebase Phone Number Verification.
21+
* Check the error code and message for more
22+
* details.
23+
*/
24+
public class FirebasePnvException extends RuntimeException {
25+
private final FirebasePnvErrorCode errorCode;
26+
27+
/**
28+
* Exception that created from {@link FirebasePnvErrorCode} and {@link String} message.
29+
*
30+
* @param authErrorCode {@link FirebasePnvErrorCode}
31+
* @param message {@link String}
32+
*/
33+
public FirebasePnvException(
34+
FirebasePnvErrorCode authErrorCode,
35+
String message
36+
) {
37+
super(message);
38+
this.errorCode = authErrorCode;
39+
}
40+
41+
public FirebasePnvErrorCode getFpnvErrorCode() {
42+
return errorCode;
43+
}
44+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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.fpnv;
18+
19+
import static com.google.common.base.Preconditions.checkArgument;
20+
21+
import com.google.common.collect.ImmutableMap;
22+
import java.util.List;
23+
import java.util.Map;
24+
25+
/**
26+
* Represents a verified Firebase Phone Number Verification token.
27+
*/
28+
public class FirebasePnvToken {
29+
private final Map<String, Object> claims;
30+
31+
public FirebasePnvToken(Map<String, Object> claims) {
32+
// this.claims = claims != null ? Collections.unmodifiableMap(claims) : Collections.emptyMap();
33+
checkArgument(claims != null && claims.containsKey("sub"),
34+
"Claims map must at least contain sub");
35+
this.claims = ImmutableMap.copyOf(claims);
36+
}
37+
38+
/**
39+
* Returns the issuer identifier for the issuer of the response.
40+
*/
41+
public String getIssuer() {
42+
return (String) claims.get("iss");
43+
}
44+
45+
/**
46+
* Returns the phone number of the user.
47+
* This corresponds to the 'sub' claim in the JWT.
48+
*/
49+
public String getPhoneNumber() {
50+
return (String) claims.get("sub");
51+
}
52+
53+
/**
54+
* Returns the audience for which this token is intended.
55+
*/
56+
public List<String> getAudience() {
57+
return (List<String>) claims.get("aud");
58+
}
59+
60+
/**
61+
* Returns the expiration time in seconds since the Unix epoch.
62+
*/
63+
public long getExpirationTime() {
64+
return (long) claims.get("exp");
65+
}
66+
67+
/**
68+
* Returns the issued-at time in seconds since the Unix epoch.
69+
*/
70+
public long getIssuedAt() {
71+
return (long) claims.get("iat");
72+
}
73+
74+
/**
75+
* Returns the entire map of claims.
76+
*/
77+
public Map<String, Object> getClaims() {
78+
return claims;
79+
}
80+
}

0 commit comments

Comments
 (0)