-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauth_manager.go
51 lines (41 loc) · 1.46 KB
/
auth_manager.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
package auth_manager
import (
"context"
"time"
"github.com/go-redis/redis/v8"
)
const TokenByteLength = 32
type TokenType int
const (
ResetPassword TokenType = iota
VerifyEmail
AccessToken
RefreshToken
)
type AuthManager interface {
GenerateAccessToken(ctx context.Context, uuid string, expiresAt time.Duration) (string, error)
DecodeAccessToken(ctx context.Context, token string) (*AccessTokenClaims, error)
GenerateRefreshToken(ctx context.Context, uuid string, payload *RefreshTokenPayload, expiresAt time.Duration) (string, error)
TerminateRefreshTokens(ctx context.Context, uuid string) error
RemoveRefreshToken(ctx context.Context, uuid string, token string) error
DecodeRefreshToken(ctx context.Context, uuid string, token string) (*RefreshTokenPayload, error)
GeneratePlainToken(ctx context.Context, tokenType TokenType, payload *TokenPayload, expiresAt time.Duration) (string, error)
DecodePlainToken(ctx context.Context, token string, tokenType TokenType) (*TokenPayload, error)
DestroyPlainToken(ctx context.Context, key string) error
}
type AuthManagerOpts struct {
PrivateKey string
}
// Used as jwt claims
type TokenPayload struct {
UUID string `json:"uuid"`
CreatedAt time.Time `json:"createdAt"`
TokenType TokenType `json:"tokenType"`
}
type authManager struct {
redisClient *redis.Client
opts AuthManagerOpts
}
func NewAuthManager(redisClient *redis.Client, opts AuthManagerOpts) AuthManager {
return &authManager{redisClient, opts}
}