REST/NAMESTORE: rework API
[oweals/gnunet.git] / src / reclaim / oidc_helper.c
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2010-2015 GNUnet e.V.
4
5       GNUnet is free software: you can redistribute it and/or modify it
6       under the terms of the GNU Affero General Public License as published
7       by the Free Software Foundation, either version 3 of the License,
8       or (at your option) any later version.
9
10       GNUnet is distributed in the hope that it will be useful, but
11       WITHOUT ANY WARRANTY; without even the implied warranty of
12       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13       Affero General Public License for more details.
14
15       You should have received a copy of the GNU Affero General Public License
16       along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @file reclaim/oidc_helper.c
23  * @brief helper library for OIDC related functions
24  * @author Martin Schanzenbach
25  */
26 #include "platform.h"
27 #include <inttypes.h>
28 #include <jansson.h>
29
30 #include "gnunet_util_lib.h"
31
32 #include "gnunet_reclaim_attribute_lib.h"
33 #include "gnunet_reclaim_service.h"
34 #include "gnunet_signatures.h"
35 #include "oidc_helper.h"
36 static char *
37 create_jwt_header (void)
38 {
39   json_t *root;
40   char *json_str;
41
42   root = json_object ();
43   json_object_set_new (root, JWT_ALG, json_string (JWT_ALG_VALUE));
44   json_object_set_new (root, JWT_TYP, json_string (JWT_TYP_VALUE));
45
46   json_str = json_dumps (root, JSON_INDENT (0) | JSON_COMPACT);
47   json_decref (root);
48   return json_str;
49 }
50
51 static void
52 replace_char (char *str, char find, char replace)
53 {
54   char *current_pos = strchr (str, find);
55   while (current_pos)
56   {
57     *current_pos = replace;
58     current_pos = strchr (current_pos, find);
59   }
60 }
61
62 // RFC4648
63 static void
64 fix_base64 (char *str)
65 {
66   // Replace + with -
67   replace_char (str, '+', '-');
68
69   // Replace / with _
70   replace_char (str, '/', '_');
71 }
72
73 /**
74  * Create a JWT from attributes
75  *
76  * @param aud_key the public of the audience
77  * @param sub_key the public key of the subject
78  * @param attrs the attribute list
79  * @param expiration_time the validity of the token
80  * @param secret_key the key used to sign the JWT
81  * @return a new base64-encoded JWT string.
82  */
83 char *
84 OIDC_id_token_new (const struct GNUNET_CRYPTO_EcdsaPublicKey *aud_key,
85                    const struct GNUNET_CRYPTO_EcdsaPublicKey *sub_key,
86                    const struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs,
87                    const struct GNUNET_TIME_Relative *expiration_time,
88                    const char *nonce,
89                    const char *secret_key)
90 {
91   struct GNUNET_RECLAIM_ATTRIBUTE_ClaimListEntry *le;
92   struct GNUNET_HashCode signature;
93   struct GNUNET_TIME_Absolute exp_time;
94   struct GNUNET_TIME_Absolute time_now;
95   char *audience;
96   char *subject;
97   char *header;
98   char *body_str;
99   char *result;
100   char *header_base64;
101   char *body_base64;
102   char *signature_target;
103   char *signature_base64;
104   char *attr_val_str;
105   json_t *body;
106
107   // iat REQUIRED time now
108   time_now = GNUNET_TIME_absolute_get ();
109   // exp REQUIRED time expired from config
110   exp_time = GNUNET_TIME_absolute_add (time_now, *expiration_time);
111   // auth_time only if max_age
112   // nonce only if nonce
113   // OPTIONAL acr,amr,azp
114   subject =
115     GNUNET_STRINGS_data_to_string_alloc (sub_key,
116                                          sizeof (struct
117                                                  GNUNET_CRYPTO_EcdsaPublicKey));
118   audience =
119     GNUNET_STRINGS_data_to_string_alloc (aud_key,
120                                          sizeof (struct
121                                                  GNUNET_CRYPTO_EcdsaPublicKey));
122   header = create_jwt_header ();
123   body = json_object ();
124
125   // iss REQUIRED case sensitive server uri with https
126   // The issuer is the local reclaim instance (e.g.
127   // https://reclaim.id/api/openid)
128   json_object_set_new (body, "iss", json_string (SERVER_ADDRESS));
129   // sub REQUIRED public key identity, not exceed 255 ASCII  length
130   json_object_set_new (body, "sub", json_string (subject));
131   // aud REQUIRED public key client_id must be there
132   json_object_set_new (body, "aud", json_string (audience));
133   // iat
134   json_object_set_new (body,
135                        "iat",
136                        json_integer (time_now.abs_value_us / (1000 * 1000)));
137   // exp
138   json_object_set_new (body,
139                        "exp",
140                        json_integer (exp_time.abs_value_us / (1000 * 1000)));
141   // nbf
142   json_object_set_new (body,
143                        "nbf",
144                        json_integer (time_now.abs_value_us / (1000 * 1000)));
145   // nonce
146   if (NULL != nonce)
147     json_object_set_new (body, "nonce", json_string (nonce));
148
149   for (le = attrs->list_head; NULL != le; le = le->next)
150   {
151     attr_val_str =
152       GNUNET_RECLAIM_ATTRIBUTE_value_to_string (le->claim->type,
153                                                 le->claim->data,
154                                                 le->claim->data_size);
155     json_object_set_new (body, le->claim->name, json_string (attr_val_str));
156     GNUNET_free (attr_val_str);
157   }
158   body_str = json_dumps (body, JSON_INDENT (0) | JSON_COMPACT);
159   json_decref (body);
160
161   GNUNET_STRINGS_base64_encode (header, strlen (header), &header_base64);
162   fix_base64 (header_base64);
163
164   GNUNET_STRINGS_base64_encode (body_str, strlen (body_str), &body_base64);
165   fix_base64 (body_base64);
166
167   GNUNET_free (subject);
168   GNUNET_free (audience);
169
170   /**
171    * Creating the JWT signature. This might not be
172    * standards compliant, check.
173    */
174   GNUNET_asprintf (&signature_target, "%s.%s", header_base64, body_base64);
175   GNUNET_CRYPTO_hmac_raw (secret_key,
176                           strlen (secret_key),
177                           signature_target,
178                           strlen (signature_target),
179                           &signature);
180   GNUNET_STRINGS_base64_encode ((const char *) &signature,
181                                 sizeof (struct GNUNET_HashCode),
182                                 &signature_base64);
183   fix_base64 (signature_base64);
184
185   GNUNET_asprintf (&result,
186                    "%s.%s.%s",
187                    header_base64,
188                    body_base64,
189                    signature_base64);
190
191   GNUNET_free (signature_target);
192   GNUNET_free (header);
193   GNUNET_free (body_str);
194   GNUNET_free (signature_base64);
195   GNUNET_free (body_base64);
196   GNUNET_free (header_base64);
197   return result;
198 }
199
200 /* Converts a hex character to its integer value */
201 static char
202 from_hex (char ch)
203 {
204   return isdigit (ch) ? ch - '0' : tolower (ch) - 'a' + 10;
205 }
206
207 /* Converts an integer value to its hex character*/
208 static char
209 to_hex (char code)
210 {
211   static char hex[] = "0123456789abcdef";
212   return hex[code & 15];
213 }
214
215 /* Returns a url-encoded version of str */
216 /* IMPORTANT: be sure to free() the returned string after use */
217 static char *
218 url_encode (const char *str)
219 {
220   char *pstr = (char *) str;
221   char *buf = GNUNET_malloc (strlen (str) * 3 + 1);
222   char *pbuf = buf;
223   while (*pstr)
224   {
225     if (isalnum (*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' ||
226         *pstr == '~')
227       *pbuf++ = *pstr;
228     else if (*pstr == ' ')
229       *pbuf++ = '+';
230     else
231     {
232       *pbuf++ = '%';
233       *pbuf++ = to_hex (*pstr >> 4);
234       *pbuf++ = to_hex (*pstr & 15);
235     }
236     pstr++;
237   }
238   *pbuf = '\0';
239   return buf;
240 }
241
242
243 /* Returns a url-decoded version of str */
244 /* IMPORTANT: be sure to free() the returned string after use */
245 static char *
246 url_decode (const char *str)
247 {
248   char *pstr = (char *) str;
249   char *buf = GNUNET_malloc (strlen (str) + 1);
250   char *pbuf = buf;
251   while (*pstr)
252   {
253     if (*pstr == '%')
254     {
255       if (pstr[1] && pstr[2])
256       {
257         *pbuf++ = from_hex (pstr[1]) << 4 | from_hex (pstr[2]);
258         pstr += 2;
259       }
260     }
261     else if (*pstr == '+')
262     {
263       *pbuf++ = ' ';
264     }
265     else
266     {
267       *pbuf++ = *pstr;
268     }
269     pstr++;
270   }
271   *pbuf = '\0';
272   return buf;
273 }
274
275
276 /**
277  * Returns base64 encoded string urlencoded
278  *
279  * @param string the string to encode
280  * @return base64 encoded string
281  */
282 static char *
283 base64_encode (const char *data, size_t data_size)
284 {
285   char *enc;
286   char *enc_urlencode;
287
288   GNUNET_STRINGS_base64_encode (data, data_size, &enc);
289   enc_urlencode = url_encode (enc);
290   GNUNET_free (enc);
291   return enc_urlencode;
292 }
293
294
295 static void
296 derive_aes_key (struct GNUNET_CRYPTO_SymmetricSessionKey *key,
297                 struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
298                 struct GNUNET_HashCode *key_material)
299 {
300   static const char ctx_key[] = "reclaim-aes-ctx-key";
301   static const char ctx_iv[] = "reclaim-aes-ctx-iv";
302   GNUNET_CRYPTO_kdf (key,
303                      sizeof (struct GNUNET_CRYPTO_SymmetricSessionKey),
304                      ctx_key,
305                      strlen (ctx_key),
306                      key_material,
307                      sizeof (struct GNUNET_HashCode),
308                      NULL);
309   GNUNET_CRYPTO_kdf (iv,
310                      sizeof (
311                        struct GNUNET_CRYPTO_SymmetricInitializationVector),
312                      ctx_iv,
313                      strlen (ctx_iv),
314                      key_material,
315                      sizeof (struct GNUNET_HashCode),
316                      NULL);
317 }
318
319
320 static void
321 calculate_key_priv (struct GNUNET_CRYPTO_SymmetricSessionKey *key,
322                     struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
323                     const struct GNUNET_CRYPTO_EcdsaPrivateKey *ecdsa_priv,
324                     const struct GNUNET_CRYPTO_EcdhePublicKey *ecdh_pub)
325 {
326   struct GNUNET_HashCode key_material;
327   GNUNET_CRYPTO_ecdsa_ecdh (ecdsa_priv, ecdh_pub, &key_material);
328   derive_aes_key (key, iv, &key_material);
329 }
330
331
332 static void
333 calculate_key_pub (struct GNUNET_CRYPTO_SymmetricSessionKey *key,
334                    struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
335                    const struct GNUNET_CRYPTO_EcdsaPublicKey *ecdsa_pub,
336                    const struct GNUNET_CRYPTO_EcdhePrivateKey *ecdh_priv)
337 {
338   struct GNUNET_HashCode key_material;
339   GNUNET_CRYPTO_ecdh_ecdsa (ecdh_priv, ecdsa_pub, &key_material);
340   derive_aes_key (key, iv, &key_material);
341 }
342
343
344 static void
345 decrypt_payload (const struct GNUNET_CRYPTO_EcdsaPrivateKey *ecdsa_priv,
346                  const struct GNUNET_CRYPTO_EcdhePublicKey *ecdh_pub,
347                  const char *ct,
348                  size_t ct_len,
349                  char *buf)
350 {
351   struct GNUNET_CRYPTO_SymmetricSessionKey key;
352   struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
353
354   calculate_key_priv (&key, &iv, ecdsa_priv, ecdh_pub);
355   GNUNET_break (GNUNET_CRYPTO_symmetric_decrypt (ct, ct_len, &key, &iv, buf));
356 }
357
358
359 static void
360 encrypt_payload (const struct GNUNET_CRYPTO_EcdsaPublicKey *ecdsa_pub,
361                  const struct GNUNET_CRYPTO_EcdhePrivateKey *ecdh_priv,
362                  const char *payload,
363                  size_t payload_len,
364                  char *buf)
365 {
366   struct GNUNET_CRYPTO_SymmetricSessionKey key;
367   struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
368
369   calculate_key_pub (&key, &iv, ecdsa_pub, ecdh_priv);
370   GNUNET_break (
371     GNUNET_CRYPTO_symmetric_encrypt (payload, payload_len, &key, &iv, buf));
372 }
373
374
375 /**
376  * Builds an OIDC authorization code including
377  * a reclaim ticket and nonce
378  *
379  * @param issuer the issuer of the ticket, used to sign the ticket and nonce
380  * @param ticket the ticket to include in the code
381  * @param attrs list of attributes whicha re shared
382  * @param nonce the nonce to include in the code
383  * @return a new authorization code (caller must free)
384  */
385 char *
386 OIDC_build_authz_code (const struct GNUNET_CRYPTO_EcdsaPrivateKey *issuer,
387                        const struct GNUNET_RECLAIM_Ticket *ticket,
388                        struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList *attrs,
389                        const char *nonce_str)
390 {
391   char *code_payload;
392   char *plaintext;
393   char *attrs_ser;
394   char *code_str;
395   char *buf_ptr;
396   size_t signature_payload_len;
397   size_t attr_list_len;
398   size_t code_payload_len;
399   uint32_t nonce;
400   uint32_t nonce_tmp;
401   struct GNUNET_CRYPTO_EccSignaturePurpose *purpose;
402   struct GNUNET_CRYPTO_EcdhePrivateKey *ecdh_priv;
403   struct GNUNET_CRYPTO_EcdhePublicKey ecdh_pub;
404
405   attrs_ser = NULL;
406   signature_payload_len =
407     sizeof (struct GNUNET_RECLAIM_Ticket) + sizeof (uint32_t);
408   if (NULL != attrs)
409   {
410     attr_list_len = GNUNET_RECLAIM_ATTRIBUTE_list_serialize_get_size (attrs);
411     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
412                 "Length of serialized attributes: %lu\n",
413                 attr_list_len);
414     signature_payload_len += attr_list_len;
415     attrs_ser = GNUNET_malloc (attr_list_len);
416     GNUNET_RECLAIM_ATTRIBUTE_list_serialize (attrs, attrs_ser);
417   }
418   code_payload_len = sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
419                      sizeof (struct GNUNET_CRYPTO_EcdhePublicKey) +
420                      signature_payload_len +
421                      sizeof (struct GNUNET_CRYPTO_EcdsaSignature);
422   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
423               "Length of data to encode: %lu\n",
424               code_payload_len);
425   plaintext = GNUNET_malloc (signature_payload_len);
426   // First, copy ticket
427   buf_ptr = plaintext;
428   memcpy (buf_ptr, ticket, sizeof (struct GNUNET_RECLAIM_Ticket));
429   buf_ptr += sizeof (struct GNUNET_RECLAIM_Ticket);
430   // Then copy nonce
431   nonce = 0;
432   if (NULL != nonce_str)
433   {
434     if ((1 != SSCANF (nonce_str, "%u", &nonce)) || (nonce > UINT32_MAX))
435     {
436       GNUNET_break (0);
437       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid nonce %s\n", nonce_str);
438       GNUNET_free (plaintext);
439       GNUNET_free_non_null (attrs_ser);
440       return NULL;
441     }
442     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
443                 "Got nonce: %u from %s\n",
444                 nonce,
445                 nonce_str);
446   }
447   nonce_tmp = htonl (nonce);
448   memcpy (buf_ptr, &nonce_tmp, sizeof (uint32_t));
449   buf_ptr += sizeof (uint32_t);
450   // Finally, attributes
451   if (NULL != attrs_ser)
452   {
453     memcpy (buf_ptr, attrs_ser, attr_list_len);
454     buf_ptr += attr_list_len;
455     GNUNET_free (attrs_ser);
456   }
457   // Generate ECDH key
458   ecdh_priv = GNUNET_CRYPTO_ecdhe_key_create ();
459   GNUNET_CRYPTO_ecdhe_key_get_public (ecdh_priv, &ecdh_pub);
460   // Initialize code payload
461   code_payload = GNUNET_malloc (code_payload_len);
462   GNUNET_assert (NULL != code_payload);
463   purpose = (struct GNUNET_CRYPTO_EccSignaturePurpose *) code_payload;
464   purpose->size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
465                          sizeof (ecdh_pub) + signature_payload_len);
466   purpose->purpose = htonl (GNUNET_SIGNATURE_PURPOSE_RECLAIM_CODE_SIGN);
467   // Store pubkey
468   buf_ptr = (char *) &purpose[1];
469   memcpy (buf_ptr, &ecdh_pub, sizeof (ecdh_pub));
470   buf_ptr += sizeof (ecdh_pub);
471   // Encrypt plaintext and store
472   encrypt_payload (&ticket->audience,
473                    ecdh_priv,
474                    plaintext,
475                    signature_payload_len,
476                    buf_ptr);
477   GNUNET_free (ecdh_priv);
478   GNUNET_free (plaintext);
479   buf_ptr += signature_payload_len;
480   // Sign and store signature
481   if (GNUNET_SYSERR ==
482       GNUNET_CRYPTO_ecdsa_sign (issuer,
483                                 purpose,
484                                 (struct GNUNET_CRYPTO_EcdsaSignature *)
485                                   buf_ptr))
486   {
487     GNUNET_break (0);
488     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unable to sign code\n");
489     GNUNET_free (code_payload);
490     return NULL;
491   }
492   code_str = base64_encode (code_payload, code_payload_len);
493   GNUNET_free (code_payload);
494   return code_str;
495 }
496
497
498 /**
499  * Parse reclaim ticket and nonce from
500  * authorization code.
501  * This also verifies the signature in the code.
502  *
503  * @param audience the expected audience of the code
504  * @param code the string representation of the code
505  * @param ticket where to store the ticket
506  * @param attrs the attributes in the code
507  * @param nonce where to store the nonce
508  * @return GNUNET_OK if successful, else GNUNET_SYSERR
509  */
510 int
511 OIDC_parse_authz_code (const struct GNUNET_CRYPTO_EcdsaPrivateKey *ecdsa_priv,
512                        const char *code,
513                        struct GNUNET_RECLAIM_Ticket *ticket,
514                        struct GNUNET_RECLAIM_ATTRIBUTE_ClaimList **attrs,
515                        char **nonce_str)
516 {
517   char *code_payload;
518   char *ptr;
519   char *plaintext;
520   struct GNUNET_CRYPTO_EccSignaturePurpose *purpose;
521   struct GNUNET_CRYPTO_EcdsaSignature *signature;
522   struct GNUNET_CRYPTO_EcdsaPublicKey ecdsa_pub;
523   struct GNUNET_CRYPTO_EcdhePublicKey *ecdh_pub;
524   size_t code_payload_len;
525   size_t attrs_ser_len;
526   size_t signature_offset;
527   size_t plaintext_len;
528   uint32_t nonce = 0;
529
530   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to decode `%s'\n", code);
531   code_payload = NULL;
532   code_payload_len =
533     GNUNET_STRINGS_base64_decode (code, strlen (code), (void **) &code_payload);
534   if (code_payload_len < sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
535                            sizeof (struct GNUNET_CRYPTO_EcdhePublicKey) +
536                            sizeof (struct GNUNET_RECLAIM_Ticket) +
537                            sizeof (uint32_t) +
538                            sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
539   {
540     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Authorization code malformed\n");
541     GNUNET_free_non_null (code_payload);
542     return GNUNET_SYSERR;
543   }
544
545   purpose = (struct GNUNET_CRYPTO_EccSignaturePurpose *) code_payload;
546   attrs_ser_len = code_payload_len;
547   attrs_ser_len -= sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose);
548   ptr = (char *) &purpose[1];
549   // Public ECDH key
550   ecdh_pub = (struct GNUNET_CRYPTO_EcdhePublicKey *) ptr;
551   ptr += sizeof (struct GNUNET_CRYPTO_EcdhePublicKey);
552   attrs_ser_len -= sizeof (struct GNUNET_CRYPTO_EcdhePublicKey);
553
554   // Decrypt ciphertext
555   plaintext_len = attrs_ser_len - sizeof (struct GNUNET_CRYPTO_EcdsaSignature);
556   plaintext = GNUNET_malloc (plaintext_len);
557   decrypt_payload (ecdsa_priv, ecdh_pub, ptr, plaintext_len, plaintext);
558   ptr = plaintext;
559   // Ticket
560   *ticket = *((struct GNUNET_RECLAIM_Ticket *) ptr);
561   attrs_ser_len -= sizeof (struct GNUNET_RECLAIM_Ticket);
562   ptr += sizeof (struct GNUNET_RECLAIM_Ticket);
563   // Nonce
564   nonce = ntohl (*((uint32_t *) ptr));
565   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got nonce: %u\n", nonce);
566   attrs_ser_len -= sizeof (uint32_t);
567   ptr += sizeof (uint32_t);
568   // Attributes
569   attrs_ser_len -= sizeof (struct GNUNET_CRYPTO_EcdsaSignature);
570   *attrs = GNUNET_RECLAIM_ATTRIBUTE_list_deserialize (ptr, attrs_ser_len);
571   // Signature
572   signature_offset =
573     code_payload_len - sizeof (struct GNUNET_CRYPTO_EcdsaSignature);
574   signature =
575     (struct GNUNET_CRYPTO_EcdsaSignature *) &code_payload[signature_offset];
576   GNUNET_CRYPTO_ecdsa_key_get_public (ecdsa_priv, &ecdsa_pub);
577   if (0 != GNUNET_memcmp (&ecdsa_pub, &ticket->audience))
578   {
579     GNUNET_RECLAIM_ATTRIBUTE_list_destroy (*attrs);
580     GNUNET_free (code_payload);
581     GNUNET_free (plaintext);
582     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
583                 "Audience in ticket does not match client!\n");
584     return GNUNET_SYSERR;
585   }
586   if (GNUNET_OK !=
587       GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_RECLAIM_CODE_SIGN,
588                                   purpose,
589                                   signature,
590                                   &ticket->identity))
591   {
592     GNUNET_RECLAIM_ATTRIBUTE_list_destroy (*attrs);
593     GNUNET_free (code_payload);
594     GNUNET_free (plaintext);
595     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Signature of AuthZ code invalid!\n");
596     return GNUNET_SYSERR;
597   }
598   *nonce_str = NULL;
599   if (nonce != 0)
600     GNUNET_asprintf (nonce_str, "%u", nonce);
601   GNUNET_free (code_payload);
602   GNUNET_free (plaintext);
603   return GNUNET_OK;
604 }
605
606
607 /**
608  * Build a token response for a token request
609  * TODO: Maybe we should add the scope here?
610  *
611  * @param access_token the access token to include
612  * @param id_token the id_token to include
613  * @param expiration_time the expiration time of the token(s)
614  * @param token_response where to store the response
615  */
616 void
617 OIDC_build_token_response (const char *access_token,
618                            const char *id_token,
619                            const struct GNUNET_TIME_Relative *expiration_time,
620                            char **token_response)
621 {
622   json_t *root_json;
623
624   root_json = json_object ();
625
626   GNUNET_assert (NULL != access_token);
627   GNUNET_assert (NULL != id_token);
628   GNUNET_assert (NULL != expiration_time);
629   json_object_set_new (root_json, "access_token", json_string (access_token));
630   json_object_set_new (root_json, "token_type", json_string ("Bearer"));
631   json_object_set_new (root_json,
632                        "expires_in",
633                        json_integer (expiration_time->rel_value_us /
634                                      (1000 * 1000)));
635   json_object_set_new (root_json, "id_token", json_string (id_token));
636   *token_response = json_dumps (root_json, JSON_INDENT (0) | JSON_COMPACT);
637   json_decref (root_json);
638 }
639
640 /**
641  * Generate a new access token
642  */
643 char *
644 OIDC_access_token_new ()
645 {
646   char *access_token;
647   uint64_t random_number;
648
649   random_number =
650     GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_NONCE, UINT64_MAX);
651   GNUNET_STRINGS_base64_encode (&random_number,
652                                 sizeof (uint64_t),
653                                 &access_token);
654   return access_token;
655 }