jetbridge / flask_cognito

Flask authentication with JWT against AWS Cognito
MIT License
94 stars 30 forks source link

cognito_group_permissions not working #16

Open bractus opened 3 years ago

bractus commented 3 years ago

I'm trying to use the cognito_group_permissions decorator but isn't working...

Traceback (most recent call last): File "main.py", line 5, in from flask_cognito import CognitoAuth, cognito_group_permissions ImportError: cannot import name 'cognito_group_permissions' from 'flask_cognito' (C:\Users\Cairo\anaconda3\envs\salt\lib\site-packages\flask_cognito.py)

revmischa commented 3 years ago

@aaronbrown1988

aaronbrown1988 commented 3 years ago

Had a quick look, seems like the name of the decorator in the module doesn't match that in the read me. It's looks to have been mismatched since ~ PR #9. I've made a branch which duplicates the decorator under the name that matches the readme me and adds a few tests, alternatively the readme could be updated to reflect the name in the code.

@revmischa how do you want to handle the mismatch between the name in the code in the readme to avoid breaking things for other users who might be using the current decorator name?

aaronbrown1988 commented 3 years ago

Here's the branch for reference: https://github.com/aaronbrown1988/flask_cognito/tree/fix-group-decorator-name

revmischa commented 3 years ago

Up to you, can you just export a variable that is an alias of the existing decorator?

aaronbrown1988 commented 3 years ago

Sure, that would work. I've created a PR with the change here: https://github.com/jetbridge/flask_cognito/pull/17

MordiGrip commented 3 years ago

Hi, I have encountered the same issue, in a slightly different way.

First of all, @aaronbrown1988 thanks for your solution!

But I'm afraid it's not enough, you see, the decorator generator always returns the same function and that's lead to the same function being registered on different flask routes (which is impossible).

so I think we should change this code:

def cognito_check_groups(groups: list):
    def decorator(function):
        def wrapper(*args, **kwargs):
            _cognito_check_groups(groups)
            return function(*args, **kwargs)

        return wrapper

    return decorator

to use python wraps like the other generators, so it will look roughly like this:

def cognito_check_groups(groups: list):
    def decorator(function):
        @wraps(function)
        def wrapper(*args, **kwargs):
            _cognito_check_groups(groups)
            return function(*args, **kwargs)

        return wrapper

    return decorator

This way the wrapper function will be patched to have the original metadata.