user

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2024 License: LGPL-3.0 Imports: 15 Imported by: 1

README

User management

This is an opinionated library to manage users stored using gorm database

Documentation

Index

Examples

Constants

View Source
const (
	SHA1Prefix   = "{SHA}"
	BCryp1PRefix = "$2$"
	BCryp2PRefix = "$2a$"
	BCryp3PRefix = "$2b$"
	BCryp4PRefix = "$2x$"
	BCryp5PRefix = "$2y$"
)
View Source
const (
	ExtYaml     = "YAML"
	ExtYml      = "YML"
	ExtJson     = "JSON"
	ExtHtpasswd = "htpasswd"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DbManager

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

DbManager is an opinionated user manager that stores the information on a gorm database

func NewDbManager

func NewDbManager(db *gorm.DB, opts ManagerOpts) (*DbManager, error)

NewDbManager creates an instance of user manager

func (DbManager) AllowLogin

func (mng DbManager) AllowLogin(user string, providedPass string) bool

AllowLogin checks if the user provided password is correct for login if no error is returned login is successful

func (DbManager) Create

func (mng DbManager) Create(id string, pw string) error

func (DbManager) CreateUser

func (mng DbManager) CreateUser(usr User) error

type ManagerOpts

type ManagerOpts struct {
	BcryptDifficulty int
}

type StaticUsers

type StaticUsers struct {
	Users []User `yaml:"users"`
}
Example
// load Users from a file
userMng, err := FromFile("testdata/users.yaml")
panicOnErr(err)

// manually add a user
userMng.Add("u2", "u2")

// check if the user demo (from file) can login
isOK := userMng.AllowLogin("demo", "demo")
fmt.Printf("user demo can login: %v\n", isOK)

// check if the user u2 can login
isOK = userMng.AllowLogin("u2", "u2")
fmt.Printf("user u2 can login: %v", isOK)
Output:

user demo can login: true
user u2 can login: true

func FromFile

func FromFile(file string) (*StaticUsers, error)

func (*StaticUsers) Add

func (stu *StaticUsers) Add(user string, pw string)

func (*StaticUsers) AllowLogin

func (stu *StaticUsers) AllowLogin(user string, pw string) bool

type User

type User struct {
	Name    string `yaml:"name"`
	Email   string `yaml:"email"`
	Pw      string `yaml:"pw"` // or hashed passwd
	Enabled bool   `yaml:"enabled"`
}

func (User) Reload

func (u User) Reload() error

todo implement Reload functionality maybe add filewatcher to auto reload?

Jump to

Keyboard shortcuts

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