forked from doctype/steam
-
Notifications
You must be signed in to change notification settings - Fork 2
/
twofactor.go
125 lines (104 loc) · 3.12 KB
/
twofactor.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
package steam
import (
"encoding/json"
"errors"
"net/url"
"strconv"
"time"
)
type TwoFactorInfo struct {
Status uint32 `json:"status"`
SharedSecret string `json:"shared_secret"`
IdentitySecret string `json:"identity_secret"`
Secret1 string `json:"secret_1"`
SerialNumber uint64 `json:"serial_number,string"`
RevocationCode string `json:"revocation_code"`
URI string `json:"uri"`
ServerTime uint64 `json:"server_time,string"`
TokenGID string `json:"token_gid"`
}
type FinalizeTwoFactorInfo struct {
Status uint32 `json:"status"`
ServerTime uint64 `json:"server_time,string"`
}
const (
enableTwoFactorURL = APIBaseUrl + "/ITwoFactorService/AddAuthenticator/v1/"
finalizeTwoFactorURL = APIBaseUrl + "/ITwoFactorService/FinalizeAddAuthenticator/v1/"
disableTwoFactorURL = APIBaseUrl + "/ITwoFactorService/RemoveAuthenticator/v1/"
)
var ErrCannotDisable = errors.New("unable to process disable two factor request")
func (session *Session) EnableTwoFactor() (*TwoFactorInfo, error) {
resp, err := session.client.PostForm(enableTwoFactorURL, url.Values{
"steamid": {session.oauth.SteamID.ToString()},
"access_token": {session.oauth.Token},
"authenticator_time": {strconv.FormatInt(time.Now().Unix(), 10)},
"authenticator_type": {"1"}, /* 1 = Valve's, 2 = thirdparty */
"device_identifier": {session.deviceID},
"sms_phone_id": {"1"},
})
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
type Response struct {
Inner *TwoFactorInfo `json:"response"`
}
var response Response
if err = json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, err
}
return response.Inner, nil
}
func (session *Session) FinalizeTwoFactor(authCode, mobileCode string) (*FinalizeTwoFactorInfo, error) {
resp, err := session.client.PostForm(finalizeTwoFactorURL, url.Values{
"steamid": {session.oauth.SteamID.ToString()},
"access_token": {session.oauth.Token},
"authenticator_time": {strconv.FormatInt(time.Now().Unix(), 10)},
"authenticator_code": {authCode},
"activation_code": {mobileCode},
})
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
type Response struct {
Inner *FinalizeTwoFactorInfo `json:"response"`
}
var response Response
if err = json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, err
}
return response.Inner, nil
}
func (session *Session) DisableTwoFactor(revocationCode string) error {
resp, err := session.client.PostForm(disableTwoFactorURL, url.Values{
"steamid": {session.oauth.SteamID.ToString()},
"access_token": {session.oauth.Token},
"revocation_code": {revocationCode},
"steamguard_scheme": {"1"},
})
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return err
}
type Disabled struct {
Success bool `json:"success"`
}
type Response struct {
Inner *Disabled `json:"response"`
}
var response Response
if err = json.NewDecoder(resp.Body).Decode(&response); err != nil {
return err
}
if !response.Inner.Success {
return ErrCannotDisable
}
return nil
}