-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_twofactor.go
304 lines (254 loc) · 8.51 KB
/
auth_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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright 2021 The Go Darwin Authors
// SPDX-License-Identifier: BSD-3-Clause
package appleoauth
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"regexp"
"strings"
"unicode"
)
type securityCode struct {
Code string `json:"code"`
}
type phoneNumberID struct {
ID int `json:"id"`
}
type twoFactorCodeFromPhoneRequest struct {
SecurityCode *securityCode `json:"securityCode,omitempty"`
PhoneNumber *phoneNumberID `json:"phoneNumber,omitempty"`
Mode string `json:"mode,omitempty"`
}
type submitSecurityCodeResponse struct {
ServiceErrors []ServiceError `json:"service_errors,omitempty"`
HasError bool `json:"hasError,omitempty"`
}
// handleTwoFactor handles Two-factor authentication.
func (c *Client) handleTwoFactor(ctx context.Context, authResp *authOptionsResponse) (err error) {
logger.V(1).Info(fmt.Sprintf("Two-factor Authentication (6 digits code) is enabled for %q account", c.username))
logger.V(1).Info("More information about Apple Two-factor Authentication: https://support.apple.com/en-us/HT204915")
// verification code has already be pushed to devices
secCode := authResp.SecurityCode
if secCode == nil {
return errors.New("invalid auth option response")
}
codeLength := secCode.Length
var phoneNumber string
var body io.Reader
var codeType string
envPhoneNumber, ok := os.LookupEnv(Env2FASMSDefaultPhoneNumber)
switch {
case ok:
phoneNumber = envPhoneNumber
codeType = "phone"
body, err = c.twoFactorCodeFromEnv(ctx, authResp, phoneNumber, codeLength)
case c.isSMSAutomaticallySent(authResp):
codeType = "phone"
fallbackNumber := authResp.TrustedPhoneNumbers[0]
phoneID := fallbackNumber.ID
phoneNumber = fallbackNumber.NumberWithDialCode
pushMode := fallbackNumber.PushMode
body, err = c.twoFactorCodeFromPhone(ctx, phoneID, phoneNumber, pushMode, codeLength, false)
case c.canSMSFallback(authResp):
codeType = "phone"
body, err = c.twoFactorCodeFromPhoneChoose(ctx, authResp.TrustedPhoneNumbers, codeLength)
default:
logger.Info("Input `sms` to escape this prompt and select a trusted phone number to send the code as a text message")
logger.Info(fmt.Sprintf("You can also set the environment variable %q to automate this", Env2FASMSDefaultPhoneNumber))
code, ok := ask(os.Stdin, fmt.Sprintf("Please enter the %d digit code: ", codeLength))
if !ok {
return errors.New("could not read stdin")
}
switch code {
case "sms":
codeType = "phone"
body, err = c.twoFactorCodeFromPhoneChoose(ctx, authResp.TrustedPhoneNumbers, codeLength)
default:
if !validCode(code, codeLength) {
return fmt.Errorf("not valid code: %s", code)
}
reqBody := twoFactorCodeFromPhoneRequest{
SecurityCode: &securityCode{
Code: code,
},
}
data, err := json.Marshal(reqBody)
if err != nil {
return fmt.Errorf("marshal request body: %w", err)
}
codeType = "trusteddevice"
body = bytes.NewReader(data)
}
}
if err != nil {
return err
}
logger.V(1).Info("requesting session...")
req, err := http.NewRequestWithContext(ctx, http.MethodPost, fmt.Sprintf(endpoints[submitSecurityCode], codeType), body)
if err != nil {
return err
}
req.Header.Set(HdrContentType, "application/json")
req.Header = c.updateRequestHeaders(req.Header.Clone())
resp, err := c.hc.Do(req)
if err != nil {
return err
}
var submitResp submitSecurityCodeResponse
if err := json.NewDecoder(resp.Body).Decode(&submitResp); err != nil {
return fmt.Errorf("could not unmarshal response body: %w", err)
}
for _, svcErr := range submitResp.ServiceErrors {
if strings.Contains(svcErr.Message, "Incorrect") {
return errors.New("incorrect verification code")
}
}
return nil
}
// validCode reports whether the code is valid.
func validCode(code string, codeLength int) bool {
if len(code) != codeLength {
return false
}
for _, c := range code {
if !unicode.IsDigit(c) {
return false
}
}
return true
}
// canSMSFallback reports whether can fallback to SMS Two-factor authentication.
func (c *Client) canSMSFallback(resp *authOptionsResponse) bool {
return resp.NoTrustedDevices
}
// isSMSAutomaticallySent reports whether the sent authentication code via SMS automatically.
func (c *Client) isSMSAutomaticallySent(resp *authOptionsResponse) bool {
return len(resp.TrustedDevices) == 1 && c.canSMSFallback(resp)
}
// twoFactorCodeFromEnv requests Two-factor authentication token via SMS to the specific device automatically from the environment variable.
func (c *Client) twoFactorCodeFromEnv(ctx context.Context, resp *authOptionsResponse, phoneNumber string, codeLength int) (io.Reader, error) {
logger.V(1).Info("automatically requesting 2FA token via SMS to this number from environment variable", string(Env2FASMSDefaultPhoneNumber), phoneNumber)
var phoneID int
var pushMode string
for _, phone := range resp.TrustedPhoneNumbers {
if matchPhoneNumberToMasked(phoneNumber, phone.NumberWithDialCode) {
phoneID = phone.ID
pushMode = phone.PushMode
if pushMode == "" {
// if no pushMode, assume sms mode
pushMode = "sms"
}
break
}
}
if phoneID == 0 || pushMode == "" {
return nil, fmt.Errorf("doesn't matched %q phone number to your trusted devices", phoneNumber)
}
requestCode := !c.isSMSAutomaticallySent(resp)
return c.twoFactorCodeFromPhone(ctx, phoneID, phoneNumber, pushMode, codeLength, requestCode)
}
// twoFactorCodeFromPhoneChoose choose the any device to the requests Two-factor authentication token via SMS.
func (c *Client) twoFactorCodeFromPhoneChoose(ctx context.Context, trustedPhoneNumbers []trustedPhoneNumber, codeLength int) (io.Reader, error) {
return nil, ErrNotImplemented
}
// twoFactorCodeFromPhone requests Two-factor authentication token via SMS to phoneID device if neeeded, and returns the
// request body for the submit of security code to Apple.
func (c *Client) twoFactorCodeFromPhone(ctx context.Context, phoneID int, phoneNumber, pushMode string, codeLength int, requestCode bool) (io.Reader, error) {
if requestCode {
reqBody := twoFactorCodeFromPhoneRequest{
PhoneNumber: &phoneNumberID{
ID: phoneID,
},
Mode: pushMode,
}
data, err := json.Marshal(reqBody)
if err != nil {
return nil, err
}
// request code
req, err := http.NewRequestWithContext(ctx, http.MethodPut, endpoints[requestSecurityCode], bytes.NewReader(data))
if err != nil {
return nil, err
}
req.Header.Set(HdrContentType, "application/json")
req.Header = c.updateRequestHeaders(req.Header.Clone())
resp, err := c.hc.Do(req)
if err != nil {
return nil, err
}
if err := c.handleItuneConnect(ctx, resp.Body); err != nil {
return nil, err
}
logger.V(1).Info(fmt.Sprintf("successfully requested text message to %s", phoneNumber))
}
code, ok := ask(os.Stdin, fmt.Sprintf("Please enter the %d digit code you received at %q: ", codeLength, phoneNumber))
if !ok {
return nil, fmt.Errorf("could not read %d digit cdoe from stdin", codeLength)
}
if !validCode(code, codeLength) {
return nil, fmt.Errorf("not valid code: %s", code)
}
reqBody := twoFactorCodeFromPhoneRequest{
SecurityCode: &securityCode{
Code: code,
},
PhoneNumber: &phoneNumberID{
ID: phoneID,
},
Mode: pushMode,
}
data, err := json.Marshal(reqBody)
if err != nil {
return nil, err
}
return bytes.NewReader(data), nil
}
// matchPhoneNumberToMasked reports whether the masked phone number matched phoneNumber.
func matchPhoneNumberToMasked(phoneNumber, masked string) (matched bool) {
trimFunc := func(s string) string {
for {
idx := strings.IndexAny(s, ` -()"`)
if idx == -1 {
break
}
s = s[:idx] + s[idx+1:]
}
return s
}
// from:
// +49 162 1234567
// +1-123-456-7866
phoneNumber = trimFunc(phoneNumber)
// to:
// +491621234567
// +11234567866
// from:
// +49 •••• •••••67
// +1 (•••) •••-••66
masked = trimFunc(masked)
// to:
// +49•••••••••67
// +1••••••••66
// 9 or 8
const mark = "•"
c := strings.Count(masked, mark)
maskedRe := regexp.MustCompile(fmt.Sprintf(`^([+0-9]{2,4})(%s{%d})([0-9]{2})$`, mark, c))
maskedPair := maskedRe.FindStringSubmatch(masked)
// NOTE: range from c-2 because sometimes the masked number has 1 or 2 dots(.) more than the actual number
maskedPat := fmt.Sprintf(`^\%s([0-9]{%d,%d})%s$`, maskedPair[1], c-2, c, maskedPair[3])
// regexp:
// ^\+49([0-9]{8,9})67$
// ^\+1([0-9]{7,8})66$
// matches
// ^\+49([0-9]{8})67$
// to
// +491621234567
matched = regexp.MustCompile(maskedPat).MatchString(phoneNumber)
return matched
}