Skip to content

Instantly share code, notes, and snippets.

@Can-Sahin
Last active February 14, 2023 21:22
Show Gist options
  • Save Can-Sahin/377fee56a53a0a31bf9e5ce7f9847ac2 to your computer and use it in GitHub Desktop.
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.
#!/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
@cmllamosas
Copy link

cmllamosas commented Oct 13, 2020

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment