1 /* oauth_prof.c -- OAuth 2.0 implementation for XOAUTH2 in SMTP and POP3.
3 * This code is Copyright (c) 2014, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
10 #include "context_find.h"
25 #include "m_maildir.h"
27 static const struct mh_oauth_service_info SERVICES
[] = {
28 /* https://developers.google.com/accounts/docs/OAuth2InstalledApp */
31 /* display_name */ "Gmail",
33 /* client_id */ "91584523849-8lv9kgp1rvp8ahta6fa4b125tn2polcg.apps.googleusercontent.com",
34 /* client_secret */ "Ua8sX34xyv7hVrKM-U70dKI6",
36 /* auth_endpoint */ "https://accounts.google.com/o/oauth2/auth",
37 /* redirect_uri */ "urn:ietf:wg:oauth:2.0:oob",
38 /* token_endpoint */ "https://accounts.google.com/o/oauth2/token",
39 /* scope */ "https://mail.google.com/"
43 /* Copy service info so we don't have to free it only sometimes. */
45 copy_svc(mh_oauth_service_info
*to
, const mh_oauth_service_info
*from
)
47 to
->display_name
= from
->display_name
;
48 #define copy(_field_) to->_field_ = getcpy(from->_field_)
59 /* Return profile component node name for a service parameter. */
61 mh_oauth_node_name_for_svc(const char *base_name
, const char *svc
)
64 return concat("oauth-", svc
, "-", base_name
, NULL
);
67 /* Update one service_info field if overridden in profile. */
69 update_svc_field(char **field
, const char *base_name
, const char *svc
)
71 char *name
= mh_oauth_node_name_for_svc(base_name
, svc
);
72 const char *value
= context_find(name
);
75 *field
= mh_xstrdup(value
);
80 /* Update all service_info fields that are overridden in profile. */
82 update_svc(mh_oauth_service_info
*svc
, const char *svc_name
, char *errbuf
,
85 #define update(name) \
86 update_svc_field(&svc->name, #name, svc_name); \
87 if (svc->name == NULL) { \
88 snprintf(errbuf, errbuflen, "%s", #name " is missing"); \
89 errbuf[errbuflen - 1] = '\0'; \
94 update(client_secret
);
95 update(auth_endpoint
);
96 update(token_endpoint
);
100 if (svc
->name
== NULL
) {
101 svc
->name
= getcpy(svc_name
);
104 if (svc
->display_name
== NULL
) {
105 svc
->display_name
= svc
->name
;
112 mh_oauth_get_service_info(const char *svc_name
, mh_oauth_service_info
*svcinfo
,
113 char *errbuf
, size_t errbuflen
)
117 svcinfo
->name
= svcinfo
->display_name
= NULL
;
118 svcinfo
->scope
= svcinfo
->client_id
= NULL
;
119 svcinfo
->client_secret
= svcinfo
->auth_endpoint
= NULL
;
120 svcinfo
->token_endpoint
= svcinfo
->redirect_uri
= NULL
;
122 for (i
= 0; i
< (int)DIM(SERVICES
); i
++) {
123 if (strcmp(SERVICES
[i
].name
, svc_name
) == 0) {
124 copy_svc(svcinfo
, &SERVICES
[i
]);
129 return update_svc(svcinfo
, svc_name
, errbuf
, errbuflen
);
132 /* Return value must be free(3)'d. */
134 mh_oauth_cred_fn(const char *svc
)
136 char *key
= mh_oauth_node_name_for_svc("credential-file", svc
);
137 char *value
= context_find(key
);
143 return mh_xstrdup(value
);
145 value
= concat("oauth-", svc
, NULL
);
147 const char *md
= m_maildir(value
);
151 return mh_xstrdup(md
);