I’m working on adding Oauth login for my website using Google account, and have stumbled upon one thing – how to validate provided information for Oath before executing login flow?
To start with, i have a Flask app created this way:
import logging
import os
from datetime import datetime
from datetime import timedelta
from typing import Any, Dict
import yaml
from flask import Flask, redirect, url_for, g, flash
from flask.typing import ResponseValue
from flask_babel import gettext as _
from flask_login import current_user, logout_user
from werkzeug.wrappers.response import Response
# .. many other local imports ..
def load_config(config_path: str) -> Any:
with open(config_path, 'r', encoding='utf-8') as config_file:
config = yaml.safe_load(config_file)
return config
def create_app(config_path: str = './<path>/config.yaml', config: Any = None) -> Flask:
if config is None:
config = load_config(config_path)
current_app = Flask(__name__,
template_folder=..,
static_folder=..)
current_app.config.update(config)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
db.init_app(current_app)
with current_app.app_context():
# ..
init_extensions(current_app)
# ..
return current_app
## .. other stuff ..
if __name__ == "__main__":
app = create_app()
app.run()
init_extensions
is implemented this way:
def init_extensions(app: Flask) -> None:
"""
Initialize Flask extensions.
:param app: Flask application instance
"""
# other inits ..
# init OAuth
init_oauth(app)
And init_oauth
is:
from authlib.integrations.flask_client import OAuth
from flask import Flask
oauth = OAuth()
def init_oauth(app: Flask) -> None:
oauth.init_app(app)
oauth.register(
name='google',
client_id=app.config['GOOGLE_OAUTH_CLIENT_ID'],
client_secret=app.config['GOOGLE_OAUTH_CLIENT_SECRET'],
server_metadata_url='https://accounts.google.com/.well-known/openid-configuration',
client_kwargs={
'scope': 'openid email profile'
}
)
tricky problem here is that i want to add a new function, like check_accessibility
:
oauth = OAuth()
def init_oauth(app: Flask) -> None:
# same as above
def check_accessibility() -> bool:
# True, if everything is ok;
# False - if >0 creds are incorrect OR
# 3rd party server is inaccessible OR
# whatever else ..
That will allow me to check and turn on/off login options on the fly without adding extra hustle for the user to try to login and find out server isn’t working or sth else.
Are there any uncomplicated ways to implement this function? If not, how to implement? Check server accessibility -> Check creds (how)? -> Return bool
?