The Authlib documentation describes how a resource server can validate the auth_token from the client implementing a BearerTokenValidator as such:
class MyBearerTokenValidator(BearerTokenValidator):
def authenticate_token(self, token_string):
return Token.query.filter_by(access_token=token_string).first()
require_oauth = ResourceProtector()
require_oauth.register_token_validator(MyBearerTokenValidator())
# protecting the resource
@app.route('/user')
@require_oauth()
def user_profile():
user = current_token.user
return jsonify(user)
https://docs.authlib.org/en/stable/flask/2/resource-server.html
This solution assumes the Resource server has access to the db where the Authentication server (AS) manages the token, using the ORM tool like SQLAlchemy.
In my case, I don't have access to the token db and the AS only provides a REST introspection endpoint to validate whether the token is valid.
I am planning to use the requests library and pass the token to the AS to implement my token validator
class MyBearerTokenValidator(BearerTokenValidator):
def authenticate_token(self, token_string):
resp = requests.post("https://oauth-server-host/oauth/v2/introspection", auth=(id, secret), data={"token": "TK"})
return resp.json()
Is this the correct approach or is there a better, more standard approach?