Last active
August 26, 2021 14:59
-
-
Save clucle/05f493b7ba7f432ef01bdfd5c6f0c34b to your computer and use it in GitHub Desktop.
boto3 auth flow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from flask import Flask, render_template, redirect, url_for, request | |
import boto3 | |
from botocore.exceptions import ClientError | |
app = Flask(__name__) | |
APP_CLIENT_ID = os.getenv('COGNITO_CLIENT_ID') | |
@app.route('/') | |
def lobby(): | |
return render_template('index.html') | |
@app.route('/auth/signup/', methods=['POST']) | |
def signup(): | |
user_email = request.form['Email'] | |
user_password = request.form['Password'] | |
user_name = request.form['Username'] | |
idp_client = boto3.client('cognito-idp') | |
try: | |
idp_client.sign_up(ClientId=APP_CLIENT_ID, | |
Username=user_email, | |
Password=user_password, | |
UserAttributes=[{'Name': 'name', 'Value': user_name}]) | |
except ClientError as e: | |
if e.response['Error']['Code'] == 'UsernameExistsException': | |
# Todo Handle Already Exists Email | |
print("User already exists") | |
if e.response['Error']['Code'] == 'ParamValidationError': | |
# Todo Handle Param Validate | |
print("Param Validate Error") | |
print(e) | |
return redirect(url_for('lobby')) | |
@app.route('/auth/resend/confirm/', methods=['POST']) | |
def resend_confirm(): | |
user_email = request.form['Email'] | |
idp_client = boto3.client('cognito-idp') | |
try: | |
idp_client.resend_confirmation_code(ClientId=APP_CLIENT_ID, | |
Username=user_email) | |
except ClientError as e: | |
if e.response['Error']['Code'] == 'UserNotFoundException': | |
# Todo Handle Not Found User | |
print("Can't Find user by Email") | |
if e.response['Error']['Code'] == 'ParamValidationError': | |
# Todo Handle Param Validate | |
print("Param Validate Error") | |
print(e) | |
return redirect(url_for('lobby')) | |
@app.route('/auth/confirm/signup/', methods=['POST']) | |
def confirm_sign_up(): | |
user_email = request.form['Email'] | |
confirm_code = request.form['ConfirmCode'] | |
idp_client = boto3.client('cognito-idp') | |
try: | |
idp_client.confirm_sign_up(ClientId=APP_CLIENT_ID, | |
Username=user_email, | |
ConfirmationCode=confirm_code) | |
except ClientError as e: | |
if e.response['Error']['Code'] == 'UserNotFoundException': | |
# Todo Handle Not Found User | |
print("Can't Find user by Email") | |
if e.response['Error']['Code'] == 'CodeMismatchException': | |
# Todo Handle Code Mismatch | |
print("User Code Mismatch") | |
if e.response['Error']['Code'] == 'ParamValidationError': | |
# Todo Handle Param Validate | |
print("Param Validate Error") | |
if e.response['Error']['Code'] == 'ExpiredCodeException': | |
# Todo Handle Expired Code | |
print("Expired Code") | |
print(e) | |
return redirect(url_for('lobby')) | |
@app.route('/auth/forgot/password/', methods=['POST']) | |
def forgot_password(): | |
user_email = request.form['Email'] | |
idp_client = boto3.client('cognito-idp') | |
try: | |
idp_client.forgot_password(ClientId=APP_CLIENT_ID, | |
Username=user_email) | |
except ClientError as e: | |
if e.response['Error']['Code'] == 'UserNotFoundException': | |
# Todo Handle Not Found User | |
print("Can't Find user by Email") | |
if e.response['Error']['Code'] == 'ParamValidationError': | |
# Todo Handle Param Validate | |
print("Param Validate Error") | |
print(e) | |
return redirect(url_for('lobby')) | |
@app.route('/auth/confirm/forgot/password/', methods=['POST']) | |
def confirm_forgot_password(): | |
user_email = request.form['Email'] | |
confirm_code = request.form['ConfirmCode'] | |
random_password = '!1a+random' | |
idp_client = boto3.client('cognito-idp') | |
try: | |
idp_client.confirm_forgot_password(ClientId=APP_CLIENT_ID, | |
Username=user_email, | |
ConfirmationCode=confirm_code, | |
Password=random_password) | |
except ClientError as e: | |
if e.response['Error']['Code'] == 'UserNotFoundException': | |
# Todo Handle Not Found User | |
print("Can't Find user by Email") | |
if e.response['Error']['Code'] == 'CodeMismatchException': | |
# Todo Handle Code Mismatch | |
print("User Code Mismatch") | |
if e.response['Error']['Code'] == 'ParamValidationError': | |
# Todo Handle Param Validate | |
print("Param Validate Error") | |
if e.response['Error']['Code'] == 'ExpiredCodeException': | |
# Todo Handle Expired Code | |
print("Expired Code") | |
return redirect(url_for('lobby')) | |
if __name__ == '__main__': | |
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<html> | |
<body> | |
Cognito Test | |
<form style="border: 1px solid black; padding: 10px;" action="/auth/signup/" method="post"> | |
<p> Email: <input type="email" name="Email" /></p> | |
<p> Username: <input type="text" name="Username" /></p> | |
<p> Password: <input type="password" name="Password" /></p> | |
<button type="submit">signup</button> | |
</form> | |
<form style="border: 1px solid black; padding: 10px;" action="/auth/resend/confirm/" method="post"> | |
<p> Email: <input type="email" name="Email" /></p> | |
<button type="submit">resend confirm</button> | |
</form> | |
<form style="border: 1px solid black; padding: 10px;" action="/auth/confirm/signup/" method="post"> | |
<p> Email: <input type="email" name="Email" /></p> | |
<p> Confirm Code: <input type="number" name="ConfirmCode" /></p> | |
<button type="submit">confirm signup</button> | |
</form> | |
<form style="border: 1px solid black; padding: 10px;" action="/auth/forgot/password/" method="post"> | |
<p> Email: <input type="email" name="Email" /></p> | |
<button type="submit">forgot password</button> | |
</form> | |
<form style="border: 1px solid black; padding: 10px;" action="/auth/confirm/forgot/password/" method="post"> | |
<p> Email: <input type="email" name="Email" /></p> | |
<p> Confirm Code: <input type="number" name="ConfirmCode" /></p> | |
<button type="submit">confirm forgot password</button> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment