Checkpoint: Remplacement complet de imap 0.8 par ImapFlow : OAuth2 moderne avec jeton brut, TLS strict, traitement séquentiel des messages non lus, verrou anti-concurrence conservé, marquage Seen uniquement après succès, test de connexion adapté, diagnostic OAuth2 et tests unitaires ajoutés. Validation : 31 tests, TypeScript, build et authentification réelle Microsoft 365 réussis.
This commit is contained in:
49
scripts/test-imapflow-oauth.mjs
Normal file
49
scripts/test-imapflow-oauth.mjs
Normal file
@@ -0,0 +1,49 @@
|
||||
import { ImapFlow } from "imapflow";
|
||||
|
||||
const required = (name) => {
|
||||
const value = process.env[name];
|
||||
if (!value) throw new Error(`Variable ${name} manquante`);
|
||||
return value;
|
||||
};
|
||||
|
||||
const tenantId = required("AZURE_AD_TENANT_ID");
|
||||
const clientId = required("AZURE_AD_CLIENT_ID");
|
||||
const clientSecret = required("AZURE_AD_CLIENT_SECRET");
|
||||
const email = required("IMAP_TEST_EMAIL");
|
||||
const host = process.env.IMAP_TEST_HOST || "outlook.office365.com";
|
||||
|
||||
const response = await fetch(
|
||||
`https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||
body: new URLSearchParams({
|
||||
client_id: clientId,
|
||||
client_secret: clientSecret,
|
||||
scope: "https://outlook.office365.com/.default",
|
||||
grant_type: "client_credentials",
|
||||
}),
|
||||
},
|
||||
);
|
||||
const tokenResponse = await response.json();
|
||||
if (!response.ok || !tokenResponse.access_token) {
|
||||
throw new Error(`Échec OAuth2 : ${tokenResponse.error || response.status}`);
|
||||
}
|
||||
|
||||
const client = new ImapFlow({
|
||||
host,
|
||||
port: 993,
|
||||
secure: true,
|
||||
auth: { user: email, accessToken: tokenResponse.access_token },
|
||||
tls: { servername: host, rejectUnauthorized: true },
|
||||
verifyOnly: true,
|
||||
logger: false,
|
||||
});
|
||||
|
||||
try {
|
||||
await client.connect();
|
||||
console.log(`Authentification ImapFlow OAuth2 réussie pour ${email}`);
|
||||
} finally {
|
||||
if (client.usable) await client.logout().catch(() => client.close());
|
||||
else client.close();
|
||||
}
|
||||
Reference in New Issue
Block a user