83 lines
1.8 KiB
Go
83 lines
1.8 KiB
Go
package auth
|
||
|
||
// Context 定义认证信息
|
||
type Context struct {
|
||
Payload Payload `json:"payload"`
|
||
Agent Agent `json:"agent,omitempty"`
|
||
Permissions map[string]struct{} `json:"permissions,omitempty"`
|
||
Metadata map[string]interface{} `json:"metadata,omitempty"`
|
||
}
|
||
|
||
// AnyPermission 检查认证是否包含指定权限
|
||
func (a *Context) AnyPermission(requiredPermission ...string) bool {
|
||
if a == nil || a.Permissions == nil {
|
||
return false
|
||
}
|
||
for _, permission := range requiredPermission {
|
||
if _, ok := a.Permissions[permission]; ok {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
// Payload 定义负载信息
|
||
type Payload struct {
|
||
Id int32 `json:"id,omitempty"`
|
||
Type PayloadType `json:"type,omitempty"`
|
||
Name string `json:"name,omitempty"`
|
||
Avatar string `json:"avatar,omitempty"`
|
||
}
|
||
|
||
type Agent struct {
|
||
Id int32 `json:"id,omitempty"`
|
||
Addr string `json:"addr,omitempty"`
|
||
}
|
||
|
||
type PayloadType int
|
||
|
||
const (
|
||
// PayloadNone 游客
|
||
PayloadNone PayloadType = iota
|
||
// PayloadUser 用户
|
||
PayloadUser
|
||
// PayloadAdmin 管理员
|
||
PayloadAdmin
|
||
// PayloadPublicServer 公共服务(public_client)
|
||
PayloadPublicServer
|
||
// PayloadSecuredServer 安全服务(credential_client)
|
||
PayloadSecuredServer
|
||
// PayloadInternalServer 内部服务
|
||
PayloadInternalServer
|
||
)
|
||
|
||
func (t PayloadType) ToStr() string {
|
||
switch t {
|
||
case PayloadUser:
|
||
return "user"
|
||
case PayloadAdmin:
|
||
return "admn"
|
||
case PayloadPublicServer:
|
||
return "cpub"
|
||
case PayloadSecuredServer:
|
||
return "ccnf"
|
||
default:
|
||
return "none"
|
||
}
|
||
}
|
||
|
||
func PayloadTypeFromStr(name string) PayloadType {
|
||
switch name {
|
||
case "user":
|
||
return PayloadUser
|
||
case "admn":
|
||
return PayloadAdmin
|
||
case "cpub":
|
||
return PayloadPublicServer
|
||
case "ccnf":
|
||
return PayloadSecuredServer
|
||
default:
|
||
return PayloadNone
|
||
}
|
||
}
|