Skip to content

Commit 236f1f4

Browse files
AdityaGarg8gitster
authored andcommitted
imap-send: add support for OAuth2.0 authentication
OAuth2.0 is a new way of authentication supported by various email providers these days. OAUTHBEARER and XOAUTH2 are the two most common mechanisms used for OAuth2.0. OAUTHBEARER is described in RFC5801[1] and RFC7628[2], whereas XOAUTH2 is Google's proprietary mechanism (See [3]). [1]: https://datatracker.ietf.org/doc/html/rfc5801 [2]: https://datatracker.ietf.org/doc/html/rfc7628 [3]: https://developers.google.com/workspace/gmail/imap/xoauth2-protocol#initial_client_response Signed-off-by: Aditya Garg <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent caae7ac commit 236f1f4

File tree

3 files changed

+187
-13
lines changed

3 files changed

+187
-13
lines changed

Documentation/config/imap.adoc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@ imap.authMethod::
4040
Specify the authentication method for authenticating with the IMAP server.
4141
If Git was built with the NO_CURL option, or if your curl version is older
4242
than 7.34.0, or if you're running git-imap-send with the `--no-curl`
43-
option, the only supported method is 'CRAM-MD5'. If this is not set
44-
then 'git imap-send' uses the basic IMAP plaintext LOGIN command.
43+
option, the only supported methods are `CRAM-MD5`, `OAUTHBEARER` and
44+
`XOAUTH2`. If this is not set then `git imap-send` uses the basic IMAP
45+
plaintext `LOGIN` command.

Documentation/git-imap-send.adoc

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,18 @@ Using Gmail's IMAP interface:
102102

103103
---------
104104
[imap]
105-
folder = "[Gmail]/Drafts"
106-
host = imaps://imap.gmail.com
107-
108-
port = 993
105+
folder = "[Gmail]/Drafts"
106+
host = imaps://imap.gmail.com
107+
108+
port = 993
109109
---------
110110

111+
Gmail does not allow using your regular password for `git imap-send`.
112+
If you have multi-factor authentication set up on your Gmail account, you
113+
can generate an app-specific password for use with `git imap-send`.
114+
Visit https://security.google.com/settings/security/apppasswords to create
115+
it. Alternatively, use OAuth2.0 authentication as described below.
116+
111117
[NOTE]
112118
You might need to instead use: `folder = "[Google Mail]/Drafts"` if you get an error
113119
that the "Folder doesn't exist".
@@ -116,6 +122,35 @@ that the "Folder doesn't exist".
116122
If your Gmail account is set to another language than English, the name of the "Drafts"
117123
folder will be localized.
118124

125+
If you want to use OAuth2.0 based authentication, you can specify
126+
`OAUTHBEARER` or `XOAUTH2` mechanism in your config. It is more secure
127+
than using app-specific passwords, and also does not enforce the need of
128+
having multi-factor authentication. You will have to use an OAuth2.0
129+
access token in place of your password when using this authentication.
130+
131+
---------
132+
[imap]
133+
folder = "[Gmail]/Drafts"
134+
host = imaps://imap.gmail.com
135+
136+
port = 993
137+
authmethod = OAUTHBEARER
138+
---------
139+
140+
Using Outlook's IMAP interface:
141+
142+
Unlike Gmail, Outlook only supports OAuth2.0 based authentication. Also, it
143+
supports only `XOAUTH2` as the mechanism.
144+
145+
---------
146+
[imap]
147+
folder = "Drafts"
148+
host = imaps://outlook.office365.com
149+
150+
port = 993
151+
authmethod = XOAUTH2
152+
---------
153+
119154
Once the commits are ready to be sent, run the following command:
120155

121156
$ git format-patch --cover-letter -M --stdout origin/master | git imap-send
@@ -124,6 +159,10 @@ Just make sure to disable line wrapping in the email client (Gmail's web
124159
interface will wrap lines no matter what, so you need to use a real
125160
IMAP client).
126161

162+
In case you are using OAuth2.0 authentication, it is easier to use credential
163+
helpers to generate tokens. Credential helpers suggested in
164+
linkgit:git-send-email[1] can be used for `git imap-send` as well.
165+
127166
CAUTION
128167
-------
129168
It is still your responsibility to make sure that the email message

imap-send.c

Lines changed: 141 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ enum CAPABILITY {
139139
LITERALPLUS,
140140
NAMESPACE,
141141
STARTTLS,
142-
AUTH_CRAM_MD5
142+
AUTH_CRAM_MD5,
143+
AUTH_OAUTHBEARER,
144+
AUTH_XOAUTH2,
143145
};
144146

