auth

package
v0.0.0-...-8af4378 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2014 License: BSD-3-Clause Imports: 10 Imported by: 11

Documentation

Overview

Package auth provide a interface to easy authorization for your web application.

Index

Constants

This section is empty.

Variables

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

Functions

func OAuthHandleWrapper

func OAuthHandleWrapper(handler http.HandlerFunc, groups []string, pri []string) http.HandlerFunc

func OAuthOwnerPrivilegeWrapper

func OAuthOwnerPrivilegeWrapper(handler http.HandlerFunc, userIdField string) http.HandlerFunc

func Register

func Register(name string, p AuthProvider) error

Types

type Address

type Address struct {
	Country  string `bson:"Country"`
	State    string `bson:"State"`
	City     string `bson:"City"`
	District string `bson:"District"`
	Street   string `bson:"Street"`
}

type AuthProvider

type AuthProvider interface {
	OpenUserMngr(*http.Request) (UserManager, error)
	OpenGroupMngr(*http.Request) (GroupManager, error)
}

func GetProvider

func GetProvider(name string) (AuthProvider, error)

func Provider

func Provider() AuthProvider

Provider returns the last provider added. It will panic if there's no one.

type BriefGroup

type BriefGroup struct {
	Id   interface{} `bson:"_id"`
	Name string      `bson:"Name"`
}

type FormatChecker

type FormatChecker interface {
	PasswordValidate(string) bool
	EmailValidate(string) bool
}

type Group

type Group struct {
	BriefGroup `bson:"BriefGroup,inline"`
	Info       GroupInfo `bson:"Info" datastore:",noindex"`
	Privilege  []string  `bson:"Privilege"`
}

type GroupInfo

type GroupInfo struct {
	Description string `bson:"Description"`
}

type GroupManager

type GroupManager interface {
	// AddGroupDetail adds a group with full detail to database.
	AddGroupDetail(name string, info *GroupInfo, pri []string) (*Group,
		error)
	// UpdateGroupDetail updates group detail specific by id.
	UpdateGroupDetail(id interface{}, info *GroupInfo, pri []string) error
	// FindGroup find the group specific by id.
	FindGroup(id interface{}) (*Group, error)
	// FindSomeGroup find and return a slice of group specific by thier id.
	FindSomeGroup(id ...interface{}) ([]*Group, error)
	// FindAllGroup finds and return a slice of group. offsetId define which
	// sub-sequence of matching groups to be returned.
	FindAllGroup(offsetId interface{}, limit int) ([]*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 interface{}) error
	// Close clean the resources used by the manager if need.
	Close() error
}

type Password

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

func HashPwd

func HashPwd(pwd string) (Password, error)

type SimpleChecker

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

func NewSimpleChecker

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

func (*SimpleChecker) EmailValidate

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

func (*SimpleChecker) PasswordValidate

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

type User

type User struct {
	Id           interface{}       `bson:"_id" datastore:"-"`
	Email        string            `bson:"Email"`
	OldPwd       []Password        `bson:"OldPwd" json:",omitempty"`
	Pwd          Password          `bson:"Pwd" json:",omitempty"`
	LastActivity time.Time         `bson:"LastActivity`
	Info         UserInfo          `bson:"Info" datastore:",noindex" json:",omitempty"`
	Privilege    []string          `bson:"Privilege"`
	Approved     bool              `bson:"Approved"`
	ConfirmCodes map[string]string `bson:"ConfirmCodes" datastore:"-" json:",omitempty"`
	BriefGroups  []BriefGroup      `bson:"BriefGroups"`
}

func (*User) ChangePassword

func (u *User) ChangePassword(pwd string) error

func (*User) ComparePassword

func (u *User) ComparePassword(pwd string) error

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.

type UserInfo

type UserInfo struct {
	FirstName  string    `bson:"FirstName"`
	LastName   string    `bson:"LastName"`
	MiddleName string    `bson:"MiddleName"`
	NickName   string    `bson:"NickName"`
	BirthDay   time.Time `bson:"BirthDay"`
	JoinDay    time.Time `bson:"JoinDay"`
	Address    []Address `bson:"Address"`
	Phone      []string  `bson:"Phone"`
}

type UserManager

type UserManager interface {
	// GroupManager returns the GroupManager.
	GroupManager() GroupManager
	// 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.
	// 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.
	// It returns an error describes the first issue encountered, if any.
	AddUserDetail(*User) (*User, error)
	// UpdateUserDetail changes detail of the User.
	// It returns an error describes the first issue encountered, if any.
	UpdateUserDetail(*User) 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 interface{}) error
	// FindUser finds the user with the given id;
	// Its returns an ErrNotFound if the user's id was not found.
	FindUser(id interface{}) (*User, error)
	// FindUserByEmail like FindUser but receive an email
	FindUserByEmail(email string) (*User, error)
	// FindAllUser finds and return a slice of user.
	// offsetId, limit define which sub-sequence of matching users to return.
	// Limit take an number of user per page; offsetId take the Id of the last
	// user of the previous page.
	FindAllUser(offsetId interface{}, limit int) ([]*User, error)
	// FindAllUserOline finds and return a slice of current Loged user.
	// See FindAllUser for the usage.
	FindAllUserOnline(offsetId interface{}, limit int) ([]*User, error)
	// ValidateUser validate user base on the current request.
	// It returns the user infomations if the email and password is correct.
	ValidateUser(email, pwd 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 interface{}, stay time.Duration) (string, error)
	// Logout logs the current user out.
	Logout(token string) error
	// Can uses GroupManager to determines if user have privilege to do something.
	Can(user *User, do string) bool
	// Close clean the resources used by the manager if need.
	Close() error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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