1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
/*
* ========================================================================
* Copyright 2013-2020 Eduardo Chappa
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* ========================================================================
*/
/* OAUTH2 support code goes here. This is necessary because
* 1. it helps to coordinate two different methods, such as XOAUTH2 and
* OAUTHBEARER, which use the same code, so it can all go in one place
*
* 2. It helps with coordinating with the client when the server requires
* the deviceinfo method.
*/
#include "http.h"
#include "json.h"
#include "oauth2_aux.h"
/* we generate something like a guid, but not care about
* anything, but that it is really random.
*/
char *oauth2_generate_state(void)
{
char rv[37];
int i;
rv[0] = '\0';
for(i = 0; i < 4; i++)
sprintf(rv + strlen(rv), "%x", (unsigned int) (random() % 256));
sprintf(rv + strlen(rv), "%c", '-');
for(i = 0; i < 2; i++)
sprintf(rv + strlen(rv), "%x", (unsigned int) (random() % 256));
sprintf(rv + strlen(rv), "%c", '-');
for(i = 0; i < 2; i++)
sprintf(rv + strlen(rv), "%x", (unsigned int) (random() % 256));
sprintf(rv + strlen(rv), "%c", '-');
for(i = 0; i < 2; i++)
sprintf(rv + strlen(rv), "%x", (unsigned int) (random() % 256));
sprintf(rv + strlen(rv), "%c", '-');
for(i = 0; i < 6; i++)
sprintf(rv + strlen(rv), "%x", (unsigned int) (random() % 256));
rv[36] = '\0';
return cpystr(rv);
}
JSON_S *oauth2_json_reply(OAUTH2_SERVER_METHOD_S, OAUTH2_S *, int *);
#define LOAD_HTTP_PARAMS(X, Y) { \
int i; \
for(i = 0; (X).params[i] != OA2_End; i++){ \
OA2_type j = (X).params[i]; \
(Y)[i].name = oauth2->param[j].name; \
(Y)[i].value = oauth2->param[j].value; \
} \
(Y)[i].name = (Y)[i].value = NULL; \
}
JSON_S *oauth2_json_reply(OAUTH2_SERVER_METHOD_S RefreshMethod, OAUTH2_S *oauth2, int *status)
{
JSON_S *json = NULL;
HTTP_PARAM_S params[OAUTH2_PARAM_NUMBER];
unsigned char *s;
LOAD_HTTP_PARAMS(RefreshMethod, params);
*status = 0;
if(strcmp(RefreshMethod.name, "POST") == 0
&& ((s = http_post_param(RefreshMethod.urlserver, params, status)) != NULL)){
unsigned char *u = s;
json = json_parse(&u);
fs_give((void **) &s);
}
return json;
}
void
mm_login_oauth2_c_client_method (NETMBX *mb, char *user, char *method,
OAUTH2_S *oauth2, unsigned long trial, int *tryanother)
{
int i, status;
char *s = NULL;
JSON_S *json = NULL;
if(oauth2->param[OA2_Id].value == NULL
|| (oauth2->require_secret && oauth2->param[OA2_Secret].value == NULL)){
oauth2clientinfo_t ogci =
(oauth2clientinfo_t) mail_parameters (NIL, GET_OA2CLIENTINFO, NIL);
if(ogci) (*ogci)(oauth2->name, &oauth2->param[OA2_Id].value,
&oauth2->param[OA2_Secret].value);
}
if(oauth2->param[OA2_Id].value == NULL
|| (oauth2->require_secret && oauth2->param[OA2_Secret].value == NULL))
return;
/* Do we have a method to execute? */
if (oauth2->first_time && oauth2->server_mthd[OA2_GetDeviceCode].name){
oauth2deviceinfo_t ogdi;
json = oauth2_json_reply(oauth2->server_mthd[OA2_GetDeviceCode], oauth2, &status);
if(json != NULL){
JSON_X *jx;
jx = json_body_value(json, "device_code");
if(jx && jx->jtype == JString)
oauth2->devicecode.device_code = cpystr((char *) jx->value);
jx = json_body_value(json, "user_code");
if(jx && jx->jtype == JString)
oauth2->devicecode.user_code = cpystr((char *) jx->value);
jx = json_body_value(json, "verification_uri");
if(jx && jx->jtype == JString)
oauth2->devicecode.verification_uri = cpystr((char *) jx->value);
if((jx = json_body_value(json, "expires_in")) != NULL)
switch(jx->jtype){
case JString: oauth2->devicecode.expires_in = atoi((char *) jx->value);
break;
case JLong : oauth2->devicecode.expires_in = *(long *) jx->value;
break;
}
if((jx = json_body_value(json, "interval")) != NULL)
switch(jx->jtype){
case JString: oauth2->devicecode.interval = atoi((char *) jx->value);
break;
case JLong : oauth2->devicecode.interval = *(long *) jx->value;
break;
}
jx = json_body_value(json, "message");
if(jx && jx->jtype == JString)
oauth2->devicecode.message = cpystr((char *) jx->value);
json_free(&json);
if(oauth2->devicecode.verification_uri && oauth2->devicecode.user_code){
ogdi = (oauth2deviceinfo_t) mail_parameters (NIL, GET_OA2DEVICEINFO, NIL);
if(ogdi) (*ogdi)(oauth2, method);
}
}
return;
}
/* else check if we have a refresh token, and in that case use it */
if(oauth2->param[OA2_RefreshToken].value){
json = oauth2_json_reply(oauth2->server_mthd[OA2_GetAccessTokenFromRefreshToken], oauth2, &status);
if(json != NULL){
JSON_X *jx;
jx = json_body_value(json, "access_token");
if(jx && jx->jtype == JString)
oauth2->access_token = cpystr((char *) jx->value);
if((jx = json_body_value(json, "expires_in")) != NULL)
switch(jx->jtype){
case JString: oauth2->expiration = time(0) + atol((char *) jx->value);
break;
case JLong : oauth2->expiration = time(0) + *(long *) jx->value;
break;
}
json_free(&json);
}
return;
}
/*
* else, we do not have a refresh token, nor an access token.
* We need to start the process to get an access code. We use this
* to get an access token and refresh token.
*/
{ OAUTH2_SERVER_METHOD_S RefreshMethod = oauth2->server_mthd[OA2_GetAccessCode];
HTTP_PARAM_S params[OAUTH2_PARAM_NUMBER];
LOAD_HTTP_PARAMS(RefreshMethod, params);
if(strcmp(RefreshMethod.name, "GET") == 0){
char *url = http_get_param_url(RefreshMethod.urlserver, params);
oauth2getaccesscode_t ogac =
(oauth2getaccesscode_t) mail_parameters (NIL, GET_OA2CLIENTGETACCESSCODE, NIL);
if(ogac)
oauth2->param[OA2_Code].value = (*ogac)(url, method, oauth2, tryanother);
}
if(oauth2->param[OA2_Code].value){
json = oauth2_json_reply(oauth2->server_mthd[OA2_GetAccessTokenFromAccessCode], oauth2, &status);
if(json != NULL){
JSON_X *jx;
switch(status){
case HTTP_OK : jx = json_body_value(json, "refresh_token");
if(jx && jx->jtype == JString)
oauth2->param[OA2_RefreshToken].value = cpystr((char *) jx->value);
jx = json_body_value(json, "access_token");
if(jx && jx->jtype == JString)
oauth2->access_token = cpystr((char *) jx->value);
if((jx = json_body_value(json, "expires_in")) != NULL)
switch(jx->jtype){
case JString: oauth2->expiration = time(0) + atol((char *) jx->value);
break;
case JLong : oauth2->expiration = time(0) + *(long *) jx->value;
break;
}
jx = json_body_value(json, "expires_in");
if(jx && jx->jtype == JString)
oauth2->expiration = time(0) + atol((char *) jx->value);
break;
case HTTP_BAD : break;
default : { char tmp[100];
sprintf(tmp, "Oauth Client Received Code %d", status);
fatal (tmp);
}
}
json_free(&json);
}
}
return;
}
/* Else, does this server use the /devicecode method? */
}
void oauth2deviceinfo_get_accesscode(void *inp, void *outp)
{
OAUTH2_DEVICEPROC_S *oad = (OAUTH2_DEVICEPROC_S *) inp;
OAUTH2_S *oauth2 = oad->xoauth2;
OAUTH2_DEVICECODE_S *dcode = &oauth2->devicecode;
int done = 0, status, rv;
HTTP_PARAM_S params[OAUTH2_PARAM_NUMBER];
JSON_S *json;
if(dcode->device_code && oauth2->param[OA2_DeviceCode].value == NULL)
oauth2->param[OA2_DeviceCode].value = cpystr(dcode->device_code);
rv = OA2_CODE_WAIT; /* wait by default */
json = oauth2_json_reply(oauth2->server_mthd[OA2_GetAccessTokenFromAccessCode], oauth2, &status);
if(json != NULL){
JSON_X *jx;
char *error;
switch(status){
case HTTP_BAD : jx = json_body_value(json, "error");
if(jx && jx->jtype == JString)
error = cpystr((char *) jx->value);
else
break;
if(compare_cstring(error, "authorization_pending") == 0)
rv = OA2_CODE_WAIT;
else if(compare_cstring(error, "authorization_declined") == 0)
rv = OA2_CODE_FAIL;
else if(compare_cstring(error, "bad_verification_code") == 0)
rv = OA2_CODE_FAIL;
else if(compare_cstring(error, "expired_token") == 0)
rv = OA2_CODE_FAIL;
else /* keep waiting? */
rv = OA2_CODE_WAIT;
break;
case HTTP_OK : jx = json_body_value(json, "refresh_token");
if(jx && jx->jtype == JString)
oauth2->param[OA2_RefreshToken].value = cpystr((char *) jx->value);
jx = json_body_value(json, "access_token");
if(jx && jx->jtype == JString)
oauth2->access_token = cpystr((char *) jx->value);
if((jx = json_body_value(json, "expires_in")) != NULL)
switch(jx->jtype){
case JString: oauth2->expiration = time(0) + atol((char *) jx->value);
break;
case JLong : oauth2->expiration = time(0) + *(long *) jx->value;
break;
}
rv = OA2_CODE_SUCCESS;
break;
default : { char tmp[100];
sprintf(tmp, "Oauth device Received Code %d", status);
fatal (tmp);
}
}
json_free(&json);
}
*(int *)outp = rv;
}
|