eql.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
  2. /*
  3. * Copyright 2017 Red Hat, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include "jwk.h"
  18. #include <string.h>
  19. #define SUMMARY "Determines if two or more JWKs are equal"
  20. typedef struct {
  21. json_t *keys;
  22. } jcmd_opt_t;
  23. static const char *prefix =
  24. "jose jwk eql -i JWK -i JWK\n\n" SUMMARY;
  25. static const jcmd_cfg_t cfgs[] = {
  26. {
  27. .opt = { "input", required_argument, .val = 'i' },
  28. .off = offsetof(jcmd_opt_t, keys),
  29. .set = jcmd_opt_set_jwks,
  30. .doc = jcmd_jwk_doc_input,
  31. },
  32. {}
  33. };
  34. static void
  35. jcmd_opt_cleanup(jcmd_opt_t *opt)
  36. {
  37. json_decrefp(&opt->keys);
  38. }
  39. static int
  40. jcmd_jwk_eql(int argc, char *argv[])
  41. {
  42. jcmd_opt_auto_t opt = {};
  43. if (!jcmd_opt_parse(argc, argv, cfgs, &opt, prefix))
  44. return EXIT_FAILURE;
  45. if (json_array_size(opt.keys) < 2) {
  46. fprintf(stderr, "Must specify at least two JWKs!\n");
  47. return EXIT_FAILURE;
  48. }
  49. for (size_t i = 1; i < json_array_size(opt.keys); i++) {
  50. const json_t *a = json_array_get(opt.keys, i - 1);
  51. const json_t *b = json_array_get(opt.keys, i);
  52. if (!jose_jwk_eql(NULL, a, b))
  53. return EXIT_FAILURE;
  54. }
  55. return EXIT_SUCCESS;
  56. }
  57. JCMD_REGISTER(SUMMARY, jcmd_jwk_eql, "jwk", "eql")