Replace GitHub personal access token with a simple password

GitHub has announced plans to disable account passwords when authenticating Git operations such as git clone and git push. Beginning August 13, 2021, GitHub will require token-based authentication. The following script aims to restore the password authentication functionality (though it requires a one-time setup) for users who want to operate business as usual:

Outline

Encoding your GitHub token

Encode your personal access token by running the following script. The script depends on the OpenSSL package which you may need to install through apt or other package managers.

encode.sh

#!/usr/bin/env bash
echo -n "Token: "
read -s token
echo
echo -n "Enter password: "
read -s password1
echo
echo -n "Retype password: "
read -s password2
echo
if [[ "$password1" == "$password2" ]]; then
  echo -n "Encrypted token: "
  echo $token | openssl aes-256-cbc -pbkdf2 -a -salt -pass pass:$password1
else
  echo "Password does not match!"
fi

Make sure that your token has been properly encoded by checking whether decoding the encrypted token recovers the original token with the following script:

decode.sh

#!/usr/bin/env bash
echo -n "Encrypted token: "
read secret
echo -n "Password: "
read -s password
echo
echo -n "Token: "
echo $secret | openssl aes-256-cbc -pbkdf2 -d -a -pass pass:$password

Setup script

Now you can replace your encryted token in the following script and run the script. The script automatically sets up environment variables GIT_ASKPASS and GIT_TOKEN for the current terminal session. You will be prompted to enter the password you used to encode your token. Then you can run GitHub operations from the terminal as usual!

github-setup.sh

#!/usr/bin/env bash
secret=<your encryted github token goes here>
echo -n "Password: "
read -s password
echo
token=`echo $secret | openssl aes-256-cbc -pbkdf2 -d -a -pass pass:$password`
echo 'echo $GIT_TOKEN' > $HOME/.git-askpass
chmod +x $HOME/.git-askpass
export GIT_TOKEN=$token
export GIT_ASKPASS=$HOME/.git-askpass

Create a bash alias

For easy access, assign a shortcut command for the github-setup.sh script with bash alias. Look for the .bash_aliases file in your home directory. If it doesn’t exist, make a empty file. Append the following line to the file:

.bash_aliases

alias gitoken='source /<base directory to the script>/github-setup.sh'

Now you can enter the command gitoken from anywhere in the terminal to run the script. You can replace the alias gitoken with an alias of your liking.