Last active
February 14, 2023 21:22
-
-
Save Can-Sahin/377fee56a53a0a31bf9e5ce7f9847ac2 to your computer and use it in GitHub Desktop.
Simple shell script to obtain a IdToken from Cognito User Pool. Just like logging in.
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
#!/bin/bash | |
username="COGNITO_USER_NAME" | |
password="PASSWORD" | |
clientid="APP_CLIENT_ID" | |
region="eu-west-1" | |
aws_profile="AWS_CLI_PROFILE" | |
response_json="" | |
json_field='IdToken' | |
token_request=`aws cognito-idp initiate-auth --auth-flow USER_PASSWORD_AUTH --output json --region $region --client-id $clientid --auth-parameters USERNAME=$username,PASSWORD=$password --profile $aws_profile` | |
# parse json | |
function jsonval { | |
temp=`echo $response_json | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w $json_field` | |
echo ${temp##*|} | |
} | |
response_json=$token_request | |
access_token=`jsonval` | |
result=$access_token | |
#Strip IdToken field and copy to clipboard | |
token="$(cut -d':' -f2 <<<"$result")" | |
echo $token | xargs | pbcopy | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Can-Sahin Thanks for the script, you can try this maybe save you some time in the future.
Install jq
https://stedolan.github.io/jq/download/
Then u can use it like this
idToken=`aws cognito-idp initiate-auth --auth-flow USER_PASSWORD_AUTH --output json --region $region --client-id $clientid --auth-parameters USERNAME=$username,PASSWORD=$password --profile $aws_profile | jq -r .AuthenticationResult.IdToken`
Greetings