pull from dev & fix conflicts

This commit is contained in:
Abolfazl Nourzad 2024-01-26 09:47:38 +03:30
commit 06acc01e64
Signed by: abolfazl
GPG Key ID: 183534166EB62E0B
24 changed files with 75 additions and 148 deletions

View File

@ -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"`
} }

View File

@ -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,

View File

@ -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"
) )

View File

@ -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"
} }

View File

@ -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"
) )

View File

@ -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"
) )

View File

@ -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"
) )

View File

@ -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"
) )

View File

@ -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"
) )

View File

@ -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"
) )

View File

@ -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"
) )

View File

@ -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})
} }

View File

@ -11,8 +11,7 @@ import (
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"
adminkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box_req" adminkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box_req"
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"
@ -39,25 +38,25 @@ 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,
adminKinBoxReqSvc adminkindboxreqservice.Service, adminKinBoxReqSvc adminkindboxreqservice.Service,
adminKinBoxReqVld adminkindboxreqvalidator.Validator, adminKinBoxReqVld adminkindboxreqvalidator.Validator,
) 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),
adminKindBoxReqHandler: adminkindboxreqhandler.New(cfg.Auth, authSvc, adminKinBoxReqSvc, adminKinBoxReqVld), adminKindBoxReqHandler: adminkindboxreqhandler.New(cfg.Auth, adminAuthSvc, adminKinBoxReqSvc, adminKinBoxReqVld),
} }
} }

6
entity/authenticable.go Normal file
View File

@ -0,0 +1,6 @@
package entity
type Authenticable struct {
ID uint
Role string
}

12
main.go
View File

@ -17,8 +17,7 @@ import (
adminservice "git.gocasts.ir/ebhomengo/niki/service/admin/admin" adminservice "git.gocasts.ir/ebhomengo/niki/service/admin/admin"
adminkindboxservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box" adminkindboxservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box"
adminkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box_req" adminkindboxreqservice "git.gocasts.ir/ebhomengo/niki/service/admin/kind_box_req"
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"
@ -56,15 +55,15 @@ func main() {
//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, adminSvc adminservice.Service, adminVld adminvalidator.Validator,
adminAuthSvc adminauthservice.Service, adminAuthSvc authservice.Service,
adminKindBoxReqSvc adminkindboxreqservice.Service, adminKindBoxReqVld adminkindboxreqvalidator.Validator, adminKindBoxReqSvc adminkindboxreqservice.Service, adminKindBoxReqVld adminkindboxreqvalidator.Validator,
) { ) {
authSvc = authservice.New(cfg.Auth) benefactorAuthSvc = authservice.New(cfg.Auth)
MysqlRepo := mysql.New(cfg.Mysql) MysqlRepo := mysql.New(cfg.Mysql)
@ -76,6 +75,7 @@ func setupServices(cfg config.Config) (
authGenerator := authservice.New(cfg.Auth) authGenerator := authservice.New(cfg.Auth)
benefactorSvc = benefactorservice.New(cfg.BenefactorSvc, RedisOtp, otpSmsProvider, authGenerator, benefactorMysql) benefactorSvc = benefactorservice.New(cfg.BenefactorSvc, RedisOtp, otpSmsProvider, authGenerator, 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)
@ -89,7 +89,7 @@ func setupServices(cfg config.Config) (
adminKindBoxReqSvc = adminkindboxreqservice.New(benefactorKindBoxReqMysql, adminkindboxsvc) adminKindBoxReqSvc = adminkindboxreqservice.New(benefactorKindBoxReqMysql, adminkindboxsvc)
adminKindBoxReqVld = adminkindboxreqvalidator.New(benefactorKindBoxReqMysql) adminKindBoxReqVld = adminkindboxreqvalidator.New(benefactorKindBoxReqMysql)
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)

View File

@ -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"
) )

View File

@ -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)
} }

View File

@ -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 {

View File

@ -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()
}

View File

@ -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
}

View File

@ -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 {

View File

@ -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

View File

@ -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)
} }

View File

@ -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 {