UNPKG

fnbr

Version:

A library to interact with Epic Games' Fortnite HTTP and XMPP services

88 lines 3.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const AuthSession_1 = tslib_1.__importDefault(require("./AuthSession")); const enums_1 = require("../../resources/enums"); const Endpoints_1 = tslib_1.__importDefault(require("../../resources/Endpoints")); class FortniteClientCredentialsAuthSession extends AuthSession_1.default { constructor(client, data, clientSecret) { super(client, data, clientSecret, enums_1.AuthSessionType.FortniteClientCredentials); this.clientsService = data.client_service; this.isInternalClient = data.internal_client; this.productId = data.product_id; this.applicationId = data.application_id; } async verify(forceVerify = false) { if (!forceVerify && this.isExpired) { return false; } try { await this.client.http.epicgamesRequest({ url: Endpoints_1.default.OAUTH_TOKEN_VERIFY, headers: { Authorization: `bearer ${this.accessToken}`, }, }); return true; } catch (e) { return false; } } async revoke() { clearTimeout(this.refreshTimeout); this.refreshTimeout = undefined; await this.client.http.epicgamesRequest({ method: 'DELETE', url: `${Endpoints_1.default.OAUTH_TOKEN_KILL}/${this.accessToken}`, headers: { Authorization: `bearer ${this.accessToken}`, }, }); } async refresh() { this.refreshLock.lock(); try { clearTimeout(this.refreshTimeout); this.refreshTimeout = undefined; const response = await this.client.http.epicgamesRequest({ method: 'POST', url: Endpoints_1.default.OAUTH_TOKEN_CREATE, headers: { Authorization: `basic ${Buffer.from(`${this.clientId}:${this.clientSecret}`).toString('base64')}`, 'Content-Type': 'application/x-www-form-urlencoded', }, data: new URLSearchParams({ grant_type: 'client_credentials', token_type: 'eg1', }).toString(), }); this.accessToken = response.access_token; this.expiresAt = new Date(response.expires_at); this.initRefreshTimeout(); } finally { this.refreshLock.unlock(); } } initRefreshTimeout() { clearTimeout(this.refreshTimeout); this.refreshTimeout = setTimeout(() => this.refresh(), this.expiresAt.getTime() - Date.now() - 15 * 60 * 1000); } static async create(client, clientId, clientSecret, data) { const response = await client.http.epicgamesRequest({ method: 'POST', url: Endpoints_1.default.OAUTH_TOKEN_CREATE, headers: { Authorization: `basic ${Buffer.from(`${clientId}:${clientSecret}`).toString('base64')}`, 'Content-Type': 'application/x-www-form-urlencoded', }, data: new URLSearchParams(data).toString(), }); const session = new FortniteClientCredentialsAuthSession(client, response, clientSecret); session.initRefreshTimeout(); return session; } } exports.default = FortniteClientCredentialsAuthSession; //# sourceMappingURL=FortniteClientCredentialsAuthSession.js.map