auth

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2022 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrWrongPassword      = errors.New("用户密码错误")
	ErrClientIDNotFound   = errors.New("缺少授权客户端ID")
	ErrUserNotFound       = errors.New("用户不存在")
	ErrUserMobileNotFound = errors.New("缺少获取验证码的手机号")
)

Functions

func AuthClient

func AuthClient(ctx context.Context) (string, bool)

AuthClient 获取当前请求中的ClientID

func ContextWithClient

func ContextWithClient(ctx context.Context, client string) context.Context

func ContextWithUser

func ContextWithUser(ctx context.Context, account *Principal) context.Context

ContextWithUser 将用户对象写入当前安全上下文中

func GenAuthCode

func GenAuthCode() string

func GenVerifyCode

func GenVerifyCode() string

func IsPatternMatch

func IsPatternMatch(key1 string, key2 string) bool

func LoadKeyFile

func LoadKeyFile(keyFile string) ([]byte, error)

func RandStr

func RandStr(n int) string

RandStr 生成指定长度的随机字符串

func RandStrNumber

func RandStrNumber(n int, mask []rune) string

Types

type CurrentUserKey

type CurrentUserKey struct{}

type Identity

type Identity struct {
	ID        string         `json:"id" gorm:"primaryKey;type:uuid;default:uuid_generate_v4()" pg:"id,type:uuid,pk,default:uuid_generate_v4()"` // ID 身份标识ID
	ClientID  string         `json:"client_id" gorm:"type:varchar(50)" pg:"client_id,type:varchar(50)"`                                         // ClientID 客户端ID
	UserID    string         `json:"user_id" gorm:"type:varchar(50)" pg:"user_id,type:varchar(50)"`                                             // UserID 用户统一帐号ID。可空,但可通过Bind方法与用户账号进行绑定
	Type      IdentityTypes  `json:"id_type" gorm:"column:id_type",pg:"id_type,default:1"`                                                      // Type 身份标识的类型
	Name      string         `json:"name" gorm:"type:varchar(50)" pg:"name,type:varchar(50)"`                                                   // Name 身份标识的名称,可以为用户名、手机、或邮件
	Secret    []byte         `json:"secret" pg:"secret"`                                                                                        // Secret 用户机密信息,可以是密码
	MetaData  datatypes.JSON `json:"-" pg:",json_use_number"`                                                                                   // Meta 其它信息 TODO: 可以将存储的类型与使用的类型分离
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt gorm.DeletedAt `gorm:"index"`
}

Identity 用户身份标识

func NewEmailID

func NewEmailID(clientId, email string, password string) *Identity

func NewMobileID

func NewMobileID(clientId, mobile string) *Identity

func NewOpenID

func NewOpenID(clientId, openID string) *Identity

func NewUnionID

func NewUnionID(clientId, unionID string) *Identity

func NewUserPwdID

func NewUserPwdID(clientId, userName, password string) *Identity

func (*Identity) IsCorrectPassword

func (user *Identity) IsCorrectPassword(password string) bool

IsCorrectPassword 检查输入密码是否正确

func (*Identity) SetPassword

func (user *Identity) SetPassword(password string)

type IdentityManager

type IdentityManager interface {
	// Valid 根据身份类型验证用户身份是否有效(默认仅支持内置三种方式)
	Valid(clientId, name, password string, t IdentityTypes) (*Identity, error)
	// GenVerifyCode 生成的随机手机验证码
	GenVerifyCode(clientId, mobile string) (string, error)
	// GetVerifyCode 获取手机验证码,读取后马上删除
	GetVerifyCode(clientId, mobile string) (string, error)
	// Add 创建新的身份标识
	Add(user *Identity) error
	// ChangePassword 更改用户密码
	ChangePassword(user *Identity, password string) error
	// Get 通过用户名获取身份标识
	Get(clientId, name string, idType IdentityTypes) (*Identity, error)
	// Bind 将身份标识绑定到指定的用户帐号上
	Bind(id, uid string) error
	// Remove 移除用户身份标识
	Remove(id string) error
	// List 列出指定用户ID下的全部身份标识
	List(clientId, userId string) ([]Identity, error)
	// Setup 初始化用户身分标识管理器的数据实体对象
	Setup() error
}

IdentityManager 用户身份标识管理器

func NewIDMan

func NewIDMan(repos repository.Repository) IdentityManager

type IdentityTypes

type IdentityTypes int16
const (
	IDUserNameAndPassword  IdentityTypes = 1
	IDEmailAndPassword     IdentityTypes = 2
	IDMobileAndVerifyCode  IdentityTypes = 3
	IDOAuth2NetworkAccount IdentityTypes = 4
	IDWeChatOpenID         IdentityTypes = 5
	IDWeChatUnionID        IdentityTypes = 6
)

