/
interface.go
69 lines (61 loc) 路 2.4 KB
/
interface.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package tokenservice
import (
"context"
"time"
"github.com/naofel1/api-golang-template/internal/configs"
"github.com/naofel1/api-golang-template/internal/ent"
"github.com/naofel1/api-golang-template/internal/primitive"
"github.com/google/uuid"
"github.com/uptrace/opentelemetry-go-extra/otelzap"
"go.opentelemetry.io/otel/trace"
)
// Interface specifies the business operations of the service.
type Interface interface {
NewPairFromStudent(ctx context.Context, u *ent.Student, prevTokenID string) (*PairToken, error)
NewPairFromAdmin(ctx context.Context, u *ent.Admin, prevTokenID string) (*PairToken, error)
Signout(ctx context.Context, uid uuid.UUID) error
ValidateStudentIDToken(ctx context.Context, tokenString string) (*ent.Student, error)
ValidateAdminIDToken(ctx context.Context, tokenString string) (*ent.Admin, error)
GetRoleFromIDToken(tokenString string) (primitive.Roles, error)
ValidateRefreshToken(ctx context.Context, refreshTokenString string) (*RefreshToken, error)
}
// Config will hold repository and used utils that will be injected
// into this Service layer on service initialization
type Config struct {
TokenRepository Repository
StudCert *configs.CertPair
AdminCert *configs.CertPair
Logger *otelzap.Logger
Tracer trace.Tracer
Conf *configs.Jwt
}
// New configures and returns an Interface implementation.
func New(c *Config) Interface {
return &tokenService{
TokenRepository: c.TokenRepository,
StudCert: c.StudCert,
AdminCert: c.AdminCert,
RefreshSecret: c.Conf.RefreshSecret,
IDExpiration: c.Conf.TokenDuration,
RefreshExpiration: c.Conf.RefreshDuration,
Logger: c.Logger,
Tracer: c.Tracer,
}
}
// tokenService implements tokenService.Interface.
type tokenService struct {
TokenRepository Repository
StudCert *configs.CertPair
AdminCert *configs.CertPair
Tracer trace.Tracer
Logger *otelzap.Logger
RefreshSecret string
IDExpiration time.Duration
RefreshExpiration time.Duration
}
// Repository contain all the function available in the defined domain
type Repository interface {
SetRefreshToken(ctx context.Context, userID string, tokenID string, expiresIn time.Duration) error
DeleteRefreshToken(ctx context.Context, userID string, prevTokenID string) error
DeleteUserRefreshTokens(ctx context.Context, userID string) error
}