Skip to content

Instantly share code, notes, and snippets.

@jona7o
Created February 22, 2019 18:10
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jona7o/02b55f684216b95038dfd54ca48d23a2 to your computer and use it in GitHub Desktop.
Save jona7o/02b55f684216b95038dfd54ca48d23a2 to your computer and use it in GitHub Desktop.
Bash script for the following cognito workflow: Sign Up User with Username and Mail --> Confirm User --> Login User --> Print Access Token
#!/bin/bash
debug=0
if [[ $# -eq 0 ]] ; then
echo 'usage: initCognitoUser [parameters]'
echo 'paramters:'
echo '-c --clientId: Cognito ClientId'
echo '-i --poolId: Cognito UserPoolId'
echo '-u --username: Username of new User'
echo '-p --password: Password of new User'
echo '-e --email: Email of new User'
echo '-r --region: Cogntio AWS Region'
echo '-d --debug: Enable Debug Mode'
echo 'example: ./initCognitoUser.sh --email your@mail.com --username testuser --password test123456 --clientId xyz --poolId eu-central-1_AABBCC --region eu-central-1'
exit 1
fi
while [[ "$#" > 0 ]]; do case $1 in
-c|--clientId) clientId="$2"; shift;;
-i|--poolId) userpool="$2"; shift;;
-u|--username) username="$2"; shift;;
-e|--email) email="$2"; shift;;
-p|--password) password="$2"; shift;;
-r|--region) region="$2"; shift;;
-d|--debug) debug=1;;
*) echo "Unknown parameter passed: $1"; exit 1;;
esac; shift; done
GREEN='\033[0;32m'
NC='\033[0m'
aws configure set region $region
if [ $debug -eq 1 ]; then
echo -e "[${GREEN} DEBUG MODE IS ON ${NC}]"
echo -e "KUBECONFIG is $KUBECONFIG"
echo -e "AWS Caller Identity: "
aws sts get-caller-identity
echo -e "AWS Region:"
aws configure get region
fi
if [ $debug -eq 1 ]; then
echo -e "[${GREEN} CREATING USER ${NC}]"
echo "[invoke] aws cognito-idp sign-up --client-id $clientId --username $username --password $password --user-attributes Name="email",Value="$email""
aws cognito-idp sign-up --client-id $clientId --username $username --password $password --user-attributes Name="email",Value="$email"
else
aws cognito-idp sign-up --client-id $clientId --username $username --password $password --user-attributes Name="email",Value="$email" > /dev/null 2>&1
fi
if [ $debug -eq 1 ]; then
echo -e "[${GREEN} CONFIRMING USER ${NC}]"
echo "[invoke] aws cognito-idp admin-confirm-sign-up --username $username --user-pool-id $userpool"
aws cognito-idp admin-confirm-sign-up --username $username --user-pool-id $userpool
else
aws cognito-idp admin-confirm-sign-up --username $username --user-pool-id $userpool > /dev/null 2>&1
fi
if [ $debug -eq 1 ]; then
echo -e "[${GREEN} LOGIN USER ${NC}]"
echo "aws cognito-idp admin-initiate-auth --user-pool-id $userpool --client-id $clientId --auth-flow ADMIN_NO_SRP_AUTH --auth-parameters USERNAME=$username,PASSWORD=$password"
fi
token=$(aws cognito-idp admin-initiate-auth --user-pool-id $userpool --client-id $clientId --auth-flow ADMIN_NO_SRP_AUTH --auth-parameters USERNAME=$username,PASSWORD=$password | jq -r '.AuthenticationResult.AccessToken')
echo $token
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment