kreait / firebase-tokens-php

A PHP library to work with Firebase tokens
MIT License
223 stars 33 forks source link

Undefined method 'getClaims' #55

Closed cindicate443 closed 11 months ago

cindicate443 commented 11 months ago

Describe the bug

Hello, I'm trying to get the uid from the verified Firebase token, but I'm unable to use the 'getClaims('sub')' method. I'm using the same exact code under the 'Verify an ID token' heading, just adding the token from Firebase, but when I add '$token->getClaims('sub')' to try get retrieve the uid, I get the error below. I'm fairly new to implementing libraries and I'm just unsure how to retrieve the sub-claim. Also, I'm not using Firebase Admin SDK as my backend.

Please help, thanks!

image

Installed packages

{
    "require": {
        "kreait/firebase-tokens": "^4.2",
        "kreait/firebase-php": "^7.6",
        "lcobucci/jwt": "^5.1",
        "firebase/php-jwt": "^6.9"
    }
}

PHP version and extensions

composer               2.6.5    Composer package
composer-plugin-api    2.6.0    The Composer Plugin API
composer-runtime-api   2.2.2    The Composer Runtime API
ext-bcmath             8.2.4    The bcmath PHP extension
ext-bz2                8.2.4    The bz2 PHP extension
ext-calendar           8.2.4    The calendar PHP extension
ext-ctype              8.2.4    The ctype PHP extension
ext-curl               8.2.4    The curl PHP extension
ext-date               8.2.4    The date PHP extension
ext-dom                20031129 The dom PHP extension
ext-exif               8.2.4    The exif PHP extension
ext-fileinfo           8.2.4    The fileinfo PHP extension
ext-filter             8.2.4    The filter PHP extension
ext-ftp                8.2.4    The ftp PHP extension
ext-gettext            8.2.4    The gettext PHP extension
ext-hash               8.2.4    The hash PHP extension
ext-iconv              8.2.4    The iconv PHP extension
ext-json               8.2.4    The json PHP extension
ext-libxml             8.2.4    The libxml PHP extension
ext-mbstring           8.2.4    The mbstring PHP extension
ext-mysqli             8.2.4    The mysqli PHP extension
ext-mysqlnd            0        The mysqlnd PHP extension (actual version: mysqlnd 8.2.4)
ext-openssl            8.2.4    The openssl PHP extension
ext-pcre               8.2.4    The pcre PHP extension
ext-pdo                8.2.4    The PDO PHP extension
ext-pdo_mysql          8.2.4    The pdo_mysql PHP extension
ext-pdo_sqlite         8.2.4    The pdo_sqlite PHP extension
ext-phar               8.2.4    The Phar PHP extension
ext-random             8.2.4    The random PHP extension
ext-readline           8.2.4    The readline PHP extension
ext-reflection         8.2.4    The Reflection PHP extension
ext-session            8.2.4    The session PHP extension
ext-simplexml          8.2.4    The SimpleXML PHP extension
ext-sodium             8.2.4    The sodium PHP extension
ext-spl                8.2.4    The SPL PHP extension
ext-tokenizer          8.2.4    The tokenizer PHP extension
ext-xml                8.2.4    The xml PHP extension
ext-xmlreader          8.2.4    The xmlreader PHP extension
ext-xmlwriter          8.2.4    The xmlwriter PHP extension
ext-zip                1.21.1   The zip PHP extension
ext-zlib               8.2.4    The zlib PHP extension
lib-bz2                1.0.8    The bz2 library
lib-curl               7.85.0   The curl library
lib-curl-libssh2       1.10.0   curl libssh2 version
lib-curl-openssl       3.0.8    curl OpenSSL version (3.0.8)
lib-curl-zlib          1.2.12   curl zlib version
lib-date-timelib       2022.05  date timelib version
lib-date-zoneinfo      2022.7   zoneinfo ("Olson") database for date
lib-fileinfo-libmagic  540      fileinfo libmagic version
lib-iconv              1.16     The iconv library
lib-libsodium          1.0.18   The libsodium library
lib-libxml             2.10.3   libxml library version
lib-mbstring-libmbfl   1.3.2    mbstring libmbfl version
lib-mbstring-oniguruma 6.9.8    mbstring oniguruma version
lib-openssl            3.0.8    OpenSSL 3.0.8 7 Feb 2023
lib-pcre               10.40    The pcre library
lib-pcre-unicode       14.0.0   PCRE Unicode version support
lib-pdo_sqlite-sqlite  3.39.2   The pdo_sqlite-sqlite library
lib-zip-libzip         1.7.1    The zip-libzip library
lib-zlib               1.2.12   The zlib library
php                    8.2.4    The PHP interpreter
php-64bit              8.2.4    The PHP interpreter, 64bit
php-ipv6               8.2.4    The PHP interpreter, with IPv6 support
php-zts                8.2.4    The PHP interpreter, with Zend Thread Safety

Steps to reproduce the issue.

<?php 
    require __DIR__ . '/vendor/autoload.php';
    use Kreait\Firebase\JWT\Error\IdTokenVerificationFailed;
    use Kreait\Firebase\JWT\IdTokenVerifier;

    $projectID = '...'; // firebase project id
    $verifier = IdTokenVerifier::createWithProjectId($projectID);
    $userIdToken = 'eyJh...';

    try {
        $token = $verifier->verifyIdToken($userIdToken);
        $uid = $token->getClaims('sub');
        echo "valid token";
    } catch (IdTokenVerificationFailed $e) {
        echo $e->getMessage();
    }
?>

Error message/Stack trace

-

Additional information

No response

jeromegamez commented 11 months ago

Hey there!

The tokens returned by this library don't have the same methods as other implementations, for example in lcobucci/jwt. You can find more information at https://github.com/kreait/firebase-tokens-php#tokens and directly in the interface at https://github.com/kreait/firebase-tokens-php/blob/4.x/src/JWT/Contract/Token.php

If you'd like more than just a headers and payload field, you could re-parse the JWT with another library, for example (because you have it in your composer.json) with lcobucci/jwt https://lcobucci-jwt.readthedocs.io/en/stable/parsing-tokens/

I hope this helps!

cindicate443 commented 11 months ago

@jeromegamez Thank you, that makes much more sense. I was able to resolve the issue using the lcobucci/jwt library. Thanks! :)

jeromegamez commented 11 months ago

Glad I could help! 🌺