-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathverifier.go
87 lines (74 loc) · 1.93 KB
/
verifier.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
package go_oauth_pkce_code_verifier
import (
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"fmt"
"io"
"strings"
)
const (
DefaultLength = 32
MinLength = 32
MaxLength = 96
)
type CodeVerifier struct {
Value string
}
func CreateCodeVerifier() (*CodeVerifier, error) {
return CreateCodeVerifierWithLength(DefaultLength)
}
func CreateCodeVerifierWithLength(length int) (*CodeVerifier, error) {
if length < MinLength || length > MaxLength {
return nil, fmt.Errorf("invalid length: %v", length)
}
buf, err := randomBytes(length)
if err != nil {
return nil, fmt.Errorf("failed to generate random bytes: %v", err)
}
return CreateCodeVerifierFromBytes(buf)
}
func CreateCodeVerifierFromBytes(b []byte) (*CodeVerifier, error) {
return &CodeVerifier{
Value: encode(b),
}, nil
}
func (v *CodeVerifier) String() string {
return v.Value
}
func (v *CodeVerifier) CodeChallengePlain() string {
return v.Value
}
func (v *CodeVerifier) CodeChallengeS256() string {
h := sha256.New()
h.Write([]byte(v.Value))
return encode(h.Sum(nil))
}
func encode(msg []byte) string {
encoded := base64.StdEncoding.EncodeToString(msg)
encoded = strings.Replace(encoded, "+", "-", -1)
encoded = strings.Replace(encoded, "/", "_", -1)
encoded = strings.Replace(encoded, "=", "", -1)
return encoded
}
// https://tools.ietf.org/html/rfc7636#section-4.1)
func randomBytes(length int) ([]byte, error) {
const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
const csLen = byte(len(charset))
output := make([]byte, 0, length)
for {
buf := make([]byte, length)
if _, err := io.ReadFull(rand.Reader, buf); err != nil {
return nil, fmt.Errorf("failed to read random bytes: %v", err)
}
for _, b := range buf {
// Avoid bias by using a value range that's a multiple of 62
if b < (csLen * 4) {
output = append(output, charset[b%csLen])
if len(output) == length {
return output, nil
}
}
}
}
}