From 887d46d0d17535958350919f3e29f7a0a00e1af7 Mon Sep 17 00:00:00 2001 From: Michael R Sweet Date: Fri, 16 May 2025 18:28:38 -0400 Subject: [PATCH] Update cupsJWTNew to accept a claims argument with the initial JSON claims. --- cups/jwt.c | 10 ++++++++-- cups/jwt.h | 4 ++-- cups/testjwt.c | 12 ++++++------ 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/cups/jwt.c b/cups/jwt.c index d097bdedab..6435cb1616 100644 --- a/cups/jwt.c +++ b/cups/jwt.c @@ -1264,7 +1264,8 @@ cupsJWTMakePublicKey(cups_json_t *jwk) // I - Private JSON Web Key // cups_jwt_t * // O - JWT object -cupsJWTNew(const char *type) // I - JWT type or `NULL` for default ("JWT") +cupsJWTNew(const char *type, // I - JWT type or `NULL` for default ("JWT") + cups_json_t *claims) // I - JSON claims or `NULL` for empty { cups_jwt_t *jwt; // JWT object @@ -1275,7 +1276,12 @@ cupsJWTNew(const char *type) // I - JWT type or `NULL` for default ("JWT") { cupsJSONNewString(jwt->jose, cupsJSONNewKey(jwt->jose, NULL, "typ"), type ? type : "JWT"); - if ((jwt->claims = cupsJSONNew(NULL, NULL, CUPS_JTYPE_OBJECT)) != NULL) + if (claims) + jwt->claims = claims; + else + jwt->claims = cupsJSONNew(NULL, NULL, CUPS_JTYPE_OBJECT); + + if (jwt->claims) return (jwt); } } diff --git a/cups/jwt.h b/cups/jwt.h index 1fdbab33e4..7bdfaf26ba 100644 --- a/cups/jwt.h +++ b/cups/jwt.h @@ -1,7 +1,7 @@ // // JSON Web Token API definitions for CUPS. // -// Copyright © 2023-2024 by OpenPrinting. +// Copyright © 2023-2025 by OpenPrinting. // // Licensed under Apache License v2.0. See the file "LICENSE" for more // information. @@ -79,7 +79,7 @@ extern cups_jwt_t *cupsJWTImportString(const char *s, cups_jws_format_t format) extern cups_json_t *cupsJWTLoadCredentials(const char *path, const char *common_name) _CUPS_PUBLIC; extern cups_json_t *cupsJWTMakePrivateKey(cups_jwa_t alg) _CUPS_PUBLIC; extern cups_json_t *cupsJWTMakePublicKey(cups_json_t *jwk) _CUPS_PUBLIC; -extern cups_jwt_t *cupsJWTNew(const char *type) _CUPS_PUBLIC; +extern cups_jwt_t *cupsJWTNew(const char *type, cups_json_t *claims) _CUPS_PUBLIC; extern void cupsJWTSetClaimNumber(cups_jwt_t *jwt, const char *claim, double value) _CUPS_PUBLIC; extern void cupsJWTSetClaimString(cups_jwt_t *jwt, const char *claim, const char *value) _CUPS_PUBLIC; extern void cupsJWTSetClaimValue(cups_jwt_t *jwt, const char *claim, cups_json_t *value) _CUPS_PUBLIC; diff --git a/cups/testjwt.c b/cups/testjwt.c index 3fbab61d9c..91da59189d 100644 --- a/cups/testjwt.c +++ b/cups/testjwt.c @@ -99,8 +99,8 @@ main(int argc, // I - Number of command-line arguments }*/ }; - testBegin("cupsJWTNew(NULL)"); - jwt = cupsJWTNew(NULL); + testBegin("cupsJWTNew(NULL, NULL)"); + jwt = cupsJWTNew(NULL, NULL); testEnd(jwt != NULL); testBegin("cupsJWTSetClaimNumber(CUPS_JWT_IAT)"); @@ -302,8 +302,8 @@ main(int argc, // I - Number of command-line arguments testMessage("jwk=\"%s\"", s); free(s); - testBegin("cupsJWTNew(NULL)"); - jwt = cupsJWTNew(NULL); + testBegin("cupsJWTNew(NULL, NULL)"); + jwt = cupsJWTNew(NULL, NULL); testEnd(jwt != NULL); testBegin("cupsJWTSetClaimNumber(CUPS_JWT_IAT)"); @@ -346,8 +346,8 @@ main(int argc, // I - Number of command-line arguments testMessage("jwk=\"%s\"", s); free(s); - testBegin("cupsJWTNew(NULL)"); - jwt = cupsJWTNew(NULL); + testBegin("cupsJWTNew(NULL, NULL)"); + jwt = cupsJWTNew(NULL, NULL); testEnd(jwt != NULL); testBegin("cupsJWTSetClaimNumber(CUPS_JWT_IAT)"); -- 2.47.2