Golang : How to implement two-factor authentication?
Nowadays, having a password no longer protects your account or website been hacked. This is because password is a "something you know" and it can be shared, leaked and reuse by someone else.
To improve online security, the 2FA or better known as two-factor authentication adoption is growing worldwide because 2FA has the additional benefit of "something you have" -- such as a token( a string) -- which is unique to the person holding the token generator.
Cyber criminals(or some state sponsored crackers) will have harder time to access your account or server even with plaintext password...when the password is used together with a one time token generated by app such as Authy (https://www.authy.com/) or Google Authentication App.
To implement 2FA on your website. Basically the steps are:
- Create a button to let your user to enable 2FA on their own account.
- Generate a QR code that compliant to Authy or Google Authentication.
- Show the QR code and have an input prompt for the token.
- Have the user scan the QR code and enter the one time unique token for verification.
- Once the user is authenticated, mark the user's account as 2FA enabled in the database and memory.
- For best result, logout the user and force the user to login again with 2FA.
For step 2, see this tutorial on how to generate QR codes that are compliant with Google Authenticator App.
For step 3 to 4 :
https://www.socketloop.com/tutorials/golang-verify-token-from-google-authenticator-app
Anything that I've missed out or suggestion to add? Leave your comment below.
References :
https://github.com/google/google-authenticator/wiki/Key-Uri-Format
https://github.com/dgryski/dgoogauth
http://blog.gopheracademy.com/advent-2013/day-21-two-factor-auth/
See also : Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+3.2k Apt-get to install and uninstall Golang
+12.7k Golang : How to count the number of repeated characters in a string?
+9.3k Golang : Convert date string to variants of time.Time type examples
+6.3k Golang : Exit, terminating or aborting a program
+1k Golang : Calculate a pip value and distance to target profit example
+4.5k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example
+25.5k Golang : How to get HTTP request header information?
+11.1k Golang :Trim white spaces from a string
+2.6k Unix/Linux : secure copying between servers with SCP command examples
+9k Golang : read gzipped http response
+9.9k Golang : Strings comparison
+10.9k Golang : Logging with logrus