UNPKG

@react-keycloak/keycloak-ts

Version:
105 lines (95 loc) 2.96 kB
import jwtDecode from 'jwt-decode'; import { extractQuerystringParameters, formatQuerystringParameters } from './url'; function fromEntries(iterable) { return [...iterable].reduce((obj, _ref) => { let [key, val] = _ref; return { ...obj, [key]: val }; }, {}); } export 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); } } export 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; } }; } export function decodeToken(str) { return jwtDecode(str); } export function parseCallbackParams(paramsString, supportedParams) { const params = 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: formatQuerystringParameters(otherParams), oauthParams: fromEntries(oAuthParams.entries()) }; } export function isKeycloakConfig(config) { return !!config && typeof config !== 'string'; } //# sourceMappingURL=keycloak.js.map