Skip to content

Commit 6e6f95b

Browse files
AdityaGarg8gitster
authored andcommitted
send-email: add option to generate passswords like OAuth2 tokens
Some email providers like outlook allow only OAuth2 tokens to be used for authentication. This commit adds an option to generate OAuth2 tokens using scripts like M365-IMAP[1]. This option is similar to passwordeval in msmtp. Example usage: [sendemail] smtpEncryption = tls smtpServer = smtp.office365.com smtpUser = [email protected] smtpServerPort = 587 smtpauth = XOAUTH2 smtpPassEval = cd /workspaces/codespaces-blank/M365-IMAP && python3 ./refresh_token.py Signed-off-by: Aditya Garg <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ec76059 commit 6e6f95b

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

Documentation/git-send-email.adoc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,14 @@ or on the command line. If a username has been specified (with
230230
specified (with `--smtp-pass` or `sendemail.smtpPass`), then
231231
a password is obtained using 'git-credential'.
232232

233+
--smtp-passeval[=<command>]::
234+
Generate password like OAuth2 token for SMTP AUTH. If specified,
235+
it will use the output of the command specified as a password for
236+
authentication.
237+
+
238+
Note that it will override any existing password specified using
239+
`--smtp-pass` or a `sendemail.smtpPass`.
240+
233241
--no-smtp-auth::
234242
Disable SMTP authentication. Short hand for `--smtp-auth=none`
235243

git-send-email.perl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ sub usage {
5959
--smtp-server-port <int> * Outgoing SMTP server port.
6060
--smtp-user <str> * Username for SMTP-AUTH.
6161
--smtp-pass <str> * Password for SMTP-AUTH; not necessary.
62+
--smtp-passeval <str> * Path to script or a command to generate
63+
password like OAuth2 token for SMTP-AUTH.
6264
--smtp-encryption <str> * tls or ssl; anything else disables.
6365
--smtp-ssl * Deprecated. Use '--smtp-encryption ssl'.
6466
--smtp-ssl-cert-path <str> * Path to ca-certificates (either directory or file).
@@ -280,6 +282,7 @@ sub do_edit {
280282
my ($auto_8bit_encoding);
281283
my ($compose_encoding);
282284
my ($sendmail_cmd);
285+
my ($smtp_authpasseval);
283286
my ($mailmap_file, $mailmap_blob);
284287
# Variables with corresponding config settings & hardcoded defaults
285288
my ($debug_net_smtp) = 0; # Net::SMTP, see send_message()
@@ -316,6 +319,7 @@ sub do_edit {
316319
"smtppass" => \$smtp_authpass,
317320
"smtpdomain" => \$smtp_domain,
318321
"smtpauth" => \$smtp_auth,
322+
"smtppasseval" => \$smtp_authpasseval,
319323
"smtpbatchsize" => \$batch_size,
320324
"smtprelogindelay" => \$relogin_delay,
321325
"to" => \@config_to,
@@ -516,6 +520,7 @@ sub config_regexp {
516520
"smtp-server-port=s" => \$smtp_server_port,
517521
"smtp-user=s" => \$smtp_authuser,
518522
"smtp-pass:s" => \$smtp_authpass,
523+
"smtp-passeval=s" => \$smtp_authpasseval,
519524
"smtp-ssl" => sub { $smtp_encryption = 'ssl' },
520525
"smtp-encryption=s" => \$smtp_encryption,
521526
"smtp-ssl-cert-path=s" => \$smtp_ssl_cert_path,
@@ -1463,6 +1468,16 @@ sub smtp_auth_maybe {
14631468
return 1;
14641469
}
14651470

1471+
# If smtpPassEval is set, run the user specified command to get the password
1472+
if (defined $smtp_authpasseval) {
1473+
printf __("Executing token generating script: %s\n"), $smtp_authpasseval;
1474+
chomp(my $generated_password = `$smtp_authpasseval 2>&1`);
1475+
if ($? != 0) {
1476+
die sprintf(__("Failed to execute token generating script: %s\n"), $smtp_authpasseval);
1477+
}
1478+
$smtp_authpass = $generated_password;
1479+
}
1480+
14661481
# Workaround AUTH PLAIN/LOGIN interaction defect
14671482
# with Authen::SASL::Cyrus
14681483
eval {

0 commit comments

Comments
 (0)