def fetch_token(name: str, oauth: OAuth2Session, code: str) -> dict: """ Returns and saves a token. """ token = oauth.fetch_token(CONFIG[make_key(name, "url")] + "oauth/token", code=code, client_secret=CONFIG[make_key(name, "secret")]) make_save_token(name)(token) return token
def _authenticate( sesh: OAuth2Session, client_secret: str, api_url: str, redirect_uri: str, state: Optional[str], token_path: str, authorize_path: str, authorize_params: QueryParams, ) -> str: """Perform OAuth2 Flow and get a new token Note: Implicitly updates the OAuth2Session """ authorization_url, state = sesh.authorization_url( urljoin(api_url, authorize_path), state=state, kwargs=authorize_params, ) print( 'Opening browser to visit:\n\n' f'{Fore.BLUE}{Style.BRIGHT}{authorization_url}{Style.RESET_ALL}\n\n' 'Sign in and go through the DSA, then copy the url at the end.\n' ) sleep(DRAMATIC_PAUSE) try: webbrowser.open(authorization_url) except NotADirectoryError: # If permissions error print_exception( title='I couldn\'t open your browser', message=( 'Go ahead and copy/paste the url into your browser\n' 'Then sign in and go through the DSA.' ), ) sleep(DRAMATIC_PAUSE) authorization_response = _get_authorization_response_url() print() environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1' # allow non-http redirect_uri token = sesh.fetch_token( urljoin(api_url, token_path), authorization_response=authorization_response, client_secret=client_secret, include_client_id=True, ) return str(token['access_token'])
def fetch_token(self, token_url=_TOKEN_URL, code=None, authorization_response=None, body='', auth=None, username=None, password=None, method='POST', timeout=None, headers=None, verify=True, proxies=None, include_client_id=None, client_secret=creds['CLIENT_SECRET'], **kwargs): return OAuth2Session.fetch_token( self, token_url, authorization_response=authorization_response, client_secret=client_secret)
def resolve_token( oauth_library: OAuth2Session, provider_instance, authorization_response, client_secret, **kwargs, ): if "path" in kwargs: provider_instance["token_path"] = kwargs["path"] full_url = f"{provider_instance['base_url']}{provider_instance['token_path']}" string = authorization_response if settings.DEBUG: if ("localhost" in authorization_response.lower() or "127.0.0.1" in authorization_response.lower()): string = string.replace("http://", "https://") token = oauth_library.fetch_token( full_url, authorization_response=string, client_secret=client_secret, ) return token