-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth2.go
86 lines (70 loc) · 1.68 KB
/
oauth2.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
package utils
import (
"crypto/md5"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"github.com/google/uuid"
"math/rand"
"strings"
)
const (
hexTable = "0123456789abcdef"
)
type Oauth2 struct {
codeVerifier string
codeChallenge string
}
func NewOauth2() *Oauth2 {
return &Oauth2{}
}
var alphabet = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
func GenerateRandomString(length uint8) string {
b := make([]rune, length)
for i := range b {
b[i] = alphabet[rand.Intn(len(alphabet))]
}
return string(b)
}
func (o *Oauth2) GenerateCodeVerifier(length uint8) {
o.codeVerifier = GenerateRandomString(length)
o.codeChallenge = GetHashOf(o.codeVerifier)
}
func (o *Oauth2) GetCodeVerifier() string {
return o.codeVerifier
}
func (o *Oauth2) GetCodeChallenge() string {
return o.codeChallenge
}
func (o *Oauth2) GetCodeChallengeMethod() string {
return "S256"
}
func (o *Oauth2) GetState() string {
return uuid.New().String()
}
func (o *Oauth2) GetNonce() string {
return uuid.New().String()
}
func GetHexString(length int) string {
var result = make([]byte, length)
for i := 0; i < length; i++ {
result[i] = hexTable[rand.Intn(15)]
}
return string(result)
}
func GetHashOf(text string) string {
var result = sha256.Sum256([]byte(text))
return strings.TrimRight(base64.URLEncoding.EncodeToString(result[:]), "=")
}
func GetHashOfWithSalt(text, salt string, timestamp int) string {
bText := []byte(text + salt)
for i, el := range bText {
bText[i] = el + byte(timestamp%i)
}
hash := md5.Sum(bText)
return hex.EncodeToString(hash[:])
}
func NewSHA256(data string) string {
hash := sha256.Sum256([]byte(data))
return hex.EncodeToString(hash[:])
}