aws / chalice

Python Serverless Microframework for AWS
Apache License 2.0
10.67k stars 1.01k forks source link

How to get scopes from authorizer to inspect it? #2004

Closed hitrust closed 1 year ago

hitrust commented 2 years ago

According to Doc, it said

Scopes can also be used with custom authorizers and built-in authorizers. These authorizers will need to inspect the access token to determine if access should be granted based on the scopes configured for the authorizer and route.

from chalice import Blueprint

extra_routes = Blueprint(__name__)

@extra_routes.authorizer()
def demo_auth(auth_request):
    token = auth_request.token
    # we can decode token in this part, and get scope that sets in token payload,
    # then how to get scopes from authorizer that sets in route using with_scopes.
    if token == 'allow':
        return AuthResponse(routes=['/'], principal_id='user')
    else:
        return AuthResponse(routes=[], principal_id='user')

@extra_routes.route('/',  methods=['GET'], authorizer=demo_auth.with_scopes(["author", "editor"]))
def index():
    return {'context': app.current_request.context}
yogesh-ti commented 1 year ago

@hitrust did you find a way to do it ?

hitrust commented 1 year ago

Currently just add it to your jwt token, then decode token and put it in context, then you can verify it.