Skip to content

Commit cdb12d1

Browse files
committed
refactoring: optimize oidc_jwk_to_json
Signed-off-by: Hans Zandbelt <[email protected]>
1 parent 78e1635 commit cdb12d1

File tree

1 file changed

+14
-34
lines changed

1 file changed

+14
-34
lines changed

src/jose.c

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -525,58 +525,38 @@ apr_byte_t oidc_is_jwks(json_t *json) {
525525
*/
526526
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) {
527527
apr_byte_t rv = FALSE;
528-
char *cjose_jwk_json = NULL;
528+
char *s_cjose = NULL;
529529
cjose_err err;
530530
json_t *json = NULL, *temp = NULL;
531531
json_error_t json_error;
532532
int i = 0;
533533
void *iter = NULL;
534534

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))
537537
goto end;
538-
}
539-
540-
// get current
541-
cjose_jwk_json = cjose_jwk_to_json(jwk->cjose_jwk, TRUE, &err);
542538

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));
545543
goto end;
546544
}
547545

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");
551549
goto end;
552550
}
553551

554-
json = json_object();
555-
556552
if (jwk->use)
557553
json_object_set_new(json, OIDC_JOSE_JWK_USE_STR, json_string(jwk->use));
558554

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-
567555
// set x5c
568556
if ((jwk->x5c != NULL) && (jwk->x5c->nelts > 0)) {
569557
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 *)));
580560
json_object_set_new(json, OIDC_JOSE_JWK_X5C_STR, temp);
581561
}
582562

@@ -595,10 +575,10 @@ apr_byte_t oidc_jwk_to_json(apr_pool_t *pool, const oidc_jwk_t *jwk, char **s_js
595575

596576
end:
597577

598-
if (cjose_jwk_json)
599-
cjose_get_dealloc()(cjose_jwk_json);
600578
if (json)
601579
json_decref(json);
580+
if (s_cjose)
581+
cjose_get_dealloc()(s_cjose);
602582

603583
return rv;
604584
}

0 commit comments

Comments
 (0)