UNPKG

fnbr

Version:

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

95 lines 3.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const url_1 = require("url"); const AuthSession_1 = tslib_1.__importDefault(require("./AuthSession")); const enums_1 = require("../../resources/enums"); const Endpoints_1 = tslib_1.__importDefault(require("../../resources/Endpoints")); class EOSAuthSession extends AuthSession_1.default { constructor(client, data, clientSecret, basePayload) { super(client, data, clientSecret, enums_1.AuthSessionType.EOS); this.applicationId = data.application_id; this.mergedAccounts = data.merged_accounts; this.scope = data.scope; this.refreshToken = data.refresh_token; this.refreshTokenExpiresAt = new Date(data.refresh_expires_at); this.basePayload = basePayload; } async verify(forceVerify = false) { if (!forceVerify && this.isExpired) { return false; } const tokenInfo = await this.client.http.epicgamesRequest({ method: 'POST', url: Endpoints_1.default.EOS_TOKEN_INFO, headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, data: new url_1.URLSearchParams({ token: this.accessToken, }).toString(), }); return tokenInfo.active === true; } async revoke() { clearTimeout(this.refreshTimeout); this.refreshTimeout = undefined; await this.client.http.epicgamesRequest({ method: 'POST', url: Endpoints_1.default.EOS_TOKEN_REVOKE, headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, data: new url_1.URLSearchParams({ token: this.accessToken, }).toString(), }); } async refresh() { this.refreshLock.lock(); try { clearTimeout(this.refreshTimeout); this.refreshTimeout = undefined; const response = await EOSAuthSession.authenticate(this.client, this.clientId, this.clientSecret, { ...this.basePayload, grant_type: 'refresh_token', refresh_token: this.refreshToken, }); this.accessToken = response.access_token; this.expiresAt = new Date(response.expires_at); this.refreshToken = response.refresh_token; this.refreshTokenExpiresAt = new Date(response.refresh_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 authenticate(client, clientId, clientSecret, payload) { const auth = await client.http.epicgamesRequest({ method: 'POST', url: Endpoints_1.default.EOS_TOKEN, headers: { Authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString('base64')}`, 'Content-Type': 'application/x-www-form-urlencoded', }, data: new url_1.URLSearchParams(payload).toString(), }); return auth; } static async create(client, clientId, clientSecret, createPayload, basePayload) { const auth = await EOSAuthSession.authenticate(client, clientId, clientSecret, { ...basePayload, ...createPayload, }); const session = new EOSAuthSession(client, auth, clientSecret, basePayload); session.initRefreshTimeout(); return session; } } exports.default = EOSAuthSession; //# sourceMappingURL=EOSAuthSession.js.map