This project is a simple OTP (One Time Password) generation and verification service built using the Go programming language and the Fiber web framework. It uses TOTP (Time-based One-Time Password) for generating and verifying OTPs.
- Generate a TOTP key for a user and return the QR code image.
- Verify the TOTP code provided by the user.
- Go 1.22+
- MongoDB
- Fiber framework
-
Clone the repository:
git clone https://github.com/watchakorn-18k/go-otp-fiber cd go-otp-fiber
-
Install dependencies:
go mod tidy
-
Set up your MongoDB database and update the connection string in the
domain
package.
-
Start the application:
go run main.go
-
The server will start on port
3000
.
-
Endpoint:
GET /api/otp/generate_link/:username
-
Description: Generates a TOTP key for the specified username and returns the secret, URL, and QR code image.
-
Parameters:
username
: The username for which to generate the TOTP key.
-
Response:
{ "secret": "generated-secret", "url": "otpauth-url", "qrcode": "base64-encoded-qrcode" }
-
Endpoint:
POST /api/otp/verify_otp
-
Description: Verifies the TOTP code provided by the user.
-
Request Body:
{ "username": "user123", "otp": "123456" }
-
Response:
-
Success:
{ "message": "valid OTP" }
-
Failure:
{ "message": "invalid OTP" }
-
- Fiber: An Express-inspired web framework for Go.
- MongoDB Go Driver: The official MongoDB driver for Go.
- pquerna/otp: A Go library for generating and verifying TOTP/HOTP codes.
This project is licensed under the MIT License. See the LICENSE file for details.