♻️ Refactored more modules into nexus
This commit is contained in:
@@ -151,8 +151,6 @@ func getToken(c *fiber.Ctx) error {
|
||||
idk = atk
|
||||
}
|
||||
|
||||
exts.SetAuthCookies(c, atk, rtk)
|
||||
|
||||
return c.JSON(fiber.Map{
|
||||
"id_token": idk,
|
||||
"access_token": atk,
|
||||
|
@@ -2,40 +2,11 @@ package exts
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func AuthMiddleware(c *fiber.Ctx) error {
|
||||
var atk string
|
||||
if cookie := c.Cookies(hyper.CookieAtk); len(cookie) > 0 {
|
||||
atk = cookie
|
||||
}
|
||||
if header := c.Get(fiber.HeaderAuthorization); len(header) > 0 {
|
||||
tk := strings.Replace(header, "Bearer", "", 1)
|
||||
atk = strings.TrimSpace(tk)
|
||||
}
|
||||
if tk := c.Query("tk"); len(tk) > 0 {
|
||||
atk = strings.TrimSpace(tk)
|
||||
}
|
||||
|
||||
c.Locals("p_token", atk)
|
||||
|
||||
rtk := c.Cookies(hyper.CookieRtk)
|
||||
if ctx, perms, newAtk, newRtk, err := services.Authenticate(atk, rtk, 0); err == nil {
|
||||
if newAtk != atk {
|
||||
SetAuthCookies(c, newAtk, newRtk)
|
||||
}
|
||||
c.Locals("permissions", perms)
|
||||
c.Locals("user", ctx.Account)
|
||||
}
|
||||
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
func EnsureAuthenticated(c *fiber.Ctx) error {
|
||||
if _, ok := c.Locals("user").(models.Account); !ok {
|
||||
return fiber.NewError(fiber.StatusUnauthorized)
|
||||
|
@@ -1,12 +1,11 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"strings"
|
||||
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/admin"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/api"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/cors"
|
||||
"github.com/gofiber/fiber/v2/middleware/idempotency"
|
||||
@@ -20,6 +19,8 @@ type HTTPApp struct {
|
||||
app *fiber.App
|
||||
}
|
||||
|
||||
var IReader *sec.InternalTokenReader
|
||||
|
||||
func NewServer() *HTTPApp {
|
||||
app := fiber.New(fiber.Config{
|
||||
DisableStartupMessage: true,
|
||||
@@ -54,7 +55,7 @@ func NewServer() *HTTPApp {
|
||||
Output: log.Logger,
|
||||
}))
|
||||
|
||||
app.Use(exts.AuthMiddleware)
|
||||
app.Use(sec.ContextMiddleware(IReader))
|
||||
|
||||
admin.MapAdminAPIs(app, "/api/admin")
|
||||
api.MapAPIs(app, "/api")
|
||||
|
Reference in New Issue
Block a user