forked from doctype/steam
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfirmations.go
126 lines (102 loc) · 3.25 KB
/
confirmations.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
package steam
import (
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"strconv"
)
type ConfirmationResponse struct {
Success bool `json:"success"`
Confirmations []*Confirmation `json:"conf"`
}
type Confirmation struct {
ID string `json:"id"`
Type uint8 `json:"type"`
Creator string `json:"creator_id"`
Nonce string `json:"nonce"`
CreationTime uint64 `json:"creation"`
// TypeName string `json:"type_name"`
// Cancel string `json:"cancel"`
// Accept string `json:"accept"`
// Icon string `json:"icon"`
// Multi bool `json:"multi"`
// Headline string `json:"headline"`
// Summary []string `json:"summary"`
// Warn interface{} `json:"warn"`
}
var (
//ErrConfirmationsUnknownError = errors.New("unknown error occurred finding confirmation")
ErrCannotFindConfirmations = errors.New("unable to find confirmation")
ErrCannotFindDescriptions = errors.New("unable to find confirmation descriptions")
ErrConfirmationsDescMismatch = errors.New("cannot match confirmation with their respective descriptions")
ErrWGTokenExpired = errors.New("WGToken expired")
)
func (session *Session) execConfirmationRequest(request, key, tag string, current int64, values map[string]string) (*http.Response, error) {
params := url.Values{
"p": {session.deviceID},
"a": {session.oauth.SteamID.ToString()},
"k": {key},
"t": {strconv.FormatInt(current, 10)},
"m": {"android"},
"tag": {tag},
}
for k, v := range values {
params.Add(k, v)
}
return session.client.Get("https://steamcommunity.com/mobileconf/" + request + params.Encode())
}
func (session *Session) GetConfirmations(identitySecret string, current int64) ([]*Confirmation, error) {
key, err := GenerateConfirmationCode(identitySecret, "conf", current)
if err != nil {
return nil, err
}
resp, err := session.execConfirmationRequest("getlist?", key, "conf", current, nil)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
var confirmationResponse ConfirmationResponse
b, _ := io.ReadAll(resp.Body)
//d := json.NewDecoder(resp.Body)
if err = json.Unmarshal(b, &confirmationResponse); err != nil || !confirmationResponse.Success {
return nil, err
}
return confirmationResponse.Confirmations, nil
}
func (session *Session) AnswerConfirmation(confirmation *Confirmation, identitySecret, answer string, current int64) error {
key, err := GenerateConfirmationCode(identitySecret, answer, current)
if err != nil {
return err
}
op := map[string]string{
"op": answer,
"cid": confirmation.ID,
"ck": confirmation.Nonce,
}
resp, err := session.execConfirmationRequest("ajaxop?", key, answer, current, op)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return err
}
type Response struct {
Success bool `json:"success"`
Message string `json:"message"`
}
var response Response
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return err
}
if !response.Success {
return errors.New(response.Message)
}
return nil
}
func (confirmation *Confirmation) Answer(session *Session, key, answer string, current int64) error {
return session.AnswerConfirmation(confirmation, key, answer, current)
}