type Principal

type Principal struct {
	ID        string            `json:"id"`      // ID 用户ID
	Type      string            `json:"type"`    // Type 标识账号身份的类型, 例如: service
	Name      string            `json:"name"`    // Name 用户名
	Issuer    string            `json:"issuer"`  // Issuer 访问令牌的签发方
	IssuedAt  int64             `json:"issued"`  // IssuedAt 签发时间
	ExpiresAt int64             `json:"expires"` // ExpiresAt 过期时间
	Scopes    []string          `json:"scopes"`  // Scopes 私隐授权的范围
	Roles     []string          `json:"roles"`   // Roles 用户的角色
	Meta      map[string]string `json:"meta"`    // Meta 用户帐号中的其它信息
}

Principal 用于表示在微服务间传递的用户信息

func AuthUser

func AuthUser(ctx context.Context) (*Principal, bool)

AuthUser 获取当前已进行授权验证的用户对象,如果失败则返回false

func NewUser

func NewUser(username string, scopes ...string) *Principal

func (*Principal) HasRole

func (user *Principal) HasRole(role string) bool

func (*Principal) InRoles

func (user *Principal) InRoles(roles ...string) bool

func (*Principal) SetRoles

func (user *Principal) SetRoles(role ...string) *Principal

SetRoles 设置用户角色

func (*Principal) SetScopes

func (user *Principal) SetScopes(scope ...string) *Principal

SetScopes 设置授权范围

type Resource

type Resource struct {
	ID       string `csv:"id" gorm:"primaryKey;type:varchar(200)"` // ID 标识资源的使用标识,即 scope 的定义
	Name     string `csv:"name" gorm:"type:varchar(200)"`          // Name 资源的中文使用说明
	Domain   string `csv:"domain" gorm:"type:varchar(1024)"`       // ClientID 用于标记当前资源所属的应用编号。也可以作为Domain使用
	Type     string `csv:"type" gorm:"type:varchar(50)"`           // Type 资源的类型 如 service、api、file
	Action   string `csv:"action" gorm:"type:varchar(50)"`         // Action HTTP方法POST、PUT、GET
	EndPoint string `csv:"url" gorm:"type:varchar(2048)"`          // EndPoint 资源指向的具体URL
}

Resource 定义可访问资源

type Rules

type Rules interface {
	// Resources 加载系统定义的全部可访问资源
	Resources(domains ...string) ([]*Resource, error)
	// GetRes 获取指定ID的Resouce资源
	GetRes(id string) *Resource
	// Verify 检查用户的授权范围是否有效
	Verify(user *Principal, res *Resource) (bool, error)
	// VerifyRequest 验证请求是否合法
	VerifyRequest(req *http.Request) (bool, error)
}

Rules 提供基于授权范围的权限控制

func NewRules

func NewRules(params ...interface{}) (Rules, error)

gocsv 无法下载使用

type Token

type Token struct {
	AccessToken  string        `json:"access_token"`  // AccessToken 资源访问令牌
	RefreshToken string        `json:"refresh_token"` // RefreshToken 刷新令牌
	Created      time.Time     `json:"created"`       // Created 创建日期
	ExpiresIn    time.Duration `json:"expires_in"`    // ExpiresIn 过期时限
}

Token 定义访问令牌的数据结构

func (*Token) Expired

func (t *Token) Expired() bool

Expired 判断令牌是否已经过期

type Tokens

type Tokens interface {
	Inspect(string) (*Principal, error)  // Inspect 检查 AccessToken是否有效,并从中读取用户身份信息
	Generate(*Principal) (*Token, error) // Generate 生成的令牌实例对象
	Options() interface{}                // Options 获取身份验证组件全部的选项
}

Tokens 访问令牌验证组件

type UserClaims

type UserClaims struct {
	go_jwt.StandardClaims
	Principal
}

UserClaims JWT claims 仅供内部使用

func NewUserClaims

func NewUserClaims(user *Principal, duration time.Duration) *UserClaims

NewUserClaims creates a new UserClaim

type VerifyCode

type VerifyCode struct {
	ClientID string    `json:"client_id" gorm:"primaryKey;type:varchar(50)" pg:"client_id,type:varchar(50),pk"`
	Mobile   string    `json:"mobile" gorm:"primaryKey;type:varchar(20)" pg:"mobile,type:varchar(20),pk"`
	Code     string    `json:"code" pg:"code"`
	ExpireAt time.Time `json:"expire_at" pg:"expire_at"`
}

func NewVerifyCode

func NewVerifyCode(clientId, mobile string, duration time.Duration) *VerifyCode

type XClientKey

type XClientKey struct{}

Directories

Path Synopsis
tokens
jwt

Jump to

Keyboard shortcuts

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