-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
44 lines (37 loc) · 1.21 KB
/
token.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
package router
import (
"fmt"
"github.com/dgrijalva/jwt-go"
"github.com/dolong2110/memorization-apps/account/model"
"io/ioutil"
)
func initAccessToken(accessTokenConfig AccessToken) (*model.AccessTokenInfo, error) {
// load rsa keys
publicKeyByte, err := ioutil.ReadFile(accessTokenConfig.PublicKeyFile)
if err != nil {
return nil, fmt.Errorf("could not read public key pem file: %w", err)
}
publicKey, err := jwt.ParseRSAPublicKeyFromPEM(publicKeyByte)
if err != nil {
return nil, fmt.Errorf("could not parse public key: %w", err)
}
privateKeyByte, err := ioutil.ReadFile(accessTokenConfig.PrivateKeyFile)
if err != nil {
return nil, fmt.Errorf("could not read private key pem file: %w", err)
}
privateKey, err := jwt.ParseRSAPrivateKeyFromPEM(privateKeyByte)
if err != nil {
return nil, fmt.Errorf("could not parse private key: %w", err)
}
return &model.AccessTokenInfo{
PublicKey: publicKey,
PrivateKey: privateKey,
Expires: accessTokenConfig.AccessTokenExpire,
}, nil
}
func initRefreshToken(refreshTokenConfig RefreshToken) *model.RefreshTokenInfo {
return &model.RefreshTokenInfo{
Secret: refreshTokenConfig.RefreshTokenSecret,
Expires: refreshTokenConfig.RefreshTokenExpire,
}
}