tokusumi / fastapi-cloudauth

Simple integration between FastAPI and cloud authentication services (AWS Cognito, Auth0, Firebase Authentication).
MIT License
323 stars 35 forks source link

JWKError #48

Closed AlviseSembenico closed 2 years ago

AlviseSembenico commented 3 years ago

Hi, I am trying to make it work with Firebase. I am using exaclty the same code you have in the examples

import os
from fastapi import FastAPI, Depends
from fastapi_cloudauth.firebase import FirebaseCurrentUser, FirebaseClaims

app = FastAPI()

get_current_user = FirebaseCurrentUser(
    project_id="my-project-id"
)

@app.get("/user/")
def secure_user(current_user: FirebaseClaims = Depends(get_current_user)):
    # ID token is valid and getting user info from ID token
    return f"Hello, {current_user.user_id}"

but I have the following error:

 File "./src/main.py", line 7, in <module>
    get_current_user = FirebaseCurrentUser(
  File "/home/elvis/Documents/dev-tools/API/env/lib/python3.9/site-packages/fastapi_cloudauth/firebase.py", line 28, in __init__
    jwks = JWKS.firebase(url)
  File "/home/elvis/Documents/dev-tools/API/env/lib/python3.9/site-packages/fastapi_cloudauth/verification.py", line 70, in firebase
    keys = {
  File "/home/elvis/Documents/dev-tools/API/env/lib/python3.9/site-packages/fastapi_cloudauth/verification.py", line 71, in <dictcomp>
    kid: jwk.construct(publickey, algorithm="RS256")
  File "/home/elvis/Documents/dev-tools/API/env/lib/python3.9/site-packages/jose/jwk.py", line 79, in construct
    return key_class(key_data, algorithm)
  File "/home/elvis/Documents/dev-tools/API/env/lib/python3.9/site-packages/jose/backends/rsa_backend.py", line 171, in __init__
    raise JWKError(e)
jose.exceptions.JWKError: No PEM start marker "b'-----BEGIN PRIVATE KEY-----'" found

Any idea?

Thank you

tokusumi commented 3 years ago

Did you assign correct project_id in 8 lines? "my-project-id" is just example. Please check your Firebase project id and replace above with it.

AlviseSembenico commented 3 years ago

Yes, i do replace it with the project ID, i just changed it to post it here.

souless94 commented 3 years ago

Hi

You can try to install python-jose. It worked for me.

pip install 'python-jose[cryptography]'

Hope this helps !

vlad-pro commented 2 years ago

Hey,

@souless94 solution works.

I think this is also related to https://github.com/tokusumi/fastapi-cloudauth/issues/51

tokusumi commented 2 years ago

From v0.4.2 cryptography is installed by default. Please try that!