Files

47 lines
978 B
Go
Raw Permalink Normal View History

package auth
import (
"git.apinb.com/bsm-sdk/core/middleware"
"github.com/gin-gonic/gin"
)
const (
ActorTypeUser = "user"
ActorTypeService = "service"
actorContextKey = "files.auth.actor"
)
type Actor struct {
Type string
ID uint
Identity string
}
func FromContext(ctx *gin.Context) (Actor, bool) {
if actor, ok := ctx.Get(actorContextKey); ok {
serviceActor, ok := actor.(Actor)
if ok && serviceActor.Type == ActorTypeService && serviceActor.ID == 0 && serviceActor.Identity != "" {
return serviceActor, true
}
}
claims, err := middleware.ParseAuth(ctx)
if err != nil || claims == nil || claims.ID == 0 || claims.Identity == "" {
return Actor{}, false
}
return Actor{
Type: ActorTypeUser,
ID: claims.ID,
Identity: claims.Identity,
}, true
}
func withActor(ctx *gin.Context, actor Actor) {
if actor.Type != ActorTypeService || actor.ID != 0 || actor.Identity == "" {
return
}
ctx.Set(actorContextKey, actor)
}