|
| 1 | +export class ApplePortalSessionService implements IApplePortalSessionService { |
| 2 | + private loginConfigEndpoint = "https://appstoreconnect.apple.com/olympus/v1/app/config?hostname=itunesconnect.apple.com"; |
| 3 | + private defaultLoginConfig = { |
| 4 | + authServiceUrl : "https://idmsa.apple.com/appleautcodh", |
| 5 | + authServiceKey : "e0b80c3bf78523bfe80974d320935bfa30add02e1bff88ec2166c6bd5a706c42" |
| 6 | + }; |
| 7 | + |
| 8 | + constructor( |
| 9 | + private $applePortalCookieService: IApplePortalCookieService, |
| 10 | + private $httpClient: Server.IHttpClient, |
| 11 | + private $logger: ILogger |
| 12 | + ) { } |
| 13 | + |
| 14 | + public async createUserSession(credentials: ICredentials): Promise<IApplePortalUserDetail> { |
| 15 | + const loginConfig = await this.getLoginConfig(); |
| 16 | + const loginUrl = `${loginConfig.authServiceUrl}/auth/signin`; |
| 17 | + const loginResponse = await this.$httpClient.httpRequest({ |
| 18 | + url: loginUrl, |
| 19 | + method: "POST", |
| 20 | + body: JSON.stringify({ |
| 21 | + accountName: credentials.username, |
| 22 | + password: credentials.password, |
| 23 | + rememberMe: true |
| 24 | + }), |
| 25 | + headers: { |
| 26 | + 'Content-Type': 'application/json', |
| 27 | + 'X-Requested-With': 'XMLHttpRequest', |
| 28 | + 'X-Apple-Widget-Key': loginConfig.authServiceKey, |
| 29 | + 'Accept': 'application/json, text/javascript' |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + this.$applePortalCookieService.updateUserSessionCookie(loginResponse.headers["set-cookie"]); |
| 34 | + |
| 35 | + const sessionResponse = await this.$httpClient.httpRequest({ |
| 36 | + url: "https://appstoreconnect.apple.com/olympus/v1/session", |
| 37 | + method: "GET", |
| 38 | + headers: { |
| 39 | + 'Cookie': this.$applePortalCookieService.getUserSessionCookie() |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + this.$applePortalCookieService.updateUserSessionCookie(sessionResponse.headers["set-cookie"]); |
| 44 | + |
| 45 | + const userDetailResponse = await this.$httpClient.httpRequest({ |
| 46 | + url: "https://appstoreconnect.apple.com/WebObjects/iTunesConnect.woa/ra/user/detail", |
| 47 | + method: "GET", |
| 48 | + headers: { |
| 49 | + 'Content-Type': 'application/json', |
| 50 | + 'Cookie': this.$applePortalCookieService.getUserSessionCookie(), |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + this.$applePortalCookieService.updateUserSessionCookie(userDetailResponse.headers["set-cookie"]); |
| 55 | + |
| 56 | + return JSON.parse(userDetailResponse.body).data; |
| 57 | + } |
| 58 | + |
| 59 | + public async createWebSession(contentProviderId: number, dsId: string): Promise<string> { |
| 60 | + const webSessionResponse = await this.$httpClient.httpRequest({ |
| 61 | + url: "https://appstoreconnect.apple.com/WebObjects/iTunesConnect.woa/ra/v1/session/webSession", |
| 62 | + method: "POST", |
| 63 | + body: JSON.stringify({ |
| 64 | + contentProviderId, |
| 65 | + dsId, |
| 66 | + ipAddress: null |
| 67 | + }), |
| 68 | + headers: { |
| 69 | + 'Accept': 'application/json, text/plain, */*', |
| 70 | + 'Accept-Encoding': 'gzip, deflate, br', |
| 71 | + 'X-Csrf-Itc': 'itc', |
| 72 | + 'Content-Type': 'application/json;charset=UTF-8', |
| 73 | + 'Cookie': this.$applePortalCookieService.getUserSessionCookie() |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + const webSessionCookie = this.$applePortalCookieService.getWebSessionCookie(webSessionResponse.headers["set-cookie"]); |
| 78 | + |
| 79 | + return webSessionCookie; |
| 80 | + } |
| 81 | + |
| 82 | + private async getLoginConfig(): Promise<{authServiceUrl: string, authServiceKey: string}> { |
| 83 | + let config = null; |
| 84 | + |
| 85 | + try { |
| 86 | + const response = await this.$httpClient.httpRequest({ url: this.loginConfigEndpoint, method: "GET" }); |
| 87 | + config = JSON.parse(response.body); |
| 88 | + } catch (err) { |
| 89 | + this.$logger.trace(`Error while executing request to ${this.loginConfigEndpoint}. More info: ${err}`); |
| 90 | + } |
| 91 | + |
| 92 | + return config || this.defaultLoginConfig; |
| 93 | + } |
| 94 | +} |
| 95 | +$injector.register("applePortalSessionService", ApplePortalSessionService); |
0 commit comments