|
| 1 | +import { ORPCError } from "@orpc/client"; |
| 2 | +import { inject, injectable } from "tsyringe"; |
| 3 | +import { Catch } from "@/application/decorators/Catch"; |
| 4 | +import type { DiscordApiClient } from "@/domain/clients"; |
| 5 | +import type { ExecutionContext } from "@/domain/context"; |
| 6 | +import type { |
| 7 | + AccountConfirmationsRepository, |
| 8 | + AccountOauthRepository, |
| 9 | + AccountRepository, |
| 10 | +} from "@/domain/repositories"; |
| 11 | +import { TOKENS } from "@/infra/di/tokens"; |
| 12 | +import { env } from "@/infra/env"; |
| 13 | +import type { AccountConfirmationsService } from "../accountConfirmations"; |
| 14 | + |
| 15 | +@injectable() |
| 16 | +export class AccountOauthService { |
| 17 | + constructor( |
| 18 | + @inject(TOKENS.AccountConfirmationsService) |
| 19 | + private readonly accountConfirmationsService: AccountConfirmationsService, |
| 20 | + @inject(TOKENS.AccountConfirmationsRepository) |
| 21 | + private readonly accountConfirmationsRepository: AccountConfirmationsRepository, |
| 22 | + @inject(TOKENS.ExecutionContext) |
| 23 | + private readonly executionContext: ExecutionContext, |
| 24 | + @inject(TOKENS.AccountRepository) |
| 25 | + private readonly accountRepository: AccountRepository, |
| 26 | + @inject(TOKENS.AccountOauthRepository) |
| 27 | + private readonly accountOauthRepository: AccountOauthRepository, |
| 28 | + @inject(TOKENS.DiscordApiClient) |
| 29 | + private readonly discordApiClient: DiscordApiClient, |
| 30 | + ) {} |
| 31 | + |
| 32 | + @Catch() |
| 33 | + async requestDiscordLink() { |
| 34 | + if (!env.DISCORD_ENABLED) { |
| 35 | + throw new ORPCError("UNAVAILABLE", { |
| 36 | + message: "[Discord] integration is disabled", |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + const session = this.executionContext.session(); |
| 41 | + |
| 42 | + const account = await this.accountRepository.findByEmail(session.email); |
| 43 | + |
| 44 | + if (!account) { |
| 45 | + throw new ORPCError("NOT_FOUND", { |
| 46 | + message: "Account not found", |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + const alreadyLink = |
| 51 | + await this.accountOauthRepository.findByProviderAccountId( |
| 52 | + "DISCORD", |
| 53 | + account.id, |
| 54 | + ); |
| 55 | + |
| 56 | + if (alreadyLink) { |
| 57 | + throw new ORPCError("CONFLICT", { |
| 58 | + message: "This account is already linked to a Discord account", |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + const alreadyHasConfirmation = |
| 63 | + await this.accountConfirmationsRepository.findByAccountAndType( |
| 64 | + account.id, |
| 65 | + "DISCORD_OAUTH_LINK", |
| 66 | + ); |
| 67 | + |
| 68 | + if (alreadyHasConfirmation) { |
| 69 | + throw new ORPCError("CONFLICT", { |
| 70 | + message: |
| 71 | + "There is already a pending Discord link confirmation for this account", |
| 72 | + data: { |
| 73 | + expiresAt: alreadyHasConfirmation.expires_at, |
| 74 | + }, |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + const { token, tokenHash, expiresAt } = |
| 79 | + await this.accountConfirmationsService.generateTokenAndHash(5); |
| 80 | + |
| 81 | + await this.accountConfirmationsRepository.create(account.id, { |
| 82 | + channel: "CODE", |
| 83 | + type: "DISCORD_OAUTH_LINK", |
| 84 | + tokenHash, |
| 85 | + expiresAt, |
| 86 | + }); |
| 87 | + |
| 88 | + if (!env.DISCORD_CLIENT_ID || !env.DISCORD_REDIRECT_URI) { |
| 89 | + throw new ORPCError("UNAVAILABLE", { |
| 90 | + message: "Discord OAuth configuration is missing", |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + const params = new URLSearchParams({ |
| 95 | + client_id: env.DISCORD_CLIENT_ID, |
| 96 | + redirect_uri: env.DISCORD_REDIRECT_URI, |
| 97 | + response_type: "code", |
| 98 | + scope: ["identify", "email"].join(" "), |
| 99 | + state: token, |
| 100 | + prompt: "consent", |
| 101 | + }); |
| 102 | + |
| 103 | + const discordBaseUrl = this.discordApiClient.getRouteOauthRoute(); |
| 104 | + |
| 105 | + return { |
| 106 | + url: `${discordBaseUrl}?${params.toString()}`, |
| 107 | + }; |
| 108 | + } |
| 109 | + |
| 110 | + @Catch() |
| 111 | + async confirmDiscordLink(code: string, state: string) { |
| 112 | + if (!env.DISCORD_ENABLED) { |
| 113 | + throw new ORPCError("UNAVAILABLE", { |
| 114 | + message: "[Discord] integration is disabled", |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + const session = this.executionContext.session(); |
| 119 | + |
| 120 | + const account = await this.accountRepository.findByEmail(session.email); |
| 121 | + |
| 122 | + if (!account) { |
| 123 | + throw new ORPCError("NOT_FOUND", { |
| 124 | + message: "Account not found", |
| 125 | + }); |
| 126 | + } |
| 127 | + |
| 128 | + const confirmation = await this.accountConfirmationsService.isValid(state); |
| 129 | + |
| 130 | + const token = await this.discordApiClient.exchangeCodeForToken(code); |
| 131 | + const discordUser = await this.discordApiClient.getUserInfo( |
| 132 | + token.access_token, |
| 133 | + ); |
| 134 | + |
| 135 | + await this.accountOauthRepository.upsert( |
| 136 | + { |
| 137 | + accessToken: token.access_token, |
| 138 | + provider: "DISCORD", |
| 139 | + avatarUrl: null, |
| 140 | + displayName: discordUser.username, |
| 141 | + email: discordUser.email ?? null, |
| 142 | + refreshToken: token.refresh_token ?? null, |
| 143 | + expiresAt: new Date(Date.now() + token.expires_in * 1000), |
| 144 | + username: discordUser.username, |
| 145 | + }, |
| 146 | + { |
| 147 | + accountId: account.id, |
| 148 | + providerAccountId: discordUser.id, |
| 149 | + }, |
| 150 | + ); |
| 151 | + |
| 152 | + await this.accountConfirmationsService.verifyConfirmation( |
| 153 | + confirmation, |
| 154 | + state, |
| 155 | + ); |
| 156 | + |
| 157 | + return { |
| 158 | + url: `${env.FRONTEND_URL}/account/details`, |
| 159 | + }; |
| 160 | + } |
| 161 | + |
| 162 | + @Catch() |
| 163 | + async unlinkDiscord() { |
| 164 | + const session = this.executionContext.session(); |
| 165 | + |
| 166 | + const account = await this.accountRepository.findByEmail(session.email); |
| 167 | + |
| 168 | + if (!account) { |
| 169 | + throw new ORPCError("NOT_FOUND", { |
| 170 | + message: "Account not found", |
| 171 | + }); |
| 172 | + } |
| 173 | + |
| 174 | + const oauthAccount = |
| 175 | + await this.accountOauthRepository.findByProviderAccountId( |
| 176 | + "DISCORD", |
| 177 | + account.id, |
| 178 | + ); |
| 179 | + |
| 180 | + if (!oauthAccount) { |
| 181 | + throw new ORPCError("NOT_FOUND", { |
| 182 | + message: "No linked Discord account found", |
| 183 | + }); |
| 184 | + } |
| 185 | + |
| 186 | + await this.accountOauthRepository.deleteById(oauthAccount.id); |
| 187 | + } |
| 188 | +} |
0 commit comments