51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package jwt
|
|
|
|
import (
|
|
"git.0x7f.app/WOJ/woj-server/internal/e"
|
|
"git.0x7f.app/WOJ/woj-server/internal/misc/config"
|
|
"git.0x7f.app/WOJ/woj-server/internal/misc/log"
|
|
"git.0x7f.app/WOJ/woj-server/internal/model"
|
|
"git.0x7f.app/WOJ/woj-server/internal/repo/cache"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/samber/do"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
var _ Service = (*service)(nil)
|
|
|
|
type Service interface {
|
|
ParseToken(tokenText string) (*model.Claim, e.Status)
|
|
SignClaim(claim *model.Claim) (string, e.Status)
|
|
Validate(claim *model.Claim) bool
|
|
|
|
Handler(forced bool) gin.HandlerFunc
|
|
|
|
HealthCheck() error
|
|
}
|
|
|
|
func NewService(i *do.Injector) (Service, error) {
|
|
srv := &service{}
|
|
srv.log = do.MustInvoke[log.Service](i).GetLogger("jwt")
|
|
srv.cacheService = do.MustInvoke[cache.Service](i) // .Get().(*redis.Client)
|
|
|
|
conf := do.MustInvoke[config.Service](i).GetConfig()
|
|
srv.SigningKey = []byte(conf.WebServer.JwtSigningKey)
|
|
srv.ExpireHour = conf.WebServer.JwtExpireHour
|
|
|
|
return srv, srv.err
|
|
}
|
|
|
|
type service struct {
|
|
cacheService cache.Service
|
|
log *zap.Logger
|
|
|
|
SigningKey []byte
|
|
ExpireHour int
|
|
|
|
err error
|
|
}
|
|
|
|
func (s *service) HealthCheck() error {
|
|
return s.err
|
|
}
|