auth

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

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

Go to latest
Published: Jul 30, 2023 License: MIT Imports: 4 Imported by: 0

README

auth

Simple authentication and authorization for Go projects

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorUserNotFound    = fmt.Errorf("user not found")
	ErrorUserExists      = fmt.Errorf("user already exists")
	ErrorGroupNotFound   = fmt.Errorf("group not found")
	ErrorGroupExists     = fmt.Errorf("group already exists")
	ErrorRoleNotFound    = fmt.Errorf("role not found")
	ErrorRoleExists      = fmt.Errorf("role already exists")
	ErrorUnAuthorized    = fmt.Errorf("unauthorized")
	ErrorInvalidPassword = fmt.Errorf("invalid password")
)

declare errors

Functions

func HashPassword

func HashPassword(password string) (string, error)

HashPassword hashes a password using bcrypt

Types

type AuthStore

type AuthStore interface {
	FindUserByID(id int) (*User, error)
	FindUserByUsername(username string) (*User, error)
	FindUserByEmail(email string) (*User, error)
	CreateUser(user *User) error
	UpdateUser(user *User) error
	Authenticate(username, password string) (*User, error)
	Authorize(user *User, role string) bool
	AuthorizeGroup(user *User, group string) bool
}

type Group

type Group struct {
	ID          int
	Name        string
	Description string
	Roles       []Role
}

type InMemoryAuthStore

type InMemoryAuthStore struct {
	Users  map[int]*User
	Groups map[int]*Group
	Roles  map[int]*Role
}

InMemoryAuthStore provides an in-memory implementation of AuthStore

func NewInMemoryAuthStore

func NewInMemoryAuthStore() *InMemoryAuthStore

func NewInMemoryAuthStoreFromBackup

func NewInMemoryAuthStoreFromBackup(filename string) *InMemoryAuthStore

func (*InMemoryAuthStore) Authenticate

func (s *InMemoryAuthStore) Authenticate(username, password string) (*User, error)

Authenticate authenticates a user by their username and password

func (*InMemoryAuthStore) Authorize

func (s *InMemoryAuthStore) Authorize(user *User, role string) bool

func (*InMemoryAuthStore) AuthorizeGroup

func (s *InMemoryAuthStore) AuthorizeGroup(user *User, group string) bool

func (*InMemoryAuthStore) Backup

func (s *InMemoryAuthStore) Backup(filename string) error

Backup writes the in-memory data to a file

func (*InMemoryAuthStore) CreateGroup

func (s *InMemoryAuthStore) CreateGroup(group *Group) error

func (*InMemoryAuthStore) CreateRole

func (s *InMemoryAuthStore) CreateRole(role *Role) error

func (*InMemoryAuthStore) CreateUser

func (s *InMemoryAuthStore) CreateUser(user *User) error

func (*InMemoryAuthStore) FindUserByEmail

func (s *InMemoryAuthStore) FindUserByEmail(email string) (*User, error)

func (*InMemoryAuthStore) FindUserByID

func (s *InMemoryAuthStore) FindUserByID(id int) (*User, error)

func (*InMemoryAuthStore) FindUserByUsername

func (s *InMemoryAuthStore) FindUserByUsername(username string) (*User, error)

func (*InMemoryAuthStore) Restore

func (s *InMemoryAuthStore) Restore(filename string) error

Restore reads the in-memory data from a file

func (*InMemoryAuthStore) UpdateRole

func (s *InMemoryAuthStore) UpdateRole(role *Role) error

func (*InMemoryAuthStore) UpdateUser

func (s *InMemoryAuthStore) UpdateUser(user *User) error

type Role

type Role struct {
	ID          int
	Name        string
	Description string
}

type User

type User struct {
	ID       int
	Username string
	Password string
	Email    string
	Groups   []Group
	Roles    []Role
}

Jump to

Keyboard shortcuts

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