import * as qs from 'qs'; export const getUniqueScopes = (scopeString: string) => Array.from( new Set( scopeString .replace(/\s/g, ',') .split(',') .map(s => s.trim()) .filter(s => !!s) ) ) .join(' ') .trim(); export const parseQueryResult = hash => { var hashed = qs.parse(hash); return { ...hashed, expires_in: parseInt(hashed.expires_in) }; }; export const runIframe = (authorizeUrl, eventOrigin) => { return new Promise((res, rej) => { var iframe = document.createElement('iframe'); iframe.setAttribute('width', '0'); iframe.setAttribute('height', '0'); iframe.style.display = 'none'; const timeoutSetTimeoutId = setTimeout(() => { rej({ error: 'timeout' }); window.document.body.removeChild(iframe); }, 60 * 1000); const iframeEventHandler = function(evt: MessageEvent) { if (evt.origin != eventOrigin) return; if (!evt.data.type) return; switch (evt.data.type) { case 'authorization_response': (evt.source).close(); evt.data.response.error ? rej(evt.data.response) : res(evt.data.response); clearTimeout(timeoutSetTimeoutId); window.removeEventListener('message', iframeEventHandler, false); window.document.body.removeChild(iframe); break; default: } }; window.addEventListener('message', iframeEventHandler, false); window.document.body.appendChild(iframe); iframe.setAttribute('src', authorizeUrl); }); }; export const openPopup = () => { const popup = window.open( '', 'auth0:authorize:popup', 'left=100,top=100,width=400,height=600,resizable,scrollbars=yes,status=1' ); if (!popup) { throw new Error('Could not open popup'); } return popup; }; export const runPopup = (popup, authorizeUrl) => { popup.location.href = authorizeUrl; return new Promise((resolve, reject) => { const timeoutId = setTimeout(() => { reject({ error: 'timeout', error_description: 'Timeout' }); }, 60 * 1000); window.addEventListener('message', e => { if (!e.data || e.data.type !== 'authorization_response') { return; } clearTimeout(timeoutId); popup.close(); if (e.data.response.error) { return reject(e.data.response); } resolve(e.data.response); }); }); }; export const createRandomString = () => { const charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-_'; let random = ''; const randomValues = crypto.getRandomValues(new Uint8Array(32)); randomValues.forEach(v => (random += charset[v % charset.length])); return random; }; export const encodeState = (state: string) => btoa(state); export const decodeState = (state: string) => atob(state); export const createQueryParams = (params: any) => qs.stringify(params); export const sha256 = s => window.crypto.subtle.digest({ name: 'SHA-256' }, new TextEncoder().encode(s)); export const urlEncodeB64 = input => { const b64Chars = { '+': '-', '/': '_', '=': '' }; return input.replace(/[\+\/=]/g, m => b64Chars[m]); }; export const bufferToBase64UrlEncoded = input => urlEncodeB64( window.btoa(String.fromCharCode(...Array.from(new Uint8Array(input)))) ); export const oauthToken = async ({ baseUrl, ...options }: OAuthTokenOptions) => await fetch(`${baseUrl}/oauth/token`, { method: 'POST', body: JSON.stringify({ grant_type: 'authorization_code', redirect_uri: window.location.origin, ...options }), headers: new Headers({ 'Content-type': 'application/json' }) }).then(r => r.json());