auth.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 } 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. type Env = {
  19. AuthStorage: KVNamespace
  20. }
  21. export const subjects = createSubjects({
  22. account: z.object({
  23. accountID: z.string(),
  24. email: z.string(),
  25. }),
  26. user: z.object({
  27. userID: z.string(),
  28. workspaceID: z.string(),
  29. }),
  30. })
  31. const MY_THEME: Theme = {
  32. ...THEME_OPENAUTH,
  33. logo: "https://opencode.ai/favicon.svg",
  34. }
  35. export default {
  36. async fetch(request: Request, env: Env, ctx: ExecutionContext) {
  37. const result = await issuer({
  38. theme: MY_THEME,
  39. providers: {
  40. github: GithubProvider({
  41. clientID: Resource.GITHUB_CLIENT_ID_CONSOLE.value,
  42. clientSecret: Resource.GITHUB_CLIENT_SECRET_CONSOLE.value,
  43. scopes: ["read:user", "user:email"],
  44. }),
  45. google: GoogleOidcProvider({
  46. clientID: Resource.GOOGLE_CLIENT_ID.value,
  47. scopes: ["openid", "email"],
  48. }),
  49. // email: CodeProvider({
  50. // async request(req, state, form, error) {
  51. // console.log(state)
  52. // const params = new URLSearchParams()
  53. // if (error) {
  54. // params.set("error", error.type)
  55. // }
  56. // if (state.type === "start") {
  57. // return Response.redirect(process.env.AUTH_FRONTEND_URL + "/auth/email?" + params.toString(), 302)
  58. // }
  59. //
  60. // if (state.type === "code") {
  61. // return Response.redirect(process.env.AUTH_FRONTEND_URL + "/auth/code?" + params.toString(), 302)
  62. // }
  63. //
  64. // return new Response("ok")
  65. // },
  66. // async sendCode(claims, code) {
  67. // const email = z.string().email().parse(claims.email)
  68. // const cmd = new SendEmailCommand({
  69. // Destination: {
  70. // ToAddresses: [email],
  71. // },
  72. // FromEmailAddress: `SST <auth@${Resource.Email.sender}>`,
  73. // Content: {
  74. // Simple: {
  75. // Body: {
  76. // Html: {
  77. // Data: `Your pin code is <strong>${code}</strong>`,
  78. // },
  79. // Text: {
  80. // Data: `Your pin code is ${code}`,
  81. // },
  82. // },
  83. // Subject: {
  84. // Data: "SST Console Pin Code: " + code,
  85. // },
  86. // },
  87. // },
  88. // })
  89. // await ses.send(cmd)
  90. // },
  91. // }),
  92. },
  93. storage: CloudflareStorage({
  94. // @ts-ignore
  95. namespace: env.AuthStorage,
  96. }),
  97. subjects,
  98. async success(ctx, response) {
  99. console.log(response)
  100. let email: string | undefined
  101. if (response.provider === "github") {
  102. const emails = (await fetch("https://api.github.com/user/emails", {
  103. headers: {
  104. Authorization: `Bearer ${response.tokenset.access}`,
  105. "User-Agent": "opencode",
  106. Accept: "application/vnd.github+json",
  107. },
  108. }).then((x) => x.json())) as any
  109. email = emails.find((x: any) => x.primary && x.verified)?.email
  110. } else if (response.provider === "google") {
  111. if (!response.id.email_verified) throw new Error("Google email not verified")
  112. email = response.id.email as string
  113. } else throw new Error("Unsupported provider")
  114. if (!email) throw new Error("No email found")
  115. let accountID = await Account.fromEmail(email).then((x) => x?.id)
  116. if (!accountID) {
  117. console.log("creating account for", email)
  118. accountID = await Account.create({
  119. email: email!,
  120. })
  121. }
  122. await Actor.provide("account", { accountID, email }, async () => {
  123. await User.joinInvitedWorkspaces()
  124. const workspaces = await Database.transaction(async (tx) =>
  125. tx
  126. .select({ id: WorkspaceTable.id })
  127. .from(WorkspaceTable)
  128. .innerJoin(UserTable, eq(UserTable.workspaceID, WorkspaceTable.id))
  129. .where(
  130. and(
  131. eq(UserTable.accountID, accountID),
  132. isNull(UserTable.timeDeleted),
  133. isNull(WorkspaceTable.timeDeleted),
  134. ),
  135. ),
  136. )
  137. if (workspaces.length === 0) {
  138. await Workspace.create({ name: "Default" })
  139. }
  140. })
  141. return ctx.subject("account", accountID, { accountID, email })
  142. },
  143. }).fetch(request, env, ctx)
  144. return result
  145. },
  146. }