From d6beaf530203022f12df0e7e44e375e1c2692138 Mon Sep 17 00:00:00 2001 From: SpatzlHD Date: Fri, 22 Apr 2022 21:31:09 +0200 Subject: [PATCH] Fixed 403 Forbidden Error and reduced used npm packages to 1 --- package-lock.json | 104 +++------ package.json | 6 +- src/api.js | 580 ++++++++++++++++++++++++++-------------------- 3 files changed, 373 insertions(+), 317 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7101324..ed12984 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,86 +1,58 @@ { "name": "@liamcottle/valorant.js", "version": "1.2.1", - "lockfileVersion": 1, + "lockfileVersion": 2, "requires": true, - "dependencies": { - "axios": { + "packages": { + "": { + "name": "@liamcottle/valorant.js", + "version": "1.2.1", + "license": "MIT", + "dependencies": { + "axios": "^0.21.1" + } + }, + "node_modules/axios": { "version": "0.21.1", "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz", "integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==", - "requires": { + "dependencies": { "follow-redirects": "^1.10.0" } }, - "axios-cookiejar-support": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/axios-cookiejar-support/-/axios-cookiejar-support-1.0.1.tgz", - "integrity": "sha512-IZJxnAJ99XxiLqNeMOqrPbfR7fRyIfaoSLdPUf4AMQEGkH8URs0ghJK/xtqBsD+KsSr3pKl4DEQjCn834pHMig==", + "node_modules/follow-redirects": { + "version": "1.13.1", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.1.tgz", + "integrity": "sha512-SSG5xmZh1mkPGyKzjZP8zLjltIfpW32Y5QpdNJyjcfGxK3qo3NDDkZOZSFiGn1A6SclQxY9GzEwAHQ3dmYRWpg==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + } + }, + "dependencies": { + "axios": { + "version": "0.21.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz", + "integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==", "requires": { - "is-redirect": "^1.0.0", - "pify": "^5.0.0" + "follow-redirects": "^1.10.0" } }, "follow-redirects": { "version": "1.13.1", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.1.tgz", "integrity": "sha512-SSG5xmZh1mkPGyKzjZP8zLjltIfpW32Y5QpdNJyjcfGxK3qo3NDDkZOZSFiGn1A6SclQxY9GzEwAHQ3dmYRWpg==" - }, - "is-redirect": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/is-redirect/-/is-redirect-1.0.0.tgz", - "integrity": "sha1-HQPd7VO9jbDzDCbk+V02/HyH3CQ=" - }, - "pify": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/pify/-/pify-5.0.0.tgz", - "integrity": "sha512-eW/gHNMlxdSP6dmG6uJip6FXN0EQBwm2clYYd8Wul42Cwu/DK8HEftzsapcNdYe2MfLiIwZqsDk2RDEsTE79hA==" - }, - "psl": { - "version": "1.8.0", - "resolved": "https://registry.npmjs.org/psl/-/psl-1.8.0.tgz", - "integrity": "sha512-RIdOzyoavK+hA18OGGWDqUTsCLhtA7IcZ/6NCs4fFJaHBDab+pDDmDIByWFRQJq2Cd7r1OoQxBGKOaztq+hjIQ==" - }, - "punycode": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", - "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==" - }, - "querystring": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/querystring/-/querystring-0.2.0.tgz", - "integrity": "sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA=" - }, - "tough-cookie": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-4.0.0.tgz", - "integrity": "sha512-tHdtEpQCMrc1YLrMaqXXcj6AxhYi/xgit6mZu1+EDWUn+qhUf8wMQoFIy9NXuq23zAwtcB0t/MjACGR18pcRbg==", - "requires": { - "psl": "^1.1.33", - "punycode": "^2.1.1", - "universalify": "^0.1.2" - } - }, - "universalify": { - "version": "0.1.2", - "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz", - "integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==" - }, - "url": { - "version": "0.11.0", - "resolved": "https://registry.npmjs.org/url/-/url-0.11.0.tgz", - "integrity": "sha1-ODjpfPxgUh63PFJajlW/3Z4uKPE=", - "requires": { - "punycode": "1.3.2", - "querystring": "0.2.0" - }, - "dependencies": { - "punycode": { - "version": "1.3.2", - "resolved": "https://registry.npmjs.org/punycode/-/punycode-1.3.2.tgz", - "integrity": "sha1-llOgNvt8HuQjQvIyXM7v6jkmxI0=" - } - } } } } diff --git a/package.json b/package.json index 6602688..067b8b4 100644 --- a/package.json +++ b/package.json @@ -24,10 +24,6 @@ }, "homepage": "https://github.com/liamcottle/valorant.js#readme", "dependencies": { - "axios": "^0.21.1", - "axios-cookiejar-support": "^1.0.1", - "querystring": "^0.2.0", - "tough-cookie": "^4.0.0", - "url": "^0.11.0" + "axios": "^0.21.1" } } diff --git a/src/api.js b/src/api.js index a9a22a1..3813079 100644 --- a/src/api.js +++ b/src/api.js @@ -1,254 +1,342 @@ "use strict"; -const axios = require('axios').default; -const axiosCookieJarSupport = require('axios-cookiejar-support').default; -const querystring = require('querystring'); -const tough = require('tough-cookie'); -const url = require('url'); - -const regions = require('./regions'); - -axiosCookieJarSupport(axios); +const axios = require("axios").default; +const { Agent } = require("https"); + +const regions = require("./regions"); + +const ciphers = [ + "TLS_CHACHA20_POLY1305_SHA256", + "TLS_AES_128_GCM_SHA256", + "TLS_AES_256_GCM_SHA384", + "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256", +]; +const agent = new Agent({ + ciphers: ciphers.join(":"), + honorCipherOrder: true, + minVersion: "TLSv1.2", +}); +const parseUrl = (uri) => { + let url = new URL(uri); + let params = new URLSearchParams(url.hash.substring(1)); + let access_token = params.get("access_token"); + let id_token = params.get("id_token"); + + return { access_token, id_token }; +}; class API { - - constructor(region = regions.AsiaPacific) { - this.region = region; - this.username = null; - this.user_id = null; - this.access_token = null; - this.entitlements_token = null; - this.client_version = 'release-03.00-shipping-22-574489'; - this.client_platform = { - "platformType": "PC", - "platformOS": "Windows", - "platformOSVersion": "10.0.19042.1.256.64bit", - "platformChipset": "Unknown" - }; - } - - getPlayerDataServiceUrl(region) { - return `https://pd.${region}.a.pvp.net`; - } - - getPartyServiceUrl(region) { - return `https://glz-${region}-1.${region}.a.pvp.net`; - } - - getSharedDataServiceUrl(region) { - return `https://shared.${region}.a.pvp.net`; - } - - generateRequestHeaders(extraHeaders = {}) { - // generate default headers - const defaultHeaders = { - 'Authorization': `Bearer ${this.access_token}`, - 'X-Riot-Entitlements-JWT': this.entitlements_token, - 'X-Riot-ClientVersion': this.client_version, - 'X-Riot-ClientPlatform': Buffer.from(JSON.stringify(this.client_platform)).toString('base64'), - }; - - // merge in extra headers - return { - ...defaultHeaders, - ...extraHeaders, + constructor(region = regions.AsiaPacific) { + this.region = region; + this.username = null; + this.user_id = null; + this.access_token = null; + this.entitlements_token = null; + this.client_version = "release-03.00-shipping-22-574489"; + this.client_platform = { + platformType: "PC", + platformOS: "Windows", + platformOSVersion: "10.0.19042.1.256.64bit", + platformChipset: "Unknown", + }; + } + + getPlayerDataServiceUrl(region) { + return `https://pd.${region}.a.pvp.net`; + } + + getPartyServiceUrl(region) { + return `https://glz-${region}-1.${region}.a.pvp.net`; + } + + getSharedDataServiceUrl(region) { + return `https://shared.${region}.a.pvp.net`; + } + + generateRequestHeaders(extraHeaders = {}) { + // generate default headers + const defaultHeaders = { + Authorization: `Bearer ${this.access_token}`, + "X-Riot-Entitlements-JWT": this.entitlements_token, + "X-Riot-ClientVersion": this.client_version, + "X-Riot-ClientPlatform": Buffer.from( + JSON.stringify(this.client_platform) + ).toString("base64"), + }; + + // merge in extra headers + return { + ...defaultHeaders, + ...extraHeaders, + }; + } + + async authorize(username, password) { + const cookie = ( + await axios.post( + "https://auth.riotgames.com/api/v1/authorization", + { + client_id: "play-valorant-web-prod", + nonce: 1, + redirect_uri: "https://playvalorant.com/opt_in", + response_type: "token id_token", + scope: "account openid", + }, + { + headers: { + "User-Agent": + "RiotClient/43.0.1.4195386.4190634 rso-auth (Windows; 10;;Professional, x64)", + }, + httpsAgent: agent, } - } - - authorize(username, password) { - - const cookieJar = new tough.CookieJar(); - - return axios.post('https://auth.riotgames.com/api/v1/authorization', { - 'client_id': 'play-valorant-web-prod', - 'nonce': '1', - 'redirect_uri': 'https://playvalorant.com/opt_in', - 'response_type': 'token id_token', - },{ - jar: cookieJar, - withCredentials: true, - }).then(() => { - return axios.put('https://auth.riotgames.com/api/v1/authorization', { - 'type': 'auth', - 'username': username, - 'password': password, - },{ - jar: cookieJar, - withCredentials: true, - }).then((response) => { - - // check for error - if(response.data.error){ - throw new Error(response.data.error); - } - - // parse uri - var parsedUrl = url.parse(response.data.response.parameters.uri); - - // strip # from hash - var hash = parsedUrl.hash.replace('#', ''); - - // parse query string from hash - var parts = querystring.parse(hash); - - // return access token - return parts.access_token - }); - }).then((access_token) => { - return axios.post('https://entitlements.auth.riotgames.com/api/token/v1',{},{ - jar: cookieJar, - withCredentials: true, - headers: { - 'Authorization': `Bearer ${access_token}`, - }, - }).then((response) => { - this.username = username; - this.access_token = access_token; - this.entitlements_token = response.data.entitlements_token; - }); - }).then(() => { - return axios.post('https://auth.riotgames.com/userinfo',{},{ - jar: cookieJar, - withCredentials: true, - headers: { - 'Authorization': `Bearer ${this.access_token}`, - }, - }).then((response) => { - this.user_id = response.data.sub; - }); - }); - } - - getConfig(region = this.region) { - return axios.get(this.getSharedDataServiceUrl(region) + '/v1/config/' + region); - } - - getContent() { - return axios.get(this.getSharedDataServiceUrl(this.region) + '/content-service/v2/content', { - headers: this.generateRequestHeaders(), - }); - } - - getEntitlements(playerId) { - return axios.get(this.getPlayerDataServiceUrl(this.region) + `/store/v1/entitlements/${playerId}`,{ - headers: this.generateRequestHeaders(), - }); - } - - getMatch(matchId) { - return axios.get(this.getPlayerDataServiceUrl(this.region) + `/match-details/v1/matches/${matchId}`,{ - headers: this.generateRequestHeaders(), - }); - } - - getParty(partyId) { - return axios.get(this.getPartyServiceUrl(this.region) + `/parties/v1/parties/${partyId}`,{ - headers: this.generateRequestHeaders(), - }); - } - - getPartyByPlayer(playerId) { - return axios.get(this.getPartyServiceUrl(this.region) + `/parties/v1/players/${playerId}`,{ - headers: this.generateRequestHeaders(), - }); - } - - getCompetitiveLeaderboard(seasonId, startIndex = 0, size = 510) { - return axios.get(this.getPlayerDataServiceUrl(this.region) + `/mmr/v1/leaderboards/affinity/${this.region}/queue/competitive/season/${seasonId}?startIndex=${startIndex}&size=${size}`,{ - headers: this.generateRequestHeaders(), - }); - } - - getPlayerLoadout(playerId) { - return axios.get(this.getPlayerDataServiceUrl(this.region) + `/personalization/v2/players/${playerId}/playerloadout`,{ - headers: this.generateRequestHeaders(), - }); - } - - getPlayerMMR(playerId) { - return axios.get(this.getPlayerDataServiceUrl(this.region) + `/mmr/v1/players/${playerId}`,{ - headers: this.generateRequestHeaders(), - }); - } - - getPlayerMatchHistory(playerId, startIndex = 0, endIndex = 10) { - return axios.get(this.getPlayerDataServiceUrl(this.region) + `/match-history/v1/history/${playerId}?startIndex=${startIndex}&endIndex=${endIndex}`,{ - headers: this.generateRequestHeaders(), - }); - } - - getPlayerCompetitiveHistory(playerId, startIndex = 0, endIndex = 10) { - return axios.get(this.getPlayerDataServiceUrl(this.region) + `/mmr/v1/players/${playerId}/competitiveupdates?startIndex=${startIndex}&endIndex=${endIndex}`,{ - headers: this.generateRequestHeaders(), - }); - } - - getPlayerAccountXp(playerId) { - return axios.get(this.getPlayerDataServiceUrl(this.region) + `/account-xp/v1/players/${playerId}`,{ - headers: this.generateRequestHeaders(), - }); - } - - getPlayerWallet(playerId) { - return axios.get(this.getPlayerDataServiceUrl(this.region) + `/store/v1/wallet/${playerId}`,{ - headers: this.generateRequestHeaders(), - }); - } - - getPlayerStoreFront(playerId) { - return axios.get(this.getPlayerDataServiceUrl(this.region) + `/store/v2/storefront/${playerId}`,{ - headers: this.generateRequestHeaders(), - }); - } - - getPlayers(playerIds) { - return axios.put(this.getPlayerDataServiceUrl(this.region) + '/name-service/v2/players', playerIds,{ - headers: this.generateRequestHeaders(), - }); - } - - getSession(playerId) { - return axios.get(this.getPartyServiceUrl(this.region) + `/session/v1/sessions/${playerId}`,{ - headers: this.generateRequestHeaders(), - }); - } - - getContractDefinitions() { - return axios.get(this.getPlayerDataServiceUrl(this.region) + '/contract-definitions/v2/definitions',{ - headers: this.generateRequestHeaders(), - }); - } - - getStoryContractDefinitions() { - return axios.get(this.getPlayerDataServiceUrl(this.region) + '/contract-definitions/v2/definitions/story',{ - headers: this.generateRequestHeaders(), - }); - } - - getStoreOffers() { - return axios.get(this.getPlayerDataServiceUrl(this.region) + `/store/v1/offers`,{ - headers: this.generateRequestHeaders(), - }); - } - - getContract(playerId) { - return axios.get(this.getPlayerDataServiceUrl(this.region) + `/contracts/v1/contracts/${playerId}`,{ - headers: this.generateRequestHeaders(), - }); - } - - getItemUpgradesV2() { - return axios.get(this.getPlayerDataServiceUrl(this.region) + `/contract-definitions/v2/item-upgrades`,{ - headers: this.generateRequestHeaders(), - }); - } - - getItemUpgradesV3() { - return axios.get(this.getPlayerDataServiceUrl(this.region) + `/contract-definitions/v3/item-upgrades`,{ - headers: this.generateRequestHeaders(), - }); - } - + ) + ).headers["set-cookie"].find((elem) => /^asid/.test(elem)); + + var access_tokens = await axios.put( + "https://auth.riotgames.com/api/v1/authorization", + { + type: "auth", + username: username, + password: password, + }, + { + headers: { + Cookie: cookie, + "User-Agent": + "RiotClient/43.0.1.4195386.4190634 rso-auth (Windows; 10;;Professional, x64)", + }, + httpsAgent: agent, + } + ); + + var tokens = parseUrl(access_tokens.data.response.parameters.uri); + this.access_token = tokens.access_token; + + this.entitlements_token = ( + await axios.post( + "https://entitlements.auth.riotgames.com/api/token/v1", + {}, + { + headers: { + Authorization: `Bearer ${tokens.access_token}`, + }, + } + ) + ).data.entitlements_token; + this.user_id = JSON.parse( + Buffer.from(tokens.access_token.split(".")[1], "base64").toString() + ).sub; + } + + getConfig(region = this.region) { + return axios.get( + this.getSharedDataServiceUrl(region) + "/v1/config/" + region + ); + } + + getContent() { + return axios.get( + this.getSharedDataServiceUrl(this.region) + "/content-service/v2/content", + { + headers: this.generateRequestHeaders(), + } + ); + } + + getEntitlements(playerId) { + return axios.get( + this.getPlayerDataServiceUrl(this.region) + + `/store/v1/entitlements/${playerId}`, + { + headers: this.generateRequestHeaders(), + } + ); + } + + getMatch(matchId) { + return axios.get( + this.getPlayerDataServiceUrl(this.region) + + `/match-details/v1/matches/${matchId}`, + { + headers: this.generateRequestHeaders(), + } + ); + } + + getParty(partyId) { + return axios.get( + this.getPartyServiceUrl(this.region) + `/parties/v1/parties/${partyId}`, + { + headers: this.generateRequestHeaders(), + } + ); + } + + getPartyByPlayer(playerId) { + return axios.get( + this.getPartyServiceUrl(this.region) + `/parties/v1/players/${playerId}`, + { + headers: this.generateRequestHeaders(), + } + ); + } + + getCompetitiveLeaderboard(seasonId, startIndex = 0, size = 510) { + return axios.get( + this.getPlayerDataServiceUrl(this.region) + + `/mmr/v1/leaderboards/affinity/${this.region}/queue/competitive/season/${seasonId}?startIndex=${startIndex}&size=${size}`, + { + headers: this.generateRequestHeaders(), + } + ); + } + + getPlayerLoadout(playerId) { + return axios.get( + this.getPlayerDataServiceUrl(this.region) + + `/personalization/v2/players/${playerId}/playerloadout`, + { + headers: this.generateRequestHeaders(), + } + ); + } + + getPlayerMMR(playerId) { + return axios.get( + this.getPlayerDataServiceUrl(this.region) + `/mmr/v1/players/${playerId}`, + { + headers: this.generateRequestHeaders(), + } + ); + } + + getPlayerMatchHistory(playerId, startIndex = 0, endIndex = 10) { + return axios.get( + this.getPlayerDataServiceUrl(this.region) + + `/match-history/v1/history/${playerId}?startIndex=${startIndex}&endIndex=${endIndex}`, + { + headers: this.generateRequestHeaders(), + } + ); + } + + getPlayerCompetitiveHistory(playerId, startIndex = 0, endIndex = 10) { + return axios.get( + this.getPlayerDataServiceUrl(this.region) + + `/mmr/v1/players/${playerId}/competitiveupdates?startIndex=${startIndex}&endIndex=${endIndex}`, + { + headers: this.generateRequestHeaders(), + } + ); + } + + getPlayerAccountXp(playerId) { + return axios.get( + this.getPlayerDataServiceUrl(this.region) + + `/account-xp/v1/players/${playerId}`, + { + headers: this.generateRequestHeaders(), + } + ); + } + + getPlayerWallet(playerId) { + return axios.get( + this.getPlayerDataServiceUrl(this.region) + + `/store/v1/wallet/${playerId}`, + { + headers: this.generateRequestHeaders(), + } + ); + } + + getPlayerStoreFront(playerId) { + return axios.get( + this.getPlayerDataServiceUrl(this.region) + + `/store/v2/storefront/${playerId}`, + { + headers: this.generateRequestHeaders(), + } + ); + } + + getPlayers(playerIds) { + return axios.put( + this.getPlayerDataServiceUrl(this.region) + "/name-service/v2/players", + playerIds, + { + headers: this.generateRequestHeaders(), + } + ); + } + + getSession(playerId) { + return axios.get( + this.getPartyServiceUrl(this.region) + `/session/v1/sessions/${playerId}`, + { + headers: this.generateRequestHeaders(), + } + ); + } + + getContractDefinitions() { + return axios.get( + this.getPlayerDataServiceUrl(this.region) + + "/contract-definitions/v2/definitions", + { + headers: this.generateRequestHeaders(), + } + ); + } + + getStoryContractDefinitions() { + return axios.get( + this.getPlayerDataServiceUrl(this.region) + + "/contract-definitions/v2/definitions/story", + { + headers: this.generateRequestHeaders(), + } + ); + } + + getStoreOffers() { + return axios.get( + this.getPlayerDataServiceUrl(this.region) + `/store/v1/offers`, + { + headers: this.generateRequestHeaders(), + } + ); + } + + getContract(playerId) { + return axios.get( + this.getPlayerDataServiceUrl(this.region) + + `/contracts/v1/contracts/${playerId}`, + { + headers: this.generateRequestHeaders(), + } + ); + } + + getItemUpgradesV2() { + return axios.get( + this.getPlayerDataServiceUrl(this.region) + + `/contract-definitions/v2/item-upgrades`, + { + headers: this.generateRequestHeaders(), + } + ); + } + + getItemUpgradesV3() { + return axios.get( + this.getPlayerDataServiceUrl(this.region) + + `/contract-definitions/v3/item-upgrades`, + { + headers: this.generateRequestHeaders(), + } + ); + } } module.exports = API;