auth.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { Resource } from "sst"
  2. import { z } from "zod"
  3. import { issuer } from "@openauthjs/openauth"
  4. import { createSubjects } from "@openauthjs/openauth/subject"
  5. import { GithubProvider } from "@openauthjs/openauth/provider/github"
  6. import { GoogleOidcProvider } from "@openauthjs/openauth/provider/google"
  7. import { CloudflareStorage } from "@openauthjs/openauth/storage/cloudflare"
  8. import { Account } from "@opencode/cloud-core/account.js"
  9. import { Workspace } from "@opencode/cloud-core/workspace.js"
  10. import { Actor } from "@opencode/cloud-core/actor.js"
  11. type Env = {
  12. AuthStorage: KVNamespace
  13. }
  14. export const subjects = createSubjects({
  15. account: z.object({
  16. accountID: z.string(),
  17. email: z.string(),
  18. }),
  19. user: z.object({
  20. userID: z.string(),
  21. workspaceID: z.string(),
  22. }),
  23. })
  24. export default {
  25. async fetch(request: Request, env: Env, ctx: ExecutionContext) {
  26. return issuer({
  27. providers: {
  28. github: GithubProvider({
  29. clientID: Resource.GITHUB_CLIENT_ID_CONSOLE.value,
  30. clientSecret: Resource.GITHUB_CLIENT_SECRET_CONSOLE.value,
  31. scopes: ["read:user", "user:email"],
  32. }),
  33. google: GoogleOidcProvider({
  34. clientID: Resource.GOOGLE_CLIENT_ID.value,
  35. scopes: ["openid", "email"],
  36. }),
  37. // email: CodeProvider({
  38. // async request(req, state, form, error) {
  39. // console.log(state)
  40. // const params = new URLSearchParams()
  41. // if (error) {
  42. // params.set("error", error.type)
  43. // }
  44. // if (state.type === "start") {
  45. // return Response.redirect(process.env.AUTH_FRONTEND_URL + "/auth/email?" + params.toString(), 302)
  46. // }
  47. //
  48. // if (state.type === "code") {
  49. // return Response.redirect(process.env.AUTH_FRONTEND_URL + "/auth/code?" + params.toString(), 302)
  50. // }
  51. //
  52. // return new Response("ok")
  53. // },
  54. // async sendCode(claims, code) {
  55. // const email = z.string().email().parse(claims.email)
  56. // const cmd = new SendEmailCommand({
  57. // Destination: {
  58. // ToAddresses: [email],
  59. // },
  60. // FromEmailAddress: `SST <auth@${Resource.Email.sender}>`,
  61. // Content: {
  62. // Simple: {
  63. // Body: {
  64. // Html: {
  65. // Data: `Your pin code is <strong>${code}</strong>`,
  66. // },
  67. // Text: {
  68. // Data: `Your pin code is ${code}`,
  69. // },
  70. // },
  71. // Subject: {
  72. // Data: "SST Console Pin Code: " + code,
  73. // },
  74. // },
  75. // },
  76. // })
  77. // await ses.send(cmd)
  78. // },
  79. // }),
  80. },
  81. storage: CloudflareStorage({
  82. namespace: env.AuthStorage,
  83. }),
  84. subjects,
  85. async success(ctx, response) {
  86. console.log(response)
  87. let email: string | undefined
  88. if (response.provider === "github") {
  89. const userResponse = await fetch("https://api.github.com/user", {
  90. headers: {
  91. Authorization: `Bearer ${response.tokenset.access}`,
  92. "User-Agent": "opencode",
  93. Accept: "application/vnd.github+json",
  94. },
  95. })
  96. const user = (await userResponse.json()) as { email: string }
  97. email = user.email
  98. } else if (response.provider === "google") {
  99. if (!response.id.email_verified) throw new Error("Google email not verified")
  100. email = response.id.email as string
  101. }
  102. //if (response.provider === "email") {
  103. // email = response.claims.email
  104. //}
  105. else throw new Error("Unsupported provider")
  106. if (!email) throw new Error("No email found")
  107. let accountID = await Account.fromEmail(email).then((x) => x?.id)
  108. if (!accountID) {
  109. console.log("creating account for", email)
  110. accountID = await Account.create({
  111. email: email!,
  112. })
  113. }
  114. await Actor.provide("account", { accountID, email }, async () => {
  115. const workspaces = await Account.workspaces()
  116. if (workspaces.length === 0) {
  117. await Workspace.create()
  118. }
  119. })
  120. return ctx.subject("account", accountID, { accountID, email })
  121. },
  122. }).fetch(request, env, ctx)
  123. },
  124. }