Writing a TOTP client in Go
A TOTP1 based 2FA system has two parts. One is a client that generates the TOTP code. The
other part is a server. The server verifies the code. If the client and the server-generated
codes match, the server allows the inbound user to access the target system. The code
usually expires after 30 seconds and then, you’ll have to regenerate it to be able to
authenticate.
As per RFC-62382, the server shares a base-32 encoded secret key with the client. Using
this shared secret and the current UNIX timestamp, the client generates a 6-digit code.
Independently, the server also generates a 6-digit code using the same secret string and its
own current timestamp. If the user-entered client code matches the server-generated code,
the auth succeeds. Otherwise, it fails. The client’s and the server’s current timestamp
wouldn’t be an exact match. So the algorithm usually adjusts it for ~30 seconds duration.