woj-server/internal/web/jwt/service.go

51 lines
1.1 KiB
Go
Raw Normal View History

2023-07-15 16:19:49 +08:00
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()
2024-01-05 00:44:49 +08:00
srv.SigningKey = []byte(conf.WebServer.JWT.SigningKey)
srv.ExpireHour = conf.WebServer.JWT.ExpireHour
2023-07-15 16:19:49 +08:00
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
}