forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tokengen.go
53 lines (45 loc) · 1.26 KB
/
tokengen.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
package osinserver
import (
"encoding/base64"
"strings"
"crypto/rand"
"github.com/RangelReale/osin"
)
func randomBytes(len int) []byte {
b := make([]byte, len)
if _, err := rand.Read(b); err != nil {
// rand.Read should never fail
panic(err)
}
return b
}
func randomToken() string {
for {
// 32 bytes (256 bits) = 43 base64-encoded characters
b := randomBytes(32)
// Use URLEncoding to ensure we don't get / characters
s := base64.URLEncoding.EncodeToString(b)
// Don't generate tokens with leading dashes... they're hard to use on the command line
if strings.HasPrefix(s, "-") {
continue
}
// Strip trailing ='s... they're ugly
return strings.TrimRight(s, "=")
}
}
// TokenGen is an authorization and access token generator
type TokenGen struct {
}
// GenerateAuthorizeToken generates a random UUID code
func (TokenGen) GenerateAuthorizeToken(data *osin.AuthorizeData) (ret string, err error) {
return randomToken(), nil
}
// GenerateAccessToken generates random UUID access and refresh tokens
func (TokenGen) GenerateAccessToken(data *osin.AccessData, generaterefresh bool) (string, string, error) {
accesstoken := randomToken()
refreshtoken := ""
if generaterefresh {
refreshtoken = randomToken()
}
return accesstoken, refreshtoken, nil
}