@@ -525,58 +525,38 @@ apr_byte_t oidc_is_jwks(json_t *json) {
525
525
*/
526
526
apr_byte_t oidc_jwk_to_json (apr_pool_t * pool , const oidc_jwk_t * jwk , char * * s_json , oidc_jose_error_t * oidc_err ) {
527
527
apr_byte_t rv = FALSE;
528
- char * cjose_jwk_json = NULL ;
528
+ char * s_cjose = NULL ;
529
529
cjose_err err ;
530
530
json_t * json = NULL , * temp = NULL ;
531
531
json_error_t json_error ;
532
532
int i = 0 ;
533
533
void * iter = NULL ;
534
534
535
- if (! jwk ) {
536
- oidc_jose_error ( oidc_err , "oidc_jwk_to_json failed: NULL oidc_jwk" );
535
+ // input sanity checks
536
+ if (( jwk == NULL ) || ( s_json == NULL ))
537
537
goto end ;
538
- }
539
-
540
- // get current
541
- cjose_jwk_json = cjose_jwk_to_json (jwk -> cjose_jwk , TRUE, & err );
542
538
543
- if (cjose_jwk_json == NULL ) {
544
- oidc_jose_error (oidc_err , "cjose_jwk_to_json failed: %s" , oidc_cjose_e2s (pool , err ));
539
+ // get the JWK string representation from cjose
540
+ s_cjose = cjose_jwk_to_json (jwk -> cjose_jwk , TRUE, & err );
541
+ if (s_cjose == NULL ) {
542
+ oidc_jose_error (oidc_err , "oidc_jwk_to_json: cjose_jwk_to_json failed: %s" , oidc_cjose_e2s (pool , err ));
545
543
goto end ;
546
544
}
547
545
548
- temp = json_loads (cjose_jwk_json , 0 , & json_error );
549
- if (! temp ) {
550
- oidc_jose_error (oidc_err , "json_loads failed" );
546
+ json = json_loads (s_cjose , 0 , & json_error );
547
+ if (json == NULL ) {
548
+ oidc_jose_error (oidc_err , "oidc_jwk_to_json: json_loads failed" );
551
549
goto end ;
552
550
}
553
551
554
- json = json_object ();
555
-
556
552
if (jwk -> use )
557
553
json_object_set_new (json , OIDC_JOSE_JWK_USE_STR , json_string (jwk -> use ));
558
554
559
- iter = json_object_iter (temp );
560
- while (iter ) {
561
- json_object_set (json , json_object_iter_key (iter ), json_object_iter_value (iter ));
562
- iter = json_object_iter_next (temp , iter );
563
- }
564
- json_decref (temp );
565
- temp = NULL ;
566
-
567
555
// set x5c
568
556
if ((jwk -> x5c != NULL ) && (jwk -> x5c -> nelts > 0 )) {
569
557
temp = json_array ();
570
- if (temp == NULL ) {
571
- oidc_jose_error (oidc_err , "json_array failed" );
572
- goto end ;
573
- }
574
- for (i = 0 ; i < jwk -> x5c -> nelts ; i ++ ) {
575
- if (json_array_append_new (temp , json_string (APR_ARRAY_IDX (jwk -> x5c , i , const char * ))) == -1 ) {
576
- oidc_jose_error (oidc_err , "json_array_append failed" );
577
- goto end ;
578
- }
579
- }
558
+ for (i = 0 ; i < jwk -> x5c -> nelts ; i ++ )
559
+ json_array_append_new (temp , json_string (APR_ARRAY_IDX (jwk -> x5c , i , const char * )));
580
560
json_object_set_new (json , OIDC_JOSE_JWK_X5C_STR , temp );
581
561
}
582
562
@@ -595,10 +575,10 @@ apr_byte_t oidc_jwk_to_json(apr_pool_t *pool, const oidc_jwk_t *jwk, char **s_js
595
575
596
576
end :
597
577
598
- if (cjose_jwk_json )
599
- cjose_get_dealloc ()(cjose_jwk_json );
600
578
if (json )
601
579
json_decref (json );
580
+ if (s_cjose )
581
+ cjose_get_dealloc ()(s_cjose );
602
582
603
583
return rv ;
604
584
}
0 commit comments