How to verify firebase ID token with PHP(JWT)?

前端 未结 4 1387
盖世英雄少女心
盖世英雄少女心 2020-12-14 17:32

I have a shared hosting plan which has only PHP(no Java, no node.js). I need to send firebase ID token from my android app and verify it by PHP-JWT.

I am following t

4条回答
  •  隐瞒了意图╮
    2020-12-14 17:53

    Instead of doing it all manually, you can take a look at this library:
    Firebase Tokens or even Firebase Admin SDK for PHP. Caching stuff etc. is already implemented, just take a look at the docs.

    Basically you would simply do the following using Firebase Tokens Library:

    use Firebase\Auth\Token\HttpKeyStore;
    use Firebase\Auth\Token\Verifier;
    use Symfony\Component\Cache\Simple\FilesystemCache;
    
    $cache = new FilesystemCache();
    $keyStore = new HttpKeyStore(null, $cache);
    $verifier = new Verifier($projectId, $keyStore);
    
        try {
            $verifiedIdToken = $verifier->verifyIdToken($idToken);
    
            // "If all the above verifications are successful, you can use the subject 
            // (sub) of the ID token as the uid of the corresponding user or device. (see https://firebase.google.com/docs/auth/admin/verify-id-tokens#verify_id_tokens_using_a_third-party_jwt_library)
            echo $verifiedIdToken->getClaim('sub'); // "a-uid"
        } catch (\Firebase\Auth\Token\Exception\ExpiredToken $e) {
            echo $e->getMessage();
        } catch (\Firebase\Auth\Token\Exception\IssuedInTheFuture $e) {
            echo $e->getMessage();
        } catch (\Firebase\Auth\Token\Exception\InvalidToken $e) {
            echo $e->getMessage();
        }
    

提交回复
热议问题