cgauge / Flask-AWSCognito

Extension for Flask that adds support for AWSCognito into your application
https://flask-awscognito.readthedocs.io
MIT License
28 stars 28 forks source link

Issue while trying to use aws_auth.get_user_info #3

Closed devireds closed 4 years ago

devireds commented 4 years ago
from flask import Flask, Response, json, request, redirect
from botocore.vendored import requests
import requests
import boto3
from flask import jsonify
from flask_awscognito import AWSCognitoAuthentication

app = Flask(__name__)

app.config['AWS_DEFAULT_REGION'] = 'XXXXX'
app.config['AWS_COGNITO_DOMAIN'] = 'XXXXXXXXXXX'
app.config['AWS_COGNITO_USER_POOL_ID'] = 'XXXXXXXXXXX'
app.config['AWS_COGNITO_USER_POOL_CLIENT_ID'] = 'XXXXXXXXXXX'
app.config['AWS_COGNITO_USER_POOL_CLIENT_SECRET'] = 'XXXXXXXXXXX'
app.config['AWS_COGNITO_REDIRECT_URL'] = 'XXXXXXXXXXX'

aws_auth = AWSCognitoAuthentication(app)

@app.route('/login')
def sign_in():
    return redirect(aws_auth.get_sign_in_url())

@app.route('/aws_cognito_redirect')
def aws_cognito_redirect():
    access_token = aws_auth.get_access_token(request.args)
    return jsonify({'access_token': access_token})

@app.route('/userdata')
#curl -sv 'https://api.saidevireddy.com/userdata' \ --header 'Authorization: <access_token>'  
def userdata():
    headers = request.headers
    access_token = headers.get("Authorization")
    data = aws_auth.get_user_info(access_token)
    return {
        'statusCode': 200,
        'body': json.dumps(data)
    }

@app.route('/')
@aws_auth.authentication_required
def index():
    claims = aws_auth.claims # or g.cognito_claims
    return jsonify({'claims': claims})

if __name__ == '__main__':
    app.run(host = '127.0.0.1', port=5005, debug= True)

Firstly, thank you for developing this plugin. It's very useful.

I was successfully able to integrate this plugin with my application on AWS with API Gateway.

In my code, both sign_in() and aws_cognito_redirect() works without any issues. However, when I try to use aws_auth.get_user_info(access_token) in userdata(), I am getting the following error.

AttributeError: 'AWSCognitoAuthentication' object has no attribute 'get_user_info'
babaMar commented 4 years ago

Hi @devireds thanks for using the plugin. Can you please give some context and post the error message? Also, the code you posted seems to have few indentation errors, generally you can format it enclosing it between ```python

and ```

devireds commented 4 years ago

Hi @devireds thanks for using the plugin. Can you please give some context and post the error message? Also, the code you posted seems to have few indentation errors, generally you can format it enclosing it between ```python

and ```

Thank you for the quick response @babaMar I have updated my comment with proper formatting and the error message. Appreciate your help!

babaMar commented 4 years ago

Hi @devireds thanks for using the plugin. Can you please give some context and post the error message? Also, the code you posted seems to have few indentation errors, generally you can format it enclosing it between python and

Thank you for the quick response @babaMar I have updated my comment with proper formatting and the error message. Appreciate your help!

Guess you have installed with pip? Because the get_user_info(...) has been recently added and the PyPi release does not include that. If that's the case cloning the project would solve the issue

devireds commented 4 years ago

Hi @devireds thanks for using the plugin. Can you please give some context and post the error message? Also, the code you posted seems to have few indentation errors, generally you can format it enclosing it between python and

Thank you for the quick response @babaMar I have updated my comment with proper formatting and the error message. Appreciate your help!

Guess you have installed with pip? Because the get_user_info(...) has been recently added and the PyPi release does not include that. If that's the case cloning the project would solve the issue

Thank you. Yes, I did install the package using pip. Will try to clone the project and update the outcome.

Also, I assume that you are using this plugin in your app. How are you maintaining the session? Are you string the access_token on client side and passing it every time or using any cookie mechanism?

babaMar commented 4 years ago

Which session specifically?

We use it by instantiating it aws_auth = AWSCognitoAuthentication() and then decorating the endpoints with @aws_auth.authentication_required. After that, cognito_claims are made available in the Flask request context g by g.cognito_claims

babaMar commented 4 years ago

@devireds let me know if I can close the issue

devireds commented 4 years ago

Uninstalling the pip package and cloning the project did the thing. Thanks for the quick pointer.

My application architecture:

Application

Once we get the the access_token from Cognito, how do we store the access-token so that the user can send the token with in the subsequent requests? Thank you!

Which session specifically?

We use it by instantiating it aws_auth = AWSCognitoAuthentication() and then decorating the endpoints with @aws_auth.authentication_required. After that, cognito_claims are made available in the Flask request context g by g.cognito_claims