Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFound = errors.New("no such user") // ErrNotFound is returned when trying to do something with uknown username ErrValidateUser = errors.New("missing username or secret field for user") // ErrValidateUser is returned when User struct is missing Username or Secret fields ErrIncorrectPassword = errors.New("incorrect password") // ErrIncorrectPassword is returned when user password does not check out ErrUnknownStorageType = errors.New("unknown storage type, use 'postgres' or 'sqlite'") // ErrUnknownStorageType is returned when unknown value is passed as storage type in RepoConfig )
Functions ¶
func BcryptHash ¶
BcryptHash return bcrypt hash of provided password.
func New ¶
func New(conf RepoConfig) (Repository, Authenticator, error)
New return both the user repository and the Authenticator using it to check passwords against the hash kept in the database
func OpenPG ¶
OpenPG opens connects to Postgres database using specified dsn and executes create table statement. CREATE TABLE IF NOT EXISTS users (
username varchar(90) primary key, pwdhash varchar(150) not null, enabled bool default true, attrs bytea, created timestamp with time zone not null default now(), updated timestamp with time zone not null default now()
) If table named "users" already exists in the database the create statement will have no effect and will return nil error.
func OpenSQlite ¶
OpenSQlite opens SQLite database using specified path and executes create table statement. CREATE TABLE IF NOT EXISTS users (
username text primary key, pwdhash text not null, enabled bool default true, attrs blob, created datetime, updated datetime
) If table named "users" already exists in the database the create statement will have no effect and will return nil error.
Types ¶
type Authenticator ¶
type Authenticator interface {
Authenticate(username, password string) (bool, error) // Authenticate returns true if password for username checks out. If error is not nil the boolean resualt is unusable.
}
func NewBcryptAuth ¶
func NewBcryptAuth(repo Repository) Authenticator
NewBcryptAuth returns Authenticator using bcrypt algorithm in Authenticate method to compare provided password with stored hash.
type RepoConfig ¶
type RepoConfig struct {
Type StorageType // type of storage: "postgres" or "sqlite"
Source string // dsn for Postgres or filename for SQLite
}
RepoConfig specifies storage to use for keeping user records
type Repository ¶
type Repository interface {
Get(username string) (User, error) // Get returns user with username
Put(user User) error // Put saves User struct
Upd(user User) error // Update updates User
Del(username string) error // Del deletes user with username
Rename(oldname string, newname string) error // Rename renames user with oldname setting username to newname
Close() error // Closes safely closes the repository
}
Repository is a basic CRUD interface to store User structs.
func NewSQLRepo ¶
func NewSQLRepo(db *sql.DB) Repository
NewSQLRepo accepts SQL database instance and return Repostory. Note that Put, Upd and Rename methods enforce non-zero timestapms of User struct. If either Created or Updated fields are zero time values (IsZero() returns true), then they are updated with time.Now() Put method also sets Enabled field to true for User struct. This way it is safe to actually fill only Name and Secret fields of User struct and pass it to Put method.
type StorageType ¶
type StorageType string
const ( StoragePostgres StorageType = "postgres" StorageSQlite StorageType = "sqlite" )
type User ¶
type User struct {
Username string // username
Secret string // secret (e.g. password hash)
Enabled bool // is user active
Attrs map[string]string // various stuff you might put here
Created time.Time // create time
Updated time.Time // last update time
}
User is a very basic struct representing user