forked from NativeScript/nativescript-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapple-portal-session-service.ts
359 lines (313 loc) · 10.2 KB
/
apple-portal-session-service.ts
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import { isInteractive } from "../../common/helpers";
import { IErrors, Server, ICredentials } from "../../common/declarations";
import { injector } from "../../common/yok";
import {
IAppleCreateUserSessionOptions,
IApplePortalUserDetail,
IApplePortalCookieService,
IAppleLoginResult,
IApplePortalSessionService,
} from "./definitions";
import * as crypto from "crypto";
export class ApplePortalSessionService implements IApplePortalSessionService {
private loginConfigEndpoint =
"https://appstoreconnect.apple.com/olympus/v1/app/config?hostname=itunesconnect.apple.com";
private defaultLoginConfig = {
authServiceUrl: "https://idmsa.apple.com/appleautcodh",
authServiceKey:
"e0b80c3bf78523bfe80974d320935bfa30add02e1bff88ec2166c6bd5a706c42",
};
constructor(
private $applePortalCookieService: IApplePortalCookieService,
private $errors: IErrors,
private $httpClient: Server.IHttpClient,
private $logger: ILogger,
private $prompter: IPrompter
) {}
public async createUserSession(
credentials: ICredentials,
opts?: IAppleCreateUserSessionOptions
): Promise<IApplePortalUserDetail> {
const loginResult = await this.login(credentials, opts);
if (!opts || !opts.sessionBase64) {
if (loginResult.isTwoFactorAuthenticationEnabled) {
const authServiceKey = (await this.getLoginConfig()).authServiceKey;
await this.handleTwoFactorAuthentication(
loginResult.scnt,
loginResult.xAppleIdSessionId,
authServiceKey,
loginResult.hashcash
);
}
const sessionResponse = await this.$httpClient.httpRequest({
url: "https://appstoreconnect.apple.com/olympus/v1/session",
method: "GET",
headers: {
Cookie: this.$applePortalCookieService.getUserSessionCookie(),
},
});
this.$applePortalCookieService.updateUserSessionCookie(
sessionResponse.headers["set-cookie"]
);
}
const userDetailsResponse = await this.$httpClient.httpRequest({
url:
"https://appstoreconnect.apple.com/WebObjects/iTunesConnect.woa/ra/user/detail",
method: "GET",
headers: {
"Content-Type": "application/json",
Cookie: this.$applePortalCookieService.getUserSessionCookie(),
},
});
this.$applePortalCookieService.updateUserSessionCookie(
userDetailsResponse.headers["set-cookie"]
);
const userdDetails = JSON.parse(userDetailsResponse.body).data;
const result = {
...userdDetails,
...loginResult,
userSessionCookie: this.$applePortalCookieService.getUserSessionCookie(),
};
return result;
}
public async createWebSession(contentProviderId: number): Promise<string> {
const webSessionResponse = await this.$httpClient.httpRequest({
url: "https://appstoreconnect.apple.com/olympus/v1/session",
method: "POST",
body: {
provider: {
providerId: contentProviderId,
},
},
headers: {
Accept: "application/json, text/plain, */*",
"Accept-Encoding": "gzip, deflate, br",
"X-Csrf-Itc": "itc",
"Content-Type": "application/json;charset=UTF-8",
"X-Requested-With": "olympus-ui",
Cookie: this.$applePortalCookieService.getUserSessionCookie(),
},
});
const webSessionCookie = this.$applePortalCookieService.getWebSessionCookie(
webSessionResponse.headers["set-cookie"]
);
return webSessionCookie;
}
private async login(
credentials: ICredentials,
opts?: IAppleCreateUserSessionOptions
): Promise<IAppleLoginResult> {
const result = {
scnt: <string>null,
xAppleIdSessionId: <string>null,
isTwoFactorAuthenticationEnabled: false,
areCredentialsValid: true,
hashcash: <string>null,
};
if (opts && opts.sessionBase64) {
const decodedSession = Buffer.from(opts.sessionBase64, "base64").toString(
"utf8"
);
this.$applePortalCookieService.updateUserSessionCookie([decodedSession]);
result.isTwoFactorAuthenticationEnabled =
decodedSession.indexOf("DES") > -1;
} else {
try {
await this.loginCore(credentials);
} catch (err) {
const statusCode = err && err.response && err.response.status;
const bits = err?.response?.headers["x-apple-hc-bits"];
const challenge = err?.response?.headers["x-apple-hc-challenge"];
const hashcash = makeHashCash(bits, challenge);
result.hashcash = hashcash;
result.areCredentialsValid = statusCode !== 401 && statusCode !== 403;
result.isTwoFactorAuthenticationEnabled = statusCode === 409;
if (
result.isTwoFactorAuthenticationEnabled &&
opts &&
opts.requireApplicationSpecificPassword &&
!opts.applicationSpecificPassword
) {
this.$errors
.fail(`Your account has two-factor authentication enabled but --appleApplicationSpecificPassword option is not provided.
To generate an application-specific password, please go to https://appleid.apple.com/account/manage.
This password will be used for the iTunes Transporter, which is used to upload your application.`);
}
if (
result.isTwoFactorAuthenticationEnabled &&
opts &&
opts.requireInteractiveConsole &&
!isInteractive()
) {
this.$errors
.fail(`Your account has two-factor authentication enabled, but your console is not interactive.
For more details how to set up your environment, please execute "tns publish ios --help".`);
}
const headers = (err && err.response && err.response.headers) || {};
result.scnt = headers.scnt;
result.xAppleIdSessionId = headers["x-apple-id-session-id"];
}
}
return result;
}
private async loginCore(credentials: ICredentials): Promise<void> {
const loginConfig = await this.getLoginConfig();
const loginUrl = `${loginConfig.authServiceUrl}/auth/signin`;
const headers = {
"Content-Type": "application/json",
"X-Requested-With": "XMLHttpRequest",
"X-Apple-Widget-Key": loginConfig.authServiceKey,
Accept: "application/json, text/javascript",
};
const body = {
accountName: credentials.username,
password: credentials.password,
rememberMe: true,
};
const loginResponse = await this.$httpClient.httpRequest({
url: loginUrl,
method: "POST",
body,
headers,
});
this.$applePortalCookieService.updateUserSessionCookie(
loginResponse.headers["set-cookie"]
);
}
private async getLoginConfig(): Promise<{
authServiceUrl: string;
authServiceKey: string;
}> {
let config = null;
try {
const response = await this.$httpClient.httpRequest({
url: this.loginConfigEndpoint,
method: "GET",
});
config = JSON.parse(response.body);
} catch (err) {
this.$logger.trace(
`Error while executing request to ${this.loginConfigEndpoint}. More info: ${err}`
);
}
return config || this.defaultLoginConfig;
}
private async handleTwoFactorAuthentication(
scnt: string,
xAppleIdSessionId: string,
authServiceKey: string,
hashcash: string
): Promise<void> {
const headers = {
scnt: scnt,
"X-Apple-Id-Session-Id": xAppleIdSessionId,
"X-Apple-Widget-Key": authServiceKey,
"X-Apple-HC": hashcash,
Accept: "application/json",
};
const authResponse = await this.$httpClient.httpRequest({
url: "https://idmsa.apple.com/appleauth/auth",
method: "GET",
headers,
});
const data = JSON.parse(authResponse.body);
const isSMS =
data.trustedPhoneNumbers &&
data.trustedPhoneNumbers.length === 1 &&
data.noTrustedDevices; // 1 device and no trusted devices means sms was automatically sent.
const multiSMS =
data.trustedPhoneNumbers &&
data.trustedPhoneNumbers.length !== 1 &&
data.noTrustedDevices; // Not handling more than 1 sms device and no trusted devices.
let token: string;
if (
data.trustedPhoneNumbers &&
data.trustedPhoneNumbers.length &&
!multiSMS
) {
const parsedAuthResponse = JSON.parse(authResponse.body);
token = await this.$prompter.getString(
`Please enter the ${parsedAuthResponse.securityCode.length} digit code`,
{ allowEmpty: false }
);
const body: any = {
securityCode: {
code: token.toString(),
},
};
let url = `https://idmsa.apple.com/appleauth/auth/verify/trusteddevice/securitycode`;
if (isSMS) {
// No trusted devices means it must be sms.
body.mode = "sms";
body.phoneNumber = {
id: data.trustedPhoneNumbers[0].id,
};
url = `https://idmsa.apple.com/appleauth/auth/verify/phone/securitycode`;
}
await this.$httpClient.httpRequest({
url,
method: "POST",
body,
headers: { ...headers, "Content-Type": "application/json" },
});
const authTrustResponse = await this.$httpClient.httpRequest({
url: "https://idmsa.apple.com/appleauth/auth/2sv/trust",
method: "GET",
headers,
});
this.$applePortalCookieService.updateUserSessionCookie(
authTrustResponse.headers["set-cookie"]
);
} else if (multiSMS) {
this.$errors.fail(
`The NativeScript CLI does not support SMS authenticaton with multiple registered phone numbers.`
);
} else {
this.$errors.fail(
`Although response from Apple indicated activated Two-step Verification or Two-factor Authentication, NativeScript CLI don't know how to handle this response: ${data}`
);
}
}
}
injector.register("applePortalSessionService", ApplePortalSessionService);
function makeHashCash(bits: string, challenge: string): string {
const version = 1;
const dateString = getHashCanDateString();
let result: string;
for (let counter = 0; ; counter++) {
const hc = [version, bits, dateString, challenge, `:${counter}`].join(":");
const shasumData = crypto.createHash("sha1");
shasumData.update(hc);
const digest = shasumData.digest();
if (checkBits(+bits, digest)) {
result = hc;
break;
}
}
return result;
}
function getHashCanDateString(): string {
const now = new Date();
return `${now.getFullYear()}${padTo2Digits(now.getMonth() + 1)}${padTo2Digits(
now.getDate()
)}${padTo2Digits(now.getHours())}${padTo2Digits(
now.getMinutes()
)}${padTo2Digits(now.getSeconds())}`;
}
function padTo2Digits(num: number) {
return num.toString().padStart(2, "0");
}
function checkBits(bits: number, digest: Buffer) {
let result = true;
for (let i = 0; i < bits; ++i) {
result = checkBit(i, digest);
if (!result) break;
}
return result;
}
function checkBit(position: number, buffer: Buffer): boolean {
const bitOffset = position & 7; // in byte
const byteIndex = position >> 3; // in buffer
const bit = (buffer[byteIndex] >> bitOffset) & 1;
return bit === 0;
}