UNPKG

@react-keycloak/keycloak-ts

Version:
123 lines (105 loc) 3.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.decodeToken = decodeToken; exports.getRealmUrl = getRealmUrl; exports.isKeycloakConfig = isKeycloakConfig; exports.parseCallbackParams = parseCallbackParams; exports.setupOidcEndoints = setupOidcEndoints; var _jwtDecode = _interopRequireDefault(require("jwt-decode")); var _url = require("./url"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function fromEntries(iterable) { return [...iterable].reduce((obj, _ref) => { let [key, val] = _ref; return { ...obj, [key]: val }; }, {}); } function getRealmUrl(realm, authServerUrl) { if (typeof authServerUrl === 'undefined') { return undefined; } if (authServerUrl.charAt(authServerUrl.length - 1) === '/') { return authServerUrl + 'realms/' + encodeURIComponent(realm); } else { return authServerUrl + '/realms/' + encodeURIComponent(realm); } } function setupOidcEndoints(_ref2) { let { oidcConfiguration, realm, authServerUrl } = _ref2; if (!oidcConfiguration) { if (!realm) { throw new Error('Missing realm'); } return { authorize: function () { return getRealmUrl(realm, authServerUrl) + '/protocol/openid-connect/auth'; }, token: function () { return getRealmUrl(realm, authServerUrl) + '/protocol/openid-connect/token'; }, logout: function () { return getRealmUrl(realm, authServerUrl) + '/protocol/openid-connect/logout'; }, register: function () { return getRealmUrl(realm, authServerUrl) + '/protocol/openid-connect/registrations'; }, userinfo: function () { return getRealmUrl(realm, authServerUrl) + '/protocol/openid-connect/userinfo'; } }; } return { authorize: function () { return oidcConfiguration.authorization_endpoint; }, token: function () { return oidcConfiguration.token_endpoint; }, logout: function () { if (!oidcConfiguration.end_session_endpoint) { throw 'Not supported by the OIDC server'; } return oidcConfiguration.end_session_endpoint; }, register: function () { throw 'Redirection to "Register user" page not supported in standard OIDC mode'; }, userinfo: function () { if (!oidcConfiguration.userinfo_endpoint) { throw 'Not supported by the OIDC server'; } return oidcConfiguration.userinfo_endpoint; } }; } function decodeToken(str) { return (0, _jwtDecode.default)(str); } function parseCallbackParams(paramsString, supportedParams) { const params = (0, _url.extractQuerystringParameters)(paramsString); const [otherParams, oAuthParams] = Object.keys(params).reduce((_ref3, key) => { let [oParams, oauthParams] = _ref3; if (supportedParams.includes(key)) { oauthParams.set(key, params[key]); } else { oParams.set(key, params[key]); } return [oParams, oauthParams]; }, [new Map(), new Map()]); return { paramsString: (0, _url.formatQuerystringParameters)(otherParams), oauthParams: fromEntries(oAuthParams.entries()) }; } function isKeycloakConfig(config) { return !!config && typeof config !== 'string'; } //# sourceMappingURL=keycloak.js.map