@okta-dfuhriman/okta-auth-js
Version:
The Okta Auth SDK
82 lines (78 loc) • 3.7 kB
JavaScript
/*!
* Copyright (c) 2015-present, Okta, Inc. and/or its affiliates. All rights reserved.
* The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
*
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and limitations under the License.
*/
import AuthSdkError from '../../errors/AuthSdkError.js';
function addListener(eventTarget, name, fn) {
if (eventTarget.addEventListener) {
eventTarget.addEventListener(name, fn);
}
else {
eventTarget.attachEvent('on' + name, fn);
}
}
function removeListener(eventTarget, name, fn) {
if (eventTarget.removeEventListener) {
eventTarget.removeEventListener(name, fn);
}
else {
eventTarget.detachEvent('on' + name, fn);
}
}
function loadFrame(src) {
var iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.src = src;
return document.body.appendChild(iframe);
}
function loadPopup(src, options) {
var title = options.popupTitle || 'External Identity Provider User Authentication';
var appearance = 'toolbar=no, scrollbars=yes, resizable=yes, ' +
'top=100, left=500, width=600, height=600';
return window.open(src, title, appearance);
}
function addPostMessageListener(sdk, timeout, state) {
var responseHandler;
var timeoutId;
var msgReceivedOrTimeout = new Promise(function (resolve, reject) {
responseHandler = function responseHandler({ data, origin }) {
var _a;
const oauthResponse = ((_a = sdk === null || sdk === void 0 ? void 0 : sdk.options) === null || _a === void 0 ? void 0 : _a.provider) === 'okta-cic'
&& (data === null || data === void 0 ? void 0 : data.type) === 'authorization_response'
&& (data === null || data === void 0 ? void 0 : data.response)
? data.response
: (data === null || data === void 0 ? void 0 : data.code) && (data === null || data === void 0 ? void 0 : data.state)
? data
: undefined;
if (oauthResponse === null || oauthResponse === void 0 ? void 0 : oauthResponse.error) {
return reject(new AuthSdkError(oauthResponse.error));
}
if ((oauthResponse === null || oauthResponse === void 0 ? void 0 : oauthResponse.code) && (oauthResponse === null || oauthResponse === void 0 ? void 0 : oauthResponse.state) === state) {
const { allowedOrigins = [] } = (sdk === null || sdk === void 0 ? void 0 : sdk.options) || {};
if (![...allowedOrigins, sdk.getIssuerOrigin()].includes(origin)) {
return reject(new AuthSdkError('The request does not match client configuration'));
}
resolve(oauthResponse);
}
return;
};
addListener(window, 'message', responseHandler);
timeoutId = setTimeout(function () {
reject(new AuthSdkError('OAuth flow timed out'));
}, timeout || 120000);
});
return msgReceivedOrTimeout
.finally(function () {
clearTimeout(timeoutId);
removeListener(window, 'message', responseHandler);
});
}
export { addListener, addPostMessageListener, loadFrame, loadPopup, removeListener };
//# sourceMappingURL=browser.js.map