gootp
is a Go package that provides an interface for One-Time Password (OTP) operations. It includes methods to validate an OTP code, retrieve the raw setup key, and generate a QR code image for the setup key. This package is particularly useful for implementing two-factor authentication (2FA) in your Go applications.
To install the package, run:
go get github.com/mekramy/gootp
To create a new Google OTP instance, use the NewGoogleOTP
function. This function requires an issuer, a username, and user keys list (id, username, etc...).
package main
import (
"fmt"
"gootp"
)
func main() {
otp := gootp.NewGoogleOTP("MyIssuer", "MyUsername", "fasdf-32123-3123", "MyUsername")
fmt.Println("OTP instance created")
}
The OTP
interface provides the following methods:
Validates the provided OTP code. Returns true
if the code is valid, otherwise false
. An error is returned if the validation process fails.
valid, err := otp.Validate("123456")
if err != nil {
fmt.Println("Error validating OTP:", err)
} else if valid {
fmt.Println("OTP is valid")
} else {
fmt.Println("OTP is invalid")
}
Retrieves the raw setup key. Returns the raw setup key as a string. An error is returned if the retrieval process fails.
rawKey, err := otp.RAW()
if err != nil {
fmt.Println("Error retrieving raw key:", err)
} else {
fmt.Println("Raw setup key:", rawKey)
}
Generates a QR code image for the setup key. Returns the QR code image as a byte slice. An error is returned if the generation process fails.
qrCode, err := otp.QR()
if err != nil {
fmt.Println("Error generating QR code:", err)
} else {
fmt.Println("QR code generated")
// Save or display the QR code image
}