otp_golang

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 15, 2022 License: MIT Imports: 10 Imported by: 4

README

otp-golang

This is an OTP Auth package for GOLang.

Screen Shot 2022-11-08 at 22 44 13

⭐️ This package is still in development. I welcome suggestions for changes that will bring it closer to compliance without overly complicating the code, or useful test cases to add to the test suite.

⚡️ Quick Start

go get -u github.com/yahya077/otp-golang

📖 Prebuild Handlers

Note: There is an available documentation with Postman API Documentation

Title Route Method Handler Middleware Customizable Description
OTP /auth/otp POST OtpHandler none Sends OTP to user
Login /auth/login POST LoginHandler none Returns Bearer token
Register /auth/register POST RegisterHandler AuthMiddleware Registers user by User Repository

💡 Usage

Note: Example usage on otp-golang-example

// create instance off Auth
authApp := auth.New(app, database.Postgresql.DB, auth.Config{})

// set sms provider for sending otp code to phone number
authApp.SetSmsProvider(smsProvider.MockedSmsSenderProviderClient{})

// set your custom user model as a subject for verify, insert and register
authApp.SetUserRepository(repository.UserRepository{})

// finally initialize the auth app
authApp.Initialize()

// You can use middleware as shown below
app.Get("/test-auth", authApp.Config.AuthMiddleware, func(ctx *fiber.Ctx) error {
		return ctx.JSON("authorized")
})
  

🧬 Auth Middleware

This middleware will be ready after initialization of Auth. You can override Middleware if you want to

Middleware Description
authApp.Config.AuthMiddleware Looks for Bearer token

📫  Have a question? Want to chat? Ran into a problem?

Website yahyahindioglu.com
LinkedIn Account

Documentation

Index

Constants

View Source
const (
	PathAuth     = "/auth"
	PathRegister = "/register"
	PathLogin    = "/login"
	PathOtp      = "/otp"
)

Variables

This section is empty.

Functions

func AuthMiddleware added in v1.0.2

func AuthMiddleware(c *fiber.Ctx) error

Types

type Auth

type Auth struct {
	App    *fiber.App
	DB     *gorm.DB
	Config Config
}

func New

func New(app *fiber.App, db *gorm.DB, config Config) *Auth

func (*Auth) GetLoginPath

func (a *Auth) GetLoginPath() string

func (*Auth) GetOtpPath

func (a *Auth) GetOtpPath() string

func (*Auth) GetRegisterPath

func (a *Auth) GetRegisterPath() string

func (*Auth) Initialize

func (a *Auth) Initialize()

func (*Auth) SetOtpSender

func (a *Auth) SetOtpSender()

func (*Auth) SetRoutes

func (a *Auth) SetRoutes()

func (*Auth) SetSmsProvider

func (a *Auth) SetSmsProvider(provider ISmsProvider)

func (*Auth) SetUserRepository

func (a *Auth) SetUserRepository(subject IUserRepository)

type AuthMiddlewareHandler

type AuthMiddlewareHandler struct {
	Header HeaderBearer
	Token  *jwt.Token
	Claims jwt.MapClaims
}

func (*AuthMiddlewareHandler) GetMappedClaims

func (a *AuthMiddlewareHandler) GetMappedClaims() (jwt.MapClaims, bool)

func (*AuthMiddlewareHandler) GetTokenString

func (a *AuthMiddlewareHandler) GetTokenString() string

func (*AuthMiddlewareHandler) HasBearer

func (a *AuthMiddlewareHandler) HasBearer() bool

func (*AuthMiddlewareHandler) ParseToken

func (a *AuthMiddlewareHandler) ParseToken() (*jwt.Token, error)

type Config

type Config struct {
	OtpHandler        fiber.Handler
	LoginHandler      fiber.Handler
	RegisterHandler   fiber.Handler
	AuthMiddleware    func(c *fiber.Ctx) error
	SmsProvider       ISmsProvider
	UserRepository    IUserRepository
	OtpCodeRepository OtpCodeRepository
	SendOtp           func(phone string, code string) error
}

type GormRepository

type GormRepository struct {
	DB *gorm.DB
}

type HeaderBearer

type HeaderBearer struct {
	Authorization string `reqHeader:"Authorization"`
}

type ISmsProvider

type ISmsProvider interface {
	SendOtp(phone string, code string) error
}

type IUserRepository

type IUserRepository interface {
	Register(parser func(interface{}) error) error
	Registered(phone string) bool
}

type OtpBaseUserModel

type OtpBaseUserModel struct {
	Phone string `json:"phone"`
}

type OtpCheckerResponse

type OtpCheckerResponse struct {
	Authenticated bool
	Registered    bool
	Phone         string
	Expiration    time.Time
}

type OtpCode

type OtpCode struct {
	ID        uint   `gorm:"primarykey"`
	Phone     string `json:"phone"`
	Code      string `json:"code"`
	ExpiredAt time.Time
	CreatedAt time.Time
	UpdatedAt time.Time
}

func (OtpCode) IsExpired

func (o OtpCode) IsExpired() bool

type OtpCodeRepository

type OtpCodeRepository struct {
	GormRepository
	OtpCode OtpCode
}

func (OtpCodeRepository) Insert

func (repository OtpCodeRepository) Insert(phone string, code string, expiredAt time.Time)

func (OtpCodeRepository) Validate

func (repository OtpCodeRepository) Validate(phone string, otp string) (OtpCode, error)

type OtpModel

type OtpModel struct {
	Phone string `json:"phone"`
}

type Router

type Router struct {
	LoginPrefix    string
	RegisterPrefix string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL