quria
Version:
A user-friendly Destiny 2 API Wrapper written with TypeScript and approved by -Axis Minds- Oryx.
60 lines (59 loc) • 2.05 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OAuth = void 0;
const adapters_1 = require("../../adapters");
class OAuth {
authUrl;
tokenUrl;
headers;
client_id;
client_secret;
get environment() {
return (0, adapters_1.checkRunningEnvironment)();
}
constructor(authUrl, tokenUrl, headers, client_id, client_secret) {
this.authUrl = authUrl;
this.tokenUrl = tokenUrl;
this.headers = headers;
this.client_id = client_id;
this.client_secret = client_secret;
}
btoa(data) {
if (this.environment === "node") {
return Buffer.from(data).toString("base64");
}
else {
return btoa(data);
}
}
encodeCredentials() {
return this.btoa(`${this.client_id}:${this.client_secret}`);
}
/**
* Generate authorization url with default client id and state.
* @param {string} state OPTIONAL: String containing some information, particulary for avoid cross site scripting.
* @returns Authorization url.
*/
GenerateAuthorizationURL(state) {
return (0, adapters_1.formatQueryStrings)(this.authUrl, {
client_id: this.client_id,
response_type: "code",
state,
});
}
GetOAuthAccessToken(code) {
return adapters_1.Controller.request(this.tokenUrl, true, "POST", {
...this.headers,
Authorization: `Basic ${this.encodeCredentials()}`,
"Content-Type": "application/x-www-form-urlencoded",
}, `grant_type=authorization_code&code=${encodeURIComponent(code)}`);
}
RefreshAccessToken(refresh_token) {
return adapters_1.Controller.request(this.tokenUrl, true, "POST", {
...this.headers,
Authorization: `Basic ${this.encodeCredentials()}`,
"Content-Type": "application/x-www-form-urlencoded",
}, `grant_type=refresh_token&refresh_token=${refresh_token}`);
}
}
exports.OAuth = OAuth;