mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-12-25 06:26:35 +08:00
[API节点]支持HTTP API
This commit is contained in:
37
internal/rpc/utils/plain_context.go
Normal file
37
internal/rpc/utils/plain_context.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package rpcutils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
type PlainContext struct {
|
||||
UserType string
|
||||
UserId int64
|
||||
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func NewPlainContext(userType string, userId int64) *PlainContext {
|
||||
return &PlainContext{
|
||||
UserType: userType,
|
||||
UserId: userId,
|
||||
ctx: context.Background(),
|
||||
}
|
||||
}
|
||||
|
||||
func (this *PlainContext) Deadline() (deadline time.Time, ok bool) {
|
||||
return this.ctx.Deadline()
|
||||
}
|
||||
|
||||
func (this *PlainContext) Done() <-chan struct{} {
|
||||
return this.ctx.Done()
|
||||
}
|
||||
|
||||
func (this *PlainContext) Err() error {
|
||||
return this.ctx.Err()
|
||||
}
|
||||
|
||||
func (this *PlainContext) Value(key interface{}) interface{} {
|
||||
return this.ctx.Value(key)
|
||||
}
|
||||
@@ -33,6 +33,29 @@ const (
|
||||
|
||||
// 校验请求
|
||||
func ValidateRequest(ctx context.Context, userTypes ...UserType) (userType UserType, userId int64, err error) {
|
||||
if ctx == nil {
|
||||
err = errors.New("context should not be nil")
|
||||
return
|
||||
}
|
||||
|
||||
// 支持直接认证
|
||||
plainCtx, ok := ctx.(*PlainContext)
|
||||
if ok {
|
||||
userType = plainCtx.UserType
|
||||
userId = plainCtx.UserId
|
||||
|
||||
if len(userTypes) > 0 && !lists.ContainsString(userTypes, userType) {
|
||||
userType = UserTypeNone
|
||||
userId = 0
|
||||
}
|
||||
|
||||
if userId <= 0 {
|
||||
err = errors.New("context: can not find user or permission denied")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
if !ok {
|
||||
return UserTypeNone, 0, errors.New("context: need 'nodeId'")
|
||||
|
||||
Reference in New Issue
Block a user