auth.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import type { KVNamespace } from "@cloudflare/workers-types"
  2. import { z } from "zod"
  3. import { issuer } from "@openauthjs/openauth"
  4. import type { Theme } from "@openauthjs/openauth/ui/theme"
  5. import { createSubjects } from "@openauthjs/openauth/subject"
  6. import { THEME_OPENAUTH } from "@openauthjs/openauth/ui/theme"
  7. import { GithubProvider } from "@openauthjs/openauth/provider/github"
  8. import { GoogleOidcProvider } from "@openauthjs/openauth/provider/google"
  9. import { CloudflareStorage } from "@openauthjs/openauth/storage/cloudflare"
  10. import { Account } from "@opencode-ai/console-core/account.js"
  11. import { Workspace } from "@opencode-ai/console-core/workspace.js"
  12. import { Actor } from "@opencode-ai/console-core/actor.js"
  13. import { Resource } from "@opencode-ai/console-resource"
  14. import { User } from "@opencode-ai/console-core/user.js"
  15. import { and, Database, eq, isNull, or } from "@opencode-ai/console-core/drizzle/index.js"
  16. import { WorkspaceTable } from "@opencode-ai/console-core/schema/workspace.sql.js"
  17. import { UserTable } from "@opencode-ai/console-core/schema/user.sql.js"
  18. import { AuthTable } from "@opencode-ai/console-core/schema/auth.sql.js"
  19. import { Identifier } from "@opencode-ai/console-core/identifier.js"
  20. type Env = {
  21. AuthStorage: KVNamespace
  22. }
  23. export const subjects = createSubjects({
  24. account: z.object({
  25. accountID: z.string(),
  26. email: z.string(),
  27. }),
  28. user: z.object({
  29. userID: z.string(),
  30. workspaceID: z.string(),
  31. }),
  32. })
  33. const MY_THEME: Theme = {
  34. ...THEME_OPENAUTH,
  35. logo: "https://opencode.ai/favicon.svg",
  36. }
  37. export default {
  38. async fetch(request: Request, env: Env, ctx: ExecutionContext) {
  39. const result = await issuer({
  40. theme: MY_THEME,
  41. providers: {
  42. github: GithubProvider({
  43. clientID: Resource.GITHUB_CLIENT_ID_CONSOLE.value,
  44. clientSecret: Resource.GITHUB_CLIENT_SECRET_CONSOLE.value,
  45. scopes: ["read:user", "user:email"],
  46. }),
  47. google: GoogleOidcProvider({
  48. clientID: Resource.GOOGLE_CLIENT_ID.value,
  49. scopes: ["openid", "email"],
  50. }),
  51. // email: CodeProvider({
  52. // async request(req, state, form, error) {
  53. // console.log(state)
  54. // const params = new URLSearchParams()
  55. // if (error) {
  56. // params.set("error", error.type)
  57. // }
  58. // if (state.type === "start") {
  59. // return Response.redirect(process.env.AUTH_FRONTEND_URL + "/auth/email?" + params.toString(), 302)
  60. // }
  61. //
  62. // if (state.type === "code") {
  63. // return Response.redirect(process.env.AUTH_FRONTEND_URL + "/auth/code?" + params.toString(), 302)
  64. // }
  65. //
  66. // return new Response("ok")
  67. // },
  68. // async sendCode(claims, code) {
  69. // const email = z.string().email().parse(claims.email)
  70. // const cmd = new SendEmailCommand({
  71. // Destination: {
  72. // ToAddresses: [email],
  73. // },
  74. // FromEmailAddress: `SST <auth@${Resource.Email.sender}>`,
  75. // Content: {
  76. // Simple: {
  77. // Body: {
  78. // Html: {
  79. // Data: `Your pin code is <strong>${code}</strong>`,
  80. // },
  81. // Text: {
  82. // Data: `Your pin code is ${code}`,
  83. // },
  84. // },
  85. // Subject: {
  86. // Data: "SST Console Pin Code: " + code,
  87. // },
  88. // },
  89. // },
  90. // })
  91. // await ses.send(cmd)
  92. // },
  93. // }),
  94. },
  95. storage: CloudflareStorage({
  96. // @ts-ignore
  97. namespace: env.AuthStorage,
  98. }),
  99. subjects,
  100. async success(ctx, response) {
  101. console.log(response)
  102. let subject: string | undefined
  103. let email: string | undefined
  104. if (response.provider === "github") {
  105. const emails = (await fetch("https://api.github.com/user/emails", {
  106. headers: {
  107. Authorization: `Bearer ${response.tokenset.access}`,
  108. "User-Agent": "opencode",
  109. Accept: "application/vnd.github+json",
  110. },
  111. }).then((x) => x.json())) as any
  112. const user = (await fetch("https://api.github.com/user", {
  113. headers: {
  114. Authorization: `Bearer ${response.tokenset.access}`,
  115. "User-Agent": "opencode",
  116. Accept: "application/vnd.github+json",
  117. },
  118. }).then((x) => x.json())) as any
  119. subject = user.id.toString()
  120. const primaryEmail = emails.find((x: any) => x.primary)
  121. if (!primaryEmail) throw new Error("No primary email found for GitHub user")
  122. if (!primaryEmail.verified) throw new Error("Primary email for GitHub user not verified")
  123. email = primaryEmail.email
  124. } else if (response.provider === "google") {
  125. if (!response.id.email_verified) throw new Error("Google email not verified")
  126. subject = response.id.sub as string
  127. email = response.id.email as string
  128. } else throw new Error("Unsupported provider")
  129. if (!email) throw new Error("No email found")
  130. if (!subject) throw new Error("No subject found")
  131. if (Resource.App.stage !== "production" && !email.endsWith("@anoma.ly")) {
  132. throw new Error("Invalid email")
  133. }
  134. // Get account
  135. const accountID = await (async () => {
  136. const matches = await Database.use(async (tx) =>
  137. tx
  138. .select({
  139. provider: AuthTable.provider,
  140. accountID: AuthTable.accountID,
  141. })
  142. .from(AuthTable)
  143. .where(
  144. or(
  145. and(eq(AuthTable.provider, response.provider), eq(AuthTable.subject, subject)),
  146. and(eq(AuthTable.provider, "email"), eq(AuthTable.subject, email)),
  147. ),
  148. ),
  149. )
  150. const idByProvider = matches.find((x) => x.provider === response.provider)?.accountID
  151. const idByEmail = matches.find((x) => x.provider === "email")?.accountID
  152. if (idByProvider && idByEmail) return idByProvider
  153. // create account if not found
  154. let accountID = idByProvider ?? idByEmail
  155. if (!accountID) {
  156. console.log("creating account for", email)
  157. accountID = await Account.create({})
  158. }
  159. await Database.use(async (tx) =>
  160. tx
  161. .insert(AuthTable)
  162. .values([
  163. {
  164. id: Identifier.create("auth"),
  165. accountID,
  166. provider: response.provider,
  167. subject,
  168. },
  169. {
  170. id: Identifier.create("auth"),
  171. accountID,
  172. provider: "email",
  173. subject: email,
  174. },
  175. ])
  176. .onDuplicateKeyUpdate({
  177. set: {
  178. timeDeleted: null,
  179. },
  180. }),
  181. )
  182. return accountID
  183. })()
  184. // Get workspace
  185. await Actor.provide("account", { accountID, email }, async () => {
  186. await User.joinInvitedWorkspaces()
  187. const workspaces = await Database.use((tx) =>
  188. tx
  189. .select({ id: WorkspaceTable.id })
  190. .from(WorkspaceTable)
  191. .innerJoin(UserTable, eq(UserTable.workspaceID, WorkspaceTable.id))
  192. .where(
  193. and(
  194. eq(UserTable.accountID, accountID),
  195. isNull(UserTable.timeDeleted),
  196. isNull(WorkspaceTable.timeDeleted),
  197. ),
  198. ),
  199. )
  200. if (workspaces.length === 0) {
  201. await Workspace.create({ name: "Default" })
  202. }
  203. })
  204. return ctx.subject("account", accountID, { accountID, email })
  205. },
  206. }).fetch(request, env, ctx)
  207. return result
  208. },
  209. }