29 lines
685 B
Go
29 lines
685 B
Go
package app
|
|
|
|
import (
|
|
"git.optclblast.xyz/draincloud/draincloud-core/internal/reqcontext"
|
|
"git.optclblast.xyz/draincloud/draincloud-core/internal/storage/models"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type authorizer interface {
|
|
authorize(ctx *gin.Context) (*models.Session, error)
|
|
}
|
|
|
|
func WithAuth(handler gin.HandlerFunc, auth authorizer) gin.HandlerFunc {
|
|
return func(ctx *gin.Context) {
|
|
sess, err := auth.authorize(ctx)
|
|
if err != nil {
|
|
writeError(ctx, err)
|
|
ctx.Abort()
|
|
return
|
|
}
|
|
|
|
authCtx := reqcontext.WithSession(ctx.Request.Context(), sess)
|
|
authCtx = reqcontext.WithUserID(authCtx, sess.UserID)
|
|
ctx.Request = ctx.Request.WithContext(authCtx)
|
|
|
|
handler(ctx)
|
|
}
|
|
}
|