Skip to content

Instantly share code, notes, and snippets.

@devodla
Forked from Can-Sahin/cognito-idtoken-cli.sh
Created May 26, 2022 22:21
Show Gist options
  • Select an option

  • Save devodla/65d824115e2b6cbfc239104b7d865a47 to your computer and use it in GitHub Desktop.

Select an option

Save devodla/65d824115e2b6cbfc239104b7d865a47 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment