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
)
60 return concat("oauth-", svc
, "-", base_name
, NULL
);
63 /* Update one service_info field if overridden in profile. */
65 update_svc_field(char **field
, const char *base_name
, const char *svc
)
67 char *name
= mh_oauth_node_name_for_svc(base_name
, svc
);
68 const char *value
= context_find(name
);
71 *field
= mh_xstrdup(value
);
76 /* Update all service_info fields that are overridden in profile. */
78 update_svc(mh_oauth_service_info
*svc
, const char *svc_name
, char *errbuf
,
81 #define update(name) \
82 update_svc_field(&svc->name, #name, svc_name); \
83 if (svc->name == NULL) { \
84 snprintf(errbuf, errbuflen, "%s", #name " is missing"); \
85 errbuf[errbuflen - 1] = '\0'; \
90 update(client_secret
);
91 update(auth_endpoint
);
92 update(token_endpoint
);
96 if (svc
->name
== NULL
) {
97 svc
->name
= getcpy(svc_name
);
100 if (svc
->display_name
== NULL
) {
101 svc
->display_name
= svc
->name
;
108 mh_oauth_get_service_info(const char *svc_name
, mh_oauth_service_info
*svcinfo
,
109 char *errbuf
, size_t errbuflen
)
113 svcinfo
->name
= svcinfo
->display_name
= NULL
;
114 svcinfo
->scope
= svcinfo
->client_id
= NULL
;
115 svcinfo
->client_secret
= svcinfo
->auth_endpoint
= NULL
;
116 svcinfo
->token_endpoint
= svcinfo
->redirect_uri
= NULL
;
118 for (i
= 0; i
< (int) (sizeof SERVICES
/ sizeof SERVICES
[0]); i
++) {
119 if (strcmp(SERVICES
[i
].name
, svc_name
) == 0) {
120 copy_svc(svcinfo
, &SERVICES
[i
]);
125 if (!update_svc(svcinfo
, svc_name
, errbuf
, errbuflen
)) {
133 mh_oauth_cred_fn(const char *svc
)
135 char *result
, *result_if_allocated
;
137 char *component
= mh_oauth_node_name_for_svc("credential-file", svc
);
138 result
= context_find(component
);
141 if (result
== NULL
) {
142 result
= concat("oauth-", svc
, NULL
);
143 result_if_allocated
= result
;
145 result_if_allocated
= NULL
;
148 if (result
[0] != '/') {
149 const char *tmp
= m_maildir(result
);
150 free(result_if_allocated
);
151 result
= getcpy(tmp
);