From bbc31ee48f522a3c7d9cf27e71bbf9acae2ed25b Mon Sep 17 00:00:00 2001 From: masoodk Date: Thu, 25 Jan 2024 15:13:39 +0330 Subject: [PATCH] fix(service): remove duplicated auth service and use it in both of admin and benefactor side --- config/config.go | 17 ++-- config/default.go | 8 +- delivery/http_server/admin/admin/handler.go | 2 +- delivery/http_server/admin/admin/route.go | 6 +- .../http_server/admin/kind_box/handler.go | 2 +- .../http_server/admin/kind_box_req/handler.go | 2 +- .../http_server/benefactor/address/handler.go | 2 +- .../benefactor/benefactor/handler.go | 2 +- .../benefactor/kind_box/handler.go | 2 +- .../benefactor/kind_box_req/handler.go | 2 +- delivery/http_server/middleware/auth.go | 2 +- .../middleware/benefactor_authorization.go | 2 +- delivery/http_server/server.go | 13 ++-- entity/authenticable.go | 6 ++ main.go | 19 +++-- pkg/claim/echo.go | 2 +- service/admin/admin/login.go | 10 ++- service/admin/admin/service.go | 4 +- service/auth/benefactor/claims.go | 16 ---- service/auth/benefactor/service.go | 78 ------------------- service/auth/{admin => }/claims.go | 7 +- service/auth/{admin => }/service.go | 12 +-- .../benefactor/benefactor/login_register.go | 8 +- service/benefactor/benefactor/service.go | 4 +- 24 files changed, 77 insertions(+), 151 deletions(-) create mode 100644 entity/authenticable.go delete mode 100644 service/auth/benefactor/claims.go delete mode 100644 service/auth/benefactor/service.go rename service/auth/{admin => }/claims.go (52%) rename service/auth/{admin => }/service.go (76%) diff --git a/config/config.go b/config/config.go index ee971f4..fb4c3d3 100644 --- a/config/config.go +++ b/config/config.go @@ -4,8 +4,7 @@ import ( "git.gocasts.ir/ebhomengo/niki/adapter/redis" smsprovider "git.gocasts.ir/ebhomengo/niki/adapter/sms_provider/kavenegar" "git.gocasts.ir/ebhomengo/niki/repository/mysql" - adminauthservice "git.gocasts.ir/ebhomengo/niki/service/auth/admin" - benefactorauthservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor" + authservice "git.gocasts.ir/ebhomengo/niki/service/auth" benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor" ) @@ -14,11 +13,11 @@ type HTTPServer struct { } type Config struct { - HTTPServer HTTPServer `koanf:"http_server"` - Mysql mysql.Config `koanf:"mysql"` - Auth benefactorauthservice.Config `koanf:"auth"` - AdminAuth adminauthservice.Config `koanf:"admin_auth"` - Redis redis.Config `koanf:"redis"` - KavenegarSmsProvider smsprovider.Config `koanf:"kavenegar_sms_provider"` - BenefactorSvc benefactorservice.Config `koanf:"benefactor_service"` + HTTPServer HTTPServer `koanf:"http_server"` + Mysql mysql.Config `koanf:"mysql"` + Auth authservice.Config `koanf:"auth"` + AdminAuth authservice.Config `koanf:"admin_auth"` + Redis redis.Config `koanf:"redis"` + KavenegarSmsProvider smsprovider.Config `koanf:"kavenegar_sms_provider"` + BenefactorSvc benefactorservice.Config `koanf:"benefactor_service"` } diff --git a/config/default.go b/config/default.go index 3db87f6..b7cf72a 100644 --- a/config/default.go +++ b/config/default.go @@ -1,7 +1,7 @@ package config import ( - authservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor" + authservice "git.gocasts.ir/ebhomengo/niki/service/auth" benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor" ) @@ -13,6 +13,12 @@ func Default() Config { AccessSubject: AccessTokenSubject, RefreshSubject: RefreshTokenSubject, }, + AdminAuth: authservice.Config{ + AccessExpirationTime: AccessTokenExpireDuration, + RefreshExpirationTime: RefreshTokenExpireDuration, + AccessSubject: AccessTokenSubject, + RefreshSubject: RefreshTokenSubject, + }, BenefactorSvc: benefactorservice.Config{ OtpChars: OtpChars, diff --git a/delivery/http_server/admin/admin/handler.go b/delivery/http_server/admin/admin/handler.go index e867ec5..7db1cc5 100644 --- a/delivery/http_server/admin/admin/handler.go +++ b/delivery/http_server/admin/admin/handler.go @@ -2,7 +2,7 @@ package adminhandler import ( adminservice "git.gocasts.ir/ebhomengo/niki/service/admin/admin" - adminauthservice "git.gocasts.ir/ebhomengo/niki/service/auth/admin" + adminauthservice "git.gocasts.ir/ebhomengo/niki/service/auth" adminvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/admin" ) diff --git a/delivery/http_server/admin/admin/route.go b/delivery/http_server/admin/admin/route.go index 90552a5..d15776d 100644 --- a/delivery/http_server/admin/admin/route.go +++ b/delivery/http_server/admin/admin/route.go @@ -1,6 +1,9 @@ package adminhandler -import "github.com/labstack/echo/v4" +import ( + "git.gocasts.ir/ebhomengo/niki/delivery/http_server/middleware" + "github.com/labstack/echo/v4" +) func (h Handler) SetRoutes(e *echo.Echo) { r := e.Group("/admins") @@ -9,6 +12,7 @@ func (h Handler) SetRoutes(e *echo.Echo) { //r.POST("/", h.Add).Name = "admin-addkindboxreq" r.POST("/register", h.Register) r.POST("/login-by-phone", h.LoginByPhoneNumber) + r.GET("/test", h.LoginByPhoneNumber, middleware.Auth(h.authSvc, h.authConfig)) //nolint:gocritic //r.PATCH("/:id", h.Update).Name = "admin-updatekindboxreq" } diff --git a/delivery/http_server/admin/kind_box/handler.go b/delivery/http_server/admin/kind_box/handler.go index 481a699..df616d4 100644 --- a/delivery/http_server/admin/kind_box/handler.go +++ b/delivery/http_server/admin/kind_box/handler.go @@ -2,7 +2,7 @@ package adminkindboxhandler import ( adminkindboxservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box" - authservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor" + authservice "git.gocasts.ir/ebhomengo/niki/service/auth" adminkindboxvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box" ) diff --git a/delivery/http_server/admin/kind_box_req/handler.go b/delivery/http_server/admin/kind_box_req/handler.go index 7b7d42b..cc99b6c 100644 --- a/delivery/http_server/admin/kind_box_req/handler.go +++ b/delivery/http_server/admin/kind_box_req/handler.go @@ -2,7 +2,7 @@ package adminkindboxreqhandler import ( adminkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box_req" - authservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor" + authservice "git.gocasts.ir/ebhomengo/niki/service/auth" adminkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box_req" ) diff --git a/delivery/http_server/benefactor/address/handler.go b/delivery/http_server/benefactor/address/handler.go index 5357b9a..5f25668 100644 --- a/delivery/http_server/benefactor/address/handler.go +++ b/delivery/http_server/benefactor/address/handler.go @@ -1,7 +1,7 @@ package benefactoraddresshandler import ( - authservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor" + authservice "git.gocasts.ir/ebhomengo/niki/service/auth" benefactoraddressservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/address" benefactoraddressvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/address" ) diff --git a/delivery/http_server/benefactor/benefactor/handler.go b/delivery/http_server/benefactor/benefactor/handler.go index d6947a0..274b990 100644 --- a/delivery/http_server/benefactor/benefactor/handler.go +++ b/delivery/http_server/benefactor/benefactor/handler.go @@ -1,7 +1,7 @@ package benefactorhandler import ( - authservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor" + authservice "git.gocasts.ir/ebhomengo/niki/service/auth" benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor" benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor" ) diff --git a/delivery/http_server/benefactor/kind_box/handler.go b/delivery/http_server/benefactor/kind_box/handler.go index 5993b78..0808277 100644 --- a/delivery/http_server/benefactor/kind_box/handler.go +++ b/delivery/http_server/benefactor/kind_box/handler.go @@ -1,7 +1,7 @@ package benefactorkindboxhandler import ( - authservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor" + authservice "git.gocasts.ir/ebhomengo/niki/service/auth" benefactorkindboxservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box" benefactorkindboxvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/kind_box" ) diff --git a/delivery/http_server/benefactor/kind_box_req/handler.go b/delivery/http_server/benefactor/kind_box_req/handler.go index 7e3a534..5b803a6 100644 --- a/delivery/http_server/benefactor/kind_box_req/handler.go +++ b/delivery/http_server/benefactor/kind_box_req/handler.go @@ -1,7 +1,7 @@ package benefactorkindboxreqhandler import ( - authservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor" + authservice "git.gocasts.ir/ebhomengo/niki/service/auth" benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req" benefactorkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/kind_box_req" ) diff --git a/delivery/http_server/middleware/auth.go b/delivery/http_server/middleware/auth.go index a721ccf..24515be 100644 --- a/delivery/http_server/middleware/auth.go +++ b/delivery/http_server/middleware/auth.go @@ -2,7 +2,7 @@ package middleware import ( "git.gocasts.ir/ebhomengo/niki/config" - authservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor" + authservice "git.gocasts.ir/ebhomengo/niki/service/auth" mw "github.com/labstack/echo-jwt/v4" "github.com/labstack/echo/v4" ) diff --git a/delivery/http_server/middleware/benefactor_authorization.go b/delivery/http_server/middleware/benefactor_authorization.go index 547b7fa..0371cc0 100644 --- a/delivery/http_server/middleware/benefactor_authorization.go +++ b/delivery/http_server/middleware/benefactor_authorization.go @@ -13,7 +13,7 @@ func BenefactorAuthorization(role entity.UserRole) echo.MiddlewareFunc { return func(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { claims := claim.GetClaimsFromEchoContext(c) - if claims.Role != role { + if claims.Role != role.String() { return c.JSON(http.StatusForbidden, echo.Map{"message": errmsg.ErrorMsgUserNotAllowed}) } diff --git a/delivery/http_server/server.go b/delivery/http_server/server.go index 261dddb..76e412d 100644 --- a/delivery/http_server/server.go +++ b/delivery/http_server/server.go @@ -9,8 +9,7 @@ import ( benefactorhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/benefactor" benefactorkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/kind_box_req" adminservice "git.gocasts.ir/ebhomengo/niki/service/admin/admin" - adminauthservice "git.gocasts.ir/ebhomengo/niki/service/auth/admin" - authservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor" + authservice "git.gocasts.ir/ebhomengo/niki/service/auth" benefactoraddressservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/address" benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor" benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req" @@ -35,21 +34,21 @@ func New( cfg config.Config, benefactorSvc benefactorservice.Service, benefactorVld benefactorvalidator.Validator, - authSvc authservice.Service, + benefactorAuthSvc authservice.Service, benefactorKindBoxReqSvc benefactorkindboxreqservice.Service, benefactorKindBoxReqVld benefactorkindboxreqvalidator.Validator, benefactorAddressSvc benefactoraddressservice.Service, benefactorAddressVld benefactoraddressvalidator.Validator, adminSvc adminservice.Service, adminVld adminvalidator.Validator, - adminAuthSvc adminauthservice.Service, + adminAuthSvc authservice.Service, ) Server { return Server{ Router: echo.New(), config: cfg, - benefactorHandler: benefactorhandler.New(cfg.Auth, authSvc, benefactorSvc, benefactorVld), - benefactorKindBoxReqHandler: benefactorkindboxreqhandler.New(cfg.Auth, authSvc, benefactorKindBoxReqSvc, benefactorKindBoxReqVld), - benefactorAddressHandler: benefactoraddresshandler.New(cfg.Auth, authSvc, benefactorAddressSvc, benefactorAddressVld), + benefactorHandler: benefactorhandler.New(cfg.Auth, benefactorAuthSvc, benefactorSvc, benefactorVld), + benefactorKindBoxReqHandler: benefactorkindboxreqhandler.New(cfg.Auth, benefactorAuthSvc, benefactorKindBoxReqSvc, benefactorKindBoxReqVld), + benefactorAddressHandler: benefactoraddresshandler.New(cfg.Auth, benefactorAuthSvc, benefactorAddressSvc, benefactorAddressVld), adminHandler: adminhandler.New(cfg.AdminAuth, adminAuthSvc, adminSvc, adminVld), } } diff --git a/entity/authenticable.go b/entity/authenticable.go new file mode 100644 index 0000000..9f60e70 --- /dev/null +++ b/entity/authenticable.go @@ -0,0 +1,6 @@ +package entity + +type Authenticable struct { + ID uint + Role string +} diff --git a/main.go b/main.go index c834341..d41602b 100644 --- a/main.go +++ b/main.go @@ -14,8 +14,7 @@ import ( mysqlkindboxreq "git.gocasts.ir/ebhomengo/niki/repository/mysql/kind_box_req" redisotp "git.gocasts.ir/ebhomengo/niki/repository/redis/redis_otp" adminservice "git.gocasts.ir/ebhomengo/niki/service/admin/admin" - adminauthservice "git.gocasts.ir/ebhomengo/niki/service/auth/admin" - authservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor" + authservice "git.gocasts.ir/ebhomengo/niki/service/auth" benefactoraddressservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/address" benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor" benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req" @@ -32,22 +31,22 @@ func main() { mgr := migrator.New(cfg.Mysql) mgr.Up() - authSvc, benefactorSvc, benefactorVld, benefactorKindBoxReqSvc, benefactorKindBoxReqVld, benefactorAddressSvc, benefactorAddressVld, + benefactorAuthSvc, benefactorSvc, benefactorVld, benefactorKindBoxReqSvc, benefactorKindBoxReqVld, benefactorAddressSvc, benefactorAddressVld, adminSvc, adminVld, adminAuthSvc := setupServices(cfg) - server := httpserver.New(cfg, benefactorSvc, benefactorVld, authSvc, benefactorKindBoxReqSvc, benefactorKindBoxReqVld, + server := httpserver.New(cfg, benefactorSvc, benefactorVld, benefactorAuthSvc, benefactorKindBoxReqSvc, benefactorKindBoxReqVld, benefactorAddressSvc, benefactorAddressVld, adminSvc, adminVld, adminAuthSvc) server.Serve() } //nolint:nakedret,gocritic // we are sure of this func setupServices(cfg config.Config) ( - authSvc authservice.Service, benefactorSvc benefactorservice.Service, benefactorVld benefactorvalidator.Validator, + benefactorAuthSvc authservice.Service, benefactorSvc benefactorservice.Service, benefactorVld benefactorvalidator.Validator, benefactorKindBoxReqSvc benefactorkindboxreqservice.Service, benefactorKindBoxReqVld benefactorkindboxreqvalidator.Validator, benefactorAddressSvc benefactoraddressservice.Service, benefactorAddressVld benefactoraddressvalidator.Validator, - adminSvc adminservice.Service, adminVld adminvalidator.Validator, adminAuthSvc adminauthservice.Service, + adminSvc adminservice.Service, adminVld adminvalidator.Validator, adminAuthSvc authservice.Service, ) { - authSvc = authservice.New(cfg.Auth) + benefactorAuthSvc = authservice.New(cfg.Auth) MysqlRepo := mysql.New(cfg.Mysql) @@ -56,8 +55,8 @@ func setupServices(cfg config.Config) ( benefactorMysql := mysqlbenefactor.New(MysqlRepo) kavenegarSmsProvider := smsprovider.New(cfg.KavenegarSmsProvider) otpSmsProvider := kavenegarotp.New(kavenegarSmsProvider) - authGenerator := authservice.New(cfg.Auth) - benefactorSvc = benefactorservice.New(cfg.BenefactorSvc, RedisOtp, otpSmsProvider, authGenerator, benefactorMysql) + + benefactorSvc = benefactorservice.New(cfg.BenefactorSvc, RedisOtp, otpSmsProvider, benefactorAuthSvc, benefactorMysql) benefactorAddressMysql := mysqladdress.New(MysqlRepo) benefactorAddressSvc = benefactoraddressservice.New(benefactorAddressMysql) benefactorAddressVld = benefactoraddressvalidator.New(benefactorSvc, benefactorAddressMysql) @@ -67,7 +66,7 @@ func setupServices(cfg config.Config) ( benefactorKindBoxReqSvc = benefactorkindboxreqservice.New(benefactorKindBoxReqMysql) benefactorKindBoxReqVld = benefactorkindboxreqvalidator.New(benefactorKindBoxReqMysql, benefactorSvc, benefactorAddressSvc) - adminAuthSvc = adminauthservice.New(cfg.AdminAuth) + adminAuthSvc = authservice.New(cfg.AdminAuth) adminMysql := mysqladmin.New(MysqlRepo) adminVld = adminvalidator.New(adminMysql) adminSvc = adminservice.New(adminMysql, adminAuthSvc) diff --git a/pkg/claim/echo.go b/pkg/claim/echo.go index e3db441..7fcff1a 100644 --- a/pkg/claim/echo.go +++ b/pkg/claim/echo.go @@ -2,7 +2,7 @@ package claim import ( "git.gocasts.ir/ebhomengo/niki/config" - authservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor" + authservice "git.gocasts.ir/ebhomengo/niki/service/auth" "github.com/labstack/echo/v4" ) diff --git a/service/admin/admin/login.go b/service/admin/admin/login.go index f8b6d87..9726a6b 100644 --- a/service/admin/admin/login.go +++ b/service/admin/admin/login.go @@ -3,6 +3,7 @@ package adminservice import ( "context" + "git.gocasts.ir/ebhomengo/niki/entity" adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" @@ -19,13 +20,16 @@ func (s Service) LoginWithPhoneNumber(ctx context.Context, req adminserviceparam if cErr := CompareHash(admin.GetPassword(), req.Password); cErr != nil { return adminserviceparam.LoginWithPhoneNumberResponse{}, richerror.New(op).WithErr(cErr).WithMessage(errmsg.ErrorMsgPhoneNumberOrPassIsIncorrect).WithKind(richerror.KindForbidden) } - - accessToken, aErr := s.auth.CreateAccessToken(admin) + authenticableAdmin := entity.Authenticable{ + ID: admin.ID, + Role: admin.Role.String(), + } + accessToken, aErr := s.auth.CreateAccessToken(authenticableAdmin) if aErr != nil { return adminserviceparam.LoginWithPhoneNumberResponse{}, richerror.New(op).WithErr(aErr).WithKind(richerror.KindUnexpected) } - refreshToken, rErr := s.auth.CreateRefreshToken(admin) + refreshToken, rErr := s.auth.CreateRefreshToken(authenticableAdmin) if rErr != nil { return adminserviceparam.LoginWithPhoneNumberResponse{}, richerror.New(op).WithErr(rErr).WithKind(richerror.KindUnexpected) } diff --git a/service/admin/admin/service.go b/service/admin/admin/service.go index e8617ab..a5fe3dc 100644 --- a/service/admin/admin/service.go +++ b/service/admin/admin/service.go @@ -10,8 +10,8 @@ import ( ) type AuthGenerator interface { - CreateAccessToken(benefactor entity.Admin) (string, error) - CreateRefreshToken(benefactor entity.Admin) (string, error) + CreateAccessToken(benefactor entity.Authenticable) (string, error) + CreateRefreshToken(benefactor entity.Authenticable) (string, error) } type Repository interface { diff --git a/service/auth/benefactor/claims.go b/service/auth/benefactor/claims.go deleted file mode 100644 index a57c59d..0000000 --- a/service/auth/benefactor/claims.go +++ /dev/null @@ -1,16 +0,0 @@ -package benefactorauthservice - -import ( - "git.gocasts.ir/ebhomengo/niki/entity" - "github.com/golang-jwt/jwt/v4" -) - -type Claims struct { - jwt.RegisteredClaims - UserID uint `json:"user_id"` - Role entity.UserRole `json:"role"` -} - -func (c Claims) Valid() error { - return c.RegisteredClaims.Valid() -} diff --git a/service/auth/benefactor/service.go b/service/auth/benefactor/service.go deleted file mode 100644 index cbf7edc..0000000 --- a/service/auth/benefactor/service.go +++ /dev/null @@ -1,78 +0,0 @@ -package benefactorauthservice - -import ( - "strings" - "time" - - "git.gocasts.ir/ebhomengo/niki/entity" - "github.com/golang-jwt/jwt/v4" -) - -type Config struct { - SignKey string `koanf:"sign_key"` - AccessExpirationTime time.Duration `koanf:"access_expiration_time"` - RefreshExpirationTime time.Duration `koanf:"refresh_expiration_time"` - AccessSubject string `koanf:"access_subject"` - RefreshSubject string `koanf:"refresh_subject"` -} - -type Service struct { - config Config -} - -func New(cfg Config) Service { - return Service{ - config: cfg, - } -} - -func (s Service) CreateAccessToken(benefactor entity.Benefactor) (string, error) { - return s.createToken(benefactor.ID, benefactor.Role, s.config.AccessSubject, s.config.AccessExpirationTime) -} - -func (s Service) CreateRefreshToken(benefactor entity.Benefactor) (string, error) { - return s.createToken(benefactor.ID, benefactor.Role, s.config.RefreshSubject, s.config.RefreshExpirationTime) -} - -func (s Service) ParseToken(bearerToken string) (*Claims, error) { - // https://pkg.go.dev/github.com/golang-jwt/jwt/v5#example-ParseWithClaims-CustomClaimsType - - tokenStr := strings.Replace(bearerToken, "Bearer ", "", 1) - - token, err := jwt.ParseWithClaims(tokenStr, &Claims{}, func(token *jwt.Token) (interface{}, error) { - return []byte(s.config.SignKey), nil - }) - if err != nil { - return nil, err - } - - if claims, ok := token.Claims.(*Claims); ok && token.Valid { - return claims, nil - } - - return nil, err -} - -func (s Service) createToken(userID uint, role entity.UserRole, subject string, expireDuration time.Duration) (string, error) { - // create a signer for rsa 256 - // TODO - replace with rsa 256 RS256 - https://github.com/golang-jwt/jwt/blob/main/http_example_test.go - - // set our claims - claims := Claims{ - RegisteredClaims: jwt.RegisteredClaims{ - Subject: subject, - ExpiresAt: jwt.NewNumericDate(time.Now().Add(expireDuration)), - }, - UserID: userID, - Role: role, - } - - // TODO - add sign method to config - accessToken := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) - tokenString, err := accessToken.SignedString([]byte(s.config.SignKey)) - if err != nil { - return "", err - } - - return tokenString, nil -} diff --git a/service/auth/admin/claims.go b/service/auth/claims.go similarity index 52% rename from service/auth/admin/claims.go rename to service/auth/claims.go index 25ed388..361e01f 100644 --- a/service/auth/admin/claims.go +++ b/service/auth/claims.go @@ -1,14 +1,13 @@ -package adminauthservice +package auth import ( - "git.gocasts.ir/ebhomengo/niki/entity" "github.com/golang-jwt/jwt/v4" ) type Claims struct { jwt.RegisteredClaims - UserID uint `json:"user_id"` - Role entity.AdminRole `json:"role"` + UserID uint `json:"user_id"` + Role string `json:"role"` } func (c Claims) Valid() error { diff --git a/service/auth/admin/service.go b/service/auth/service.go similarity index 76% rename from service/auth/admin/service.go rename to service/auth/service.go index aada499..e0945fe 100644 --- a/service/auth/admin/service.go +++ b/service/auth/service.go @@ -1,4 +1,4 @@ -package adminauthservice +package auth import ( "strings" @@ -26,12 +26,12 @@ func New(cfg Config) Service { } } -func (s Service) CreateAccessToken(admin entity.Admin) (string, error) { - return s.createToken(admin.ID, admin.Role, s.config.AccessSubject, s.config.AccessExpirationTime) +func (s Service) CreateAccessToken(user entity.Authenticable) (string, error) { + return s.createToken(user.ID, user.Role, s.config.AccessSubject, s.config.AccessExpirationTime) } -func (s Service) CreateRefreshToken(admin entity.Admin) (string, error) { - return s.createToken(admin.ID, admin.Role, s.config.RefreshSubject, s.config.RefreshExpirationTime) +func (s Service) CreateRefreshToken(user entity.Authenticable) (string, error) { + return s.createToken(user.ID, user.Role, s.config.RefreshSubject, s.config.RefreshExpirationTime) } func (s Service) ParseToken(bearerToken string) (*Claims, error) { @@ -53,7 +53,7 @@ func (s Service) ParseToken(bearerToken string) (*Claims, error) { return nil, err } -func (s Service) createToken(userID uint, role entity.AdminRole, subject string, expireDuration time.Duration) (string, error) { +func (s Service) createToken(userID uint, role, subject string, expireDuration time.Duration) (string, error) { // create a signer for rsa 256 // TODO - replace with rsa 256 RS256 - https://github.com/golang-jwt/jwt/blob/main/http_example_test.go diff --git a/service/benefactor/benefactor/login_register.go b/service/benefactor/benefactor/login_register.go index 59b843b..e67f8b4 100644 --- a/service/benefactor/benefactor/login_register.go +++ b/service/benefactor/benefactor/login_register.go @@ -42,12 +42,16 @@ func (s Service) LoginOrRegister(ctx context.Context, req benefactoreparam.Login benefactor = newBenefactor } - accessToken, aErr := s.auth.CreateAccessToken(benefactor) + athenticableBenefactor := entity.Authenticable{ + ID: benefactor.ID, + Role: benefactor.Role.String(), + } + accessToken, aErr := s.auth.CreateAccessToken(athenticableBenefactor) if aErr != nil { return benefactoreparam.LoginOrRegisterResponse{}, richerror.New(op).WithErr(aErr).WithKind(richerror.KindUnexpected) } - refreshToken, rErr := s.auth.CreateRefreshToken(benefactor) + refreshToken, rErr := s.auth.CreateRefreshToken(athenticableBenefactor) if rErr != nil { return benefactoreparam.LoginOrRegisterResponse{}, richerror.New(op).WithErr(rErr).WithKind(richerror.KindUnexpected) } diff --git a/service/benefactor/benefactor/service.go b/service/benefactor/benefactor/service.go index 8e2136a..7d79838 100644 --- a/service/benefactor/benefactor/service.go +++ b/service/benefactor/benefactor/service.go @@ -20,8 +20,8 @@ type Repository interface { } type AuthGenerator interface { - CreateAccessToken(benefactor entity.Benefactor) (string, error) - CreateRefreshToken(benefactor entity.Benefactor) (string, error) + CreateAccessToken(benefactor entity.Authenticable) (string, error) + CreateRefreshToken(benefactor entity.Authenticable) (string, error) } type RedisOtp interface {