145147
static const char *cap_list[] = {
@@ -149,6 +151,8 @@ static const char *cap_list[] = {
149151
"NAMESPACE",
150152
"STARTTLS",
151153
"AUTH=CRAM-MD5",
154+
"AUTH=OAUTHBEARER",
155+
"AUTH=XOAUTH2",
152156
};
153157

154158
#define RESP_OK 0
@@ -885,6 +889,68 @@ static char *cram(const char *challenge_64, const char *user, const char *pass)
885889
return (char *)response_64;
886890
}
887891

892+
static char *oauthbearer_base64(const char *user, const char *access_token)
893+
{
894+
int raw_len, b64_len;
895+
char *raw, *b64;
896+
897+
/*
898+
* Compose the OAUTHBEARER string
899+
*
900+
* "n,a=" {User} ",^Ahost=" {Host} "^Aport=" {Port} "^Aauth=Bearer " {Access Token} "^A^A
901+
*
902+
* The first part `n,a=" {User} ",` is the gs2 header described in RFC5801.
903+
* * gs2-cb-flag `n` -> client does not support CB
904+
* * gs2-authzid `a=" {User} "`
905+
*
906+
* The second part are key value pairs containing host, port and auth as
907+
* described in RFC7628.
908+
*
909+
* https://datatracker.ietf.org/doc/html/rfc5801
910+
* https://datatracker.ietf.org/doc/html/rfc7628
911+
*/
912+
raw_len = strlen(user) + strlen(access_token) + 20;
913+
raw = xmallocz(raw_len + 1);
914+
snprintf(raw, raw_len + 1, "n,a=%s,\001auth=Bearer %s\001\001", user, access_token);
915+
916+
/* Base64 encode */
917+
b64 = xmallocz(ENCODED_SIZE(strlen(raw)));
918+
b64_len = EVP_EncodeBlock((unsigned char *)b64, (unsigned char *)raw, strlen(raw));
919+
free(raw);
920+
921+
if (b64_len < 0) {
922+
free(b64);
923+
return NULL;
924+
}
925+
return b64;
926+
}
927+
928+
static char *xoauth2_base64(const char *user, const char *access_token)
929+
{
930+
int raw_len, b64_len;
931+
char *raw, *b64;
932+
933+
/*
934+
* Compose the XOAUTH2 string
935+
* "user=" {User} "^Aauth=Bearer " {Access Token} "^A^A"
936+
* https://developers.google.com/workspace/gmail/imap/xoauth2-protocol#initial_client_response
937+
*/
938+
raw_len = strlen(user) + strlen(access_token) + 20;
939+
raw = xmallocz(raw_len + 1);
940+
snprintf(raw, raw_len + 1, "user=%s\001auth=Bearer %s\001\001", user, access_token);
941+
942+
/* Base64 encode */
943+
b64 = xmallocz(ENCODED_SIZE(strlen(raw)));
944+
b64_len = EVP_EncodeBlock((unsigned char *)b64, (unsigned char *)raw, strlen(raw));
945+
free(raw);
946+
947+
if (b64_len < 0) {
948+
free(b64);
949+
return NULL;
950+
}
951+
return b64;
952+
}
953+
888954
static int auth_cram_md5(struct imap_store *ctx, const char *prompt)
889955
{
890956
int ret;
@@ -903,9 +969,51 @@ static int auth_cram_md5(struct imap_store *ctx, const char *prompt)
903969
return 0;
904970
}
905971

972+
static int auth_oauthbearer(struct imap_store *ctx, const char *prompt UNUSED)
973+
{
974+
int ret;
975+
char *b64;
976+
977+
b64 = oauthbearer_base64(ctx->cfg->user, ctx->cfg->pass);
978+
if (!b64)
979+
return error("OAUTHBEARER: base64 encoding failed");
980+
981+
/* Send the base64-encoded response */
982+
ret = socket_write(&ctx->imap->buf.sock, b64, strlen(b64));
983+
if (ret != (int)strlen(b64)) {
984+
free(b64);
985+
return error("IMAP error: sending OAUTHBEARER response failed");
986+
}
987+
988+
free(b64);
989+
return 0;
990+
}
991+
992+
static int auth_xoauth2(struct imap_store *ctx, const char *prompt UNUSED)
993+
{
994+
int ret;
995+
char *b64;
996+
997+
b64 = xoauth2_base64(ctx->cfg->user, ctx->cfg->pass);
998+
if (!b64)
999+
return error("XOAUTH2: base64 encoding failed");
1000+
1001+
/* Send the base64-encoded response */
1002+
ret = socket_write(&ctx->imap->buf.sock, b64, strlen(b64));
1003+
if (ret != (int)strlen(b64)) {
1004+
free(b64);
1005+
return error("IMAP error: sending XOAUTH2 response failed");
1006+
}
1007+
1008+
free(b64);
1009+
return 0;
1010+
}
1011+
9061012
#else
9071013

