]> diplodocus.org Git - nmh/blob - sbr/oauth_prof.c
Alter mh-chart(7)'s NAME to be lowercase.
[nmh] / sbr / oauth_prof.c
1 /*
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.
5 */
6
7 #include <h/mh.h>
8
9 #ifdef OAUTH_SUPPORT
10
11 #include <sys/stat.h>
12
13 #include <stdarg.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <strings.h>
18 #include <unistd.h>
19
20 #include <h/oauth.h>
21 #include <h/utils.h>
22
23 static const struct mh_oauth_service_info SERVICES[] = {
24 /* https://developers.google.com/accounts/docs/OAuth2InstalledApp */
25 {
26 /* name */ "gmail",
27 /* display_name */ "Gmail",
28
29 /* client_id */ "91584523849-8lv9kgp1rvp8ahta6fa4b125tn2polcg.apps.googleusercontent.com",
30 /* client_secret */ "Ua8sX34xyv7hVrKM-U70dKI6",
31
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/"
36 }
37 };
38
39 /* Copy service info so we don't have to free it only sometimes. */
40 static void
41 copy_svc(mh_oauth_service_info *to, const mh_oauth_service_info *from)
42 {
43 to->display_name = from->display_name;
44 #define copy(_field_) to->_field_ = getcpy(from->_field_)
45 copy(name);
46 copy(scope);
47 copy(client_id);
48 copy(client_secret);
49 copy(auth_endpoint);
50 copy(token_endpoint);
51 copy(redirect_uri);
52 #undef copy
53 }
54
55 /* Return profile component node name for a service parameter. */
56 char *
57 mh_oauth_node_name_for_svc(const char *base_name, const char *svc)
58 {
59 /* TODO: s/_/-/g ? */
60 return concat("oauth-", svc, "-", base_name, NULL);
61 }
62
63 /* Update one service_info field if overridden in profile. */
64 static void
65 update_svc_field(char **field, const char *base_name, const char *svc)
66 {
67 char *name = mh_oauth_node_name_for_svc(base_name, svc);
68 const char *value = context_find(name);
69 if (value != NULL) {
70 free(*field);
71 *field = mh_xstrdup(value);
72 }
73 free(name);
74 }
75
76 /* Update all service_info fields that are overridden in profile. */
77 static boolean
78 update_svc(mh_oauth_service_info *svc, const char *svc_name, char *errbuf,
79 size_t errbuflen)
80 {
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'; \
86 return FALSE; \
87 }
88 update(scope);
89 update(client_id);
90 update(client_secret);
91 update(auth_endpoint);
92 update(token_endpoint);
93 update(redirect_uri);
94 #undef update
95
96 if (svc->name == NULL) {
97 svc->name = getcpy(svc_name);
98 }
99
100 if (svc->display_name == NULL) {
101 svc->display_name = svc->name;
102 }
103
104 return TRUE;
105 }
106
107 boolean
108 mh_oauth_get_service_info(const char *svc_name, mh_oauth_service_info *svcinfo,
109 char *errbuf, size_t errbuflen)
110 {
111 int i;
112
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;
117
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]);
121 break;
122 }
123 }
124
125 if (!update_svc(svcinfo, svc_name, errbuf, errbuflen)) {
126 return FALSE;
127 }
128
129 return TRUE;
130 }
131
132 const char *
133 mh_oauth_cred_fn(const char *svc)
134 {
135 char *result, *result_if_allocated;
136
137 char *component = mh_oauth_node_name_for_svc("credential-file", svc);
138 result = context_find(component);
139 free(component);
140
141 if (result == NULL) {
142 result = concat("oauth-", svc, NULL);
143 result_if_allocated = result;
144 } else {
145 result_if_allocated = NULL;
146 }
147
148 if (result[0] != '/') {
149 const char *tmp = m_maildir(result);
150 free(result_if_allocated);
151 result = getcpy(tmp);
152 }
153
154 return result;
155 }
156 #endif