userdb

package
v0.0.0-...-7405641 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2023 License: MIT Imports: 11 Imported by: 0

README

Libraries for user and role management.

userdb

A simple user database, saved on disk as a text file.

Tab-separated file format:

  1. username
  2. argon2 hashed password

In some cases, the file may also contain database internal instructions, e.g., DELETE followed by a username.

Sample file:

 angela	$argon2id$v=19$m=65536,t=3,p=2$9e8pod5QJIVEXND92rjxnQ$IX0Oq3bNhfq4K9lZDUlIfLwH0ZAE0pDv/q55xi8Yasc
 james	$argon2id$v=19$m=65536,t=3,p=2$U4sN8dpRsI2TTEqImgWLig$VEhw7GHD0O8cW0Pl+CB26OHfIpbloBtfj/BsbFesU8c

roles

A simple database of roles/permissions, saved on disk as a text file.

Tab-separated file format:

  1. role name
  2. comma-separated list of users

In some cases, the file may also contain database internal instructions, e.g., DELETE followed by a role name.

Sample file:

member	angela james
admin	james

Documentation

Overview

Package userdb contains libraries for user and role databases (for user permissions)

Index

Constants

View Source
const FieldSeparator = "\t"

FieldSeparator separates fields in a file (for file reading/writing)

View Source
const ItemSeparator = " "

ItemSeparator separates items in a list (for file reading/writing)

Variables

View Source
var (
	//ErrInvalidHash error message for invalid hash format
	ErrInvalidHash = errors.New("the encoded hash is not in the correct format")

	// ErrIncompatibleVersion error message for incompatible version of argon2
	ErrIncompatibleVersion = errors.New("incompatible version of argon2")
)

Functions

func Validate

func Validate(userDB *UserDB, roleDB *RoleDB) error

Validate user db with role db (all user names in the role db must be defined in the user db)

Types

type RoleDB

type RoleDB struct {

	// Constraints is used to validate an input role + users
	// returns true + empty string if the role/users are valid
	// returns false + message if the role/users are invalid
	Constraints func(role string, users []string) (bool, string)
	// contains filtered or unexported fields
}

RoleDB a database of roles (username - roles)

func EmptyRoleDB

func EmptyRoleDB(fileName string) (*RoleDB, error)

EmptyRoleDB creates a new role database with the specified file name, which will be removed if it already exists

func NewRoleDB

func NewRoleDB() *RoleDB

NewRoleDB creates a new user database

func ReadRoleDB

func ReadRoleDB(fileName string) (*RoleDB, error)

ReadRoleDB reads a role db from file

func (*RoleDB) Authorized

func (rdb *RoleDB) Authorized(role, userName string) bool

Authorized is used to check if a user has access to a specified role

func (*RoleDB) CheckConstraints

func (rdb *RoleDB) CheckConstraints(role string, users []string) (bool, string)

CheckConstraints to check if the db entry is valid given certain constraints

func (*RoleDB) CreateRole

func (rdb *RoleDB) CreateRole(role string) error

CreateRole is used to insert a user into the database

func (*RoleDB) DeleteRole

func (rdb *RoleDB) DeleteRole(role string) error

DeleteRole is used to delete a user role from the database

func (*RoleDB) DeleteUserRole

func (rdb *RoleDB) DeleteUserRole(role, userName string) error

DeleteUserRole is used to delete a user role from the database

func (*RoleDB) GetRoles

func (rdb *RoleDB) GetRoles() []string

GetRoles returns the roles defined in the database

func (*RoleDB) InsertRole

func (rdb *RoleDB) InsertRole(role string, userNames []string) error

InsertRole is used to insert a user into the database

func (*RoleDB) ListRolesAndUsers

func (rdb *RoleDB) ListRolesAndUsers() map[string][]string

ListRolesAndUsers list all roles with users

func (*RoleDB) ListUsers

func (rdb *RoleDB) ListUsers(role string) ([]string, bool)

ListUsers looks up the users for the specified role

func (*RoleDB) RoleExists

func (rdb *RoleDB) RoleExists(role string) bool

RoleExists looks up the role with the specified name

func (*RoleDB) SaveFile

func (rdb *RoleDB) SaveFile() error

SaveFile save the db to file

type UserDB

type UserDB struct {

	// Constraints is used to validate an input user + password
	// returns true + empty string if the user is valid
	// returns false + message if the user is invalid
	Constraints func(user string, password string) (bool, string)
	// contains filtered or unexported fields
}

UserDB a database of users

func EmptyUserDB

func EmptyUserDB(fileName string) (*UserDB, error)

EmptyUserDB creates a new user database with the specified file name, which will be removed if it already exists

func NewUserDB

func NewUserDB() *UserDB

NewUserDB creates a new user database

func ReadUserDB

func ReadUserDB(fileName string) (*UserDB, error)

ReadUserDB reads a user db from file

func (*UserDB) Authorized

func (udb *UserDB) Authorized(userName, password string) (bool, error)

Authorized is used to check if the password matches the specified user name

func (*UserDB) CheckConstraints

func (udb *UserDB) CheckConstraints(userName, password string) (bool, string)

CheckConstraints to check if the db entry is valid given certain constraints

func (*UserDB) DeleteUser

func (udb *UserDB) DeleteUser(userName string) error

DeleteUser is used to delete a user from the database

func (*UserDB) GetPasswordHash

func (udb *UserDB) GetPasswordHash(userName string) (string, error)

GetPasswordHash returns the password_hash value for userName. If no such value is found, the empty string is returned (along with a non-nil error value)

func (*UserDB) GetUsers

func (udb *UserDB) GetUsers() []string

GetUsers returns the users defined in the database

func (*UserDB) InsertUser

func (udb *UserDB) InsertUser(userName, password string) error

InsertUser is used to insert a user into the database

func (*UserDB) SaveFile

func (udb *UserDB) SaveFile() error

SaveFile save the db to file

func (*UserDB) UpdatePassword

func (udb *UserDB) UpdatePassword(userName string, password string) error

UpdatePassword updates the password for the specified user

func (*UserDB) UserExists

func (udb *UserDB) UserExists(userName string) (bool, string)

UserExists check if a user with the specified user name. Second return value is the normalised version of the input user name.

Jump to

Keyboard shortcuts

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