Skip to content

Instantly share code, notes, and snippets.

@pirica
Forked from MixV2/auth.js
Created July 24, 2021 11:01
Show Gist options
  • Save pirica/95cd58c4a9a4158ba5290534ad6ee12b to your computer and use it in GitHub Desktop.
Save pirica/95cd58c4a9a4158ba5290534ad6ee12b to your computer and use it in GitHub Desktop.

Revisions

  1. @MixV2 MixV2 created this gist Mar 24, 2020.
    74 changes: 74 additions & 0 deletions auth.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    // by joe/mix
    const request = require("request-promise");

    module.exports = {
    /**
    * Login to an account.
    * @param {string} email
    * @param {string} password
    */
    async authenticate(email, password) {
    let jar = await request.jar();

    await this.sendEpicRequest("https://www.epicgames.com/id/api/reputation", jar);
    await this.sendEpicRequest("https://www.epicgames.com/id/api/authenticate", jar);
    await this.sendEpicRequest("https://www.epicgames.com/id/api/location", jar);

    const csrf = await this.sendEpicRequest("https://www.epicgames.com/id/api/csrf", jar);

    const cookies = JSON.parse(JSON.stringify(jar))._jar["cookies"];

    const login = await request({
    url: "https://www.epicgames.com/id/api/login",
    method: "POST",
    headers: {
    "x-xsrf-token": cookies.find(i => i.key === "XSRF-TOKEN").value,
    "Content-Type": "application/json",
    },
    jar: jar,
    form: {
    "email": email,
    "password": password,
    "rememberMe": true
    }
    });

    const exchange = await request({
    url: "https://www.epicgames.com/id/api/exchange",
    headers: {
    "x-xsrf-token": cookies.find(i => i.key === "XSRF-TOKEN").value,
    "Content-Type": "application/json",
    },
    jar: jar,
    json: true,
    });

    const token = await request({
    url: "https://account-public-service-prod03.ol.epicgames.com/account/api/oauth/token",
    method: "POST",
    headers: {
    "Content-Type": "application/x-www-form-urlencoded",
    "Authorization": "basic ZWM2ODRiOGM2ODdmNDc5ZmFkZWEzY2IyYWQ4M2Y1YzY6ZTFmMzFjMjExZjI4NDEzMTg2MjYyZDM3YTEzZmM4NGQ=" // https://github.com/MixV2/EpicResearch#1231-authorization-header
    },
    form: {
    grant_type: "exchange_code",
    exchange_code: exchange["code"],
    includePerms: false
    },
    json: true
    })

    return token;
    },

    /**
    * Sends an "Epic" request, reuses cookie jar and shortens to one line.
    * @param {string} url
    */
    async sendEpicRequest(url, jar) {
    return await request({
    url,
    jar
    })
    }
    }
    9 changes: 9 additions & 0 deletions test.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    const auth = require("./auth");

    /**
    * An async function that authenticates and logs the account token.
    */
    ( async () => {
    const login = await auth.authenticate("[email protected]", "GreatPassword");
    console.log(login);
    })();