Help us shape the future of Go! Take the 2026 Go Developer Survey

auth

package module
v0.0.0-...-a9555f8 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2017 License: BSD-2-Clause Imports: 7 Imported by: 0

README

go-auth GoDoc Build Status

Authentication Module for Go and Gin Web Framework

Documentation

Index

Constants

View Source
const (
	// StatusActive represent user with active status
	StatusActive = true
	// StatusInactive represent user with inactive status
	StatusInactive = false
)
View Source
const AUTH_KEY = "auth"
View Source
const (
	// USER as global key identifier when storing data into session
	USER = "user"
)

Variables

View Source
var (
	// ErrAlreadyActived displaying error messeage when user has already activated
	ErrAlreadyActived = errors.New("user already actived")
	// ErrAlreadyInactived displaying error messeage when user has already Inactivated
	ErrAlreadyInactived = errors.New("user already inactived")
	// ErrInactive displaying error message when user has inactive status
	ErrInactive = errors.New("inactive user")
	// ErrMissmatch displaying error message when user failed to be authenticated
	ErrMissmatch = errors.New("missmatch username / password combination")
	// ErrNotFound displaying error message when user has not been found
	ErrNotFound = errors.New("user not found")
	// ErrUserDuplicate displaying error message when the same user has been registered
	ErrUserDuplicate = errors.New("user duplicate")
)

Functions

func AuthHandler

func AuthHandler(mod Module) gin.HandlerFunc

AuthHandler store session into module

func UserRestricted

func UserRestricted() gin.HandlerFunc

UserRestricted is a middleware to restrict some route for authenticated user only

Types

type Module

type Module struct {
	// contains filtered or unexported fields
}

Module for Authentication

func New

func New(repo Repository) Module

New create new Authentication Module

func (*Module) ActivateUser

func (mod *Module) ActivateUser(user UserModel) (UserModel, error)

ActivateUser set inactive user as active

func (*Module) Authenticate

func (mod *Module) Authenticate(user UserModel, password string) (UserModel, error)

Authenticate verify username and password is match with bcrypt Encryption

func (*Module) CreateNewUser

func (mod *Module) CreateNewUser(user UserModel) (UserModel, error)

CreateNewUser storing new user data into repository

func (*Module) DeactivatedUser

func (mod *Module) DeactivatedUser(user UserModel) (UserModel, error)

DeactivateUser set inactive user as Deactived

func (*Module) Deauthenticate

func (mod *Module) Deauthenticate() error

Deauthenticate delete user from current session

func (*Module) IsAuthenticated

func (mod *Module) IsAuthenticated() bool

IsAuthenticated helper to check whether current session has been authenticated or not

func (*Module) UpdateSession

func (mod *Module) UpdateSession(sess sessions.Session)

UpdateSession replacing current sessions.Session in the module

func (*Module) User

func (mod *Module) User() (User, error)

User returning user data from session

type Repository

type Repository interface {
	Find(uint64) (UserModel, error)
	FindByUsername(string) (UserModel, error)
	FindAll() ([]UserModel, error)
	Store(UserModel) (UserModel, error)
	Update(UserModel) (UserModel, error)
	Remove(uint64) error
}

Repository talk to database

type User

type User struct {
	UserID              uint64
	Username            string
	FirstName, LastName *string
}

User data to store in session

type UserModel

type UserModel interface {
	SetID(uint64)
	SetIsActive(bool)
	SetCreatedTime(time.Time)
	GetID() uint64
	GetFirstName() *string
	GetLastName() *string
	GetUsername() string
	GetPassword() []byte
	GetIsActive() bool
	TableName() string
}

UserModel data

Jump to

Keyboard shortcuts

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