def get_login_url(request): if request.session.has_key( 'pkce_verifier') is False or request.session.has_key( 'code_challenge') is False: code_verifier = pkce.generate_code_verifier(length=128) code_challenge = pkce.get_code_challenge(code_verifier) request.session['pkce_verifier'] = code_verifier request.session['code_challenge'] = code_challenge redirect_url = request.build_absolute_uri(reverse("dashboard")) login_url = f"{settings.FUSION_AUTH_BASE_URL}/oauth2/authorize?client_id={settings.FUSION_AUTH_APP_ID}&redirect_uri={redirect_url}&response_type=code&code_challenge={request.session['code_challenge']}&code_challenge_method=S256" login_url = login_url.format( settings.FUSION_AUTH_BASE_URL, settings.FUSION_AUTH_APP_ID, ) return login_url
def _code_verifier(self) -> str: if self._codeverifier is None: self._codeverifier = generate_code_verifier(length=43) return self._codeverifier
def test_generate_code_verifier_too_short_or_too_long(length): with pytest.raises(ValueError): pkce.generate_code_verifier(length)
def test_generate_code_verifier(length): verifier1 = pkce.generate_code_verifier(length) verifier2 = pkce.generate_code_verifier(length) assert len(verifier1) == length assert len(verifier2) == length assert verifier1 != verifier2
def create_code_verifier(): code_verifier = pkce.generate_code_verifier(length=128) return code_verifier
from flask import Flask, render_template, request, jsonify, redirect, url_for import youtube_spotify_sync.app_secrets as app_secrets import string import requests import random import sys import pkce app = Flask("MyApp") code_verifier = pkce.generate_code_verifier(length=128) with open('temp/youtube_code.txt', 'a') as file: file.write(f"{code_verifier}\n") def generate_state(): letters = string.ascii_letters numbers = string.digits chars = letters + numbers return "".join(random.choice(chars) for i in range(16)) def get_spotify_auth_url(): state = generate_state() endpoint = "https://accounts.spotify.com/authorize" payload = { "client_id": app_secrets.spotify_client_id, "response_type": "code", "redirect_uri": "http://localhost:5000/callback", "state": state, "scope":