3

설명했듯이 here으로, 안드로이드 응용 프로그램에서 python3을 실행하는 서버로 전달되는 토큰을 확인하려고합니다.Android 백엔드에서 안드로이드 백엔드 호출에서 확인하십시오.

전달 된 토큰을 확인하고 싶습니다. 문제는 내가 google-api-python-client 라이브러리에서 지원하지 않는 서버에서 python3을 실행하고 있다는 것입니다. 나는 pyjwt를 사용하여, 다음과 같은 해결 방법을 발견하고이 site에서, 라이브러리를 요청합니다

import json 
import jwt 
import requests 

GOOGLE_CERTS_URI = 'https://www.googleapis.com/oauth2/v1/certs' 


class GoogleIdToken(object): 
    def __init__(self): 
     self._certs = {} 
     self._token = {} 

    def getCerts(self): 
     cert = requests.get(GOOGLE_CERTS_URI) 
     if cert.status_code == 200: 
      return json.loads(cert.content) 

    def isValid(self, token, audience, clientId=None): 
     self._certs = self.getCerts() 
     for key in self._certs: 
      try: 
       token = jwt.decode(token, key=self._certs[key], verify=False) 
       if 'email' in token and 'aud' in token: 
        if token['aud'] == audience and (clientId == token['cid'] if clientId is not None else True): 
         self._token = token 
         return True 
      except Exception, e: 
       print("Error decoding: %s" % e.message) 
     return False 

내 두 가지 질문

은 다음과 같습니다

  1. 사람이 작동하는 다른 및/또는 더 나은 기존 솔루션을 알고 있나요 python3?
  2. 위의 해결책이 완료 되었습니까?
+0

더 나은 솔루션을 찾았습니까? – drfence

+0

아니요, 좋은 해결책을 찾지 못했습니다. – Alex

+0

여전히 작동합니까, 아니면 Google에서 아무 것도 변경하지 않았습니까? 네이티브/자체 구현을 직접 찾고 있습니다. Google 도서관은 끔찍하게 부풀어 오릅니다. –

답변

0

Google API는 최근 Python 3x로 이식되었으며 jwt 검증을 포함합니다. here에 액세스 할 수 있습니다.

유일한 강조점은 내가 게시 한 링크에서 스테판이 한 것과 동일한 점입니다. 이는 jwt 디코드 호출에서 verify = False를 사용하여 가져온 보안 취약점입니다.

+0

고마워요. 포트를 찾았지만 너무 비 대한 비공식 라이브러리를 사용하지 않기를 바라고 있습니다. (제 목적으로는 몇 가지 방법 만 필요합니다). 내가 google-api-python-client 라이브러리를 고려한 유일한 이유는 LTS가 있다고 가정했기 때문입니다. 또한 시도하지는 않았지만 PyWWT가 이제 RS256을 지원하기 때문에 위의 해결 방법에서 kwarg가 True 값이 될 수 있는지 확인합니다. – Alex

0

몇 시간의 인터넷 검색과 시행 착오를 거쳐 결국이 작업이 끝났습니다.

종속성

pip install cryptography PyJWT requests 

코드 그냥 자신의 oauth2client 라이브러리를 사용하여, 그것을위한 파이썬에 Google provides an example을 실현

import jwt, requests 
from cryptography.x509 import load_pem_x509_certificate 
from cryptography.hazmat.backends import default_backend 

GOOGLE_CERTS_URI = 'https://www.googleapis.com/oauth2/v1/certs' 

def verify_id_token(id_token, audience): 
    certs = requests.get(GOOGLE_CERTS_URI).json() 

    for cert in certs.values(): 
     cert = str.encode(cert) 
     cert_obj = load_pem_x509_certificate(cert, default_backend()) 
     pub_key = cert_obj.public_key() 
     try: 
      return jwt.decode(id_token, pub_key, algorithm='RS256', 
           audience=audience) 
     except (jwt.exceptions.DecodeError, 
       jwt.exceptions.ExpiredSignatureError) as e: 
      pass 

편집 할 수 있습니다.