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
+6.5k Get Facebook friends working in same company
+7.1k Javascript : Push notifications to browser with Push.js
+10.1k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+6.3k Golang : Find the longest line of text example
+12.1k Golang : "https://" not allowed in import path
+16.2k Golang : Merge video(OpenCV) and audio(PortAudio) into a mp4 file
+10.8k Android Studio : Create custom icons for your application example
+7.4k Golang : Test if an input is an Armstrong number example
+35.3k Golang : Get file last modified date and time
+5.4k Unix/Linux : Get reboot history or check when was the last reboot date
+8k Golang: Prevent over writing file with md5 hash
+11.1k Golang : Generate DSA private, public key and PEM files example