-
Notifications
You must be signed in to change notification settings - Fork 3
/
challenge.go
162 lines (141 loc) · 4.36 KB
/
challenge.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
154
155
156
157
158
159
160
161
162
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"time"
aesGo "github.com/aasaam/aes-go"
"github.com/pquerna/otp"
"github.com/pquerna/otp/totp"
)
type captchaRequest struct {
Lang string `json:"lang,omitempty"`
Quality int `json:"quality,omitempty"`
DifficultyLevel string `json:"level,omitempty"`
}
type captchaResponse struct {
Value int `json:"value"`
Image string `json:"image"`
}
type challenge struct {
ID string `json:"i"`
ClientTemporaryChecksum string `json:"c_t_ch"`
ClientPersistChecksum string `json:"c_p_ch"`
ChallengeType string `json:"ch_t"`
Lang string `json:"l"`
TimeInit int64 `json:"t_i"`
TimeWait int64 `json:"t_w"`
ExpireTime int64 `json:"t_o"`
TTL int64 `json:"ttl"`
TOTPSecret string `json:"totp_s"`
JSChallengeValue string `json:"js_ch_v"`
CaptchaChallengeValue int `json:"ch_ch_v"`
}
func newChallenge(
lang string,
challengeType string,
clientTemporaryChecksum string,
clientPersistChecksum string,
waitSeconds int64,
timeoutSeconds int64,
ttl int64,
) *challenge {
ch := challenge{
ID: randomBase64(),
Lang: lang,
ClientTemporaryChecksum: clientTemporaryChecksum,
ClientPersistChecksum: clientPersistChecksum,
ChallengeType: challengeType,
TimeInit: time.Now().Unix(),
TimeWait: time.Now().Unix() + waitSeconds,
ExpireTime: time.Now().Unix() + timeoutSeconds,
TTL: ttl,
}
return &ch
}
func newChallengeFromString(tokenString string, secret string) (*challenge, error) {
if tokenString == "" {
return nil, errors.New("empty token data")
}
aes := aesGo.NewAasaamAES(secret)
data := aes.DecryptTTL(tokenString)
if data == "" {
return nil, errors.New("invalid token data")
}
var ch challenge
err := json.Unmarshal([]byte(data), &ch)
if err != nil {
return nil, errors.New("invalid token structure")
}
return &ch, nil
}
func (ch *challenge) verify(clientTemporaryChecksum string) bool {
now := time.Now().Unix()
return len(clientTemporaryChecksum) > 16 && ch.ClientTemporaryChecksum == clientTemporaryChecksum && now >= ch.TimeWait && now <= ch.ExpireTime
}
func (ch *challenge) setJSValue() string {
value := randomBase64()
jsEval := fmt.Sprintf(`window.jsv=function(){return '%s'};`, value)
jsEvalEncode := base64.StdEncoding.EncodeToString([]byte(jsEval))
ch.JSChallengeValue = value
return jsEvalEncode
}
func (ch *challenge) setTOTPSecret(totpSecret string) {
ch.TOTPSecret = totpSecret
}
func (ch *challenge) setCaptchaValue(restCaptchaURL string, difficultyLevel string) (string, error) {
r := captchaRequest{
Lang: ch.Lang,
Quality: 1,
DifficultyLevel: difficultyLevel,
}
jsonBytes, _ := json.Marshal(r)
req, err0 := http.NewRequest("POST", restCaptchaURL+"/new", bytes.NewBuffer(jsonBytes))
if err0 != nil {
return "", errors.New("failed to init request captcha server")
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err1 := client.Do(req)
if err1 != nil {
return "", errors.New("cannot request to captcha server")
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var captchaRes captchaResponse
err2 := json.Unmarshal(body, &captchaRes)
if err2 != nil {
return "", errors.New("captcha server response invalid data")
}
if captchaRes.Value < 1 {
return "", errors.New("captcha server value not return")
}
ch.CaptchaChallengeValue = captchaRes.Value
return captchaRes.Image, nil
}
func (ch *challenge) verifyJSValue(v string) bool {
return v != "" && len(v) >= 32 && v == ch.JSChallengeValue
}
func (ch *challenge) verifyCaptchaValue(v int) bool {
return v > 9 && v < 9999999999 && v == ch.CaptchaChallengeValue
}
func (ch *challenge) verifyTOTP(v string) bool {
passCode, err := totp.GenerateCodeCustom(ch.TOTPSecret, time.Now(), totp.ValidateOpts{
Skew: 1,
Digits: otp.DigitsSix,
Algorithm: otp.AlgorithmSHA1,
})
return err == nil && passCode == v
}
func (ch *challenge) getChallengeToken(secret string) (string, error) {
aes := aesGo.NewAasaamAES(secret)
byteData, err := json.Marshal(ch)
if err != nil {
return "", err
}
return aes.EncryptTTL(string(byteData), ch.TTL), nil
}