Files
platform/web/auth/context.go

104 lines
2.3 KiB
Go
Raw Normal View History

package auth
import (
client2 "platform/web/domains/client"
)
// Context 定义认证信息
type Context struct {
Payload Payload `json:"payload"`
Permissions map[string]struct{} `json:"permissions,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
func (a *Context) AnyType(types ...PayloadType) bool {
if a == nil {
return false
}
for _, t := range types {
if a.Payload.Type == t {
return true
}
}
return false
}
// 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 PayloadType int
const (
PayloadNone PayloadType = iota // 游客
PayloadUser // 用户
PayloadAdmin // 管理员
PayloadPublicServer // 公共服务public_client
PayloadSecuredServer // 安全服务credential_client
PayloadInternalServer // 内部服务
)
func (t PayloadType) ToStr() string {
switch t {
case PayloadUser:
return "user"
case PayloadAdmin:
return "admn"
2025-05-09 18:56:17 +08:00
case PayloadPublicServer:
return "cpub"
2025-05-09 18:56:17 +08:00
case PayloadSecuredServer:
return "ccnf"
case PayloadInternalServer:
return "inte"
2025-05-09 15:06:22 +08:00
default:
return "none"
}
}
2025-05-09 15:06:22 +08:00
func PayloadTypeFromStr(name string) PayloadType {
switch name {
case "user":
2025-05-09 15:06:22 +08:00
return PayloadUser
case "admn":
2025-05-09 15:06:22 +08:00
return PayloadAdmin
case "cpub":
2025-05-09 18:56:17 +08:00
return PayloadPublicServer
case "ccnf":
2025-05-09 18:56:17 +08:00
return PayloadSecuredServer
case "inte":
return PayloadInternalServer
2025-05-09 15:06:22 +08:00
default:
return PayloadNone
}
}
func PayloadTypeFromClientSpec(spec client2.Spec) PayloadType {
var clientType PayloadType
switch spec {
case client2.SpecNative, client2.SpecBrowser:
clientType = PayloadPublicServer
case client2.SpecWeb:
clientType = PayloadSecuredServer
case client2.SpecTrusted:
clientType = PayloadInternalServer
}
return clientType
}