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