79 lines
1.7 KiB
Go
79 lines
1.7 KiB
Go
package auth
|
|
|
|
import "platform/pkg/u"
|
|
|
|
// 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 (
|
|
// PayloadUser 用户类型
|
|
PayloadUser PayloadType = iota + 1
|
|
// PayloadAdmin 管理员类型
|
|
PayloadAdmin
|
|
// PayloadClientPublic 公共客户端类型
|
|
PayloadClientPublic
|
|
// PayloadClientConfidential 机密客户端类型
|
|
PayloadClientConfidential
|
|
)
|
|
|
|
func (t PayloadType) ToStr() string {
|
|
switch t {
|
|
case PayloadUser:
|
|
return "user"
|
|
case PayloadAdmin:
|
|
return "admn"
|
|
case PayloadClientPublic:
|
|
return "cpub"
|
|
case PayloadClientConfidential:
|
|
return "ccnf"
|
|
}
|
|
return "none"
|
|
}
|
|
|
|
func PayloadTypeFromStr(name string) *PayloadType {
|
|
switch name {
|
|
case "user":
|
|
return u.P(PayloadUser)
|
|
case "admn":
|
|
return u.P(PayloadAdmin)
|
|
case "cpub":
|
|
return u.P(PayloadClientPublic)
|
|
case "ccnf":
|
|
return u.P(PayloadClientConfidential)
|
|
}
|
|
return nil
|
|
}
|