UNPKG

neo4j-client-sso

Version:

Single sign-on client (frontend) library for Neo4j products

127 lines (126 loc) 5.89 kB
/* * Copyright (c) "Neo4j" * Neo4j Sweden AB [http://neo4j.com] * * This file is part of Neo4j. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with 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. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { getValidSSOProviders } from './common'; import { authLog } from './helpers'; export const Success = 'Success'; export const FetchError = 'FetchError'; export const NoProviderError = 'NoProviderError'; export const fetchDiscoveryDataFromUrl = (url) => __awaiter(void 0, void 0, void 0, function* () { var _a; try { const response = yield window.fetch(url, { method: 'get', headers: { Accept: 'application/json' } }); if (response === null || response === void 0 ? void 0 : response.ok) { const result = yield response.json(); const isDiscoveryAPI = result === null || result === void 0 ? void 0 : result.hasOwnProperty('auth_config'); const ssoProviderField = isDiscoveryAPI ? (_a = result === null || result === void 0 ? void 0 : result['auth_config']) === null || _a === void 0 ? void 0 : _a['oidc_providers'] : (result === null || result === void 0 ? void 0 : result['sso_providers']) || (result === null || result === void 0 ? void 0 : result.ssoproviders) || (result === null || result === void 0 ? void 0 : result.ssoProviders); if (!ssoProviderField) { const noProviderMsg = `No SSO providers found on endpoint: ${url}`; authLog(noProviderMsg); return { status: NoProviderError, message: noProviderMsg, otherDataDiscovered: _prepareOtherDataDiscovered(result), SSOProviders: [] }; } const unifiedSSOProviders = isDiscoveryAPI ? transform(ssoProviderField) : ssoProviderField; const SSOProviders = getValidSSOProviders(unifiedSSOProviders); if (SSOProviders.length === 0) { authLog(`None of the SSO providers found at ${url} were valid`); } else { authLog(`Found SSO providers with ids: ${SSOProviders.map(p => p.id).join(', ')} on ${url}`); } return { status: Success, message: Success, otherDataDiscovered: _prepareOtherDataDiscovered(result), SSOProviders }; } else { const invalidResponseMsg = `Invalid response for SSO provider discovery attempt, endpoint: ${url}`; const noHttpPrefixMessage = url.toLowerCase().startsWith('http') ? '' : 'Double check that the url is a valid url (including HTTP(S)).'; const noJsonSuffixMessage = url.toLowerCase().endsWith('.json') ? '' : 'Double check that the discovery url returns a valid JSON file.'; const messages = [ invalidResponseMsg, noHttpPrefixMessage, noJsonSuffixMessage ]; messages.forEach(m => authLog(m)); return { status: FetchError, message: invalidResponseMsg, otherDataDiscovered: {}, SSOProviders: [] }; } } catch (err) { const errMsg = `SSO provider discovery attempt failed on endpoint: ${url} error: ${err}`; authLog(errMsg); return { status: FetchError, message: errMsg, otherDataDiscovered: {}, SSOProviders: [] }; } }); export const transform = (toTransformSSOProviders) => { return toTransformSSOProviders.map(provider => { const tmpProvider = Object.assign(Object.assign({}, provider), { params: Object.assign(Object.assign({}, provider === null || provider === void 0 ? void 0 : provider.params), { redirect_uri: provider.redirect_uri }) }); if (!tmpProvider.params.redirect_uri) delete tmpProvider.params.redirect_uri; if (Object.keys(tmpProvider.params).length === 0) delete tmpProvider.params; delete tmpProvider.redirect_uri; return tmpProvider; }); }; const _prepareOtherDataDiscovered = (result) => { const otherDataDiscovered = result; otherDataDiscovered === null || otherDataDiscovered === void 0 ? true : delete otherDataDiscovered['auth_config']; otherDataDiscovered === null || otherDataDiscovered === void 0 ? true : delete otherDataDiscovered['sso_providers']; return otherDataDiscovered; };