-
-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: initial oauth work for email providers
- Loading branch information
Showing
7 changed files
with
388 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { prisma } from "../../prisma"; | ||
|
||
const nodemailer = require("nodemailer"); | ||
const { google } = require("google-auth-library"); | ||
const { ConfidentialClientApplication } = require("@azure/identity"); | ||
|
||
// Environment variables or configuration | ||
const CLIENT_ID = process.env.CLIENT_ID; | ||
const CLIENT_SECRET = process.env.CLIENT_SECRET; | ||
const REFRESH_TOKEN = process.env.REFRESH_TOKEN; | ||
const TENANT_ID = process.env.TENANT_ID; | ||
|
||
export async function createTransportProvider() { | ||
const provider = await prisma.email.findFirst({}); | ||
|
||
if (CLIENT_ID && CLIENT_SECRET && (REFRESH_TOKEN || TENANT_ID)) { | ||
// OAuth2 configuration | ||
if (EMAIL_SERVICE === "gmail") { | ||
// Gmail | ||
const oAuth2Client = new google.auth.OAuth2(CLIENT_ID, CLIENT_SECRET); | ||
oAuth2Client.setCredentials({ refresh_token: REFRESH_TOKEN }); | ||
const accessToken = await oAuth2Client.getAccessToken(); | ||
|
||
return nodemailer.createTransport({ | ||
service: "gmail", | ||
auth: { | ||
type: "OAuth2", | ||
user: EMAIL_USER, | ||
clientId: CLIENT_ID, | ||
clientSecret: CLIENT_SECRET, | ||
refreshToken: REFRESH_TOKEN, | ||
accessToken: accessToken.token, | ||
}, | ||
}); | ||
} else if (EMAIL_SERVICE === "microsoft") { | ||
// Microsoft | ||
const cca = new ConfidentialClientApplication({ | ||
auth: { | ||
clientId: CLIENT_ID, | ||
authority: `https://login.microsoftonline.com/${TENANT_ID}`, | ||
clientSecret: CLIENT_SECRET, | ||
}, | ||
}); | ||
|
||
const result = await cca.acquireTokenByClientCredential({ | ||
scopes: ["https://graph.microsoft.com/.default"], | ||
}); | ||
|
||
return nodemailer.createTransport({ | ||
service: "hotmail", | ||
auth: { | ||
type: "OAuth2", | ||
user: EMAIL_USER, | ||
clientId: CLIENT_ID, | ||
clientSecret: CLIENT_SECRET, | ||
accessToken: result.accessToken, | ||
}, | ||
}); | ||
} | ||
} else if (EMAIL_USER && EMAIL_PASS) { | ||
// Username/password configuration | ||
return nodemailer.createTransport({ | ||
service: EMAIL_SERVICE, | ||
auth: { | ||
user: EMAIL_USER, | ||
pass: EMAIL_PASS, | ||
}, | ||
}); | ||
} else { | ||
throw new Error("No valid authentication method configured."); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
apps/api/src/prisma/migrations/20241020165331_oauth_email_authentication/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
-- AlterTable | ||
ALTER TABLE "Email" ADD COLUMN "clientId" TEXT, | ||
ADD COLUMN "clientSecret" TEXT, | ||
ADD COLUMN "refreshToken" TEXT, | ||
ADD COLUMN "serviceType" TEXT NOT NULL DEFAULT 'other', | ||
ADD COLUMN "tenantId" TEXT, | ||
ALTER COLUMN "pass" DROP NOT NULL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.