cloud-instance.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import type { KeyObject } from 'node:crypto'
  2. import { embeddedKeys, signedPayload } from './license/signature'
  3. /**
  4. * Whether this app is the Torqvoice cloud instance.
  5. *
  6. * `TORQVOICE_MODE=cloud` alone is a line anyone can put in an environment
  7. * file, and cloud mode changes what a workshop gets: branding comes off on
  8. * every plan, plans follow the `subscription` table (which on a self-hosted
  9. * install is the operator's to write), and the one-workshop limit no longer
  10. * counts the install. So cloud mode also needs a token torqvoice.com signed
  11. * for this app's public URL, in TORQVOICE_CLOUD_TOKEN. Without one the app
  12. * runs as a self-hosted install, and says so in the log.
  13. *
  14. * The token is minted by hand on torqvoice.com (`scripts/mint-cloud-token.mjs`)
  15. * with the key that signs licence tokens, under its own prefix so a licence
  16. * can never pass for it. The production token has no expiry: nothing refreshes
  17. * it, and an expiry would only schedule an outage. Revoking one means rotating
  18. * the signing key. Throwaway tokens, like the one the e2e run mints for
  19. * 127.0.0.1, carry `expiresAt` so a copy that escapes the run is soon dead.
  20. *
  21. * Checked offline against the embedded public key, so the answer never waits
  22. * on the network and a torqvoice.com outage changes nothing.
  23. */
  24. export const CLOUD_TOKEN_PREFIX = 'tvc1'
  25. export type CloudTokenPayload = {
  26. v: 1
  27. /** the public origin this token was minted for, e.g. https://app.torqvoice.com */
  28. origin: string
  29. /** ISO 8601, when torqvoice.com signed this token */
  30. issuedAt: string
  31. /** ISO 8601; only throwaway tokens have one */
  32. expiresAt?: string
  33. }
  34. export type CloudTokenStatus =
  35. | 'missing'
  36. /** malformed, bad signature, or not a cloud token */
  37. | 'invalid'
  38. /** signature fine, minted for another URL */
  39. | 'wrong-origin'
  40. /** signature fine, past its expiresAt */
  41. | 'expired'
  42. | 'valid'
  43. function originOf(url: string | null | undefined): string | null {
  44. if (!url) return null
  45. try {
  46. return new URL(url).origin
  47. } catch {
  48. return null
  49. }
  50. }
  51. /** Verifies with an explicit key set. Tests use this with a throwaway pair. */
  52. export function verifyCloudTokenWith(
  53. keys: readonly KeyObject[],
  54. token: string | null | undefined,
  55. appUrl: string | null | undefined,
  56. now: Date = new Date()
  57. ): CloudTokenStatus {
  58. if (!token?.trim()) return 'missing'
  59. const p = signedPayload(keys, token, CLOUD_TOKEN_PREFIX)
  60. if (!p || p.v !== 1 || typeof p.origin !== 'string' || typeof p.issuedAt !== 'string') {
  61. return 'invalid'
  62. }
  63. if (p.expiresAt !== undefined) {
  64. if (typeof p.expiresAt !== 'string' || Number.isNaN(Date.parse(p.expiresAt))) return 'invalid'
  65. }
  66. const minted = originOf(p.origin)
  67. if (!minted) return 'invalid'
  68. if (minted !== originOf(appUrl)) return 'wrong-origin'
  69. if (p.expiresAt && Date.parse(p.expiresAt) <= now.getTime()) return 'expired'
  70. return 'valid'
  71. }
  72. let memo: { key: string; cloud: boolean } | null = null
  73. export function isCloudInstance(): boolean {
  74. if (process.env.TORQVOICE_MODE !== 'cloud') return false
  75. const token = process.env.TORQVOICE_CLOUD_TOKEN
  76. const appUrl = process.env.NEXT_PUBLIC_APP_URL
  77. // Keyed on the inputs rather than computed once, so a test that changes
  78. // the environment gets a fresh answer.
  79. const key = `${token}|${appUrl}`
  80. if (memo?.key === key) return memo.cloud
  81. const status = verifyCloudTokenWith(embeddedKeys(), token, appUrl)
  82. if (status !== 'valid') {
  83. const reason =
  84. status === 'missing'
  85. ? 'TORQVOICE_CLOUD_TOKEN is not set'
  86. : status === 'wrong-origin'
  87. ? `TORQVOICE_CLOUD_TOKEN was minted for another URL than ${appUrl}`
  88. : status === 'expired'
  89. ? 'TORQVOICE_CLOUD_TOKEN has expired'
  90. : 'TORQVOICE_CLOUD_TOKEN is not a valid cloud token'
  91. console.error(
  92. `[cloud] TORQVOICE_MODE=cloud is ignored: ${reason}. Running as a self-hosted install.`
  93. )
  94. }
  95. memo = { key, cloud: status === 'valid' }
  96. return memo.cloud
  97. }