-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathapple-portal-session-service.ts
95 lines (81 loc) · 3.31 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
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 $httpClient: Server.IHttpClient,
private $logger: ILogger
) { }
public async createUserSession(credentials: ICredentials): Promise<IApplePortalUserDetail> {
const loginConfig = await this.getLoginConfig();
const loginUrl = `${loginConfig.authServiceUrl}/auth/signin`;
const loginResponse = await this.$httpClient.httpRequest({
url: loginUrl,
method: "POST",
body: JSON.stringify({
accountName: credentials.username,
password: credentials.password,
rememberMe: true
}),
headers: {
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest',
'X-Apple-Widget-Key': loginConfig.authServiceKey,
'Accept': 'application/json, text/javascript'
}
});
this.$applePortalCookieService.updateUserSessionCookie(loginResponse.headers["set-cookie"]);
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 userDetailResponse = 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(userDetailResponse.headers["set-cookie"]);
return JSON.parse(userDetailResponse.body).data;
}
public async createWebSession(contentProviderId: number, dsId: string): Promise<string> {
const webSessionResponse = await this.$httpClient.httpRequest({
url: "https://appstoreconnect.apple.com/WebObjects/iTunesConnect.woa/ra/v1/session/webSession",
method: "POST",
body: JSON.stringify({
contentProviderId,
dsId,
ipAddress: null
}),
headers: {
'Accept': 'application/json, text/plain, */*',
'Accept-Encoding': 'gzip, deflate, br',
'X-Csrf-Itc': 'itc',
'Content-Type': 'application/json;charset=UTF-8',
'Cookie': this.$applePortalCookieService.getUserSessionCookie()
}
});
const webSessionCookie = this.$applePortalCookieService.getWebSessionCookie(webSessionResponse.headers["set-cookie"]);
return webSessionCookie;
}
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;
}
}
$injector.register("applePortalSessionService", ApplePortalSessionService);