重构认证授权逻辑,集中到 auth 包中
This commit is contained in:
@@ -1,13 +1,28 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
client2 "platform/web/domains/client"
|
||||
)
|
||||
|
||||
// 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"`
|
||||
}
|
||||
|
||||
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 {
|
||||
@@ -29,26 +44,15 @@ type Payload struct {
|
||||
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
|
||||
PayloadNone PayloadType = iota // 游客
|
||||
PayloadUser // 用户
|
||||
PayloadAdmin // 管理员
|
||||
PayloadPublicServer // 公共服务(public_client)
|
||||
PayloadSecuredServer // 安全服务(credential_client)
|
||||
PayloadInternalServer // 内部服务
|
||||
)
|
||||
|
||||
func (t PayloadType) ToStr() string {
|
||||
@@ -80,3 +84,16 @@ func PayloadTypeFromStr(name string) PayloadType {
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user