authlib.integrations.base_client.errors.MismatchingStateError: mismatching_state: CSRF Warning! State not equal in request and response. Python error
Я работаю с Flask и Authlib (Oauth 2.0). Сейчас я делаю регистрацию и возникла проблема с входом точнее после входа, перезагружаю страницу сервер вылетает и выдаёт вот такую ошибку: "authlib.integrations.base_client.errors.MismatchingStateError: mismatching_state: CSRF Warning! State not equal in request and response."
Вот код:
import os
import requests
from threading import Lock
from flask import Flask,render_template,flash, redirect,url_for ,logging,request, session
from authlib.integrations.flask_client import OAuth, OAuthError
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__, static_url_path='', static_folder='static')
oauth = OAuth(app)
oauth.init_app(app)
app.secret_key = os.urandom(24)
app.config['SESSION_COOKIE_NAME'] = "<Name>"
app.config['GOOGLE_CLIENT_ID'] = "<client id>"
app.config['GOOGLE_CLIENT_SECRET'] = "<client secret>"
app.config['GITHUB_CLIENT_ID'] = "<client id>"
app.config['GITHUB_CLIENT_SECRET'] = "<client secret>"
app.config['UPLOADS_FOLDER'] = 'static\profile-picture'
app.config['MAX_CONTENT-PATH'] = 1024*1024
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///./database.sqlite3'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
google = oauth.register(
name = 'google',
client_id = app.config["GOOGLE_CLIENT_ID"],
client_secret = app.config["GOOGLE_CLIENT_SECRET"],
access_token_url = 'https://oauth2.googleapis.com/token',
access_token_params = None,
authorize_url = 'https://accounts.google.com/o/oauth2/auth',
authorize_params = None,
api_base_url = 'https://www.googleapis.com/oauth2/v1/certs',
userinfo_endpoint = 'https://openidconnect.googleapis.com/v1/userinfo',
server_metadata_url= 'https://accounts.google.com/.well-known/openid-configuration',
client_kwargs = {'scope': 'openid email profile'}
)
github = oauth.register (
name = 'github',
client_id = app.config["GITHUB_CLIENT_ID"],
client_secret = app.config["GITHUB_CLIENT_SECRET"],
access_token_url = 'https://github.com/login/oauth/access_token',
access_token_params = None,
authorize_url = 'https://github.com/login/oauth/authorize',
authorize_params = None,
api_base_url = 'https://api.github.com/',
client_kwargs = {'scope': 'user:email'}
)
db = SQLAlchemy(app)
mutex = Lock()
class user(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80))
email = db.Column(db.String(120))
password = db.Column(db.String(80))
@app.route("/")
def index():
return render_template("index.html")
@app.route("/login",methods=["GET", "POST"])
def login():
if request.method == "POST":
with mutex:
uname = request.form["uname"]
passw = request.form["passw"]
logining = user.query.filter_by(username=uname, password=passw).first()
if logining is not None:
return redirect(url_for("index"))
return render_template("login.html")
@app.route("/register", methods=["GET", "POST"])
def register():
if request.method == "POST":
with mutex:
uname = request.form['uname']
mail = request.form['mail']
passw = request.form['passw']
photo = request.files['photo']
photo.filename = uname
photo.save(os.path.join(app.config['UPLOADS_FOLDER'], photo.filename + '.png'))
registering = user(username = uname, email = mail, password = passw)
db.session.add(registering)
db.session.commit()
return redirect(url_for("login"))
return render_template("register.html")
@app.route('/register/google')
def google_login():
with mutex:
google = oauth.create_client('google')
redirect_uri = url_for('google_authorize', _external=True)
return google.authorize_redirect(redirect_uri)
@app.route('/register/google/authorize')
def google_authorize():
with mutex:
google = oauth.create_client('google')
token = google.authorize_access_token()
resp = google.get('userinfo').json()
print(f"\n{resp}\n")
return "You are successfully signed in using google"
@app.route('/register/github')
def github_login():
with mutex:
github = oauth.create_client('github')
redirect_uri = url_for('github_authorize', _external=True)
return github.authorize_redirect(redirect_uri)
@app.route('/register/github/authorize')
def github_authorize():
with mutex:
github = oauth.create_client('github')
token = github.authorize_access_token()
resp = github.get('user').json()
print(f"\n{resp}\n")
return "You are successfully signed in using github"
if __name__ == "__main__":
db.create_all()
db.session.commit()
app.run(debug=True)