From e98e2d0c895918ab09ab5d5c1b0be04dc51cfdea Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 9 Jun 2023 04:06:03 -0400 Subject: [PATCH] refactor: add jwt auth middleware to files controller --- controller/files.go | 5 +++++ middleware/jwt.go | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 middleware/jwt.go diff --git a/controller/files.go b/controller/files.go index c3b217f..525a181 100644 --- a/controller/files.go +++ b/controller/files.go @@ -5,6 +5,7 @@ import ( "git.lumeweb.com/LumeWeb/portal/cid" "git.lumeweb.com/LumeWeb/portal/controller/response" "git.lumeweb.com/LumeWeb/portal/logger" + "git.lumeweb.com/LumeWeb/portal/middleware" "git.lumeweb.com/LumeWeb/portal/service/files" "github.com/kataras/iris/v12" "go.uber.org/zap" @@ -15,6 +16,10 @@ type FilesController struct { Controller } +func (f *FilesController) BeginRequest(ctx iris.Context) { + ctx.AddHandler(middleware.VerifyJwt) +} + func (f *FilesController) PostUpload() { ctx := f.Ctx diff --git a/middleware/jwt.go b/middleware/jwt.go new file mode 100644 index 0000000..8594920 --- /dev/null +++ b/middleware/jwt.go @@ -0,0 +1,22 @@ +package middleware + +import ( + "git.lumeweb.com/LumeWeb/portal/service/auth" + "github.com/kataras/iris/v12" +) + +func VerifyJwt(ctx iris.Context) { + token := auth.GetRequestAuthCode(ctx) + + if len(token) == 0 { + ctx.StopWithError(iris.StatusUnauthorized, auth.ErrInvalidToken) + return + } + + if err := auth.VerifyLoginToken(token); err != nil { + ctx.StopWithError(iris.StatusUnauthorized, auth.ErrInvalidToken) + return + } + + ctx.Next() +}