-
Notifications
You must be signed in to change notification settings - Fork 0
/
security.go
153 lines (135 loc) · 3.43 KB
/
security.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package oauth
import (
"crypto/rc4"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
)
type TokenSecureFormatter interface {
CryptToken(source []byte) ([]byte, error)
DecryptToken(source []byte) ([]byte, error)
}
type TokenProvider struct {
secureFormatter TokenSecureFormatter
}
func NewTokenProvider(formatter TokenSecureFormatter) *TokenProvider {
return &TokenProvider{secureFormatter: formatter}
}
func (tp *TokenProvider) CryptToken(t *Token) (token string, err error) {
bToken, err := json.Marshal(t)
if err != nil {
return "", err
}
return tp.crypt(bToken)
}
func (tp *TokenProvider) CryptRefreshToken(t *RefreshToken) (token string, err error) {
bToken, err := json.Marshal(t)
if err != nil {
return "", err
}
return tp.crypt(bToken)
}
func (tp *TokenProvider) DecryptToken(token string) (t *Token, err error) {
bToken, err := tp.decrypt(token)
if err != nil {
return nil, err
}
err = json.Unmarshal(bToken, &t)
if err != nil {
return nil, err
}
return t, nil
}
func (tp *TokenProvider) DecryptRefreshTokens(refreshToken string) (refresh *RefreshToken, err error) {
bRefresh, err := tp.decrypt(refreshToken)
if err != nil {
return nil, err
}
err = json.Unmarshal(bRefresh, &refresh)
if err != nil {
return nil, err
}
return refresh, nil
}
func (tp *TokenProvider) crypt(token []byte) (string, error) {
ctoken, err := tp.secureFormatter.CryptToken(token)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(ctoken), nil
}
func (tp *TokenProvider) decrypt(token string) ([]byte, error) {
b, err := base64.StdEncoding.DecodeString(token)
if err != nil {
return nil, err
}
return tp.secureFormatter.DecryptToken(b)
}
type RC4TokenSecureFormatter struct {
key []byte
cipher *rc4.Cipher
}
func NewRC4TokenSecurityProvider(key []byte) *RC4TokenSecureFormatter {
var sc = &RC4TokenSecureFormatter{key: key}
return sc
}
func (sc *RC4TokenSecureFormatter) CryptToken(source []byte) ([]byte, error) {
dest := make([]byte, len(source))
cipher, err := rc4.NewCipher(sc.key)
if err != nil {
return nil, err
}
cipher.XORKeyStream(dest, source)
return dest, nil
}
func (sc *RC4TokenSecureFormatter) DecryptToken(source []byte) ([]byte, error) {
dest := make([]byte, len(source))
cipher, err := rc4.NewCipher(sc.key)
if err != nil {
panic(err)
}
cipher.XORKeyStream(dest, source)
return dest, nil
}
type SHA256RC4TokenSecureFormatter struct {
key []byte
cipher *rc4.Cipher
}
func NewSHA256RC4TokenSecurityProvider(key []byte) *SHA256RC4TokenSecureFormatter {
var sc = &SHA256RC4TokenSecureFormatter{key: key}
return sc
}
func (sc *SHA256RC4TokenSecureFormatter) CryptToken(source []byte) ([]byte, error) {
hasher := sha256.New()
hasher.Write(source)
hash := hasher.Sum(nil)
newSource := append(hash, source...)
dest := make([]byte, len(newSource))
cipher, err := rc4.NewCipher(sc.key)
if err != nil {
return nil, err
}
cipher.XORKeyStream(dest, newSource)
return dest, nil
}
func (sc *SHA256RC4TokenSecureFormatter) DecryptToken(source []byte) ([]byte, error) {
if len(source) < 32 {
return nil, errors.New("Invalid token")
}
dest := make([]byte, len(source))
cipher, err := rc4.NewCipher(sc.key)
if err != nil {
return nil, err
}
cipher.XORKeyStream(dest, source)
hasher := sha256.New()
hasher.Write(dest[32:])
hash := hasher.Sum(nil)
for i, b := range hash {
if b != dest[i] {
return nil, errors.New("Invalid token")
}
}
return dest[32:], nil
}