Added access checking middleware

Added URLAccessRepository
Refactor
This commit is contained in:
PB
2025-10-22 10:53:20 +02:00
parent 89b665c3d9
commit 662a9b7ffd
11 changed files with 192 additions and 34 deletions

View File

@@ -0,0 +1,25 @@
package server
import (
domain "git.ego.freeddns.org/egommerce/identity-service/domain/repository"
"git.ego.freeddns.org/egommerce/identity-service/internal/service"
"github.com/gofiber/fiber/v2"
)
func (s *Server) AccessHandlerFn(c *fiber.Ctx) error {
url, srvName := c.Query("q"), c.Query("srv")
urlRepo := domain.NewURLAccessRepository(s.GetDatabase())
userRepo := domain.NewUserRepository(s.GetDatabase())
authSrv := service.NewAuthService(userRepo, s.GetCache())
authSrv.VerifyToken("asd")
urlAcc, err := urlRepo.FindByURLAndService(url, srvName)
if err != nil {
return s.Error(c, fiber.StatusBadRequest, "unable to fetch requested url data")
}
return c.JSON(urlAcc.Roles)
}

View File

@@ -3,6 +3,8 @@ package server
import (
"log"
cnf "git.ego.freeddns.org/egommerce/go-api-pkg/config"
jwt "github.com/gofiber/contrib/jwt"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
)
@@ -29,25 +31,25 @@ func LoggingMiddleware() func(c *fiber.Ctx) error {
func XRequestIDMiddleware() func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
requestID := uuid.New().String()
c.Set("X-Request-ID", requestID)
c.Set("X-Request-ID", uuid.New().String())
return c.Next()
}
}
// JWTProtected func for specify routes group with JWT authentication.
// See: https://github.com/gofiber/jwt
// func JWTProtected() func(*fiber.Ctx) error {
// // Create config for JWT authentication middleware.
// config := jwt.Config{
// SigningKey: []byte(baseCnf.GetEnv("JWT_ACCESS_TOKEN_SECRET_KEY", "FallbackAccessTokenSecret")),
// ContextKey: "jwt", // used in private routes
// ErrorHandler: jwtError,
// }
func JWTProtected(s *Server) func(c *fiber.Ctx) error {
secret := []byte(cnf.GetEnv("JWT_ACCESS_TOKEN_SECRET_KEY", "FallbackAccessTokenSecret"))
// return jwt.New(config)
// }
return func(c *fiber.Ctx) error {
return jwt.New(jwt.Config{
SigningKey: jwt.SigningKey{Key: secret},
ContextKey: "jwt",
ErrorHandler: func(c *fiber.Ctx, err error) error {
return s.Error(c, fiber.StatusUnauthorized, "unauthorized")
},
})(c)
}
}
// func jwtError(c *fiber.Ctx, err error) error {
// // Return status 400 Bad Request and failed authentication error.

View File

@@ -22,5 +22,6 @@ func SetupRouter(s *Server) {
s.Group("/v1").
Post("/login", s.LoginHandlerFn).
Post("/refresh", s.RefreshHandlerFn).
Post("/register", s.RegisterHandlerFn)
Post("/register", s.RegisterHandlerFn).
Get("/access", JWTProtected(s), s.AccessHandlerFn)
}

View File

@@ -36,7 +36,7 @@ func NewAuthService(userRepo *domain.UserRepository, cache *redis.Client) *Auth
}
func (a *Auth) Login(login, passwd string) (string, error) {
user, err := a.userRepo.GetByUsername(login)
user, err := a.userRepo.FindByUsername(login)
if err != nil {
// if err = database.NoRowsInQuerySet(err); err != nil {
// return "", errors.New("no user found")
@@ -75,6 +75,11 @@ func (a *Auth) RefreshToken(accessToken string) (string, error) {
return newAccessToken, nil
}
func (a *Auth) VerifyToken(token string) (string, error) {
return token, nil
}
func (a *Auth) Register(email, login, passwd string) (string, error) {
passwd, _ = passSrv.Hash(passwd)

View File

@@ -48,6 +48,9 @@ type JWT struct {
}
func (s *JWT) CreateAccessToken(id string) (string, error) {
fmt.Println(time.Now().Add(s.accessTokenExpireTime).Unix())
fmt.Println(s.accessTokenExpireTime)
claims := &jwt.StandardClaims{
Subject: id,
IssuedAt: time.Now().Unix(),