feat: adjust route register
This commit is contained in:
@@ -26,7 +26,7 @@ type TokenResponse struct {
|
|||||||
func (ctl *auth) Login(ctx fiber.Ctx, body *AuthBody) (*TokenResponse, error) {
|
func (ctl *auth) Login(ctx fiber.Ctx, body *AuthBody) (*TokenResponse, error) {
|
||||||
if body.Username == "admin" && body.Password == "xixi@0202" {
|
if body.Username == "admin" && body.Password == "xixi@0202" {
|
||||||
claim := ctl.jwt.CreateClaims(jwt.BaseClaims{
|
claim := ctl.jwt.CreateClaims(jwt.BaseClaims{
|
||||||
UserID: 1,
|
UserID: -20140202,
|
||||||
})
|
})
|
||||||
|
|
||||||
token, err := ctl.jwt.CreateToken(claim)
|
token, err := ctl.jwt.CreateToken(claim)
|
||||||
|
|||||||
@@ -11,37 +11,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (f *Middlewares) Auth(ctx fiber.Ctx) error {
|
func (f *Middlewares) Auth(ctx fiber.Ctx) error {
|
||||||
if ctx.Path() == "/v1/admin/auth" {
|
|
||||||
return ctx.Next()
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.HasPrefix(ctx.Path(), "/MP_verify_") {
|
if strings.HasPrefix(ctx.Path(), "/MP_verify_") {
|
||||||
return ctx.Next()
|
return ctx.Next()
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(ctx.Path(), "/v1/admin/") {
|
|
||||||
token := ctx.Get("Authorization")
|
|
||||||
if token == "" {
|
|
||||||
token = ctx.Query("token")
|
|
||||||
if token == "" {
|
|
||||||
return ctx.Status(fiber.StatusUnauthorized).SendString("Unauthorized")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
jwt, err := f.jwt.Parse(token)
|
|
||||||
if err != nil {
|
|
||||||
return ctx.Status(fiber.StatusUnauthorized).SendString("Unauthorized")
|
|
||||||
}
|
|
||||||
if jwt.UserID != 1 {
|
|
||||||
return ctx.Status(fiber.StatusForbidden).SendString("Forbidden")
|
|
||||||
}
|
|
||||||
|
|
||||||
return ctx.Next()
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.HasPrefix(ctx.Path(), "/auth/") {
|
if strings.HasPrefix(ctx.Path(), "/auth/") {
|
||||||
return ctx.Next()
|
return ctx.Next()
|
||||||
}
|
}
|
||||||
return ctx.Next()
|
|
||||||
|
|
||||||
fullUrl := string(ctx.Request().URI().FullURI())
|
fullUrl := string(ctx.Request().URI().FullURI())
|
||||||
u, err := url.Parse(fullUrl)
|
u, err := url.Parse(fullUrl)
|
||||||
|
|||||||
28
backend/app/middlewares/mid_auth_admin.go
Normal file
28
backend/app/middlewares/mid_auth_admin.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package middlewares
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gofiber/fiber/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (f *Middlewares) AuthAdmin(ctx fiber.Ctx) error {
|
||||||
|
if ctx.Path() == "/v1/admin/auth" {
|
||||||
|
return ctx.Next()
|
||||||
|
}
|
||||||
|
|
||||||
|
token := ctx.Get("Authorization")
|
||||||
|
if token == "" {
|
||||||
|
token = ctx.Query("token")
|
||||||
|
if token == "" {
|
||||||
|
return ctx.Status(fiber.StatusUnauthorized).SendString("Unauthorized")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
jwt, err := f.jwt.Parse(token)
|
||||||
|
if err != nil {
|
||||||
|
return ctx.Status(fiber.StatusUnauthorized).SendString("Unauthorized")
|
||||||
|
}
|
||||||
|
if jwt.UserID != -20140202 {
|
||||||
|
return ctx.Status(fiber.StatusForbidden).SendString("Forbidden")
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx.Next()
|
||||||
|
}
|
||||||
@@ -86,7 +86,6 @@ func Serve(cmd *cobra.Command, args []string) error {
|
|||||||
}
|
}
|
||||||
svc.Http.Engine.Use(errorx.Middleware)
|
svc.Http.Engine.Use(errorx.Middleware)
|
||||||
svc.Http.Engine.Use(svc.Middlewares.DebugMode)
|
svc.Http.Engine.Use(svc.Middlewares.DebugMode)
|
||||||
svc.Http.Engine.Use(svc.Middlewares.Auth)
|
|
||||||
|
|
||||||
svc.Http.Engine.Use(favicon.New(favicon.Config{
|
svc.Http.Engine.Use(favicon.New(favicon.Config{
|
||||||
Data: []byte{},
|
Data: []byte{},
|
||||||
@@ -94,7 +93,11 @@ func Serve(cmd *cobra.Command, args []string) error {
|
|||||||
|
|
||||||
group := svc.Http.Engine.Group("")
|
group := svc.Http.Engine.Group("")
|
||||||
for _, route := range svc.Routes {
|
for _, route := range svc.Routes {
|
||||||
route.Register(group)
|
if route.Name() == "admin" {
|
||||||
|
route.Register(group.Use(svc.Middlewares.AuthAdmin))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
route.Register(group.Use(svc.Middlewares.Auth))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := svc.Job.Start(ctx); err != nil {
|
if err := svc.Job.Start(ctx); err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user