Skip to content

Commit 453d324

Browse files
authored
Merge pull request #2803 from Geod24/mlang/xoauth2
Mail: Implement SMTP XOAUTH2 authentication
2 parents 24c4630 + 15a07f7 commit 453d324

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

mail/vibe/mail/smtp.d

+22-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,15 @@ enum SMTPAuthType {
7171
none,
7272
plain,
7373
login,
74-
cramMd5
74+
cramMd5,
75+
/// Authenticate using XOAUTH2 protocol
76+
///
77+
/// Works for GMail and Office 365
78+
///
79+
/// See_Also:
80+
/// - https://developers.google.com/gmail/imap/xoauth2-protocol#using_oauth_20
81+
/// - https://learn.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth
82+
xoauth2,
7583
}
7684

7785

@@ -191,6 +199,19 @@ void sendMail(in SMTPClientSettings settings, Mail mail)
191199
conn.write(Base64.encode(cast(const(ubyte)[])settings.password) ~ "\r\n");
192200
expectStatus(conn, 235, "login password");
193201
break;
202+
case SMTPAuthType.xoauth2:
203+
// Ideally we should inspect the server's capabilities instead of
204+
// making this decision, but since the RFC for OAuth requires TLS,
205+
// be a bit conservative.
206+
enforce(settings.connectionType != SMTPConnectionType.plain,
207+
"Cannot use XOAUTH2 without TLS");
208+
conn.write("AUTH XOAUTH2\r\n");
209+
expectStatus(conn, SMTPStatus.serverAuthReady, "AUTH XOAUTH2");
210+
const authStr = "user=%s\1auth=Bearer %s\1\1".format(
211+
settings.username, settings.password);
212+
conn.write(Base64.encode(authStr.representation) ~ "\r\n");
213+
expectStatus(conn, 235, "XOAUTH2 authentication");
214+
break;
194215
case SMTPAuthType.cramMd5: assert(false, "TODO!");
195216
}
196217

0 commit comments

Comments
 (0)