2 * Implementation of OAuth 2.0 [1] for XOAUTH2 in SMTP [2] and POP3 [3].
4 * Google defined XOAUTH2 for SMTP, and that's what we use here. If other
5 * providers implement XOAUTH2 or some similar OAuth-based SMTP authentication
6 * protocol, it should be simple to extend this.
8 * [1] https://tools.ietf.org/html/rfc6749
9 * [2] https://developers.google.com/gmail/xoauth2_protocol
10 * [3] http://googleappsdeveloper.blogspot.com/2014/10/updates-on-authentication-for-gmail.html
12 * Presumably [2] should document POP3 and that is an over-sight. As it stands,
13 * that blog post is the closest we have to documentation.
15 * According to [1] 2.1 Client Types, this is a "native application", a
18 * To summarize the flow:
20 * 1. User runs mhlogin which prints a URL the user must visit, and prompts for
21 * a code retrieved from that page.
23 * 2. User vists this URL in browser, signs in with some Google account, and
24 * copies and pastes the resulting code back to mhlogin.
26 * 3. mhlogin does HTTP POST to Google to exchange the user-provided code for a
27 * short-lived access token and a long-lived refresh token.
29 * 4. send uses the access token in SMTP auth if not expired. If it is expired,
30 * it does HTTP POST to Google including the refresh token and gets back a
31 * new access token (and possibly refresh token). If the refresh token has
32 * become invalid (e.g. if the user took some reset action on the Google
33 * account), the user must use mhlogin again, then re-run send.
37 /* error loading profile */
38 MH_OAUTH_BAD_PROFILE
= OK
+ 1,
40 /* error initializing libcurl */
43 /* local error initializing HTTP request */
44 MH_OAUTH_REQUEST_INIT
,
46 /* error executing HTTP POST request */
49 /* HTTP response body is too big. */
50 MH_OAUTH_RESPONSE_TOO_BIG
,
52 /* Can't process HTTP response body. */
53 MH_OAUTH_RESPONSE_BAD
,
55 /* The authorization server rejected the grant (authorization code or
56 * refresh token); possibly the user entered a bad code, or the refresh
57 * token has become invalid, etc. */
60 /* HTTP server indicates something is wrong with our request. */
63 /* Attempting to refresh an access token without a refresh token. */
67 /* requested user not in cred file */
68 MH_OAUTH_CRED_USER_NOT_FOUND
,
70 /* error loading serialized credentials */
74 typedef struct mh_oauth_ctx mh_oauth_ctx
;
76 typedef struct mh_oauth_cred mh_oauth_cred
;
78 typedef struct mh_oauth_service_info mh_oauth_service_info
;
80 struct mh_oauth_service_info
{
81 /* Name of service, so we can search static internal services array
82 * and for determining default credential file name. */
85 /* Human-readable name of the service; in mh_oauth_ctx::svc this is not
86 * another buffer to free, but a pointer to either static SERVICE data
87 * (below) or to the name field. */
90 /* [1] 2.2 Client Identifier, 2.3.1 Client Password */
92 /* [1] 2.3.1 Client Password */
94 /* [1] 3.1 Authorization Endpoint */
96 /* [1] 3.1.2 Redirection Endpoint */
98 /* [1] 3.2 Token Endpoint */
100 /* [1] 3.3 Access Token Scope */
105 * Do the complete dance for XOAUTH2 as used by POP3 and SMTP.
107 * Load tokens for svc from disk, refresh if necessary, and return the
108 * base64-encoded client response.
110 * If refreshing, writes freshened tokens to disk.
112 * Exits via adios on any error.
115 mh_oauth_do_xoauth(const char *user
, const char *svc
, FILE *log
);
118 * Allocate and initialize a new OAuth context.
120 * Caller must call mh_oauth_free(ctx) when finished, even on error.
122 * svc_name must point to a null-terminated string identifying the service
123 * provider. Support for "gmail" is built-in; anything else must be defined in
124 * the user's profile. The profile can also override "gmail" settings.
126 * Accesses global m_defs via context_find.
128 * On error, return FALSE and set an error in ctx; ctx is always allocated.
131 mh_oauth_new(mh_oauth_ctx
**ctx
, const char *svc_name
);
134 * Free all resources associated with ctx.
137 mh_oauth_free(mh_oauth_ctx
*ctx
);
140 * Return null-terminated human-readable name of the service, e.g. "Gmail".
142 * Never returns NULL.
145 mh_oauth_svc_display_name(const mh_oauth_ctx
*ctx
);
148 * Enable logging for subsequent operations on ctx.
150 * log must not be closed until after mh_oauth_free.
152 * For all HTTP requests, the request is logged with each line prefixed with
153 * "< ", and the response with "> ". Other messages are prefixed with "* ".
156 mh_oauth_log_to(FILE *log
, mh_oauth_ctx
*ctx
);
159 * Return the error code after some function indicated an error.
161 * Must not be called if an error was not indicated.
164 mh_oauth_get_err_code(const mh_oauth_ctx
*ctx
);
167 * Return null-terminated error message after some function indicated an error.
169 * Never returns NULL, but must not be called if an error was not indicated.
172 mh_oauth_get_err_string(mh_oauth_ctx
*ctx
);
175 * Return the null-terminated URL the user needs to visit to authorize access.
177 * URL may be invalidated by subsequent calls to mh_oauth_get_authorize_url,
178 * mh_oauth_authorize, or mh_oauth_refresh.
180 * On error, return NULL.
183 mh_oauth_get_authorize_url(mh_oauth_ctx
*ctx
);
186 * Exchange code provided by the user for access (and maybe refresh) token.
188 * On error, return NULL.
191 mh_oauth_authorize(const char *code
, mh_oauth_ctx
*ctx
);
194 * Refresh access (and maybe refresh) token if refresh token present.
196 * On error, return FALSE and leave cred untouched.
199 mh_oauth_refresh(mh_oauth_cred
*cred
);
202 * Return whether access token is present and not expired at time T.
205 mh_oauth_access_token_valid(time_t t
, const mh_oauth_cred
*cred
);
208 * Free all resources associated with cred.
211 mh_oauth_cred_free(mh_oauth_cred
*cred
);
214 * Return the null-terminated file name for storing this service's OAuth tokens.
216 * Accesses global m_defs via context_find.
218 * Never returns NULL.
221 mh_oauth_cred_fn(const char *svc_name
);
224 * Serialize OAuth tokens to file.
226 * On error, return FALSE.
229 mh_oauth_cred_save(FILE *fp
, mh_oauth_cred
*cred
, const char *user
);
232 * Load OAuth tokens from file.
234 * Calls m_getfld(), which writes to stderr with advise().
236 * On error, return NULL.
239 mh_oauth_cred_load(FILE *fp
, mh_oauth_ctx
*ctx
, const char *user
);
242 * Return null-terminated SASL client response for XOAUTH2 from access token.
244 * Store the length in res_len.
246 * Must not be called except after successful mh_oauth_access_token_valid or
247 * mh_oauth_refresh call; i.e. must have a valid access token.
250 mh_oauth_sasl_client_response(size_t *res_len
,
251 const char *user
, const mh_oauth_cred
*cred
);
254 * Retrieve the various entries for the OAuth mechanism
258 mh_oauth_get_service_info(const char *svc_name
, mh_oauth_service_info
*svcinfo
,
259 char *errbuf
, size_t errbuflen
);
261 char *mh_oauth_get_svc_name(mh_oauth_ctx
*ctx
);
262 void mh_oauth_set_cred_fn(mh_oauth_ctx
*ctx
, char *filename
);