This repository has been archived by the owner on Oct 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
/
password.c
389 lines (372 loc) · 14.8 KB
/
password.c
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/**
*
* Glewlwyd SSO Server
*
* Authentiation server
* Users are authenticated via various backend available: database, ldap
* Using various authentication methods available: password, OTP, send code, etc.
*
* Password authentication scheme module
*
* Copyright 2019-2020 Nicolas Mora <mail@babelouest.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation;
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <string.h>
#include <jansson.h>
#include <yder.h>
#include <orcania.h>
#include "glewlwyd-common.h"
/**
*
* Note on the user auth scheme module
*
* It's possible for the scheme module to get or store any value in the user object returned by the functions
* struct config_module.glewlwyd_module_callback_get_user()
* struct config_module.glewlwyd_module_callback_set_user()
*
* Although, the module can't know if any value, other than "name", "password", "email" or "enabled" can be added or updated by the scheme module
* The scheme module can store its specific data for each user by itself or store the data in the user object, or both, this will depend on the implementation
*
* The format of the structure config_module is:
*
* struct config_module {
* const char * external_url; // Absolute url of the glewlwyd service
* const char * login_url; // Relative url of the login page
* const char * admin_scope; // Value of the g_admin scope
* const char * profile_scope; // Value of the g_profile scope
* struct _h_connection * conn; // Hoel structure to access to the database
* digest_algorithm hash_algorithm; // Hash algorithm used in Glewlwyd
* struct config_elements * glewlwyd_config; // Pointer to the global config structure
* // Function used to return a user object
* json_t * (* glewlwyd_module_callback_get_user)(struct config_module * config, const char * username);
* // Function used to update a user
* int (* glewlwyd_module_callback_set_user)(struct config_module * config, const char * username, json_t * j_user);
* // Function used to check the validity of a user's password
* int (* glewlwyd_module_callback_check_user_password)(struct config_module * config, const char * username, const char * password);
* };
*
*/
/**
*
* user_auth_scheme_module_load
*
* Executed once when Glewlwyd service is started
* Used to identify the module and to show its parameters on init
* You can also use it to load resources that are required once for all
* instance modules for example
*
* @return value: a json_t * value with the following pattern:
* {
* result: number (G_OK on success, another value on error)
* name: string, mandatory, name of the module, must be unique among other scheme modules
* display_name: string, optional, long name of the module
* description: string, optional, description for the module
* parameters: object, optional, parameters description for the module
* }
*
* Example:
* {
* result: G_OK,
* name: "mock",
* display_name: "Mock scheme module",
* description: "Mock scheme module for glewlwyd tests",
* parameters: {
* mock-value: {
* type: "string",
* mandatory: true
* }
* }
* }
*
* @parameter config: a struct config_module with acess to some Glewlwyd
* service and data
*
*/
json_t * user_auth_scheme_module_load(struct config_module * config) {
UNUSED(config);
return json_pack("{sissssss}",
"result", G_OK,
"name", "retype-password",
"display_name", "Short session password",
"description", "Glewlwyd authentication via user password with a short session duration");
}
/**
*
* user_auth_scheme_module_unload
*
* Executed once when Glewlwyd service is stopped
* You can also use it to release resources that are required once for all
* instance modules for example
*
* @return value: G_OK on success, another value on error
*
* @parameter config: a struct config_module with acess to some Glewlwyd
* service and data
*
*/
int user_auth_scheme_module_unload(struct config_module * config) {
UNUSED(config);
return G_OK;
}
/**
*
* user_auth_scheme_module_init
*
* Initialize an instance of this module declared in Glewlwyd service.
* If required, you must dynamically allocate a pointer to the configuration
* for this instance and pass it to *cls
*
* @return value: a json_t * value with the following pattern:
* {
* result: number (G_OK on success, G_ERROR_PARAM on input parameters error, another value on error)
* error: array of strings containg the list of input errors, mandatory on result G_ERROR_PARAM, ignored otherwise
* }
*
* @parameter config: a struct config_module with acess to some Glewlwyd
* service and data
* @parameter j_parameters: used to initialize an instance in JSON format
* The module must validate itself its parameters
* @parameter mod_name: module name in glewlwyd service
* @parameter cls: will contain an allocated void * pointer that will be sent back
* as void * in all module functions
*
*/
json_t * user_auth_scheme_module_init(struct config_module * config, json_t * j_parameters, const char * mod_name, void ** cls) {
UNUSED(config);
UNUSED(j_parameters);
UNUSED(mod_name);
UNUSED(cls);
return json_pack("{si}", "result", G_OK);
}
/**
*
* user_auth_scheme_module_close
*
* Close an instance of this module declared in Glewlwyd service.
* You must free the memory previously allocated in
* the user_auth_scheme_module_init function as void * cls
*
* @return value: G_OK on success, another value on error
*
* @parameter config: a struct config_module with acess to some Glewlwyd
* service and data
* @parameter cls: pointer to the void * cls value allocated in user_auth_scheme_module_init
*
*/
int user_auth_scheme_module_close(struct config_module * config, void * cls) {
UNUSED(config);
UNUSED(cls);
return G_OK;
}
/**
*
* user_auth_scheme_module_can_use
*
* Validate if the user is allowed to use this scheme prior to the
* authentication or registration
*
* @return value: GLEWLWYD_IS_REGISTERED - User can use scheme and has registered
* GLEWLWYD_IS_AVAILABLE - User can use scheme but hasn't registered
* GLEWLWYD_IS_NOT_AVAILABLE - User can't use scheme
*
* @parameter config: a struct config_module with acess to some Glewlwyd
* service and data
* @parameter username: username to identify the user
* @parameter cls: pointer to the void * cls value allocated in user_auth_scheme_module_init
*
*/
int user_auth_scheme_module_can_use(struct config_module * config, const char * username, void * cls) {
UNUSED(config);
UNUSED(username);
UNUSED(username);
UNUSED(cls);
return GLEWLWYD_IS_REGISTERED;
}
/**
*
* user_auth_scheme_module_register
*
* Register the scheme for a user
* Ex: add a certificate, add new TOTP values, etc.
*
* @return value: a json_t * value with the following pattern:
* {
* result: number (G_OK on success, another value on error)
* updated: boolean (true if the scheme has been registered or updated, optional)
* response: JSON object, optional
* }
*
* @parameter config: a struct config_module with acess to some Glewlwyd
* service and data
* @parameter http_request: the original struct _u_request from the HTTP API
* @parameter username: username to identify the user
* @parameter j_scheme_data: additional data used to register the scheme for the user
* in JSON format
* @parameter cls: pointer to the void * cls value allocated in user_auth_scheme_module_init
*
*/
json_t * user_auth_scheme_module_register(struct config_module * config, const struct _u_request * http_request, const char * username, json_t * j_scheme_data, void * cls) {
UNUSED(config);
UNUSED(http_request);
UNUSED(username);
UNUSED(j_scheme_data);
UNUSED(cls);
return json_pack("{si}", "result", G_OK);
}
/**
*
* user_auth_scheme_module_register_get
*
* Get the registration value(s) of the scheme for a user
*
* @return value: a json_t * value with the following pattern:
* {
* result: number (G_OK on success, another value on error)
* response: JSON object, optional
* }
*
* @parameter config: a struct config_module with acess to some Glewlwyd
* service and data
* @parameter http_request: the original struct _u_request from the API, must be casted to be available
* @parameter username: username to identify the user
* @parameter cls: pointer to the void * cls value allocated in user_auth_scheme_module_init
*
*/
json_t * user_auth_scheme_module_register_get(struct config_module * config, const struct _u_request * http_request, const char * username, void * cls) {
UNUSED(config);
UNUSED(http_request);
UNUSED(username);
UNUSED(cls);
return json_pack("{si}", "result", G_OK);
}
/**
*
* user_auth_scheme_module_deregister
*
* Deregister the scheme for a user
* Ex: remove certificates, TOTP values, etc.
*
* @return value: G_OK on success, even if no data has been removed
* G_ERROR on another error
*
* @parameter config: a struct config_module with acess to some Glewlwyd
* service and data
* @parameter username: username to identify the user
* @parameter cls: pointer to the void * cls value allocated in user_auth_scheme_module_init
*
*/
int user_auth_scheme_module_deregister(struct config_module * config, const char * username, void * cls) {
UNUSED(config);
UNUSED(username);
UNUSED(cls);
return G_OK;
}
/**
*
* user_auth_scheme_module_trigger
*
* Trigger the scheme for a user
* Ex: send the code to a device, generate a challenge, etc.
*
* @return value: a json_t * value with the following pattern:
* {
* result: number (G_OK on success, another value on error)
* response: JSON object, optional
* }
*
* @parameter config: a struct config_module with acess to some Glewlwyd
* service and data
* @parameter http_request: the original struct _u_request from the API, must be casted to be available
* @parameter username: username to identify the user
* @parameter scheme_trigger: data sent to trigger the scheme for the user
* in JSON format
* @parameter cls: pointer to the void * cls value allocated in user_auth_scheme_module_init
*
*/
json_t * user_auth_scheme_module_trigger(struct config_module * config, const struct _u_request * http_request, const char * username, json_t * j_scheme_trigger, void * cls) {
UNUSED(config);
UNUSED(http_request);
UNUSED(username);
UNUSED(j_scheme_trigger);
UNUSED(cls);
return json_pack("{si}", "result", G_OK);
}
/**
*
* user_auth_scheme_module_validate
*
* Validate the scheme for a user
* Ex: check the code sent to a device, verify the challenge, etc.
*
* @return value: G_OK on success
* G_ERROR_UNAUTHORIZED if validation fails
* G_ERROR_PARAM if error in parameters
* G_ERROR on another error
*
* @parameter config: a struct config_module with acess to some Glewlwyd
* service and data
* @parameter http_request: the original struct _u_request from the API, must be casted to be available
* @parameter username: username to identify the user
* @parameter j_scheme_data: data sent to validate the scheme for the user
* in JSON format
* @parameter cls: pointer to the void * cls value allocated in user_auth_scheme_module_init
*
*/
int user_auth_scheme_module_validate(struct config_module * config, const struct _u_request * http_request, const char * username, json_t * j_scheme_data, void * cls) {
UNUSED(config);
UNUSED(http_request);
UNUSED(cls);
int ret;
if (!json_string_null_or_empty(json_object_get(j_scheme_data, "password"))) {
ret = config->glewlwyd_module_callback_check_user_password(config, username, json_string_value(json_object_get(j_scheme_data, "password")));
if (ret != G_OK && ret != G_ERROR_UNAUTHORIZED) {
y_log_message(Y_LOG_LEVEL_ERROR, "user_auth_scheme_module_validate password - Error glewlwyd_module_callback_check_user_password");
}
} else {
ret = G_ERROR_UNAUTHORIZED;
}
return ret;
}
/**
*
* user_auth_scheme_module_identify
*
* Identify the user using the scheme without the username to be previously given
* This functionality isn't available for all schemes, because the scheme authentification
* must be triggered without username and the authentication result must contain the username
*
* @return value: a json_t * value with the following pattern:
* {
* result: number (G_OK on success, another value on error)
* username: string value of the user identified - if the function is called within /auth
* response: JSON object, optional - if the function is called within /auth/scheme/trigger
* }
*
* @parameter config: a struct config_module with acess to some Glewlwyd
* service and data
* @parameter http_request: the original struct _u_request from the API, must be casted to be available
* @parameter j_scheme_data: data sent to validate the scheme for the user
* in JSON format
* @parameter cls: pointer to the void * cls value allocated in user_auth_scheme_module_init
*
*/
json_t * user_auth_scheme_module_identify(struct config_module * config, const struct _u_request * http_request, json_t * j_scheme_data, void * cls) {
UNUSED(config);
UNUSED(http_request);
UNUSED(j_scheme_data);
UNUSED(cls);
return json_pack("{si}", "result", G_ERROR_UNAUTHORIZED);
}