diff options
Diffstat (limited to 'src/reclaim/plugin_reclaim_credential_jwt.c')
-rw-r--r-- | src/reclaim/plugin_reclaim_credential_jwt.c | 159 |
1 files changed, 136 insertions, 23 deletions
diff --git a/src/reclaim/plugin_reclaim_credential_jwt.c b/src/reclaim/plugin_reclaim_credential_jwt.c index 38effcf78..148865223 100644 --- a/src/reclaim/plugin_reclaim_credential_jwt.c +++ b/src/reclaim/plugin_reclaim_credential_jwt.c | |||
@@ -48,7 +48,7 @@ jwt_value_to_string (void *cls, | |||
48 | { | 48 | { |
49 | switch (type) | 49 | switch (type) |
50 | { | 50 | { |
51 | case GNUNET_RECLAIM_credential_TYPE_JWT: | 51 | case GNUNET_RECLAIM_CREDENTIAL_TYPE_JWT: |
52 | return GNUNET_strndup (data, data_size); | 52 | return GNUNET_strndup (data, data_size); |
53 | 53 | ||
54 | default: | 54 | default: |
@@ -79,7 +79,7 @@ jwt_string_to_value (void *cls, | |||
79 | return GNUNET_SYSERR; | 79 | return GNUNET_SYSERR; |
80 | switch (type) | 80 | switch (type) |
81 | { | 81 | { |
82 | case GNUNET_RECLAIM_credential_TYPE_JWT: | 82 | case GNUNET_RECLAIM_CREDENTIAL_TYPE_JWT: |
83 | *data = GNUNET_strdup (s); | 83 | *data = GNUNET_strdup (s); |
84 | *data_size = strlen (s); | 84 | *data_size = strlen (s); |
85 | return GNUNET_OK; | 85 | return GNUNET_OK; |
@@ -98,8 +98,8 @@ static struct | |||
98 | { | 98 | { |
99 | const char *name; | 99 | const char *name; |
100 | uint32_t number; | 100 | uint32_t number; |
101 | } jwt_cred_name_map[] = { { "JWT", GNUNET_RECLAIM_credential_TYPE_JWT }, | 101 | } jwt_cred_name_map[] = { { "JWT", GNUNET_RECLAIM_CREDENTIAL_TYPE_JWT }, |
102 | { NULL, UINT32_MAX } }; | 102 | { NULL, UINT32_MAX } }; |
103 | 103 | ||
104 | /** | 104 | /** |
105 | * Convert a type name to the corresponding number. | 105 | * Convert a type name to the corresponding number. |
@@ -135,8 +135,8 @@ jwt_number_to_typename (void *cls, uint32_t type) | |||
135 | 135 | ||
136 | i = 0; | 136 | i = 0; |
137 | while ((NULL != jwt_cred_name_map[i].name) && (type != | 137 | while ((NULL != jwt_cred_name_map[i].name) && (type != |
138 | jwt_cred_name_map[i]. | 138 | jwt_cred_name_map[i]. |
139 | number)) | 139 | number)) |
140 | i++; | 140 | i++; |
141 | return jwt_cred_name_map[i].name; | 141 | return jwt_cred_name_map[i].name; |
142 | } | 142 | } |
@@ -151,7 +151,7 @@ jwt_number_to_typename (void *cls, uint32_t type) | |||
151 | */ | 151 | */ |
152 | struct GNUNET_RECLAIM_AttributeList * | 152 | struct GNUNET_RECLAIM_AttributeList * |
153 | jwt_parse_attributes (void *cls, | 153 | jwt_parse_attributes (void *cls, |
154 | const struct GNUNET_RECLAIM_Credential *cred) | 154 | const char *data) |
155 | { | 155 | { |
156 | char *jwt_string; | 156 | char *jwt_string; |
157 | struct GNUNET_RECLAIM_AttributeList *attrs; | 157 | struct GNUNET_RECLAIM_AttributeList *attrs; |
@@ -162,17 +162,14 @@ jwt_parse_attributes (void *cls, | |||
162 | json_t *json_val; | 162 | json_t *json_val; |
163 | json_error_t *json_err = NULL; | 163 | json_error_t *json_err = NULL; |
164 | 164 | ||
165 | /* GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "%s\n", cred->data); (not OK: 'data' is not defined as 0-terminated text, but binary) */ | ||
166 | if (GNUNET_RECLAIM_credential_TYPE_JWT != cred->type) | ||
167 | return NULL; | ||
168 | attrs = GNUNET_new (struct GNUNET_RECLAIM_AttributeList); | 165 | attrs = GNUNET_new (struct GNUNET_RECLAIM_AttributeList); |
169 | 166 | ||
170 | jwt_string = GNUNET_strdup (cred->data); | 167 | jwt_string = GNUNET_strdup (data); |
171 | const char *jwt_body = strtok (jwt_string, delim); | 168 | const char *jwt_body = strtok (jwt_string, delim); |
172 | jwt_body = strtok (NULL, delim); | 169 | jwt_body = strtok (NULL, delim); |
173 | GNUNET_STRINGS_base64url_decode (jwt_body, strlen (jwt_body), | 170 | GNUNET_STRINGS_base64url_decode (jwt_body, strlen (jwt_body), |
174 | (void **) &decoded_jwt); | 171 | (void **) &decoded_jwt); |
175 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s\n", decoded_jwt); | 172 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Decoded JWT: %s\n", decoded_jwt); |
176 | GNUNET_assert (NULL != decoded_jwt); | 173 | GNUNET_assert (NULL != decoded_jwt); |
177 | json_val = json_loads (decoded_jwt, JSON_DECODE_ANY, json_err); | 174 | json_val = json_loads (decoded_jwt, JSON_DECODE_ANY, json_err); |
178 | const char *key; | 175 | const char *key; |
@@ -203,6 +200,36 @@ jwt_parse_attributes (void *cls, | |||
203 | 200 | ||
204 | 201 | ||
205 | /** | 202 | /** |
203 | * Parse a JWT and return the respective claim value as Attribute | ||
204 | * | ||
205 | * @param cls the plugin | ||
206 | * @param cred the jwt credential | ||
207 | * @return a GNUNET_RECLAIM_Attribute, containing the new value | ||
208 | */ | ||
209 | struct GNUNET_RECLAIM_AttributeList * | ||
210 | jwt_parse_attributes_c (void *cls, | ||
211 | const struct GNUNET_RECLAIM_Credential *cred) | ||
212 | { | ||
213 | return jwt_parse_attributes (cls, cred->data); | ||
214 | } | ||
215 | |||
216 | |||
217 | /** | ||
218 | * Parse a JWT and return the respective claim value as Attribute | ||
219 | * | ||
220 | * @param cls the plugin | ||
221 | * @param cred the jwt credential | ||
222 | * @return a GNUNET_RECLAIM_Attribute, containing the new value | ||
223 | */ | ||
224 | struct GNUNET_RECLAIM_AttributeList * | ||
225 | jwt_parse_attributes_p (void *cls, | ||
226 | const struct GNUNET_RECLAIM_Presentation *cred) | ||
227 | { | ||
228 | return jwt_parse_attributes (cls, cred->data); | ||
229 | } | ||
230 | |||
231 | |||
232 | /** | ||
206 | * Parse a JWT and return the issuer | 233 | * Parse a JWT and return the issuer |
207 | * | 234 | * |
208 | * @param cls the plugin | 235 | * @param cls the plugin |
@@ -211,7 +238,7 @@ jwt_parse_attributes (void *cls, | |||
211 | */ | 238 | */ |
212 | char * | 239 | char * |
213 | jwt_get_issuer (void *cls, | 240 | jwt_get_issuer (void *cls, |
214 | const struct GNUNET_RECLAIM_Credential *cred) | 241 | const char *data) |
215 | { | 242 | { |
216 | const char *jwt_body; | 243 | const char *jwt_body; |
217 | char *jwt_string; | 244 | char *jwt_string; |
@@ -223,9 +250,7 @@ jwt_get_issuer (void *cls, | |||
223 | json_t *json_val; | 250 | json_t *json_val; |
224 | json_error_t *json_err = NULL; | 251 | json_error_t *json_err = NULL; |
225 | 252 | ||
226 | if (GNUNET_RECLAIM_credential_TYPE_JWT != cred->type) | 253 | jwt_string = GNUNET_strdup (data); |
227 | return NULL; | ||
228 | jwt_string = GNUNET_strdup (cred->data); | ||
229 | jwt_body = strtok (jwt_string, delim); | 254 | jwt_body = strtok (jwt_string, delim); |
230 | jwt_body = strtok (NULL, delim); | 255 | jwt_body = strtok (NULL, delim); |
231 | GNUNET_STRINGS_base64url_decode (jwt_body, strlen (jwt_body), | 256 | GNUNET_STRINGS_base64url_decode (jwt_body, strlen (jwt_body), |
@@ -241,6 +266,40 @@ jwt_get_issuer (void *cls, | |||
241 | 266 | ||
242 | 267 | ||
243 | /** | 268 | /** |
269 | * Parse a JWT and return the issuer | ||
270 | * | ||
271 | * @param cls the plugin | ||
272 | * @param cred the jwt credential | ||
273 | * @return a string, containing the isser | ||
274 | */ | ||
275 | char * | ||
276 | jwt_get_issuer_c (void *cls, | ||
277 | const struct GNUNET_RECLAIM_Credential *cred) | ||
278 | { | ||
279 | if (GNUNET_RECLAIM_CREDENTIAL_TYPE_JWT != cred->type) | ||
280 | return NULL; | ||
281 | return jwt_get_issuer (cls, cred->data); | ||
282 | } | ||
283 | |||
284 | |||
285 | /** | ||
286 | * Parse a JWT and return the issuer | ||
287 | * | ||
288 | * @param cls the plugin | ||
289 | * @param cred the jwt credential | ||
290 | * @return a string, containing the isser | ||
291 | */ | ||
292 | char * | ||
293 | jwt_get_issuer_p (void *cls, | ||
294 | const struct GNUNET_RECLAIM_Presentation *cred) | ||
295 | { | ||
296 | if (GNUNET_RECLAIM_CREDENTIAL_TYPE_JWT != cred->type) | ||
297 | return NULL; | ||
298 | return jwt_get_issuer (cls, cred->data); | ||
299 | } | ||
300 | |||
301 | |||
302 | /** | ||
244 | * Parse a JWT and return the expiration | 303 | * Parse a JWT and return the expiration |
245 | * | 304 | * |
246 | * @param cls the plugin | 305 | * @param cls the plugin |
@@ -249,7 +308,7 @@ jwt_get_issuer (void *cls, | |||
249 | */ | 308 | */ |
250 | int | 309 | int |
251 | jwt_get_expiration (void *cls, | 310 | jwt_get_expiration (void *cls, |
252 | const struct GNUNET_RECLAIM_Credential *cred, | 311 | const char *data, |
253 | struct GNUNET_TIME_Absolute *exp) | 312 | struct GNUNET_TIME_Absolute *exp) |
254 | { | 313 | { |
255 | const char *jwt_body; | 314 | const char *jwt_body; |
@@ -261,9 +320,7 @@ jwt_get_expiration (void *cls, | |||
261 | json_t *json_val; | 320 | json_t *json_val; |
262 | json_error_t *json_err = NULL; | 321 | json_error_t *json_err = NULL; |
263 | 322 | ||
264 | if (GNUNET_RECLAIM_credential_TYPE_JWT != cred->type) | 323 | jwt_string = GNUNET_strdup (data); |
265 | return GNUNET_NO; | ||
266 | jwt_string = GNUNET_strdup (cred->data); | ||
267 | jwt_body = strtok (jwt_string, delim); | 324 | jwt_body = strtok (jwt_string, delim); |
268 | jwt_body = strtok (NULL, delim); | 325 | jwt_body = strtok (NULL, delim); |
269 | GNUNET_STRINGS_base64url_decode (jwt_body, strlen (jwt_body), | 326 | GNUNET_STRINGS_base64url_decode (jwt_body, strlen (jwt_body), |
@@ -279,6 +336,54 @@ jwt_get_expiration (void *cls, | |||
279 | 336 | ||
280 | 337 | ||
281 | /** | 338 | /** |
339 | * Parse a JWT and return the expiration | ||
340 | * | ||
341 | * @param cls the plugin | ||
342 | * @param cred the jwt credential | ||
343 | * @return a string, containing the isser | ||
344 | */ | ||
345 | int | ||
346 | jwt_get_expiration_c (void *cls, | ||
347 | const struct GNUNET_RECLAIM_Credential *cred, | ||
348 | struct GNUNET_TIME_Absolute *exp) | ||
349 | { | ||
350 | return jwt_get_expiration (cls, cred->data, exp); | ||
351 | } | ||
352 | |||
353 | |||
354 | /** | ||
355 | * Parse a JWT and return the expiration | ||
356 | * | ||
357 | * @param cls the plugin | ||
358 | * @param cred the jwt credential | ||
359 | * @return a string, containing the isser | ||
360 | */ | ||
361 | int | ||
362 | jwt_get_expiration_p (void *cls, | ||
363 | const struct GNUNET_RECLAIM_Presentation *cred, | ||
364 | struct GNUNET_TIME_Absolute *exp) | ||
365 | { | ||
366 | return jwt_get_expiration (cls, cred->data, exp); | ||
367 | } | ||
368 | |||
369 | |||
370 | int | ||
371 | jwt_create_presentation (void *cls, | ||
372 | const struct GNUNET_RECLAIM_Credential *cred, | ||
373 | const struct GNUNET_RECLAIM_AttributeList *attrs, | ||
374 | struct GNUNET_RECLAIM_Presentation **pres) | ||
375 | { | ||
376 | // FIXME sanity checks?? | ||
377 | if (GNUNET_RECLAIM_CREDENTIAL_TYPE_JWT != cred->type) | ||
378 | return GNUNET_NO; | ||
379 | *pres = GNUNET_RECLAIM_presentation_new (GNUNET_RECLAIM_CREDENTIAL_TYPE_JWT, | ||
380 | cred->data, | ||
381 | cred->data_size); | ||
382 | return GNUNET_OK; | ||
383 | } | ||
384 | |||
385 | |||
386 | /** | ||
282 | * Entry point for the plugin. | 387 | * Entry point for the plugin. |
283 | * | 388 | * |
284 | * @param cls NULL | 389 | * @param cls NULL |
@@ -294,9 +399,17 @@ libgnunet_plugin_reclaim_credential_jwt_init (void *cls) | |||
294 | api->string_to_value = &jwt_string_to_value; | 399 | api->string_to_value = &jwt_string_to_value; |
295 | api->typename_to_number = &jwt_typename_to_number; | 400 | api->typename_to_number = &jwt_typename_to_number; |
296 | api->number_to_typename = &jwt_number_to_typename; | 401 | api->number_to_typename = &jwt_number_to_typename; |
297 | api->get_attributes = &jwt_parse_attributes; | 402 | api->get_attributes = &jwt_parse_attributes_c; |
298 | api->get_issuer = &jwt_get_issuer; | 403 | api->get_issuer = &jwt_get_issuer_c; |
299 | api->get_expiration = &jwt_get_expiration; | 404 | api->get_expiration = &jwt_get_expiration_c; |
405 | api->value_to_string_p = &jwt_value_to_string; | ||
406 | api->string_to_value_p = &jwt_string_to_value; | ||
407 | api->typename_to_number_p = &jwt_typename_to_number; | ||
408 | api->number_to_typename_p = &jwt_number_to_typename; | ||
409 | api->get_attributes_p = &jwt_parse_attributes_p; | ||
410 | api->get_issuer_p = &jwt_get_issuer_p; | ||
411 | api->get_expiration_p = &jwt_get_expiration_p; | ||
412 | api->create_presentation = &jwt_create_presentation; | ||
300 | return api; | 413 | return api; |
301 | } | 414 | } |
302 | 415 | ||