9081014
#define auth_cram_md5 NULL
1015+
#define auth_oauthbearer NULL
1016+
#define auth_xoauth2 NULL
9091017

9101018
#endif
9111019

@@ -1118,6 +1226,12 @@ static struct imap_store *imap_open_store(struct imap_server_conf *srvc, const c
11181226
if (!strcmp(srvc->auth_method, "CRAM-MD5")) {
11191227
if (try_auth_method(srvc, ctx, imap, "CRAM-MD5", AUTH_CRAM_MD5, auth_cram_md5))
11201228
goto bail;
1229+
} else if (!strcmp(srvc->auth_method, "OAUTHBEARER")) {
1230+
if (try_auth_method(srvc, ctx, imap, "OAUTHBEARER", AUTH_OAUTHBEARER, auth_oauthbearer))
1231+
goto bail;
1232+
} else if (!strcmp(srvc->auth_method, "XOAUTH2")) {
1233+
if (try_auth_method(srvc, ctx, imap, "XOAUTH2", AUTH_XOAUTH2, auth_xoauth2))
1234+
goto bail;
11211235
} else {
11221236
fprintf(stderr, "Unknown authentication method:%s\n", srvc->host);
11231237
goto bail;
@@ -1419,7 +1533,16 @@ static CURL *setup_curl(struct imap_server_conf *srvc, struct credential *cred)
14191533

14201534
server_fill_credential(srvc, cred);
14211535
curl_easy_setopt(curl, CURLOPT_USERNAME, srvc->user);
1422-
curl_easy_setopt(curl, CURLOPT_PASSWORD, srvc->pass);
1536+
1537+
/*
1538+
* Use CURLOPT_PASSWORD irrespective of whether there is
1539+
* an auth method specified or not, unless it's OAuth2.0,
1540+
* where we use CURLOPT_XOAUTH2_BEARER.
1541+
*/
1542+
if (!srvc->auth_method ||
1543+
(strcmp(srvc->auth_method, "XOAUTH2") &&
1544+
strcmp(srvc->auth_method, "OAUTHBEARER")))
1545+
curl_easy_setopt(curl, CURLOPT_PASSWORD, srvc->pass);
14231546

14241547
strbuf_addstr(&path, srvc->use_ssl ? "imaps://" : "imap://");
14251548
strbuf_addstr(&path, srvc->host);
@@ -1437,11 +1560,22 @@ static CURL *setup_curl(struct imap_server_conf *srvc, struct credential *cred)
14371560
curl_easy_setopt(curl, CURLOPT_PORT, srvc->port);
14381561

14391562
if (srvc->auth_method) {
1440-
struct strbuf auth = STRBUF_INIT;
1441-
strbuf_addstr(&auth, "AUTH=");
1442-
strbuf_addstr(&auth, srvc->auth_method);
1443-
curl_easy_setopt(curl, CURLOPT_LOGIN_OPTIONS, auth.buf);
1444-
strbuf_release(&auth);
1563+
if (!strcmp(srvc->auth_method, "XOAUTH2") ||
1564+
!strcmp(srvc->auth_method, "OAUTHBEARER")) {
1565+
1566+
/*
1567+
* While CURLOPT_XOAUTH2_BEARER looks as if it only supports XOAUTH2,
1568+
* upon debugging, it has been found that it is capable of detecting
1569+
* the best option out of OAUTHBEARER and XOAUTH2.
1570+
*/
1571+
curl_easy_setopt(curl, CURLOPT_XOAUTH2_BEARER, srvc->pass);
1572+
} else {
1573+
struct strbuf auth = STRBUF_INIT;
1574+
strbuf_addstr(&auth, "AUTH=");
1575+
strbuf_addstr(&auth, srvc->auth_method);
1576+
curl_easy_setopt(curl, CURLOPT_LOGIN_OPTIONS, auth.buf);
1577+
strbuf_release(&auth);
1578+
}
14451579
}
14461580

14471581
if (!srvc->use_ssl)

0 commit comments

Comments
 (0)