|
6 | 6 |
|
7 | 7 | use Firebase\JWT\CachedKeySet; |
8 | 8 | use Firebase\JWT\JWT; |
| 9 | +use Kreait\Firebase\Exception\AppCheck\FailedToVerifyAppCheckReplayProtection; |
9 | 10 | use Kreait\Firebase\Exception\AppCheck\FailedToVerifyAppCheckToken; |
10 | 11 | use Kreait\Firebase\Exception\AppCheck\InvalidAppCheckToken; |
11 | 12 | use LogicException; |
|
30 | 31 | public function __construct( |
31 | 32 | private string $projectId, |
32 | 33 | private CachedKeySet $keySet, |
| 34 | + private ApiClient $apiClient, |
33 | 35 | ) { |
34 | 36 | } |
35 | 37 |
|
36 | 38 | /** |
37 | 39 | * Verifies the format and signature of a Firebase App Check token. |
38 | 40 | * |
39 | 41 | * @param string $token the Firebase Auth JWT token to verify |
| 42 | + * @param bool $consume whether the token should be consumed for replay protection |
40 | 43 | * |
41 | 44 | * @throws FailedToVerifyAppCheckToken if the token could not be verified |
| 45 | + * @throws FailedToVerifyAppCheckReplayProtection if replay protection could not be verified |
42 | 46 | * @throws InvalidAppCheckToken if the token is invalid |
43 | 47 | */ |
44 | | - public function verifyToken(#[SensitiveParameter] string $token): DecodedAppCheckToken |
| 48 | + public function verifyToken(#[SensitiveParameter] string $token, bool $consume = false): VerifyAppCheckTokenResponse |
45 | 49 | { |
46 | 50 | $decodedToken = $this->decodeJwt($token); |
47 | 51 |
|
48 | 52 | $this->verifyContent($decodedToken); |
49 | 53 |
|
50 | | - return $decodedToken; |
| 54 | + $alreadyConsumed = null; |
| 55 | + |
| 56 | + if ($consume) { |
| 57 | + try { |
| 58 | + $alreadyConsumed = $this->apiClient->verifyReplayProtection($token, $this->projectId); |
| 59 | + } catch (Throwable $e) { |
| 60 | + throw new FailedToVerifyAppCheckReplayProtection( |
| 61 | + message: 'Unable to verify App Check token replay protection: '.$e->getMessage(), |
| 62 | + previous: $e, |
| 63 | + ); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return new VerifyAppCheckTokenResponse($decodedToken->app_id, $decodedToken, $alreadyConsumed); |
51 | 68 | } |
52 | 69 |
|
53 | 70 | /** |
|
0 commit comments