@pnp/adaljsclient
Advanced tools
Comparing version 2.2.0 to 2.3.0
153
index.js
@@ -1,2 +0,2 @@ | ||
import { __awaiter } from "tslib"; | ||
import { __awaiter, __extends, __generator } from "tslib"; | ||
import { BearerTokenFetchClient, isUrlAbsolute, SPFxAdalClient, getADALResource } from "@pnp/common"; | ||
@@ -7,3 +7,4 @@ import * as adal from "adal-angular/dist/adal.min.js"; | ||
*/ | ||
export class AdalClient extends BearerTokenFetchClient { | ||
var AdalClient = /** @class */ (function (_super) { | ||
__extends(AdalClient, _super); | ||
/** | ||
@@ -15,9 +16,10 @@ * Creates a new instance of AdalClient | ||
*/ | ||
constructor(clientId, tenant, redirectUri) { | ||
super(null); | ||
this.clientId = clientId; | ||
this.tenant = tenant; | ||
this.redirectUri = redirectUri; | ||
this._displayCallback = null; | ||
this._loginPromise = null; | ||
function AdalClient(clientId, tenant, redirectUri) { | ||
var _this = _super.call(this, null) || this; | ||
_this.clientId = clientId; | ||
_this.tenant = tenant; | ||
_this.redirectUri = redirectUri; | ||
_this._displayCallback = null; | ||
_this._loginPromise = null; | ||
return _this; | ||
} | ||
@@ -31,5 +33,5 @@ /** | ||
*/ | ||
static fromSPFxContext(spfxContext) { | ||
AdalClient.fromSPFxContext = function (spfxContext) { | ||
return new SPFxAdalClient(spfxContext); | ||
} | ||
}; | ||
/** | ||
@@ -41,16 +43,20 @@ * Conducts the fetch opertation against the AAD secured resource | ||
*/ | ||
fetch(url, options) { | ||
const _super = Object.create(null, { | ||
fetch: { get: () => super.fetch } | ||
AdalClient.prototype.fetch = function (url, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var token; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!isUrlAbsolute(url)) { | ||
throw Error("You must supply absolute urls to AdalClient.fetch."); | ||
} | ||
return [4 /*yield*/, this.getToken(getADALResource(url))]; | ||
case 1: | ||
token = _a.sent(); | ||
this.token = token; | ||
return [2 /*return*/, _super.prototype.fetch.call(this, url, options)]; | ||
} | ||
}); | ||
}); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!isUrlAbsolute(url)) { | ||
throw Error("You must supply absolute urls to AdalClient.fetch."); | ||
} | ||
// the url we are calling is the resource | ||
const token = yield this.getToken(getADALResource(url)); | ||
this.token = token; | ||
return _super.fetch.call(this, url, options); | ||
}); | ||
} | ||
}; | ||
/** | ||
@@ -61,52 +67,63 @@ * Gets a token based on the current user | ||
*/ | ||
getToken(resource) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield this.login(); | ||
return new Promise((resolve, reject) => { | ||
AdalClient._authContext.acquireToken(resource, (message, token) => { | ||
if (message) { | ||
reject(Error(message)); | ||
} | ||
else { | ||
resolve(token); | ||
} | ||
}); | ||
AdalClient.prototype.getToken = function (resource) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.login()]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/, new Promise(function (resolve, reject) { | ||
AdalClient._authContext.acquireToken(resource, function (message, token) { | ||
if (message) { | ||
reject(Error(message)); | ||
} | ||
else { | ||
resolve(token); | ||
} | ||
}); | ||
})]; | ||
} | ||
}); | ||
}); | ||
} | ||
}; | ||
/** | ||
* Ensures we have created and setup the adal AuthenticationContext instance | ||
*/ | ||
ensureAuthContext() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (AdalClient._authContext === null) { | ||
AdalClient._authContext = adal.inject({ | ||
clientId: this.clientId, | ||
displayCall: (url) => { | ||
if (this._displayCallback) { | ||
this._displayCallback(url); | ||
} | ||
}, | ||
navigateToLoginRequestUrl: false, | ||
redirectUri: this.redirectUri, | ||
tenant: this.tenant, | ||
}); | ||
} | ||
AdalClient.prototype.ensureAuthContext = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
if (AdalClient._authContext === null) { | ||
AdalClient._authContext = adal.inject({ | ||
clientId: this.clientId, | ||
displayCall: function (url) { | ||
if (_this._displayCallback) { | ||
_this._displayCallback(url); | ||
} | ||
}, | ||
navigateToLoginRequestUrl: false, | ||
redirectUri: this.redirectUri, | ||
tenant: this.tenant, | ||
}); | ||
} | ||
return [2 /*return*/]; | ||
}); | ||
}); | ||
} | ||
}; | ||
/** | ||
* Ensures the current user is logged in | ||
*/ | ||
login() { | ||
AdalClient.prototype.login = function () { | ||
var _this = this; | ||
if (this._loginPromise) { | ||
return this._loginPromise; | ||
} | ||
this._loginPromise = new Promise((resolve, reject) => { | ||
this._loginPromise = new Promise(function (resolve, reject) { | ||
// this triggers the login process | ||
this.ensureAuthContext().then(() => { | ||
_this.ensureAuthContext().then(function () { | ||
if (AdalClient._authContext.getCachedUser()) { | ||
return resolve(); | ||
} | ||
this._displayCallback = (url) => { | ||
const popupWindow = window.open(url, "login", "width=483, height=600"); | ||
_this._displayCallback = function (url) { | ||
var popupWindow = window.open(url, "login", "width=483, height=600"); | ||
if (!popupWindow) { | ||
@@ -118,3 +135,3 @@ return reject(Error("Could not open pop-up window for auth. Likely pop-ups are blocked by the browser.")); | ||
} | ||
const pollTimer = window.setInterval(() => { | ||
var pollTimer = window.setInterval(function () { | ||
try { | ||
@@ -124,3 +141,3 @@ if (!popupWindow || popupWindow.closed || popupWindow.closed === undefined) { | ||
} | ||
if (popupWindow.document.URL.indexOf(this.redirectUri) !== -1) { | ||
if (popupWindow.document.URL.indexOf(_this.redirectUri) !== -1) { | ||
AdalClient._authContext.handleWindowCallback(popupWindow.location.hash); | ||
@@ -141,12 +158,14 @@ popupWindow.close(); | ||
AdalClient._authContext.login(); | ||
this._displayCallback = null; | ||
_this._displayCallback = null; | ||
}); | ||
}); | ||
return this._loginPromise; | ||
} | ||
} | ||
/** | ||
* Our auth context | ||
*/ | ||
AdalClient._authContext = null; | ||
}; | ||
/** | ||
* Our auth context | ||
*/ | ||
AdalClient._authContext = null; | ||
return AdalClient; | ||
}(BearerTokenFetchClient)); | ||
export { AdalClient }; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@pnp/adaljsclient", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "pnp - provides an ADAL client for use with PnPjs", | ||
@@ -10,3 +10,3 @@ "main": "./index.js", | ||
"tslib": "2.1.0", | ||
"@pnp/common": "2.2.0" | ||
"@pnp/common": "2.3.0" | ||
}, | ||
@@ -13,0 +13,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16393
219
+ Added@pnp/common@2.3.0(transitive)
- Removed@pnp/common@2.2.0(transitive)
Updated@pnp/common@2.3.0