oauth-v2-client
Version:
Oauth V2 client based on axios
96 lines (95 loc) • 4.22 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var common_1 = require("@noreajs/common");
var query_string_1 = require("query-string");
var GrantControl_1 = __importDefault(require("./GrantControl"));
var ImplicitGrantControl = /** @class */ (function (_super) {
__extends(ImplicitGrantControl, _super);
function ImplicitGrantControl(config, options) {
var _this = _super.call(this, config) || this;
_this.options = options;
// callback url
_this.redirectUri = _this.options.callbackUrl;
return _this;
}
/**
* Get authentication url
* @param {GetAuthorizationUriFuncType} options redirect uri, response type
*/
ImplicitGrantControl.prototype.getAuthUri = function (options) {
var _a, _b, _c;
// create local properties
var localState = options === null || options === void 0 ? void 0 : options.state;
var localScopes = (_a = options === null || options === void 0 ? void 0 : options.scopes) !== null && _a !== void 0 ? _a : this.options.scopes;
var localRedirectUrl = (_b = options === null || options === void 0 ? void 0 : options.callbackUrl) !== null && _b !== void 0 ? _b : this.redirectUri;
// query params
var queryParams = {
response_type: "token",
redirect_uri: localRedirectUrl,
client_id: this.options.clientId,
state: localState,
scope: localScopes ? localScopes.join(" ") : "",
};
// merged params
var mergedParams = common_1.Obj.merge(queryParams, (_c = this.requestOptions.query) !== null && _c !== void 0 ? _c : {});
// constructing the request
var url = new URL(this.options.authUrl);
for (var param in mergedParams) {
if (Object.prototype.hasOwnProperty.call(mergedParams, param)) {
var value = mergedParams[param];
// setting the param
url.searchParams.set(param, value);
}
}
return url.toString();
};
/**
* Extract the token within the callback uri
* @param callbackUrl the full callback uri
* @param state state
*/
ImplicitGrantControl.prototype.getToken = function (callbackUrl, state) {
// callback url data
var urlData = (0, query_string_1.parseUrl)(callbackUrl);
// local state
var localState = state;
// force array
if (localState === null || localState === undefined) {
localState = [];
}
else if (!Array.isArray(localState)) {
localState = [localState];
}
// state exists
if (urlData.query.state && !localState.includes(urlData.query.state)) {
if (this.log === true) {
console.log("Corrupted answer, the state doesn't match.", {
urlData: urlData,
localState: localState,
});
}
throw new Error("Corrupted answer, the state doesn't match.");
}
return urlData.query;
};
return ImplicitGrantControl;
}(GrantControl_1.default));
exports.default = ImplicitGrantControl;