diff --git a/internal/app/handler.go b/internal/app/handler.go index 3d6f664..faf3c86 100644 --- a/internal/app/handler.go +++ b/internal/app/handler.go @@ -9,6 +9,7 @@ import ( "merch-api/internal/merch" "merch-api/internal/task" "merch-api/internal/user" + "merch-api/pkg/authCheck" "merch-api/pkg/authReg" "merch-api/pkg/dbase" "merch-api/pkg/router" @@ -20,11 +21,12 @@ import ( const pkgLogHeader string = "Application |" type App struct { - cfg config.Config - router *router.Router - modules []Module - dbPool *pgxpool.Pool - tasker *task.Handler + cfg config.Config + router *router.Router + modules []Module + dbPool *pgxpool.Pool + tasker *task.Handler + serviceId int32 } func New(ctx context.Context, cfg config.Config) *App { @@ -32,9 +34,11 @@ func New(ctx context.Context, cfg config.Config) *App { cfg: cfg, } //check if service is registered - if !newApp.isRegistered(ctx, cfg) { + serviceId, registered := newApp.isRegistered(ctx, cfg) + if !registered { log.Fatalf("%v auth registration check failed", pkgLogHeader) } + newApp.serviceId = serviceId //providers u := utils.New() @@ -51,6 +55,11 @@ func New(ctx context.Context, cfg config.Config) *App { } newApp.dbPool = dbPool + sessionCheckProvider := authCheck.New(authCheck.Deps{ + Addr: net.JoinHostPort(cfg.Auth.Host, cfg.Auth.Port), + Timeout: cfg.Auth.Timeout, + }) + //providers with deps userProv := user.New(user.Deps{ DB: dbPool, @@ -63,6 +72,8 @@ func New(ctx context.Context, cfg config.Config) *App { Prefix: cfg.Http.Prefix, GinMode: cfg.Http.GinMode, UserProvider: userProv, + AuthProvider: sessionCheckProvider, + ServiceId: serviceId, }) //modules @@ -136,7 +147,7 @@ func (app *App) collectRoutes(group *gin.RouterGroup) { log.Infof("%v routes registered", pkgLogHeader) } -func (app *App) isRegistered(ctx context.Context, cfg config.Config) bool { +func (app *App) isRegistered(ctx context.Context, cfg config.Config) (int32, bool) { log.Infof("%v checking registration in auth service...", pkgLogHeader) registrar := authReg.New(authReg.Deps{ Addr: net.JoinHostPort(cfg.Auth.Host, cfg.Auth.Port), @@ -154,19 +165,19 @@ func (app *App) isRegistered(ctx context.Context, cfg config.Config) bool { if err != nil { log.WithError(err).Errorf("%v error checking registration in auth service", pkgLogHeader) - return false + return 0, false } if response == nil { log.Error("%v error checking registration in auth service", pkgLogHeader) - return false + return 0, false } if response.AlreadyRegistered == true && response.ServiceId > 0 { log.Infof("%v service registered", pkgLogHeader) - return true + return response.ServiceId, true } log.Errorf("%v something went wrong in auth registration check", pkgLogHeader) - return false + return 0, false } diff --git a/pkg/router/handler.go b/pkg/router/handler.go index 0ef9742..6d84064 100644 --- a/pkg/router/handler.go +++ b/pkg/router/handler.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "merch-api/internal/user" + "merch-api/pkg/authCheck" "net" "net/http" @@ -21,6 +22,8 @@ type Deps struct { Prefix string GinMode string UserProvider user.Provider + AuthProvider authCheck.AuthChecker + ServiceId int32 } type Router struct { @@ -54,7 +57,7 @@ func NewRouter(deps Deps) *Router { engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) - engine.Use(authMW(deps.UserProvider)) + engine.Use(authMW(deps.UserProvider, deps.AuthProvider, deps.ServiceId)) return &Router{ srv: &http.Server{ diff --git a/pkg/router/middleware.go b/pkg/router/middleware.go index 2beac11..b648761 100644 --- a/pkg/router/middleware.go +++ b/pkg/router/middleware.go @@ -4,16 +4,22 @@ import ( "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" "merch-api/internal/user" + "merch-api/pkg/authCheck" "merch-api/pkg/responses" "net/http" ) -func authMW(up user.Provider) gin.HandlerFunc { +func authMW(up user.Provider, auth authCheck.AuthChecker, serviceId int32) gin.HandlerFunc { log.Debug("Auth Middleware enabled") return func(c *gin.Context) { - //019caeab-aa81-7f09-a220-d7e675300638 //user_id 1 - userUuid := "019cd29b-e35b-7eb2-85c6-111ca0d15bff" //TODO placeholder for dev purposes - log.Warnf("%v using placeholder uuid: %v", pkgLogHeader, userUuid) + cookie, err := c.Cookie("sessionToken") + if err != nil { + c.JSON(http.StatusUnauthorized, responses.Unauthorized{Error: err.Error()}) + c.Abort() + return + } + + userUuid, err := auth.VerifySession(c, cookie, serviceId) userId, err := up.GetUserId(c, userUuid) if err != nil {