Fix mypy complains

This commit is contained in:
Lipu Fei 2018-09-28 14:17:00 +02:00
parent 7ae6800a14
commit 3bc91f15c3

View file

@ -4,7 +4,7 @@ import json
import random import random
from hashlib import sha512 from hashlib import sha512
from base64 import b64encode from base64 import b64encode
from typing import Optional from typing import Dict, Optional
import requests import requests
@ -24,37 +24,39 @@ class AuthorizationHelpers:
def settings(self) -> "OAuth2Settings": def settings(self) -> "OAuth2Settings":
return self._settings return self._settings
# Gets a dictionary with data that need to be used for any HTTP authorization request.
def getCommonRequestDataDict(self) -> Dict[str, str]:
data_dict = {"client_id": self._settings.CLIENT_ID if self._settings.CLIENT_ID is not None else "",
"redirect_uri": self._settings.CALLBACK_URL if self._settings.CALLBACK_URL is not None else "",
"scope": self._settings.CLIENT_SCOPES if self._settings.CLIENT_SCOPES is not None else "",
}
return data_dict
# Request the access token from the authorization server. # Request the access token from the authorization server.
# \param authorization_code: The authorization code from the 1st step. # \param authorization_code: The authorization code from the 1st step.
# \param verification_code: The verification code needed for the PKCE extension. # \param verification_code: The verification code needed for the PKCE extension.
# \return: An AuthenticationResponse object. # \return: An AuthenticationResponse object.
def getAccessTokenUsingAuthorizationCode(self, authorization_code: str, verification_code: str) -> "AuthenticationResponse": def getAccessTokenUsingAuthorizationCode(self, authorization_code: str, verification_code: str) -> "AuthenticationResponse":
return self.parseTokenResponse(requests.post(self._token_url, data={ data = self.getCommonRequestDataDict()
"client_id": self._settings.CLIENT_ID, data["grant_type"] = "authorization_code"
"redirect_uri": self._settings.CALLBACK_URL, data["code"] = authorization_code
"grant_type": "authorization_code", data["code_verifier"] = verification_code
"code": authorization_code, return self.parseTokenResponse(requests.post(self._token_url, data = data)) # type: ignore
"code_verifier": verification_code,
"scope": self._settings.CLIENT_SCOPES
})) # type: ignore
# Request the access token from the authorization server using a refresh token. # Request the access token from the authorization server using a refresh token.
# \param refresh_token: # \param refresh_token:
# \return: An AuthenticationResponse object. # \return: An AuthenticationResponse object.
def getAccessTokenUsingRefreshToken(self, refresh_token: str) -> AuthenticationResponse: def getAccessTokenUsingRefreshToken(self, refresh_token: str) -> "AuthenticationResponse":
return self.parseTokenResponse(requests.post(self._token_url, data={ data = self.getCommonRequestDataDict()
"client_id": self._settings.CLIENT_ID, data["grant_type"] = "refresh_token"
"redirect_uri": self._settings.CALLBACK_URL, data["refresh_token"] = refresh_token
"grant_type": "refresh_token", return self.parseTokenResponse(requests.post(self._token_url, data = data)) # type: ignore
"refresh_token": refresh_token,
"scope": self._settings.CLIENT_SCOPES
})) # type: ignore
@staticmethod @staticmethod
# Parse the token response from the authorization server into an AuthenticationResponse object. # Parse the token response from the authorization server into an AuthenticationResponse object.
# \param token_response: The JSON string data response from the authorization server. # \param token_response: The JSON string data response from the authorization server.
# \return: An AuthenticationResponse object. # \return: An AuthenticationResponse object.
def parseTokenResponse(token_response: requests.models.Response) -> AuthenticationResponse: def parseTokenResponse(token_response: requests.models.Response) -> "AuthenticationResponse":
token_data = None token_data = None
try: try: