Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ichtrojan/gotp
Golang OTP package
https://github.com/ichtrojan/gotp
go golang otp redis
Last synced: about 1 month ago
JSON representation
Golang OTP package
- Host: GitHub
- URL: https://github.com/ichtrojan/gotp
- Owner: ichtrojan
- License: mit
- Created: 2024-10-05T18:46:50.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2024-10-06T21:52:59.000Z (3 months ago)
- Last Synced: 2024-11-11T01:32:12.094Z (about 2 months ago)
- Topics: go, golang, otp, redis
- Language: Go
- Homepage:
- Size: 9.77 KB
- Stars: 10
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GOTP
GOTP is a simple and efficient Go package for generating and verifying one-time passwords (OTPs) using Redis for storage. It provides support for different formats of tokens including alphabetic, alphanumeric, and numeric tokens.
>**NOTE**
> * This package is named after the GOAT of Formula one, none other than [Goatifi](https://en.wikipedia.org/wiki/Nicholas_Latifi) himself
> * Not named after Go + OTP 🤣## Features
* Generate one-time passwords with specified length and format.
* Store tokens in Redis with an expiration time.
* Verify tokens against stored values in Redis.
* Automatic token deletion upon successful verification.## Installation
To install the GOTP package, use the following command:
```bash
go get github.com/ichtrojan/gotp
```## Prerequisites
* Go 1.16 or later.
* A running instance of Redis.## Configuration
You need to create a configuration that includes a Redis client before using the package.
## Example Configuration
```go
package mainimport (
"fmt"
"log"
"time"
"github.com/go-redis/redis/v9"
"github.com/ichtrojan/gotp"
)func main() {
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})otp, err := gotp.New(gotp.Config{Redis: rdb})
if err != nil {
log.Fatalf("Failed to connect to Redis: %v", err)
}// Continue with token generation and verification...
}
```## Usage
### Generating a Token
You can generate a token by creating a Generate payload and calling the Generate method on your Config instance.
```go
package mainimport (
"fmt"
"log"
"time"
"github.com/go-redis/redis/v9"
"github.com/ichtrojan/gotp"
)func main() {
// (Assuming Redis client setup as above)payload := gotp.Generate{
Format: gotp.ALPHA, // or gotp.ALPHA_NUMERIC, gotp.NUMERIC
Length: 6,
Identifier: "testIdentifier",
Expires: 10 * time.Minute,
}token, err := otp.Generate(payload)
if err != nil {
log.Fatalf("Error generating token: %v", err)
}fmt.Printf("Generated Token: %s\n", token)
}
```### Verifying a Token
To verify a token, create a Verify payload and call the Verify method.
```go
payload := gotp.Verify{
Token: token, // The token you want to verify
Identifier: "testIdentifier",
}valid, err := otp.Verify(payload)
if err != nil {
log.Fatalf("Error verifying token: %v", err)
}if valid {
fmt.Println("Token is valid!")
} else {
fmt.Println("Token is invalid or expired.")
}
```## Token Formats
The Generate struct has a Format field which can take the following values:
* `ALPHA`: Generates a token with alphabetic characters only.
* `ALPHA_NUMERIC`: Generates a token with both alphabetic and numeric characters.
* `NUMERIC`: Generates a token with numeric characters only.## License
This package is licensed under the MIT License. See the LICENSE file for details.