-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from FrosTiK-SD/fiber-middleware
Fiber middleware
- Loading branch information
Showing
7 changed files
with
142 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package handler | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/FrosTiK-SD/auth/constants" | ||
"github.com/FrosTiK-SD/auth/interfaces" | ||
"github.com/FrosTiK-SD/auth/util" | ||
"github.com/gin-gonic/gin" | ||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
// For Gin based middlewares | ||
func (h *Handler) FiberVerifyStudent(ctx *fiber.Ctx) error { | ||
|
||
// Create a new session | ||
currentHandler := Handler{ | ||
MongikClient: h.MongikClient, | ||
JwkSet: h.JwkSet, | ||
Session: &Session{}, | ||
Config: Config{ | ||
Mode: MIDDLEWARE, | ||
}, | ||
} | ||
|
||
context := gin.Context{} | ||
|
||
currentHandler.HandlerVerifyStudentIdToken(&context) | ||
student := currentHandler.Session.Student | ||
|
||
if student != nil { | ||
ctx.Locals(constants.SESSION, student) | ||
ctx.Next() | ||
} else { | ||
return currentHandler.Session.Error | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (h *RoleCheckerHandler) FiberVerifyRole(ctx *fiber.Ctx) error { | ||
entity := ctx.Locals(constants.SESSION) | ||
var entityGroups *interfaces.Groups | ||
entityBytes, err := json.Marshal(entity) | ||
if err != nil { | ||
return err | ||
} | ||
err = json.Unmarshal(entityBytes, &entityGroups) | ||
if err != nil { | ||
return err | ||
} | ||
if !util.CheckRoleExists(&entityGroups.Groups, h.Role) { | ||
return errors.New(constants.ERROR_ROLE_CHECK_FAILED) | ||
} | ||
|
||
ctx.Next() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters