{"thread":{"id":"55838","subject":"[RFC] imap-send: support oauth2","startedAt":"2021-06-04T07:24:06Z","lastAt":"2021-06-04T13:52:52Z","messageCount":2,"participants":["Nicolas Morey-Chaisemartin","Felipe Contreras"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"426402","messageId":"ca1da892-f8ad-d878-a516-de5b08a99698@suse.com","threadId":"55838","inReplyTo":null,"subject":"[RFC] imap-send: support oauth2","fromName":"Nicolas Morey-Chaisemartin","fromEmail":"nmoreychaisemartin@suse.com","sentAt":"2021-06-04T07:23:59Z","receivedAt":"2021-06-04T07:24:06Z","isPatch":false,"sender":{"key":"devel-git@morey-chaisemartin.com","avatar":"https://avatars.githubusercontent.com/u/108326?v=4"},"body":"2FA/OAuth2 becoming a more and more regular thing these days (and a lot of SUSE devs being recently impacted by it), I've thrown together a quick patch\nto allow imap-send to support it.\nThis uses https://github.com/jeffmahoney/oauth2-clientd. It can be used with Outlook365 or Gmail. It creates a file with a token to be used to authenticate.\nAs libcurl supports this type of authentication, it is quite easy from there.\n\nWith this patch you still get prompted for you password even though it is not used but it overall works.\n\nBefore going any further on this, I wanted some feedback on the approach itself.\n\n---\n imap-send.c | 22 +++++++++++++++++++++-\n 1 file changed, 21 insertions(+), 1 deletion(-)\n\ndiff --git a/imap-send.c b/imap-send.c\nindex bb085d66d105..951d6bca696a 100644\n--- a/imap-send.c\n+++ b/imap-send.c\n@@ -91,6 +91,7 @@ struct imap_server_conf {\n \tconst char *folder;\n \tconst char *user;\n \tconst char *pass;\n+\tconst char *oauth;\n \tint use_ssl;\n \tint ssl_verify;\n \tint use_html;\n@@ -105,6 +106,7 @@ static struct imap_server_conf server = {\n \tNULL,\t/* folder */\n \tNULL,\t/* user */\n \tNULL,\t/* pass */\n+\tNULL,   /* oauth */\n \t0,   \t/* use_ssl */\n \t1,   \t/* ssl_verify */\n \t0,   \t/* use_html */\n@@ -1355,6 +1357,8 @@ static int git_imap_config(const char *var, const char *val, void *cb)\n \t\treturn git_config_string(&server.tunnel, var, val);\n \telse if (!strcmp(\"imap.authmethod\", var))\n \t\treturn git_config_string(&server.auth_method, var, val);\n+\telse if (!strcmp(\"imap.oauth\", var))\n+\t\treturn git_config_string(&server.oauth, var, val);\n \telse if (!strcmp(\"imap.port\", var))\n \t\tserver.port = git_config_int(var, val);\n \telse if (!strcmp(\"imap.host\", var)) {\n@@ -1432,7 +1436,23 @@ static CURL *setup_curl(struct imap_server_conf *srvc, struct credential *cred)\n \n \tserver_fill_credential(&server, cred);\n \tcurl_easy_setopt(curl, CURLOPT_USERNAME, server.user);\n-\tcurl_easy_setopt(curl, CURLOPT_PASSWORD, server.pass);\n+\n+\tif (server.oauth) {\n+\t\tstruct strbuf sb = STRBUF_INIT;\n+\t\tsize_t sz;\n+\t\tchar *token;\n+\n+\t\tsz = strbuf_read_file(&sb, server.oauth, 0);\n+\t\tif (sz < 0)\n+\t\t\tdie(\"failed to read oauth token file\");\n+\n+\t\tstrbuf_trim_trailing_newline(&sb);\n+\t\ttoken = strbuf_detach(&sb, &sz);\n+\t\tcurl_easy_setopt(curl, CURLOPT_XOAUTH2_BEARER, token);\n+\t\tfree(token);\n+\t} else {\n+\t\tcurl_easy_setopt(curl, CURLOPT_PASSWORD, server.pass);\n+\t}\n \n \tstrbuf_addstr(&path, server.use_ssl ? \"imaps://\" : \"imap://\");\n \tstrbuf_addstr(&path, server.host);\n-- \n2.31.1.5.g533053588dc3\n\n"},{"id":"426419","messageId":"60ba2ff3cff23_f62a62081b@natae.notmuch","threadId":"55838","inReplyTo":"ca1da892-f8ad-d878-a516-de5b08a99698@suse.com","subject":"RE: [RFC] imap-send: support oauth2","fromName":"Felipe Contreras","fromEmail":"felipe.contreras@gmail.com","sentAt":"2021-06-04T13:51:47Z","receivedAt":"2021-06-04T13:52:52Z","isPatch":false,"sender":{"key":"felipe.contreras@gmail.com","avatar":"https://avatars.githubusercontent.com/u/8358?v=4"},"body":"Nicolas Morey-Chaisemartin wrote:\n> 2FA/OAuth2 becoming a more and more regular thing these days (and a lot of SUSE devs being recently impacted by it), I've thrown together a quick patch\n> to allow imap-send to support it.\n> This uses https://github.com/jeffmahoney/oauth2-clientd. It can be used with Outlook365 or Gmail. It creates a file with a token to be used to authenticate.\n> As libcurl supports this type of authentication, it is quite easy from there.\n\nWhile trying to implement this is nice, it takes way more effort just to\nget a client id and secret than it takes to setup an app password.\n\nPlus I think this is abusing Google Cloud Platform. The point is to\nregister an application once, and that application can have thousands of\nusers, not thousands of users registering thousands of applications each\nused by a single user.\n\nIf you have an email service with OAuth2 authentication that doesn't\nrequire a client secret, then yeah; OAuth2 makes sense. I am not aware\nof any popular one though.\n\nCheers.\n\n-- \nFelipe Contreras\n"}]}