-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoken.go
99 lines (91 loc) · 2.63 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
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
// Package oidc provides ...
package oidc
import (
"encoding/base64"
"errors"
"time"
"github.com/deepzz0/oidc/pkg/crypto"
"github.com/deepzz0/oidc/protocol"
"github.com/golang-jwt/jwt/v4"
"github.com/google/uuid"
)
var (
defaultAESKey = []byte{89, 88, 86, 48, 97, 71, 57, 121, 97, 88, 112, 104, 100, 71, 108, 118}
testHookGenerateCode = func() (string, error) {
id, err := uuid.NewRandom()
if err != nil {
return "", err
}
return base64.RawURLEncoding.EncodeToString(id[:]), nil
}
)
// GenerateAuthorizeCodeAndSave default authorize code generator
func (s *Server) GenerateAuthorizeCodeAndSave(req *protocol.AuthorizeData) (code string, err error) {
// test hook
code, err = testHookGenerateCode()
if err != nil {
return
}
exps := req.Client.ExpirationOptions()
req.CreatedAt = time.Now()
req.ExpiresIn = exps.CodeExpiration
return code, s.options.Storage.SaveAuthorize(code, req, req.ExpiresIn)
}
// GenerateAccessTokenAndSave generate access token or refresh_token
func (s *Server) GenerateAccessTokenAndSave(req *protocol.AccessData,
genRefresh bool) (token, refresh string, err error) {
// access token, test hook
tokenID, err := testHookGenerateCode()
if err != nil {
return
}
// bearer token
now := time.Now().UTC()
exps := req.Client.ExpirationOptions()
req.CreatedAt = now
req.ExpiresIn = exps.AccessTokenExpiration
req.TokenType = string(s.options.TokenType)
switch s.options.TokenType {
case TokenTypeJWT:
claims := jwt.RegisteredClaims{
// A usual scenario is to set the expiration time relative to the current time
ExpiresAt: jwt.NewNumericDate(time.Now().Add(time.Second *
time.Duration(exps.AccessTokenExpiration))),
IssuedAt: jwt.NewNumericDate(now),
NotBefore: jwt.NewNumericDate(now),
Issuer: req.Issuer,
Subject: req.UserID,
ID: tokenID,
Audience: jwt.ClaimStrings{req.Client.ClientID()},
}
token, err = signPayload(req.Client, claims)
if err != nil {
return
}
case TokenTypeBearer:
var cipher []byte
key := make([]byte, 16)
copy(key, defaultAESKey)
copy(key, []byte(req.Client.ClientSecret()))
cipher, err = crypto.AESCBCEncrypt([]byte(tokenID), key)
if err != nil {
return
}
token = base64.RawURLEncoding.EncodeToString(cipher[:])
default:
err = errors.New("unsupported token type: " + string(s.options.TokenType))
return
}
if err = s.options.Storage.SaveAccess(token, req, exps.AccessTokenExpiration); err != nil {
return
}
// refresh token
if genRefresh {
refresh, err = testHookGenerateCode()
if err != nil {
return
}
err = s.options.Storage.SaveRefresh(refresh, token, exps.RefreshTokenExpiration)
}
return
}