forked from ebhomengo/niki
fix(service): remove duplicated auth service and use it in both of admin and benefactor side
This commit is contained in:
parent
aa129a870e
commit
bbc31ee48f
|
@ -4,8 +4,7 @@ import (
|
||||||
"git.gocasts.ir/ebhomengo/niki/adapter/redis"
|
"git.gocasts.ir/ebhomengo/niki/adapter/redis"
|
||||||
smsprovider "git.gocasts.ir/ebhomengo/niki/adapter/sms_provider/kavenegar"
|
smsprovider "git.gocasts.ir/ebhomengo/niki/adapter/sms_provider/kavenegar"
|
||||||
"git.gocasts.ir/ebhomengo/niki/repository/mysql"
|
"git.gocasts.ir/ebhomengo/niki/repository/mysql"
|
||||||
adminauthservice "git.gocasts.ir/ebhomengo/niki/service/auth/admin"
|
authservice "git.gocasts.ir/ebhomengo/niki/service/auth"
|
||||||
benefactorauthservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor"
|
|
||||||
benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor"
|
benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -14,11 +13,11 @@ type HTTPServer struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
HTTPServer HTTPServer `koanf:"http_server"`
|
HTTPServer HTTPServer `koanf:"http_server"`
|
||||||
Mysql mysql.Config `koanf:"mysql"`
|
Mysql mysql.Config `koanf:"mysql"`
|
||||||
Auth benefactorauthservice.Config `koanf:"auth"`
|
Auth authservice.Config `koanf:"auth"`
|
||||||
AdminAuth adminauthservice.Config `koanf:"admin_auth"`
|
AdminAuth authservice.Config `koanf:"admin_auth"`
|
||||||
Redis redis.Config `koanf:"redis"`
|
Redis redis.Config `koanf:"redis"`
|
||||||
KavenegarSmsProvider smsprovider.Config `koanf:"kavenegar_sms_provider"`
|
KavenegarSmsProvider smsprovider.Config `koanf:"kavenegar_sms_provider"`
|
||||||
BenefactorSvc benefactorservice.Config `koanf:"benefactor_service"`
|
BenefactorSvc benefactorservice.Config `koanf:"benefactor_service"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
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"
|
benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -13,6 +13,12 @@ func Default() Config {
|
||||||
AccessSubject: AccessTokenSubject,
|
AccessSubject: AccessTokenSubject,
|
||||||
RefreshSubject: RefreshTokenSubject,
|
RefreshSubject: RefreshTokenSubject,
|
||||||
},
|
},
|
||||||
|
AdminAuth: authservice.Config{
|
||||||
|
AccessExpirationTime: AccessTokenExpireDuration,
|
||||||
|
RefreshExpirationTime: RefreshTokenExpireDuration,
|
||||||
|
AccessSubject: AccessTokenSubject,
|
||||||
|
RefreshSubject: RefreshTokenSubject,
|
||||||
|
},
|
||||||
|
|
||||||
BenefactorSvc: benefactorservice.Config{
|
BenefactorSvc: benefactorservice.Config{
|
||||||
OtpChars: OtpChars,
|
OtpChars: OtpChars,
|
||||||
|
|
|
@ -2,7 +2,7 @@ package adminhandler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
adminservice "git.gocasts.ir/ebhomengo/niki/service/admin/admin"
|
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"
|
adminvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/admin"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package adminhandler
|
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) {
|
func (h Handler) SetRoutes(e *echo.Echo) {
|
||||||
r := e.Group("/admins")
|
r := e.Group("/admins")
|
||||||
|
@ -9,6 +12,7 @@ func (h Handler) SetRoutes(e *echo.Echo) {
|
||||||
//r.POST("/", h.Add).Name = "admin-addkindboxreq"
|
//r.POST("/", h.Add).Name = "admin-addkindboxreq"
|
||||||
r.POST("/register", h.Register)
|
r.POST("/register", h.Register)
|
||||||
r.POST("/login-by-phone", h.LoginByPhoneNumber)
|
r.POST("/login-by-phone", h.LoginByPhoneNumber)
|
||||||
|
r.GET("/test", h.LoginByPhoneNumber, middleware.Auth(h.authSvc, h.authConfig))
|
||||||
//nolint:gocritic
|
//nolint:gocritic
|
||||||
//r.PATCH("/:id", h.Update).Name = "admin-updatekindboxreq"
|
//r.PATCH("/:id", h.Update).Name = "admin-updatekindboxreq"
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ package adminkindboxhandler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
adminkindboxservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box"
|
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"
|
adminkindboxvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ package adminkindboxreqhandler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
adminkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box_req"
|
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"
|
adminkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/admin/kind_box_req"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package benefactoraddresshandler
|
package benefactoraddresshandler
|
||||||
|
|
||||||
import (
|
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"
|
benefactoraddressservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/address"
|
||||||
benefactoraddressvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/address"
|
benefactoraddressvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/address"
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package benefactorhandler
|
package benefactorhandler
|
||||||
|
|
||||||
import (
|
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"
|
benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor"
|
||||||
benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor"
|
benefactorvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/benefactor"
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package benefactorkindboxhandler
|
package benefactorkindboxhandler
|
||||||
|
|
||||||
import (
|
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"
|
benefactorkindboxservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box"
|
||||||
benefactorkindboxvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/kind_box"
|
benefactorkindboxvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/kind_box"
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package benefactorkindboxreqhandler
|
package benefactorkindboxreqhandler
|
||||||
|
|
||||||
import (
|
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"
|
benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req"
|
||||||
benefactorkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/kind_box_req"
|
benefactorkindboxreqvalidator "git.gocasts.ir/ebhomengo/niki/validator/benefactor/kind_box_req"
|
||||||
)
|
)
|
||||||
|
|
|
@ -2,7 +2,7 @@ package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.gocasts.ir/ebhomengo/niki/config"
|
"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"
|
mw "github.com/labstack/echo-jwt/v4"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
|
@ -13,7 +13,7 @@ func BenefactorAuthorization(role entity.UserRole) echo.MiddlewareFunc {
|
||||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
claims := claim.GetClaimsFromEchoContext(c)
|
claims := claim.GetClaimsFromEchoContext(c)
|
||||||
if claims.Role != role {
|
if claims.Role != role.String() {
|
||||||
return c.JSON(http.StatusForbidden, echo.Map{"message": errmsg.ErrorMsgUserNotAllowed})
|
return c.JSON(http.StatusForbidden, echo.Map{"message": errmsg.ErrorMsgUserNotAllowed})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,8 +9,7 @@ import (
|
||||||
benefactorhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/benefactor"
|
benefactorhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/benefactor"
|
||||||
benefactorkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/kind_box_req"
|
benefactorkindboxreqhandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/benefactor/kind_box_req"
|
||||||
adminservice "git.gocasts.ir/ebhomengo/niki/service/admin/admin"
|
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"
|
||||||
authservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor"
|
|
||||||
benefactoraddressservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/address"
|
benefactoraddressservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/address"
|
||||||
benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor"
|
benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor"
|
||||||
benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req"
|
benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req"
|
||||||
|
@ -35,21 +34,21 @@ func New(
|
||||||
cfg config.Config,
|
cfg config.Config,
|
||||||
benefactorSvc benefactorservice.Service,
|
benefactorSvc benefactorservice.Service,
|
||||||
benefactorVld benefactorvalidator.Validator,
|
benefactorVld benefactorvalidator.Validator,
|
||||||
authSvc authservice.Service,
|
benefactorAuthSvc authservice.Service,
|
||||||
benefactorKindBoxReqSvc benefactorkindboxreqservice.Service,
|
benefactorKindBoxReqSvc benefactorkindboxreqservice.Service,
|
||||||
benefactorKindBoxReqVld benefactorkindboxreqvalidator.Validator,
|
benefactorKindBoxReqVld benefactorkindboxreqvalidator.Validator,
|
||||||
benefactorAddressSvc benefactoraddressservice.Service,
|
benefactorAddressSvc benefactoraddressservice.Service,
|
||||||
benefactorAddressVld benefactoraddressvalidator.Validator,
|
benefactorAddressVld benefactoraddressvalidator.Validator,
|
||||||
adminSvc adminservice.Service,
|
adminSvc adminservice.Service,
|
||||||
adminVld adminvalidator.Validator,
|
adminVld adminvalidator.Validator,
|
||||||
adminAuthSvc adminauthservice.Service,
|
adminAuthSvc authservice.Service,
|
||||||
) Server {
|
) Server {
|
||||||
return Server{
|
return Server{
|
||||||
Router: echo.New(),
|
Router: echo.New(),
|
||||||
config: cfg,
|
config: cfg,
|
||||||
benefactorHandler: benefactorhandler.New(cfg.Auth, authSvc, benefactorSvc, benefactorVld),
|
benefactorHandler: benefactorhandler.New(cfg.Auth, benefactorAuthSvc, benefactorSvc, benefactorVld),
|
||||||
benefactorKindBoxReqHandler: benefactorkindboxreqhandler.New(cfg.Auth, authSvc, benefactorKindBoxReqSvc, benefactorKindBoxReqVld),
|
benefactorKindBoxReqHandler: benefactorkindboxreqhandler.New(cfg.Auth, benefactorAuthSvc, benefactorKindBoxReqSvc, benefactorKindBoxReqVld),
|
||||||
benefactorAddressHandler: benefactoraddresshandler.New(cfg.Auth, authSvc, benefactorAddressSvc, benefactorAddressVld),
|
benefactorAddressHandler: benefactoraddresshandler.New(cfg.Auth, benefactorAuthSvc, benefactorAddressSvc, benefactorAddressVld),
|
||||||
adminHandler: adminhandler.New(cfg.AdminAuth, adminAuthSvc, adminSvc, adminVld),
|
adminHandler: adminhandler.New(cfg.AdminAuth, adminAuthSvc, adminSvc, adminVld),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
package entity
|
||||||
|
|
||||||
|
type Authenticable struct {
|
||||||
|
ID uint
|
||||||
|
Role string
|
||||||
|
}
|
19
main.go
19
main.go
|
@ -14,8 +14,7 @@ import (
|
||||||
mysqlkindboxreq "git.gocasts.ir/ebhomengo/niki/repository/mysql/kind_box_req"
|
mysqlkindboxreq "git.gocasts.ir/ebhomengo/niki/repository/mysql/kind_box_req"
|
||||||
redisotp "git.gocasts.ir/ebhomengo/niki/repository/redis/redis_otp"
|
redisotp "git.gocasts.ir/ebhomengo/niki/repository/redis/redis_otp"
|
||||||
adminservice "git.gocasts.ir/ebhomengo/niki/service/admin/admin"
|
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"
|
||||||
authservice "git.gocasts.ir/ebhomengo/niki/service/auth/benefactor"
|
|
||||||
benefactoraddressservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/address"
|
benefactoraddressservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/address"
|
||||||
benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor"
|
benefactorservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/benefactor"
|
||||||
benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req"
|
benefactorkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/benefactor/kind_box_req"
|
||||||
|
@ -32,22 +31,22 @@ func main() {
|
||||||
mgr := migrator.New(cfg.Mysql)
|
mgr := migrator.New(cfg.Mysql)
|
||||||
mgr.Up()
|
mgr.Up()
|
||||||
|
|
||||||
authSvc, benefactorSvc, benefactorVld, benefactorKindBoxReqSvc, benefactorKindBoxReqVld, benefactorAddressSvc, benefactorAddressVld,
|
benefactorAuthSvc, benefactorSvc, benefactorVld, benefactorKindBoxReqSvc, benefactorKindBoxReqVld, benefactorAddressSvc, benefactorAddressVld,
|
||||||
adminSvc, adminVld, adminAuthSvc := setupServices(cfg)
|
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)
|
benefactorAddressSvc, benefactorAddressVld, adminSvc, adminVld, adminAuthSvc)
|
||||||
server.Serve()
|
server.Serve()
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:nakedret,gocritic // we are sure of this
|
//nolint:nakedret,gocritic // we are sure of this
|
||||||
func setupServices(cfg config.Config) (
|
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,
|
benefactorKindBoxReqSvc benefactorkindboxreqservice.Service, benefactorKindBoxReqVld benefactorkindboxreqvalidator.Validator,
|
||||||
benefactorAddressSvc benefactoraddressservice.Service,
|
benefactorAddressSvc benefactoraddressservice.Service,
|
||||||
benefactorAddressVld benefactoraddressvalidator.Validator,
|
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)
|
MysqlRepo := mysql.New(cfg.Mysql)
|
||||||
|
|
||||||
|
@ -56,8 +55,8 @@ func setupServices(cfg config.Config) (
|
||||||
benefactorMysql := mysqlbenefactor.New(MysqlRepo)
|
benefactorMysql := mysqlbenefactor.New(MysqlRepo)
|
||||||
kavenegarSmsProvider := smsprovider.New(cfg.KavenegarSmsProvider)
|
kavenegarSmsProvider := smsprovider.New(cfg.KavenegarSmsProvider)
|
||||||
otpSmsProvider := kavenegarotp.New(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)
|
benefactorAddressMysql := mysqladdress.New(MysqlRepo)
|
||||||
benefactorAddressSvc = benefactoraddressservice.New(benefactorAddressMysql)
|
benefactorAddressSvc = benefactoraddressservice.New(benefactorAddressMysql)
|
||||||
benefactorAddressVld = benefactoraddressvalidator.New(benefactorSvc, benefactorAddressMysql)
|
benefactorAddressVld = benefactoraddressvalidator.New(benefactorSvc, benefactorAddressMysql)
|
||||||
|
@ -67,7 +66,7 @@ func setupServices(cfg config.Config) (
|
||||||
benefactorKindBoxReqSvc = benefactorkindboxreqservice.New(benefactorKindBoxReqMysql)
|
benefactorKindBoxReqSvc = benefactorkindboxreqservice.New(benefactorKindBoxReqMysql)
|
||||||
benefactorKindBoxReqVld = benefactorkindboxreqvalidator.New(benefactorKindBoxReqMysql, benefactorSvc, benefactorAddressSvc)
|
benefactorKindBoxReqVld = benefactorkindboxreqvalidator.New(benefactorKindBoxReqMysql, benefactorSvc, benefactorAddressSvc)
|
||||||
|
|
||||||
adminAuthSvc = adminauthservice.New(cfg.AdminAuth)
|
adminAuthSvc = authservice.New(cfg.AdminAuth)
|
||||||
adminMysql := mysqladmin.New(MysqlRepo)
|
adminMysql := mysqladmin.New(MysqlRepo)
|
||||||
adminVld = adminvalidator.New(adminMysql)
|
adminVld = adminvalidator.New(adminMysql)
|
||||||
adminSvc = adminservice.New(adminMysql, adminAuthSvc)
|
adminSvc = adminservice.New(adminMysql, adminAuthSvc)
|
||||||
|
|
|
@ -2,7 +2,7 @@ package claim
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.gocasts.ir/ebhomengo/niki/config"
|
"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"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ package adminservice
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
||||||
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
|
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
|
||||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
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 {
|
if cErr := CompareHash(admin.GetPassword(), req.Password); cErr != nil {
|
||||||
return adminserviceparam.LoginWithPhoneNumberResponse{}, richerror.New(op).WithErr(cErr).WithMessage(errmsg.ErrorMsgPhoneNumberOrPassIsIncorrect).WithKind(richerror.KindForbidden)
|
return adminserviceparam.LoginWithPhoneNumberResponse{}, richerror.New(op).WithErr(cErr).WithMessage(errmsg.ErrorMsgPhoneNumberOrPassIsIncorrect).WithKind(richerror.KindForbidden)
|
||||||
}
|
}
|
||||||
|
authenticableAdmin := entity.Authenticable{
|
||||||
accessToken, aErr := s.auth.CreateAccessToken(admin)
|
ID: admin.ID,
|
||||||
|
Role: admin.Role.String(),
|
||||||
|
}
|
||||||
|
accessToken, aErr := s.auth.CreateAccessToken(authenticableAdmin)
|
||||||
if aErr != nil {
|
if aErr != nil {
|
||||||
return adminserviceparam.LoginWithPhoneNumberResponse{}, richerror.New(op).WithErr(aErr).WithKind(richerror.KindUnexpected)
|
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 {
|
if rErr != nil {
|
||||||
return adminserviceparam.LoginWithPhoneNumberResponse{}, richerror.New(op).WithErr(rErr).WithKind(richerror.KindUnexpected)
|
return adminserviceparam.LoginWithPhoneNumberResponse{}, richerror.New(op).WithErr(rErr).WithKind(richerror.KindUnexpected)
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,8 +10,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type AuthGenerator interface {
|
type AuthGenerator interface {
|
||||||
CreateAccessToken(benefactor entity.Admin) (string, error)
|
CreateAccessToken(benefactor entity.Authenticable) (string, error)
|
||||||
CreateRefreshToken(benefactor entity.Admin) (string, error)
|
CreateRefreshToken(benefactor entity.Authenticable) (string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Repository interface {
|
type Repository interface {
|
||||||
|
|
|
@ -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()
|
|
||||||
}
|
|
|
@ -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
|
|
||||||
}
|
|
|
@ -1,14 +1,13 @@
|
||||||
package adminauthservice
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.gocasts.ir/ebhomengo/niki/entity"
|
|
||||||
"github.com/golang-jwt/jwt/v4"
|
"github.com/golang-jwt/jwt/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Claims struct {
|
type Claims struct {
|
||||||
jwt.RegisteredClaims
|
jwt.RegisteredClaims
|
||||||
UserID uint `json:"user_id"`
|
UserID uint `json:"user_id"`
|
||||||
Role entity.AdminRole `json:"role"`
|
Role string `json:"role"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Claims) Valid() error {
|
func (c Claims) Valid() error {
|
|
@ -1,4 +1,4 @@
|
||||||
package adminauthservice
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -26,12 +26,12 @@ func New(cfg Config) Service {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Service) CreateAccessToken(admin entity.Admin) (string, error) {
|
func (s Service) CreateAccessToken(user entity.Authenticable) (string, error) {
|
||||||
return s.createToken(admin.ID, admin.Role, s.config.AccessSubject, s.config.AccessExpirationTime)
|
return s.createToken(user.ID, user.Role, s.config.AccessSubject, s.config.AccessExpirationTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Service) CreateRefreshToken(admin entity.Admin) (string, error) {
|
func (s Service) CreateRefreshToken(user entity.Authenticable) (string, error) {
|
||||||
return s.createToken(admin.ID, admin.Role, s.config.RefreshSubject, s.config.RefreshExpirationTime)
|
return s.createToken(user.ID, user.Role, s.config.RefreshSubject, s.config.RefreshExpirationTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Service) ParseToken(bearerToken string) (*Claims, error) {
|
func (s Service) ParseToken(bearerToken string) (*Claims, error) {
|
||||||
|
@ -53,7 +53,7 @@ func (s Service) ParseToken(bearerToken string) (*Claims, error) {
|
||||||
return nil, err
|
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
|
// create a signer for rsa 256
|
||||||
// TODO - replace with rsa 256 RS256 - https://github.com/golang-jwt/jwt/blob/main/http_example_test.go
|
// TODO - replace with rsa 256 RS256 - https://github.com/golang-jwt/jwt/blob/main/http_example_test.go
|
||||||
|
|
|
@ -42,12 +42,16 @@ func (s Service) LoginOrRegister(ctx context.Context, req benefactoreparam.Login
|
||||||
benefactor = newBenefactor
|
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 {
|
if aErr != nil {
|
||||||
return benefactoreparam.LoginOrRegisterResponse{}, richerror.New(op).WithErr(aErr).WithKind(richerror.KindUnexpected)
|
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 {
|
if rErr != nil {
|
||||||
return benefactoreparam.LoginOrRegisterResponse{}, richerror.New(op).WithErr(rErr).WithKind(richerror.KindUnexpected)
|
return benefactoreparam.LoginOrRegisterResponse{}, richerror.New(op).WithErr(rErr).WithKind(richerror.KindUnexpected)
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,8 +20,8 @@ type Repository interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
type AuthGenerator interface {
|
type AuthGenerator interface {
|
||||||
CreateAccessToken(benefactor entity.Benefactor) (string, error)
|
CreateAccessToken(benefactor entity.Authenticable) (string, error)
|
||||||
CreateRefreshToken(benefactor entity.Benefactor) (string, error)
|
CreateRefreshToken(benefactor entity.Authenticable) (string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type RedisOtp interface {
|
type RedisOtp interface {
|
||||||
|
|
Loading…
Reference in New Issue