MW fix from hardcode
This commit is contained in:
parent
a66873d12d
commit
64a242cdea
3 changed files with 36 additions and 16 deletions
|
|
@ -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"
|
||||
|
|
@ -25,6 +26,7 @@ type App struct {
|
|||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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{
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue