2 * This code is Copyright (c) 2014, by the authors of nmh. See the
3 * COPYRIGHT file in the root directory of the nmh distribution for
4 * complete copyright information.
23 static const struct mh_oauth_service_info SERVICES
[] = {
24 /* https://developers.google.com/accounts/docs/OAuth2InstalledApp */
27 /* display_name */ "Gmail",
29 /* client_id */ "91584523849-8lv9kgp1rvp8ahta6fa4b125tn2polcg.apps.googleusercontent.com",
30 /* client_secret */ "Ua8sX34xyv7hVrKM-U70dKI6",
32 /* auth_endpoint */ "https://accounts.google.com/o/oauth2/auth",
33 /* redirect_uri */ "urn:ietf:wg:oauth:2.0:oob",
34 /* token_endpoint */ "https://accounts.google.com/o/oauth2/token",
35 /* scope */ "https://mail.google.com/"
39 /* Copy service info so we don't have to free it only sometimes. */
41 copy_svc(mh_oauth_service_info
*to
, const mh_oauth_service_info
*from
)
43 to
->display_name
= from
->display_name
;
44 #define copy(_field_) to->_field_ = getcpy(from->_field_)
55 /* Return profile component node name for a service parameter. */
57 mh_oauth_node_name_for_svc(const char *base_name
, const char *svc
)
59 char *result
= mh_xmalloc(sizeof "oauth-" - 1
64 sprintf(result
, "oauth-%s-%s", svc
, base_name
);
69 /* Update one service_info field if overridden in profile. */
71 update_svc_field(char **field
, const char *base_name
, const char *svc
)
73 char *name
= mh_oauth_node_name_for_svc(base_name
, svc
);
74 const char *value
= context_find(name
);
77 *field
= mh_xstrdup(value
);
82 /* Update all service_info fields that are overridden in profile. */
84 update_svc(mh_oauth_service_info
*svc
, const char *svc_name
, char *errbuf
,
87 #define update(name) \
88 update_svc_field(&svc->name, #name, svc_name); \
89 if (svc->name == NULL) { \
90 snprintf(errbuf, errbuflen, "%s", #name " is missing"); \
91 errbuf[errbuflen - 1] = '\0'; \
96 update(client_secret
);
97 update(auth_endpoint
);
98 update(token_endpoint
);
102 if (svc
->name
== NULL
) {
103 svc
->name
= getcpy(svc_name
);
106 if (svc
->display_name
== NULL
) {
107 svc
->display_name
= svc
->name
;
114 mh_oauth_get_service_info(const char *svc_name
, mh_oauth_service_info
*svcinfo
,
115 char *errbuf
, size_t errbuflen
)
119 svcinfo
->name
= svcinfo
->display_name
= NULL
;
120 svcinfo
->scope
= svcinfo
->client_id
= NULL
;
121 svcinfo
->client_secret
= svcinfo
->auth_endpoint
= NULL
;
122 svcinfo
->token_endpoint
= svcinfo
->redirect_uri
= NULL
;
124 for (i
= 0; i
< (int) (sizeof SERVICES
/ sizeof SERVICES
[0]); i
++) {
125 if (strcmp(SERVICES
[i
].name
, svc_name
) == 0) {
126 copy_svc(svcinfo
, &SERVICES
[i
]);
131 if (!update_svc(svcinfo
, svc_name
, errbuf
, errbuflen
)) {
139 mh_oauth_cred_fn(const char *svc
)
141 char *result
, *result_if_allocated
;
143 char *component
= mh_oauth_node_name_for_svc("credential-file", svc
);
144 result
= context_find(component
);
147 if (result
== NULL
) {
148 result
= mh_xmalloc(sizeof "oauth-" - 1
151 sprintf(result
, "oauth-%s", svc
);
152 result_if_allocated
= result
;
154 result_if_allocated
= NULL
;
157 if (result
[0] != '/') {
158 const char *tmp
= m_maildir(result
);
159 free(result_if_allocated
);
160 result
= getcpy(tmp
);