gplayapi-ts
Version:
Google Play API wrapper in TypeScript
102 lines • 3.56 kB
JavaScript
import { doRequest } from "../../utils/network";
import { parseResponse, createAuthParams, setOAuthParams } from "../../utils/parser";
import { GPAuthenticationError, GPNilPayloadError } from "../../errors";
import { URL_CHECK_IN, URL_AUTH, URL_UPLOAD_DEVICE_CONFIG, URL_TOC, URL_TOS_ACCEPT, AUTH_SERVICE_URL, AUTH_APP } from "../constants";
import {
AndroidCheckinResponse
} from "../../gen_proto/googleplay_pb";
class AuthEndpoint {
constructor(authData, deviceInfo, getAuthHeaders, doAuthedReq) {
this.authData = authData;
this.deviceInfo = deviceInfo;
this.getAuthHeaders = getAuthHeaders;
this.doAuthedReq = doAuthedReq;
}
async generateGsfId() {
const req = this.deviceInfo.generateAndroidCheckInRequest();
const checkInResp = await this.checkIn(req);
const gsfId = checkInResp.androidId.toString(16);
this.authData.gsfId = gsfId;
this.authData.deviceCheckInConsistencyToken = checkInResp.deviceCheckinConsistencyToken;
return gsfId;
}
async checkIn(req) {
const headers = {
...this.getAuthHeaders(),
"Content-Type": "application/x-protobuffer",
Host: "android.clients.google.com"
};
const { data: responseData, status } = await doRequest(URL_CHECK_IN, {
method: "POST",
body: Buffer.from(req.toBinary()),
headers
});
if (status !== 200) {
throw new GPAuthenticationError(`Check-in failed with status ${status}`);
}
return AndroidCheckinResponse.fromBinary(responseData);
}
async uploadDeviceConfig() {
const { UploadDeviceConfigRequest } = await import("../../gen_proto/googleplay_pb");
const data = new UploadDeviceConfigRequest({
deviceConfiguration: this.deviceInfo.getDeviceConfigProto()
});
const payload = await this.doAuthedReq(URL_UPLOAD_DEVICE_CONFIG, {
method: "POST",
body: Buffer.from(data.toBinary())
});
if (!payload || !payload.uploadDeviceConfigResponse) {
throw new GPNilPayloadError();
}
return payload.uploadDeviceConfigResponse;
}
async generateGPToken() {
const params = createAuthParams(
this.authData.gsfId,
this.deviceInfo.build.sdkVersion,
this.authData.email,
this.deviceInfo.build.googleServices
);
setOAuthParams(params, this.authData.aasToken);
params.set("app", AUTH_APP);
params.set("service", AUTH_SERVICE_URL);
const sortedParams = new URLSearchParams([...params.entries()].sort((a, b) => a[0].localeCompare(b[0])));
const headers = this.getAuthHeaders();
const { data } = await doRequest(`${URL_AUTH}?${sortedParams.toString()}`, {
method: "POST",
headers
});
const response = parseResponse(data.toString());
const token = response.Auth;
if (!token) {
throw new GPAuthenticationError("Could not generate oauth token");
}
return token;
}
async toc() {
const payload = await this.doAuthedReq(URL_TOC, { method: "GET" });
const tocResp = payload?.tocResponse;
if (tocResp?.tosContent && tocResp?.tosToken) {
await this.acceptTos(tocResp.tosToken);
}
if (tocResp?.cookie) {
this.authData.dfeCookie = tocResp.cookie;
}
if (!tocResp) {
throw new GPNilPayloadError();
}
return tocResp;
}
async acceptTos(tosToken) {
await this.doAuthedReq(`${URL_TOS_ACCEPT}?toscme=false&tost=${tosToken}`, {
method: "POST"
});
}
async regenerateGPToken() {
this.authData.authToken = await this.generateGPToken();
}
}
export {
AuthEndpoint
};
//# sourceMappingURL=auth.mjs.map