-
Notifications
You must be signed in to change notification settings - Fork 13
/
authentication.go
126 lines (104 loc) · 3 KB
/
authentication.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package authentication
import (
"errors"
"strconv"
"time"
"github.com/coretrix/hitrix/service/component/jwt"
"github.com/coretrix/hitrix/service/component/password"
"github.com/latolukasz/orm"
)
type EmailPasswordProviderEntity interface {
orm.Entity
GetEmailCachedIndexName() string
GetPassword() string
}
type Authentication struct {
accessTokenTTL int
refreshTokenTTL int
passwordService *password.Password
jwtService *jwt.JWT
ormService *orm.Engine
secret string
}
func NewAuthenticationService(
secret string,
accessTokenTTL int,
refreshTokenTTL int,
ormService *orm.Engine,
passwordService *password.Password,
jwtService *jwt.JWT,
) *Authentication {
return &Authentication{
secret: secret,
accessTokenTTL: accessTokenTTL,
refreshTokenTTL: refreshTokenTTL,
passwordService: passwordService,
jwtService: jwtService,
ormService: ormService,
}
}
func (t *Authentication) Authenticate(email string, password string, entity EmailPasswordProviderEntity) (accessToken string, refreshToken string, err error) {
found := t.ormService.CachedSearchOne(entity, entity.GetEmailCachedIndexName(), email)
if !found {
return "", "", errors.New("user_not_found")
}
if !t.passwordService.VerifyPassword(password, entity.GetPassword()) {
return "", "", errors.New("invalid_password")
}
accessToken, err = t.generateTokenPair(entity.GetID(), t.accessTokenTTL)
if err != nil {
return "", "", err
}
refreshToken, err = t.generateTokenPair(entity.GetID(), t.refreshTokenTTL)
if err != nil {
return "", "", err
}
return accessToken, refreshToken, nil
}
func (t *Authentication) VerifyAccessToken(accessToken string, entity orm.Entity) error {
payload, err := t.jwtService.VerifyJWTAndGetPayload(t.secret, accessToken, time.Now().Unix())
if err != nil {
return err
}
id, err := strconv.ParseUint(payload["sub"], 10, 64)
if err != nil {
return err
}
found := t.ormService.LoadByID(id, entity)
if !found {
return errors.New("user_not_found")
}
return nil
}
func (t *Authentication) RefreshToken(refreshToken string) (newAccessToken string, newRefreshToken string, err error) {
payload, err := t.jwtService.VerifyJWTAndGetPayload(t.secret, refreshToken, time.Now().Unix())
if err != nil {
return "", "", err
}
id, err := strconv.ParseUint(payload["sub"], 10, 64)
if err != nil {
return "", "", err
}
newAccessToken, err = t.generateTokenPair(id, t.accessTokenTTL)
if err != nil {
return "", "", err
}
newRefreshToken, err = t.generateTokenPair(id, t.refreshTokenTTL)
if err != nil {
return "", "", err
}
return newAccessToken, newRefreshToken, err
}
func (t *Authentication) generateTokenPair(id uint64, ttl int) (string, error) {
headers := map[string]string{
"algo": "HS256",
"type": "JWT",
}
now := time.Now().Unix()
payload := map[string]string{
"sub": strconv.FormatUint(id, 10),
"exp": strconv.FormatInt(now+int64(ttl), 10),
"iat": strconv.FormatInt(now, 10),
}
return t.jwtService.EncodeJWT(t.secret, headers, payload)
}