All files / apps / web/src / features / auth / lib broker.ts

100% Statements 29/29
100% Branches 17/17
100% Functions 6/6
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69          3x     3x   3x 3x                     21x 18x 18x 15x   14x         3x         2x 2x 2x   2x         2x   2x         2x 1x         13x 13x 13x   7x 7x   7x    
// Branch previews sign in through the dev site (the one host Firebase and Google know): dev signs
// in, then sends the browser back to the preview's /signin with Google's ID token in the #fragment,
// which never reaches a server. These helpers hold the checks both ends make.
 
/** Where the broker sends the token back: only a preview's own sign-in page. */
export const BROKER_RETURN_PATH = '/signin'
 
/** The dev site's page that signs in for a preview. */
export const BROKER_PATH = '/signin/broker'
 
const TOKEN_PARAM = 'google_id_token'
const STATE_PARAM = 'state'
 
/** What the broker hands back: Google's ID token and the state the preview sent out with. */
export interface BrokerHandoff {
  googleIdToken: string
  state: string
}
 
/** True only for https `<origin>/signin` whose origin matches the whole pattern. */
export function isAllowedReturn(value: string, pattern: string): boolean {
  // `url.hash` is '' for a bare `#` too, and the token would then follow a second `#`.
  if (value.includes('#') || !URL.canParse(value)) return false
  const url = new URL(value)
  if (url.protocol !== 'https:' || url.username !== '' || url.password !== '') return false
  if (url.pathname !== BROKER_RETURN_PATH) return false
 
  return new RegExp(pattern).test(url.origin)
}
 
/** A random value the preview keeps and the broker echoes, so /signin takes only its own hand-off. */
export function createBrokerState(): string {
  return crypto.randomUUID()
}
 
/** The dev page to open from a preview, with this page as the way back. */
export function getBrokerUrl(brokerUrl: string, returnUrl: string, state: string): string {
  const url = new URL(BROKER_PATH, brokerUrl)
  url.searchParams.set('return', returnUrl)
  url.searchParams.set(STATE_PARAM, state)
 
  return url.toString()
}
 
/** The preview page to send the browser back to, carrying the hand-off in its fragment. */
export function getReturnUrl(returnUrl: string, { googleIdToken, state }: BrokerHandoff): string {
  const fragment = new URLSearchParams({ [TOKEN_PARAM]: googleIdToken, [STATE_PARAM]: state })
 
  return `${returnUrl}#${fragment.toString()}`
}
 
/** Leaves this page; `shouldReplace` keeps the page out of the history (the token's way back). */
export function setLocation(url: string, { shouldReplace }: { shouldReplace: boolean }): void {
  if (shouldReplace) window.location.replace(url)
  else window.location.assign(url)
}
 
/** Reads the hand-off the broker sent and removes it from the address bar and the history entry. */
export function takeBrokerHandoff(): BrokerHandoff | null {
  const fragment = new URLSearchParams(window.location.hash.slice(1))
  const googleIdToken = fragment.get(TOKEN_PARAM)
  if (!googleIdToken) return null
 
  const { pathname, search } = window.location
  window.history.replaceState(window.history.state, '', `${pathname}${search}`)
 
  return { googleIdToken, state: fragment.get(STATE_PARAM) ?? '' }
}