common

package
v1.0.12 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2020 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// token类型
	// 访问token
	TokenTypeAccessToken TokenType = "access_token"
	// 刷新token
	TokenTypeRefreshToken TokenType = "refresh_token"

	// GrantTypePasswordCredentials 密码模式
	GrantTypePasswordCredentials GrantType = "password"
	// GrantTypeRefreshToken token刷新模式
	GrantTypeRefreshToken GrantType = "refresh_token"

	// 请求参数token key(param、form、header)
	AccessToken   = "access_token"
	Authorization = "Authorization"

	// token 内容
	// 角色信息
	TokenDataRoles = "roles"
	// 授权信息
	TokenDataAuthorities = "authorities"
	// session数据
	TokenDataSession = "session"

	// token claims
	// token有效期
	ClaimsEXP = "exp"
	// token唯一标示
	ClaimsJTI = "jti"
	// token类型:access_token,refresh_token
	ClaimsType = "type"
	// 客户端ID
	ClaimsClientId = "client_id"
	// 用户名
	ClaimsUserName = "user_name"
	// 客户端scope
	ClaimsScope = "scope"
)

Variables

View Source
var (
	// HTTP相关
	ERR_HTTP_REQUEST_ERROR = &ErrorCode{1000, "请求错误"}
	ERR_HTTP_AUTH_FAILED   = &ErrorCode{1001, "认证错误"}

	// 业务相关
	ERR_BUSINESS_ERROR = &ErrorCode{2000, "业务错误"}

	// 系统异常相关
	ERR_SYS_ERROR = &ErrorCode{9000, "系统错误"}
)
View Source
var (
	AntPathMatcher = antpath.New()
)
View Source
var (
	// ApplicationConfigFileName 系统配置文件名称,默认 application.yaml
	ApplicationConfigFileName = "application.yaml"
)

Functions

func GenerateJwtToken

func GenerateJwtToken(tokenType TokenType, clientId, userName, key string, jti string, exp int64, scope []string, roles []string, authorities []string, payloadExtendData map[string]interface{}) (tokenString string, err error)

func GetLoginUserNameFromToken

func GetLoginUserNameFromToken(ctx *gin.Context, key string) string

GetLoginUserNameFromToken 从请求的token中获取登录用户名

func GetTokenFromRequest

func GetTokenFromRequest(ctx *gin.Context) string

GetTokenFromRequest 从请求中获取 access token (param、form、header)

func HandleErrorToPanicPolestarError added in v1.0.5

func HandleErrorToPanicPolestarError(err error, code *ErrorCode)

HandleErrorToPanicPolestarError 处理错误并Panic

func HandleErrorToPanicPolestarErrorWithMessage added in v1.0.6

func HandleErrorToPanicPolestarErrorWithMessage(err error, code *ErrorCode, detailMessage string)

HandleErrorToPanicPolestarErrorWithMessage 处理错误并Panic

func PanicPolestarError

func PanicPolestarError(code *ErrorCode, detailMessage string)

PanicPolestarError 抛出PolestarError异常

func PanicPolestarErrorByError

func PanicPolestarErrorByError(code *ErrorCode, err error)

PanicPolestarErrorByError 抛出PolestarError异常

func ParseJwtToken

func ParseJwtToken(tokenString, key string) map[string]interface{}

ParseJwtToken 解析jwt token

func SetApplicationConfigFileName

func SetApplicationConfigFileName(fileName string)

SetApplicationConfigFileName 设置系统配置文件名称

func VerifySecretOrPassword

func VerifySecretOrPassword(cipher string, plain string) bool

VerifySecretOrPassword 验证Secret或者密码

Types

type AppConfig

type AppConfig struct {
	Server struct {
		Addr string `yaml:"addr"`
		Mode string `yaml:"mode"`
		Cors bool   `yaml:"cors"`
	} `yaml:"server"`
	Mysql struct {
		Url          string `yaml:"url"'`
		MaxIdleConns int    `yaml:"maxIdleConns"`
		LogMode      bool   `yaml:"logMode"`
	} `yaml:"mysql"`
	Redis struct {
		Url string `yaml:"url"`
		DB  int    `yaml:"db"`
	} `yaml:"redis"`
	Auth struct {
		Cache struct {
			CleanupInterval time.Duration `yaml:'cleanupInterval'`
		} `yaml:"cache"`
		TokenCheck struct {
			CheckAtServer bool   `yaml:"checkAtServer"`
			CheckEndpoint string `yaml:"checkEndpoint"`
		} `yaml:"tokenCheck"`
		Jwt struct {
			Secret string `yaml:"secret"`
		} `yaml:"jwt"`
		TokenExp struct {
			AccessToken  int `yaml:"accessToken"`
			RefreshToken int `yaml:"refreshToken"`
		} `yaml:"tokenExp"`
		AuthUris []authUriInfo `yaml:"authUris"`
		SkipUris []authUriInfo `yaml:"skipUris"`
	}
}

系统配置文件

func ApplicationConfig

func ApplicationConfig() *AppConfig

ApplicationConfig 获取系统配置

type ErrorCode added in v1.0.2

type ErrorCode struct {
	Code    int
	Message string
}

ErrorCode 错误码

type GrantType

type GrantType string

GrantType

type PolestarError

type PolestarError struct {
	ErrorCode
	DetailMessage string
}

PolestarError PolestarError

func IsPolestarError

func IsPolestarError(src interface{}) (*PolestarError, bool)

IsPolestarError 判断是否是PolestarError

func NewPolestarError

func NewPolestarError(code *ErrorCode, detailMessage string) *PolestarError

NewPolestarError 创建NewPolestarError

func (*PolestarError) Error

func (pe *PolestarError) Error() string

Error PolestarError error实现

type TokenType

type TokenType string

TokenType

Jump to

Keyboard shortcuts

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