michaelconan / flask-rag-service

Web application and REST API Retrieval Augmented Generation microservice with distributed task preprocessing and vector database
1 stars 0 forks source link

Fully implement Oauth2 #1

Open michaelconan opened 3 months ago

michaelconan commented 3 months ago

Effectively just use standard conventions for authorization endpoints and payloads, still leveraging JWT.

At a minimum, implement:

See examples on the site.

michaelconan commented 3 months ago

Additionally implement scopes (at least read+write) with wrapper to validate scopes for routes.

from functools import wraps
from flask import request, jsonify
from flask_jwt_extended import verify_jwt_in_request, get_jwt

def jwt_scopes_required(required_scopes):
    def wrapper(fn):
        @wraps(fn)
        def decorator(*args, **kwargs):
            verify_jwt_in_request()
            claims = get_jwt()
            token_scopes = claims.get("scopes", [])
            if not all(scope in token_scopes for scope in required_scopes):
                return jsonify(msg="Missing required scopes"), 403
            return fn(*args, **kwargs)
        return decorator
    return wrapper
michaelconan commented 3 months ago

Consider Flask-OAuthLib instead