session-scope.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * Sessions bound to one workshop.
  3. *
  4. * A technician's phone can be signed in without a password: the desk issues
  5. * an app setup code, or the workshop texts a one-time code. Either way it is
  6. * the workshop vouching for the person, and a workshop can only vouch for
  7. * itself. So those sessions carry the workshop that minted them
  8. * (`sessions.organizationId`), and everything that decides which workshop a
  9. * request acts in reads it before anything the caller says.
  10. *
  11. * Without it the token was the person's whole account. An admin who issued a
  12. * setup code and scanned it themselves could send another workshop's id in
  13. * `x-org-id` and act there with the technician's role, or change the
  14. * technician's email and take the account.
  15. *
  16. * Ordinary sign-ins carry nothing here and behave exactly as before.
  17. */
  18. /** What a bound session is told when it reaches for the account. */
  19. export const SCOPED_SESSION_MESSAGE =
  20. 'This sign-in only works in the technician app. Sign in with your password to change your account.'
  21. /** Paths under the auth handler a workshop-bound session may still call. */
  22. const SCOPED_SESSION_AUTH_PATHS = new Set(['/get-session', '/sign-out'])
  23. /**
  24. * The workshop a session is bound to, or null for an ordinary sign-in.
  25. *
  26. * Takes what `auth.api.getSession` returns, which includes the column because
  27. * it is declared under `session.additionalFields` in `./auth`.
  28. */
  29. export function sessionOrganizationScope(
  30. session: { session?: { organizationId?: string | null } | null } | null | undefined
  31. ): string | null {
  32. const scope = session?.session?.organizationId
  33. return typeof scope === 'string' && scope ? scope : null
  34. }
  35. /**
  36. * Whether a workshop-bound session may call this auth endpoint.
  37. *
  38. * Everything the auth handler offers beyond reading the session and signing
  39. * out is about the account rather than a workshop: listing the account's
  40. * other sessions (which hands back their tokens), adding a passkey, changing
  41. * the password or the name. Any of those turns a session one workshop vouched
  42. * for into a hold on the whole account, so none of them is open to it.
  43. */
  44. export function scopedSessionMayCall(path: string | undefined): boolean {
  45. return path !== undefined && SCOPED_SESSION_AUTH_PATHS.has(path)
  46. }
  47. /**
  48. * Every session token a request to the auth handler carries, signed or not.
  49. *
  50. * Read from both the bearer header and the session cookie, because the bearer
  51. * plugin turns one into the other only after hooks like ours have run. Used to
  52. * refuse, never to grant, so the signature is not checked: a forged value can
  53. * only ever get its sender refused.
  54. */
  55. export function sessionTokensInHeaders(
  56. headers: Headers | undefined | null,
  57. cookieName: string
  58. ): string[] {
  59. if (!headers) return []
  60. const tokens = new Set<string>()
  61. const authorization = headers.get('authorization')
  62. if (authorization?.slice(0, 7).toLowerCase() === 'bearer ') {
  63. const token = tokenPart(authorization.slice(7).trim())
  64. if (token) tokens.add(token)
  65. }
  66. for (const pair of (headers.get('cookie') ?? '').split(';')) {
  67. const eq = pair.indexOf('=')
  68. if (eq < 0) continue
  69. if (pair.slice(0, eq).trim() !== cookieName) continue
  70. const token = tokenPart(pair.slice(eq + 1).trim())
  71. if (token) tokens.add(token)
  72. }
  73. return [...tokens]
  74. }
  75. /** A stored token is the part before the signature. */
  76. function tokenPart(value: string): string {
  77. let decoded = value
  78. try {
  79. decoded = decodeURIComponent(value)
  80. } catch {
  81. // Not encoded, or badly: either way the raw value is what there is.
  82. }
  83. return decoded.split('.')[0] ?? ''
  84. }