authmodel

package
v0.0.0-...-e049142 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2015 License: BSD-3-Clause Imports: 5 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidId       = errors.New("auth: invalid id")
	ErrInvalidEmail    = errors.New("auth: invalid email address")
	ErrInvalidToken    = errors.New("auth: invlaid token")
	ErrDuplicateEmail  = errors.New("auth: duplicate email address")
	ErrInvalidPassword = errors.New("auth: invalid password")
	ErrNotLogged       = errors.New("auth: no login user found")
	ErrDuplicateName   = errors.New("auth: duplicate Group Name")
	ErrNotFound        = errors.New("auth: not found")
)

Functions

This section is empty.

Types

type Address

type Address struct {
	Country  *string `bson:"Country" json:",omitempty"`
	State    *string `bson:"State" json:",omitempty"`
	City     *string `bson:"City" json:",omitempty"`
	District *string `bson:"District" json:",omitempty"`
	Street   *string `bson:"Street" json:",omitempty"`
}

type FormatChecker

type FormatChecker interface {
	// PasswordValidate validate password strength.
	PasswordValidate(string) bool
	// EmailValidate validate email format.
	EmailValidate(string) bool
}

FormatChecker is a helper interface for check formating of user input. Currently only Password and Email available.

type Group

type Group struct {
	Id         *string    `bson:"Id,omitempty"`
	Name       *string    `bson:"Name,omitempty" json:",omitempty"`
	Info       *GroupInfo `bson:"Info,omitempty" json:",omitempty"`
	Privileges []string   `bson:"Privileges,omitempty" json:",omitempty"`
}

Group is a way to group some privileges into one.

type GroupInfo

type GroupInfo struct {
	Description *string `bson:"Description" json:",omitempty"`
}

type Manager

type Manager interface {
	// AddUser adds an user to database with email and password;
	// If app is false, the user is waiting to be approved.
	// Implement of this method should valid email, pwd and make sure the user
	// email are unique then initial the LastActivity and JoinDay.
	// Remember to generate ConfirmCodes["activate"] a secure random value.
	// It returns an error describes the first issue encountered, if any.
	AddUser(email, pwd string, app bool) (*User, error)
	// AddUserDetail add a User with full detail to database.
	// Implement of this method should valid email, pwd and make sure the user
	// email are unique then initial the LastActivity and JoinDay.
	// Remember to generate ConfirmCodes["activate"] a secure random value.
	// It returns an error describes the first issue encountered, if any.
	AddUserDetail(email, pwd string, app bool, pri []string,
		code map[string]string, profile *Profile, groupIds []string) (*User, error)
	// UpdateUserDetail changes detail of the User.
	// If a agrument is nil, the implement of tis function should not affect the
	// value of that field.
	// It returns an error describes the first issue encountered, if any.
	UpdateUserDetail(id string, pwd *string, app *bool, pri []string,
		code map[string]string, profile *Profile, groupIds []string) error
	// DeleteUser deletes an user from database base on the given id;
	// It returns an error describes the first issue encountered, if any.
	DeleteUser(id string) error
	// FindUser finds the user with the given id;
	// Its returns an ErrNotFound if the user's id was not found.
	FindUser(id string) (*User, error)
	// FindUserByEmail like Find but receive an email
	FindUserByEmail(email string) (*User, error)
	// FindAllUser finds and return a slice of users belong to some groups if specific.
	// If limit < 0 the mean using the default upper limit.
	// If limit == 0 return empty result with error indicate no result found.
	// If limit can't be greater than the default upper limit.
	// Specific fields name for porjection select.
	FindAllUser(limit int, offsetId string, fields []string, groupIds []string) ([]*User, error)
	// GetUser gets the infomations and update the LastActivity of the current
	// loged user by the token (given by Login method);
	// It returns an error describes the first issue encountered, if any.
	GetUser(token string) (*User, error)
	// Login logs user in by given user id.
	// Stay is the duration to keep the user Login state.
	// It returns a token string, use the token to keep track on the user with
	// GetUser or Logout.
	Login(id string, stay time.Duration) (string, error)
	// Logout logs the current user out. if all, logout all session of the same user.
	Logout(token string, all bool) error
	// ComparePassword cmapres the given passwod string with the hashed password of the
	// User struct.
	ComparePassword(ps string, pwd *Password) error

	// AddGroupDetail adds a group with full detail to database.
	AddGroupDetail(name string, pri []string, info *GroupInfo) (*Group, error)
	// UpdateGroupDetail updates group detail specific by id.
	UpdateGroupDetail(id string, pri []string, info *GroupInfo) error
	// FindGroup find the group specific by id.
	FindGroup(id string) (*Group, error)
	// FindGroupByName find the group specific by name.
	FindGroupByName(name string) (*Group, error)
	// FindSomeGroup find and return a slice of group specific by thier id.
	// Specific fields name for porjection select.
	FindSomeGroup(id []string, fields []string) ([]*Group, error)
	// FindAllGroup finds and return a slice of group.
	// If limit < 0 the mean using the default upper limit.
	// If limit == 0 return empty result with error indicate no result found.
	// If limit can't be greater than the default upper limit.
	// Specific fields name for porjection select.
	FindAllGroup(limit int, offsetId string, fields []string) ([]*Group, error)
	// DeleteGroup deletes a group from database base on the given id;
	// It returns an error describes the first issue encountered, if any.
	DeleteGroup(id string) error
}

type Password

type Password struct {
	Hashed []byte    `bson:"Hashed"`
	Salt   []byte    `bson:"Salt"`
	InitAt time.Time `bson:"InitAt"`
}

type Profile

type Profile struct {
	FirstName  *string    `bson:"FirstName" json:",omitempty"`
	LastName   *string    `bson:"LastName" json:",omitempty"`
	MiddleName *string    `bson:"MiddleName" json:",omitempty"`
	NickName   *string    `bson:"NickName" json:",omitempty"`
	BirthDay   *time.Time `bson:"BirthDay" json:",omitempty"`
	JoinDay    *time.Time `bson:"JoinDay" json:",omitempty"`
	Addresses  []Address  `bson:"Addresses" json:",omitempty"`
	Phones     []string   `bson:"Phones" json:",omitempty"`
}

type SimpleChecker

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

SimpleChecker implement FormatChecker and ... very simple.

func NewSimpleChecker

func NewSimpleChecker(pwdlen int) (*SimpleChecker, error)

func (*SimpleChecker) EmailValidate

func (c *SimpleChecker) EmailValidate(email string) bool

EmailValidate validate email base on a simple regex

func (*SimpleChecker) PasswordValidate

func (c *SimpleChecker) PasswordValidate(pwd string) bool

PasswordValidate validate pwd base on the length

type User

type User struct {
	Id           *string           `bson:"Id,omitempty"`
	Email        *string           `bson:"Email" json:",omitempty"`
	Pwd          *Password         `bson:"Pwd" json:"-"`
	LastActivity *time.Time        `bson:"LastActivity"  json:",omitempty"`
	Privileges   []string          `bson:"Privileges" json:",omitempty"`
	Approved     *bool             `bson:"Approved" json:",omitempty"`
	ConfirmCodes map[string]string `bson:"ConfirmCodes" json:"-"`
	Profile      *Profile          `bson:"Profile,omitempty" json:",omitempty"`
	Groups       []*Group          `bson:"Groups,omitempty" json:",omitempty"`
}

func (*User) ValidConfirmCode

func (u *User) ValidConfirmCode(key, code string, regen, del bool) bool

ValidConfirmCode valid the code for specific key of the user specify by id. Re-generate or delete code for that key if need.

Jump to

Keyboard shortcuts

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