Skip to content

Instantly share code, notes, and snippets.

@Weiyuan-Lane
Last active March 23, 2023 04:54
Show Gist options
  • Save Weiyuan-Lane/cbbc8675599d8577409086c3154b57c4 to your computer and use it in GitHub Desktop.
Save Weiyuan-Lane/cbbc8675599d8577409086c3154b57c4 to your computer and use it in GitHub Desktop.
Authentication steps for MFA using AWS Cli
#!/bin/bash
# Copyright (c) 2020 by Liu Weiyuan
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Default filename values - change this or add as environment values, depending on your own needs
MFA_SERIAL_FILE=".mfaserial"
AWS_TOKEN_FILE=".awstoken"
# Validate that the configuration has been done before
# If not, prompt the user to run that first
if [ ! -e $TMP_DIR/$MFA_SERIAL_FILE ]; then
echo "Configuration is missing, please run the one-time configuration step first"
exit 0;
fi
# Retrieve the serial code
_MFA_SERIAL=`cat $TMP_DIR/$MFA_SERIAL_FILE`
# Function for prompting for MFA token code
promptForMFA(){
while true; do
read -p "Please input your 6 digit MFA token: " token
case $token in
[0-9][0-9][0-9][0-9][0-9][0-9] ) _MFA_TOKEN=$token; break;;
* ) echo "Please enter a valid 6 digit pin." ;;
esac
done
# Run the awscli command
_authenticationOutput=`aws sts get-session-token --serial-number ${_MFA_SERIAL} --token-code ${_MFA_TOKEN}`
# Save authentication to some file
echo $_authenticationOutput > $TMP_DIR/$AWS_TOKEN_FILE;
}
# If token is present, retrieve it from file
# Else invoke the prompt for mfa function
if [ -e $TMP_DIR/$AWS_TOKEN_FILE ]; then
_authenticationOutput=`cat $TMP_DIR/$AWS_TOKEN_FILE`
_authExpiration=`echo $_authenticationOutput | jq -r '.Credentials.Expiration'`
_nowTime=`date -u +'%Y-%m-%dT%H:%M:%SZ'`
# Retrieving is not sufficient, since we are not sure if this token has expired
# Check for the expiration value against the current time
# If expired, invoke the prompt for mfa function
if [ "$_authExpiration" \< "$_nowTime" ]; then
echo "Your last token has expired"
promptForMFA
fi
else
promptForMFA
fi
# "Return" the values to the calling script.
# There are a few ways to "return", for example writing to file
# Here, we assume that this script is "sourced" - see more on "sourcing" here: https://bash.cyberciti.biz/guide/Source_command
_AWS_ACCESS_KEY_ID=`echo ${_authenticationOutput} | jq -r '.Credentials.AccessKeyId'`
_AWS_SECRET_ACCESS_KEY=`echo ${_authenticationOutput} | jq -r '.Credentials.SecretAccessKey'`
_AWS_SESSION_TOKEN=`echo ${_authenticationOutput} | jq -r '.Credentials.SessionToken'`
@nk9
Copy link

nk9 commented Oct 14, 2021

What is the license for this code? Could you release it under MIT or BSD?

@Weiyuan-Lane
Copy link
Author

@nk9 Updated. Cheers

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