Unable to verify Firebase token

641 Views Asked by At

I am making an app in flutter which uses Google sign-in. I also have a Django backend linked to the app and I want to verify the user in the Django backend. I found many solutions on the internet but none is working. Probably I am messing up somewhere.

I tried using python-jose for verification and here is the code:

from jose import jwt
import urllib.request, json
token = '<token recieved using await user.getIdToken in flutter>'
target_audience = "<tried projectid/appid>"
certificate_url = 'https://www.googleapis.com/robot/v1/metadata/x509/[email protected]'
response = urllib.request.urlopen(certificate_url)
certs = response.read()
certs = json.loads(certs)
print(certs)
user = jwt.decode(token, certs, algorithms='RS256', 
audience=target_audience)

I also tried oauth2client, the code is here:

from oauth2client import crypt
import urllib.request, json
certificate_url = 'https://www.googleapis.com/robot/v1/metadata/x509/[email protected]'
target_audience = 'tried projectid/appid'
response = urllib.request.urlopen(certificate_url)
certs = response.read()
certs = json.loads(certs)
print(certs)
crypt.MAX_TOKEN_LIFETIME_SECS = 30 * 86400
idtoken = 'token received from await user.getIdToken()'
crypt.verify_signed_jwt_with_certs(idtoken, certs, target_audience)

I also tried firebase_admin for python:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import auth
cred = credentials.Certificate('<firebase service accounts private key>')
default_app = firebase_admin.initialize_app(cred)
token = 'token from flutter'
verifyied =auth.verify_id_token(id_token=token)

Just to check whether the firebase_admin library itself is working or not, I passed the userid to server from the app and tried deleting the user using firebase_admin and I could do that. But for some reason I am unable to verify the token.

Thanks for the help.

2

There are 2 best solutions below

0
On

Answering my own question. The problem was that my server was not actually deployed, so, I was copying the token printed in vscode console when the user logs in and pasting it into the python code and trying to verify the token. Turns out it doesn't work that way.

I hosted my Django app and passed the token in a post request and then tried to verify the token and it worked.

You can refer the solutions here if you are stuck : https://coders-blogs.blogspot.com/2018/11/authenticating-user-on-backend-of-your.html

1
On

I have also faced the same issue.

Case:

Initially: I was printing auth token in vscode console and was verifying in terminal.

It gave me the error: token length cannot be 1 more than % 4.

I tried verifying the token from jwt.io and it was seemingly correct.

Actual Reason for the issue:

The console output of vscode (in my case windows 7 and 64 bit). Is limited to 1064 characters for a line.

Although the actual length of token is supposed to be 1170 characters.

Workaround Solution:

Print the substring in the vscode console and the join them in python shell to verify.