/
jwt_backend.go
167 lines (134 loc) · 3.75 KB
/
jwt_backend.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package auth
import (
"bufio"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"golang.org/x/crypto/bcrypt"
"os"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/jmcvetta/neoism"
"github.com/nathandao/vantaa/services/models/user"
"github.com/nathandao/vantaa/settings"
)
type JwtAuthBackend struct {
privateKey *rsa.PrivateKey
PublicKey *rsa.PublicKey
}
const (
// expOffset is the time that the token should be removed from ledis after
// its expiration
expireOffset = 3600
)
var (
authBackendInstance *JwtAuthBackend = nil
hashCost int = settings.Get().HashCost
)
// InitJwtAuthBackend instantiate a thread-safe JwtAuthBackend instance if
// it has not been started.
func InitJwtAuthBackend() (*JwtAuthBackend, error) {
privateKey, err := getPrivateKey()
if err != nil {
return nil, err
}
publicKey, err := getPublicKey()
if err != nil {
return nil, err
}
if authBackendInstance == nil {
authBackendInstance = &JwtAuthBackend{
privateKey: privateKey,
PublicKey: publicKey,
}
}
return authBackendInstance, nil
}
// GenerateToken creates an encrypted token including the user's ID using
// signing method RS512 and the public/private key
func (authBackend *JwtAuthBackend) GenerateToken(u *user.User) (string, error) {
token := jwt.New(jwt.SigningMethodRS512)
token.Claims["exp"] = time.Now().Add(
time.Hour * time.Duration(settings.Get().JWTExpirationDelta),
).Unix()
token.Claims["iat"] = time.Now().Unix()
token.Claims["uid"] = u.Id
token.Claims["uname"] = u.Name
tokenString, err := token.SignedString(authBackend.privateKey)
if err != nil {
return "", err
}
return tokenString, nil
}
// Authenticate authenticate the provided user login credentials
func (authBackend *JwtAuthBackend) Authenticate(u *user.User) bool {
checkUser, err := user.FindUser(neoism.Props{"email": u.Email})
if err != nil {
return false
}
diff := bcrypt.CompareHashAndPassword(
[]byte(checkUser.PasswordDigest),
[]byte(u.Password),
)
if diff != nil {
return false
}
return true
}
// TerminateToken invalidates the token before its expiration time.
// This function is invoked when the user logged out.
func (authBackend *JwtAuthBackend) TerminateToken(tokenstr string, token *jwt.Token) error {
ledisExp := int64(token.Claims["exp"].(float64)) + expireOffset
if err := BlacklistToken([]byte(tokenstr), ledisExp); err != nil {
return err
}
return nil
}
// IsTerminated check if the token has been terminated before its expiration
func (authBackend *JwtAuthBackend) IsTerminated(tokenstr string) bool {
terminated, _ := IsBlacklisted([]byte(tokenstr))
if terminated {
return true
}
return false
}
func getPrivateKey() (*rsa.PrivateKey, error) {
privateKeyFile, err := os.Open(settings.Get().PrivateKeyPath)
if err != nil {
return nil, err
}
pemfileinfo, _ := privateKeyFile.Stat()
var size int64 = pemfileinfo.Size()
pembytes := make([]byte, size)
buffer := bufio.NewReader(privateKeyFile)
_, err = buffer.Read(pembytes)
data, _ := pem.Decode([]byte(pembytes))
privateKeyFile.Close()
privateKeyImported, err := x509.ParsePKCS1PrivateKey(data.Bytes)
if err != nil {
return nil, err
}
return privateKeyImported, nil
}
func getPublicKey() (*rsa.PublicKey, error) {
publicKeyFile, err := os.Open(settings.Get().PublicKeyPath)
if err != nil {
return nil, err
}
pemfileinfo, _ := publicKeyFile.Stat()
var size int64 = pemfileinfo.Size()
pembytes := make([]byte, size)
buffer := bufio.NewReader(publicKeyFile)
_, err = buffer.Read(pembytes)
data, _ := pem.Decode([]byte(pembytes))
publicKeyFile.Close()
publicKeyImported, err := x509.ParsePKIXPublicKey(data.Bytes)
if err != nil {
return nil, err
}
rsaPub, ok := publicKeyImported.(*rsa.PublicKey)
if !ok {
return nil, err
}
return rsaPub, nil
}