Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python google ouath authentication decode and verify id_token

Well, I am trying to implement google oauth authentication with my django project.

I follow the guide here:

https://developers.google.com/accounts/docs/OAuth2Login?hl=de-DE

I have got the response from exchanging code. I got a string type json which contains multiple info like access_token, id_token, etc.

Id_token is a cryptographically-signed JSON object encoded in base 64. I try to decode id_token with python module base64, but failed.

I also tried PyJWT, failed.

Is there any way to decode and verify it?

like image 505
Jerry Meng Avatar asked Sep 17 '25 21:09

Jerry Meng


1 Answers

Know this is an old post but I found it via Google so I thought somebody else might drop in...

I ended up doing:

segments = response['id_token'].split('.')

if (len(segments) != 3): 
   raise Exception('Wrong number of segments in token: %s' % id_token) 

b64string = segments[1]
b64string = b64string.encode('ascii') 
padded = b64string + '=' * (4 - len(b64string) % 4) 
padded = base64.urlsafe_b64decode(padded)
like image 76
Kristofer Källsbo Avatar answered Sep 21 '25 07:09

Kristofer Källsbo