G - Otp is a Golang client for Google Authenticator.
Google Authenticator uses a HMAC-based One-time Password Algorithm (HOTP) to generate a one-time password. The algorithm is described in RFC 4226. The algorithm uses a shared secret key and a counter value to generate a one-time password. The counter value is incremented after each password generation. The counter value is usually the current time in seconds divided by a time step. The time step is usually 30 seconds. The shared secret key is usually a random string of bytes. The shared secret key is usually stored in the user's mobile device. The one-time password is usually displayed to the user as a 6-digit number.
First, you must create a secret key for the user. (Fear not, our gotp package will do it; just run it.)
Then you have to generate a qr code with secret key and some customizable data. (gotp will do that too, just run it.)
Then show the qr to the user and tell him to scan it from the Google Authenticator app.
Then wait for the user to enter a code.
When the user enters a code, tell gotp to verify it and give him the user's secret.
This will give you a boolean value. Then you can run the logic you want.
go get github.com/9ssi7/gotp
Contributions are always welcome!
Variables |
---|
gotp.Digits |
gotp.Algorithm |
gotp.SecretSize |
gotp.Period |
gotp.ValidChars |
Functions |
---|
gotp.CreateSecret() |
gotp.GetCode(..., ...) |
gotp.GetTime() |
gotp.Verify(...) |
gotp.GetQRCode(...) |
Variables have parameters that are used when generating, decoding, and validating code. You can customize the variables.
Digits is the number of digits in the code. It must be between 6 and 8. The default is 6.
import "github.com/9ssi7/gotp"
gotp.Digits = gotp.DigitsEight
Algorithm is the algorithm used to generate the code. The default is SHA1.
import "github.com/9ssi7/gotp"
gotp.Algorithm = gotp.AlgorithmSHA256
gotp.Algorithm = gotp.AlgorithmSHA512
SecretSize is the size of the secret key in bytes. The default is 10.
import "github.com/9ssi7/gotp"
gotp.SecretSize = 20
Period is the time step in seconds. The default is 30.
import "github.com/9ssi7/gotp"
gotp.Period = 60
ValidChars is the set of characters that are used to generate the secret key. The default is "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567".
import "github.com/9ssi7/gotp"
gotp.ValidChars = "0123456789"
Functions are used to generate, decode, and validate code.
CreateSecret creates a secret key. The secret key is a random string of bytes. The secret key is usually stored in the user's mobile device.
import "github.com/9ssi7/gotp"
secret := gotp.CreateSecret()
GetCode generates a one-time password. The one-time password is usually displayed to the user as a 6-digit number.
import "github.com/9ssi7/gotp"
code := gotp.GetCode("secret", 0)
GetTime returns the current time in seconds.
import "github.com/9ssi7/gotp"
time := gotp.GetTime()
Verify verifies a one-time password. The one-time password is usually entered by the user.
import "github.com/9ssi7/gotp"
valid := gotp.Verify(gotp.VerifyConfig{
Secret: "secret",
Code: "123456",
})
GetQRCode generates a qr code with secret key and some customizable data.
import "github.com/9ssi7/gotp"
qr := gotp.GetQRCode(gotp.QrConfig{
Secret: "secret",
Issuer: "Your Company",
AccountName: "User Name / Email",
})
</docgen-api>