def authentication_oauth(): if app.authentication_mgr.authenticated: return render_template('auth_result.html', title='Already signed in', result='Already signed in', message='You are already signed in, please logout first!', link_path='/auth/logout', link_title='Logout') else: return render_template('login_oauth.html', oauth_url=AuthenticationManager.generate_authorization_url())
def main(): parser = argparse.ArgumentParser( description="Authenticate with xbox live, manual webbrowser way") parser.add_argument('url', nargs='?', default=None, help="Redirect URL of successful authentication") parser.add_argument( '--tokens', '-t', default=TOKENS_FILE, help= "Token filepath, file gets created if nonexistent and auth is successful." " Default: {}".format(TOKENS_FILE)) args = parser.parse_args() if not args.url: print( 'Visit following URL in your webbrowser and authenticate yourself, then restart' ' this script with \'<redirect url>\' argument\n\n' '{}'.format(AuthenticationManager.generate_authorization_url())) sys.exit(0) url_begin = 'https://login.live.com/oauth20_desktop.srf?' if not args.url.startswith(url_begin): print('Wrong redirect url, expected url like: \'{}...\''.format( url_begin)) sys.exit(1) try: print('Extracting tokens from URL') auth_mgr = AuthenticationManager.from_redirect_url(args.url) except Exception as e: print( 'Failed to get tokens from supplied redirect URL, err: {}'.format( e)) sys.exit(2) try: print('Authenticating with Xbox Live') auth_mgr.authenticate(do_refresh=False) except AuthenticationException as e: print('Authentication failed! err: {}'.format(e)) sys.exit(3) auth_mgr.dump(args.tokens) print('Success, tokens are stored at \'{}\''.format(args.tokens))
async def async_main(client_id: str, client_secret: str, redirect_uri: str, token_filepath: str): async with ClientSession() as session: auth_mgr = AuthenticationManager(session, client_id, client_secret, redirect_uri) # Refresh tokens if we have them if os.path.exists(token_filepath): with open(token_filepath, mode="r") as f: tokens = f.read() auth_mgr.oauth = OAuth2TokenResponse.parse_raw(tokens) await auth_mgr.refresh_tokens() # Request new ones if they are not valid if not (auth_mgr.xsts_token and auth_mgr.xsts_token.is_valid()): auth_url = auth_mgr.generate_authorization_url() webbrowser.open(auth_url) code = await queue.get() await auth_mgr.request_tokens(code) with open(token_filepath, mode="w") as f: f.write(auth_mgr.oauth.json())
async def do_authentication(args: argparse.Namespace) -> AuthenticationManager: """ Shortcut for doing xbox live authentication (uses xbox-webapi-python lib). Args: args: Parsed arguments Returns: An authenticated instance of AuthenticationManager Raises: AuthenticationException: If authentication failed """ async with aiohttp.ClientSession() as session: auth_mgr = AuthenticationManager( session, args.client_id, args.client_secret, args.redirect_uri ) # Refresh tokens if we have them if os.path.exists(args.tokens): with open(args.tokens, mode="r") as f: tokens = f.read() auth_mgr.oauth = OAuth2TokenResponse.parse_raw(tokens) await auth_mgr.refresh_tokens() # Request new ones if they are not valid if not (auth_mgr.xsts_token and auth_mgr.xsts_token.is_valid()): auth_url = auth_mgr.generate_authorization_url() print(f'Authorize with following URL: {auth_url}\n') code = input('Enter received authorization code: ') await auth_mgr.request_tokens(code) with open(args.tokens, mode="w") as f: f.write(auth_mgr.oauth.json()) return auth_mgr
def authentication_get_auth_url(): return app.success(authorization_url=AuthenticationManager.generate_authorization_url())