forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokengen.go
48 lines (40 loc) · 1.14 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
package osinserver
import (
"encoding/base64"
"io"
"strings"
"crypto/rand"
"github.com/RangelReale/osin"
)
func randomBytes(len int) []byte {
b := make([]byte, len)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
// rand.Reader should never fail
panic(err.Error())
}
return b
}
func randomToken() string {
// 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)
// 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
}