dariant Virgi
dariant Virgi

Reputation: 91

Django + Auth0 "a bytes-like object is required, not 'str'" python 3.5.2

I know there has been a bunch of "a bytes-like object is required, not 'str'" question around. but i've tried and tried and no one fixes my issues. it'll be very nice if you can help me out in my particular case.

i'm using this official example from Auth0 for django

https://github.com/auth0-samples/auth0-django-web-app/tree/master/01-Login

I'm trying to connect django 1.11 with Auth0 and got that error on this code

def get_user_details(self, response):
        # Obtain JWT and the keys to validate the signature
        idToken = response.get('id_token')
        jwks = request.urlopen("https://" + self.setting('DOMAIN') + "/.well-known/jwks.json")
        issuer = "https://" + self.setting('DOMAIN') + "/"
        audience = self.setting('KEY') #CLIENT_ID
        payload = jwt.decode(idToken, jwks.read(), algorithms=['RS256'], audience=audience, issuer=issuer).decode('UTF-8')

i have tried to use .encode('utf-8') or the b"str" or the "rb" / "wb" but still no luck.

Thanks in advance for your help

---- edit ----

using

payload = jwt.decode(idToken, jwks.read().encode('utf-8'), algorithms=['RS256'], audience=audience, issuer=issuer).decode('UTF-8')

bring me to other error 'bytes' object has no attribute 'encode'

Upvotes: 1

Views: 702

Answers (2)

Esraa
Esraa

Reputation: 11

jwks.read() type is 'bytes' but in jws.py the line:

if 'keys' in key

expect list of str so to solve this convert it to list of str:

def get_user_details(self, response):
    # Obtain JWT and the keys to validate the signature
    idToken = response.get('id_token')
    jwks = request.urlopen("https://" + self.setting('DOMAIN') + "/.well-known/jwks.json")
    issuer = "https://" + self.setting('DOMAIN') + "/"
    audience = self.setting('KEY') #CLIENT_ID
    # convert to list str
    jws_read = bytearray(jwks.read()).decode('ascii')
    payload = jwt.decode(idToken, jws_read, algorithms=['RS256'], audience=audience, issuer=issuer)

Upvotes: 1

Ger
Ger

Reputation: 1008

This is a python 3 vs python 2 issue (the Auth0 samples seem to be written for python2.7x).

I had a similar error using the Flask example project from Auth0. To fix it, I modified the python-jose library. In line 222 of jws.py I changed key = json.loads(key) to key = json.loads(key.decode('utf-8')). That has worked for me, I hope it works for you.

Hope this helps.

I've forked the jose package but haven't got around to putting a PR in.

Upvotes: 0

Related Questions