https://github.com/tsirysndr/2fa-auth
Two-factor authentication for Go. One-time passcode generator with support for Google Authenticator.
https://github.com/tsirysndr/2fa-auth
go totp twofactor
Last synced: about 2 months ago
JSON representation
Two-factor authentication for Go. One-time passcode generator with support for Google Authenticator.
- Host: GitHub
- URL: https://github.com/tsirysndr/2fa-auth
- Owner: tsirysndr
- License: bsd-3-clause
- Created: 2020-05-15T05:58:48.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-05-15T14:51:51.000Z (about 5 years ago)
- Last Synced: 2025-03-25T18:40:22.878Z (2 months ago)
- Topics: go, totp, twofactor
- Language: Go
- Size: 15.6 KB
- Stars: 7
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
2fa-auth
> 2fa-auth is a one-time passcode (time-based) generator, ideal for use in two-factor authentication, that supports Google Authenticator and other two-factor devices.
## Install
```sh
go get github.com/tsirysndr/2fa-auth
```## Usage
Let's say you have a user that wants to enable two-factor authentication, and you intend to do two-factor authentication using an app like Google Authenticator, Duo Security, Authy, etc. This is a three-step process:
1. Generate a secret
2. Show a QR code for the user to scan in
3. Authenticate the token for the first time### Generating a key
This will generate a secret key of length 16, which will be the secret key for the user.
```go
import (
fmt
auth "github.com/tsirysndr/2fa-auth"
)...
g, _ := auth.GenerateSecret(&auth.Options{
Length: 16,
Symbols: true,
OtpauthURL: true,
Name: name,
Issuer: issuer,
})
secret, _ := json.Marshal(g)
fmt.Println(string(secret))
```### Verifying the token
After the user scans the QR code, ask the user to enter in the token that they see in their app. Then, verify it against the secret.
```go
import (
fmt
auth "github.com/tsirysndr/2fa-auth"
)...
fmt.Println(auth.VerifyOTP(secret, code))
```## Author
👤 **Tsiry Sandratraina**
* Website: https://tsiry-sandratraina.netlify.com
* Github: [@tsirysndr](https://github.com/tsirysndr)## Show your support
Give a ⭐️ if this project helped you!