Skip to content

This repository features a straightforward implementation of OTP (One-Time Password) authentication using Go, the Fiber framework, and MongoDB. OTP adds an extra layer of security to user logins beyond traditional passwords.

Notifications You must be signed in to change notification settings

watchakorn-18k/go-otp-fiber

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go OTP Fiber Application

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.

Features

  • Generate a TOTP key for a user and return the QR code image.
  • Verify the TOTP code provided by the user.

Prerequisites

  • Go 1.22+
  • MongoDB
  • Fiber framework

Getting Started

Installation

  1. Clone the repository:

    git clone https://github.com/watchakorn-18k/go-otp-fiber
    cd go-otp-fiber
  2. Install dependencies:

    go mod tidy
  3. Set up your MongoDB database and update the connection string in the domain package.

Running the Application

  1. Start the application:

    go run main.go
  2. The server will start on port 3000.

API Endpoints

Generate TOTP Key

  • 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"
    }

Verify TOTP Code

  • 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"
      }

Dependencies

  • 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.

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

This repository features a straightforward implementation of OTP (One-Time Password) authentication using Go, the Fiber framework, and MongoDB. OTP adds an extra layer of security to user logins beyond traditional passwords.

Topics

Resources

Stars

Watchers

Forks

Languages