aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/security/jwt_verifier_test.c
blob: a4d65dccd9eaa9bc68affaedbbfbc9c3da553678 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/*
 *
 * Copyright 2015, Google Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include "src/core/lib/security/credentials/jwt/jwt_verifier.h"

#include <string.h>

#include <grpc/grpc.h>

#include <grpc/slice.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
#include <grpc/support/string_util.h>

#include "src/core/lib/http/httpcli.h"
#include "src/core/lib/security/credentials/jwt/json_token.h"
#include "src/core/lib/security/util/b64.h"
#include "test/core/util/test_config.h"

/* This JSON key was generated with the GCE console and revoked immediately.
   The identifiers have been changed as well.
   Maximum size for a string literal is 509 chars in C89, yay!  */
static const char json_key_str_part1[] =
    "{ \"private_key\": \"-----BEGIN PRIVATE KEY-----"
    "\\nMIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAOEvJsnoHnyHkXcp\\n7mJE"
    "qg"
    "WGjiw71NfXByguekSKho65FxaGbsnSM9SMQAqVk7Q2rG+I0OpsT0LrWQtZ\\nyjSeg/"
    "rWBQvS4hle4LfijkP3J5BG+"
    "IXDMP8RfziNRQsenAXDNPkY4kJCvKux2xdD\\nOnVF6N7dL3nTYZg+"
    "uQrNsMTz9UxVAgMBAAECgYEAzbLewe1xe9vy+2GoSsfib+28\\nDZgSE6Bu/"
    "zuFoPrRc6qL9p2SsnV7txrunTyJkkOnPLND9ABAXybRTlcVKP/sGgza\\n/"
    "8HpCqFYM9V8f34SBWfD4fRFT+n/"
    "73cfRUtGXdXpseva2lh8RilIQfPhNZAncenU\\ngqXjDvpkypEusgXAykECQQD+";
static const char json_key_str_part2[] =
    "53XxNVnxBHsYb+AYEfklR96yVi8HywjVHP34+OQZ\\nCslxoHQM8s+"
    "dBnjfScLu22JqkPv04xyxmt0QAKm9+vTdAkEA4ib7YvEAn2jXzcCI\\nEkoy2L/"
    "XydR1GCHoacdfdAwiL2npOdnbvi4ZmdYRPY1LSTO058tQHKVXV7NLeCa3\\nAARh2QJBAMKeDA"
    "G"
    "W303SQv2cZTdbeaLKJbB5drz3eo3j7dDKjrTD9JupixFbzcGw\\n8FZi5c8idxiwC36kbAL6Hz"
    "A"
    "ZoX+ofI0CQE6KCzPJTtYNqyShgKAZdJ8hwOcvCZtf\\n6z8RJm0+"
    "6YBd38lfh5j8mZd7aHFf6I17j5AQY7oPEc47TjJj/"
    "5nZ68ECQQDvYuI3\\nLyK5fS8g0SYbmPOL9TlcHDOqwG0mrX9qpg5DC2fniXNSrrZ64GTDKdzZ"
    "Y"
    "Ap6LI9W\\nIqv4vr6y38N79TTC\\n-----END PRIVATE KEY-----\\n\", ";
static const char json_key_str_part3_for_google_email_issuer[] =
    "\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", "
    "\"client_email\": "
    "\"777-abaslkan11hlb6nmim3bpspl31ud@developer.gserviceaccount."
    "com\", \"client_id\": "
    "\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent."
    "com\", \"type\": \"service_account\" }";
/* Trick our JWT library into issuing a JWT with iss=accounts.google.com. */
static const char json_key_str_part3_for_url_issuer[] =
    "\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", "
    "\"client_email\": \"accounts.google.com\", "
    "\"client_id\": "
    "\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent."
    "com\", \"type\": \"service_account\" }";
static const char json_key_str_part3_for_custom_email_issuer[] =
    "\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", "
    "\"client_email\": "
    "\"foo@bar.com\", \"client_id\": "
    "\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent."
    "com\", \"type\": \"service_account\" }";

static grpc_jwt_verifier_email_domain_key_url_mapping custom_mapping = {
    "bar.com", "keys.bar.com/jwk"};

static const char expected_user_data[] = "user data";

static const char good_jwk_set[] =
    "{"
    " \"keys\": ["
    "  {"
    "   \"kty\": \"RSA\","
    "   \"alg\": \"RS256\","
    "   \"use\": \"sig\","
    "   \"kid\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\","
    "   \"n\": "
    "\"4S8myegefIeRdynuYkSqBYaOLDvU19cHKC56RIqGjrkXFoZuydIz1IxACpWTtDasb4jQ6mxP"
    "QutZC1nKNJ6D-tYFC9LiGV7gt-KOQ_cnkEb4hcMw_xF_OI1FCx6cBcM0-"
    "RjiQkK8q7HbF0M6dUXo3t0vedNhmD65Cs2wxPP1TFU=\","
    "   \"e\": \"AQAB\""
    "  }"
    " ]"
    "}";

static gpr_timespec expected_lifetime = {3600, 0, GPR_TIMESPAN};

static const char good_google_email_keys_part1[] =
    "{\"e6b5137873db8d2ef81e06a47289e6434ec8a165\": \"-----BEGIN "
    "CERTIFICATE-----"
    "\\nMIICATCCAWoCCQDEywLhxvHjnDANBgkqhkiG9w0BAQsFADBFMQswCQYDVQQGEwJB\\nVTET"
    "MBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0\\ncyBQdHkgTHR"
    "kMB4XDTE1MDYyOTA4Mzk1MFoXDTI1MDYyNjA4Mzk1MFowRTELMAkG\\nA1UEBhMCQVUxEzARBg"
    "NVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0\\nIFdpZGdpdHMgUHR5IEx0ZDCBn"
    "zANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA4S8m\\nyegefIeRdynuYkSqBYaOLDvU19cHKC56"
    "RIqGjrkXFoZuydIz1IxACpWTtDasb4jQ\\n6mxPQutZC1nKNJ6D+tYFC9LiGV7gt+KOQ/";

static const char good_google_email_keys_part2[] =
    "cnkEb4hcMw/xF/OI1FCx6cBcM0+"
    "Rji\\nQkK8q7HbF0M6dUXo3t0vedNhmD65Cs2wxPP1TFUCAwEAATANBgkqhkiG9w0BAQsF\\nA"
    "AOBgQBfu69FkPmBknbKNFgurPz78kbs3VNN+k/"
    "PUgO5DHKskJmgK2TbtvX2VMpx\\nkftmHGzgzMzUlOtigCaGMgHWjfqjpP9uuDbahXrZBJzB8c"
    "Oq7MrQF8r17qVvo3Ue\\nPjTKQMAsU8uxTEMmeuz9L6yExs0rfd6bPOrQkAoVfFfiYB3/"
    "pA==\\n-----END CERTIFICATE-----\\n\"}";

static const char expected_audience[] = "https://foo.com";

static const char good_openid_config[] =
    "{"
    " \"issuer\": \"https://accounts.google.com\","
    " \"authorization_endpoint\": "
    "\"https://accounts.google.com/o/oauth2/v2/auth\","
    " \"token_endpoint\": \"https://www.googleapis.com/oauth2/v4/token\","
    " \"userinfo_endpoint\": \"https://www.googleapis.com/oauth2/v3/userinfo\","
    " \"revocation_endpoint\": \"https://accounts.google.com/o/oauth2/revoke\","
    " \"jwks_uri\": \"https://www.googleapis.com/oauth2/v3/certs\""
    "}";

static const char expired_claims[] =
    "{ \"aud\": \"https://foo.com\","
    "  \"iss\": \"blah.foo.com\","
    "  \"sub\": \"juju@blah.foo.com\","
    "  \"jti\": \"jwtuniqueid\","
    "  \"iat\": 100," /* Way back in the past... */
    "  \"exp\": 120,"
    "  \"nbf\": 60,"
    "  \"foo\": \"bar\"}";

static const char claims_without_time_constraint[] =
    "{ \"aud\": \"https://foo.com\","
    "  \"iss\": \"blah.foo.com\","
    "  \"sub\": \"juju@blah.foo.com\","
    "  \"jti\": \"jwtuniqueid\","
    "  \"foo\": \"bar\"}";

static const char claims_with_bad_subject[] =
    "{ \"aud\": \"https://foo.com\","
    "  \"iss\": \"evil@blah.foo.com\","
    "  \"sub\": \"juju@blah.foo.com\","
    "  \"jti\": \"jwtuniqueid\","
    "  \"foo\": \"bar\"}";

static const char invalid_claims[] =
    "{ \"aud\": \"https://foo.com\","
    "  \"iss\": 46," /* Issuer cannot be a number. */
    "  \"sub\": \"juju@blah.foo.com\","
    "  \"jti\": \"jwtuniqueid\","
    "  \"foo\": \"bar\"}";

typedef struct {
  grpc_jwt_verifier_status expected_status;
  const char *expected_issuer;
  const char *expected_subject;
} verifier_test_config;

static void test_jwt_issuer_email_domain(void) {
  const char *d = grpc_jwt_issuer_email_domain("https://foo.com");
  GPR_ASSERT(d == NULL);
  d = grpc_jwt_issuer_email_domain("foo.com");
  GPR_ASSERT(d == NULL);
  d = grpc_jwt_issuer_email_domain("");
  GPR_ASSERT(d == NULL);
  d = grpc_jwt_issuer_email_domain("@");
  GPR_ASSERT(d == NULL);
  d = grpc_jwt_issuer_email_domain("bar@foo");
  GPR_ASSERT(strcmp(d, "foo") == 0);
  d = grpc_jwt_issuer_email_domain("bar@foo.com");
  GPR_ASSERT(strcmp(d, "foo.com") == 0);
  d = grpc_jwt_issuer_email_domain("bar@blah.foo.com");
  GPR_ASSERT(strcmp(d, "foo.com") == 0);
  d = grpc_jwt_issuer_email_domain("bar.blah@blah.foo.com");
  GPR_ASSERT(strcmp(d, "foo.com") == 0);
  d = grpc_jwt_issuer_email_domain("bar.blah@baz.blah.foo.com");
  GPR_ASSERT(strcmp(d, "foo.com") == 0);

  /* This is not a very good parser but make sure we do not crash on these weird
     inputs. */
  d = grpc_jwt_issuer_email_domain("@foo");
  GPR_ASSERT(strcmp(d, "foo") == 0);
  d = grpc_jwt_issuer_email_domain("bar@.");
  GPR_ASSERT(d != NULL);
  d = grpc_jwt_issuer_email_domain("bar@..");
  GPR_ASSERT(d != NULL);
  d = grpc_jwt_issuer_email_domain("bar@...");
  GPR_ASSERT(d != NULL);
}

static void test_claims_success(void) {
  grpc_jwt_claims *claims;
  grpc_slice s = grpc_slice_from_copied_string(claims_without_time_constraint);
  grpc_json *json = grpc_json_parse_string_with_len(
      (char *)GRPC_SLICE_START_PTR(s), GRPC_SLICE_LENGTH(s));
  GPR_ASSERT(json != NULL);
  claims = grpc_jwt_claims_from_json(json, s);
  GPR_ASSERT(claims != NULL);
  GPR_ASSERT(grpc_jwt_claims_json(claims) == json);
  GPR_ASSERT(strcmp(grpc_jwt_claims_audience(claims), "https://foo.com") == 0);
  GPR_ASSERT(strcmp(grpc_jwt_claims_issuer(claims), "blah.foo.com") == 0);
  GPR_ASSERT(strcmp(grpc_jwt_claims_subject(claims), "juju@blah.foo.com") == 0);
  GPR_ASSERT(strcmp(grpc_jwt_claims_id(claims), "jwtuniqueid") == 0);
  GPR_ASSERT(grpc_jwt_claims_check(claims, "https://foo.com") ==
             GRPC_JWT_VERIFIER_OK);
  grpc_jwt_claims_destroy(claims);
}

static void test_expired_claims_failure(void) {
  grpc_jwt_claims *claims;
  grpc_slice s = grpc_slice_from_copied_string(expired_claims);
  grpc_json *json = grpc_json_parse_string_with_len(
      (char *)GRPC_SLICE_START_PTR(s), GRPC_SLICE_LENGTH(s));
  gpr_timespec exp_iat = {100, 0, GPR_CLOCK_REALTIME};
  gpr_timespec exp_exp = {120, 0, GPR_CLOCK_REALTIME};
  gpr_timespec exp_nbf = {60, 0, GPR_CLOCK_REALTIME};
  GPR_ASSERT(json != NULL);
  claims = grpc_jwt_claims_from_json(json, s);
  GPR_ASSERT(claims != NULL);
  GPR_ASSERT(grpc_jwt_claims_json(claims) == json);
  GPR_ASSERT(strcmp(grpc_jwt_claims_audience(claims), "https://foo.com") == 0);
  GPR_ASSERT(strcmp(grpc_jwt_claims_issuer(claims), "blah.foo.com") == 0);
  GPR_ASSERT(strcmp(grpc_jwt_claims_subject(claims), "juju@blah.foo.com") == 0);
  GPR_ASSERT(strcmp(grpc_jwt_claims_id(claims), "jwtuniqueid") == 0);
  GPR_ASSERT(gpr_time_cmp(grpc_jwt_claims_issued_at(claims), exp_iat) == 0);
  GPR_ASSERT(gpr_time_cmp(grpc_jwt_claims_expires_at(claims), exp_exp) == 0);
  GPR_ASSERT(gpr_time_cmp(grpc_jwt_claims_not_before(claims), exp_nbf) == 0);

  GPR_ASSERT(grpc_jwt_claims_check(claims, "https://foo.com") ==
             GRPC_JWT_VERIFIER_TIME_CONSTRAINT_FAILURE);
  grpc_jwt_claims_destroy(claims);
}

static void test_invalid_claims_failure(void) {
  grpc_slice s = grpc_slice_from_copied_string(invalid_claims);
  grpc_json *json = grpc_json_parse_string_with_len(
      (char *)GRPC_SLICE_START_PTR(s), GRPC_SLICE_LENGTH(s));
  GPR_ASSERT(grpc_jwt_claims_from_json(json, s) == NULL);
}

static void test_bad_audience_claims_failure(void) {
  grpc_jwt_claims *claims;
  grpc_slice s = grpc_slice_from_copied_string(claims_without_time_constraint);
  grpc_json *json = grpc_json_parse_string_with_len(
      (char *)GRPC_SLICE_START_PTR(s), GRPC_SLICE_LENGTH(s));
  GPR_ASSERT(json != NULL);
  claims = grpc_jwt_claims_from_json(json, s);
  GPR_ASSERT(claims != NULL);
  GPR_ASSERT(grpc_jwt_claims_check(claims, "https://bar.com") ==
             GRPC_JWT_VERIFIER_BAD_AUDIENCE);
  grpc_jwt_claims_destroy(claims);
}

static void test_bad_subject_claims_failure(void) {
  grpc_jwt_claims *claims;
  grpc_slice s = grpc_slice_from_copied_string(claims_with_bad_subject);
  grpc_json *json = grpc_json_parse_string_with_len(
      (char *)GRPC_SLICE_START_PTR(s), GRPC_SLICE_LENGTH(s));
  GPR_ASSERT(json != NULL);
  claims = grpc_jwt_claims_from_json(json, s);
  GPR_ASSERT(claims != NULL);
  GPR_ASSERT(grpc_jwt_claims_check(claims, "https://foo.com") ==
             GRPC_JWT_VERIFIER_BAD_SUBJECT);
  grpc_jwt_claims_destroy(claims);
}

static char *json_key_str(const char *last_part) {
  size_t result_len = strlen(json_key_str_part1) + strlen(json_key_str_part2) +
                      strlen(last_part);
  char *result = gpr_malloc(result_len + 1);
  char *current = result;
  strcpy(result, json_key_str_part1);
  current += strlen(json_key_str_part1);
  strcpy(current, json_key_str_part2);
  current += strlen(json_key_str_part2);
  strcpy(current, last_part);
  return result;
}

static char *good_google_email_keys(void) {
  size_t result_len = strlen(good_google_email_keys_part1) +
                      strlen(good_google_email_keys_part2);
  char *result = gpr_malloc(result_len + 1);
  char *current = result;
  strcpy(result, good_google_email_keys_part1);
  current += strlen(good_google_email_keys_part1);
  strcpy(current, good_google_email_keys_part2);
  return result;
}

static grpc_httpcli_response http_response(int status, char *body) {
  grpc_httpcli_response response;
  memset(&response, 0, sizeof(grpc_httpcli_response));
  response.status = status;
  response.body = body;
  response.body_length = strlen(body);
  return response;
}

static int httpcli_post_should_not_be_called(
    grpc_exec_ctx *exec_ctx, const grpc_httpcli_request *request,
    const char *body_bytes, size_t body_size, gpr_timespec deadline,
    grpc_closure *on_done, grpc_httpcli_response *response) {
  GPR_ASSERT("HTTP POST should not be called" == NULL);
  return 1;
}

static int httpcli_get_google_keys_for_email(
    grpc_exec_ctx *exec_ctx, const grpc_httpcli_request *request,
    gpr_timespec deadline, grpc_closure *on_done,
    grpc_httpcli_response *response) {
  *response = http_response(200, good_google_email_keys());
  GPR_ASSERT(request->handshaker == &grpc_httpcli_ssl);
  GPR_ASSERT(strcmp(request->host, "www.googleapis.com") == 0);
  GPR_ASSERT(strcmp(request->http.path,
                    "/robot/v1/metadata/x509/"
                    "777-abaslkan11hlb6nmim3bpspl31ud@developer."
                    "gserviceaccount.com") == 0);
  grpc_closure_sched(exec_ctx, on_done, GRPC_ERROR_NONE);
  return 1;
}

static void on_verification_success(void *user_data,
                                    grpc_jwt_verifier_status status,
                                    grpc_jwt_claims *claims) {
  GPR_ASSERT(status == GRPC_JWT_VERIFIER_OK);
  GPR_ASSERT(claims != NULL);
  GPR_ASSERT(user_data == (void *)expected_user_data);
  GPR_ASSERT(strcmp(grpc_jwt_claims_audience(claims), expected_audience) == 0);
  grpc_jwt_claims_destroy(claims);
}

static void test_jwt_verifier_google_email_issuer_success(void) {
  grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  grpc_jwt_verifier *verifier = grpc_jwt_verifier_create(NULL, 0);
  char *jwt = NULL;
  char *key_str = json_key_str(json_key_str_part3_for_google_email_issuer);
  grpc_auth_json_key key = grpc_auth_json_key_create_from_string(key_str);
  gpr_free(key_str);
  GPR_ASSERT(grpc_auth_json_key_is_valid(&key));
  grpc_httpcli_set_override(httpcli_get_google_keys_for_email,
                            httpcli_post_should_not_be_called);
  jwt = grpc_jwt_encode_and_sign(&key, expected_audience, expected_lifetime,
                                 NULL);
  grpc_auth_json_key_destruct(&key);
  GPR_ASSERT(jwt != NULL);
  grpc_jwt_verifier_verify(&exec_ctx, verifier, NULL, jwt, expected_audience,
                           on_verification_success, (void *)expected_user_data);
  grpc_exec_ctx_finish(&exec_ctx);
  gpr_free(jwt);
  grpc_jwt_verifier_destroy(verifier);
  grpc_httpcli_set_override(NULL, NULL);
}

static int httpcli_get_custom_keys_for_email(
    grpc_exec_ctx *exec_ctx, const grpc_httpcli_request *request,
    gpr_timespec deadline, grpc_closure *on_done,
    grpc_httpcli_response *response) {
  *response = http_response(200, gpr_strdup(good_jwk_set));
  GPR_ASSERT(request->handshaker == &grpc_httpcli_ssl);
  GPR_ASSERT(strcmp(request->host, "keys.bar.com") == 0);
  GPR_ASSERT(strcmp(request->http.path, "/jwk/foo@bar.com") == 0);
  grpc_closure_sched(exec_ctx, on_done, GRPC_ERROR_NONE);
  return 1;
}

static void test_jwt_verifier_custom_email_issuer_success(void) {
  grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  grpc_jwt_verifier *verifier = grpc_jwt_verifier_create(&custom_mapping, 1);
  char *jwt = NULL;
  char *key_str = json_key_str(json_key_str_part3_for_custom_email_issuer);
  grpc_auth_json_key key = grpc_auth_json_key_create_from_string(key_str);
  gpr_free(key_str);
  GPR_ASSERT(grpc_auth_json_key_is_valid(&key));
  grpc_httpcli_set_override(httpcli_get_custom_keys_for_email,
                            httpcli_post_should_not_be_called);
  jwt = grpc_jwt_encode_and_sign(&key, expected_audience, expected_lifetime,
                                 NULL);
  grpc_auth_json_key_destruct(&key);
  GPR_ASSERT(jwt != NULL);
  grpc_jwt_verifier_verify(&exec_ctx, verifier, NULL, jwt, expected_audience,
                           on_verification_success, (void *)expected_user_data);
  grpc_exec_ctx_finish(&exec_ctx);
  gpr_free(jwt);
  grpc_jwt_verifier_destroy(verifier);
  grpc_httpcli_set_override(NULL, NULL);
}

static int httpcli_get_jwk_set(grpc_exec_ctx *exec_ctx,
                               const grpc_httpcli_request *request,
                               gpr_timespec deadline, grpc_closure *on_done,
                               grpc_httpcli_response *response) {
  *response = http_response(200, gpr_strdup(good_jwk_set));
  GPR_ASSERT(request->handshaker == &grpc_httpcli_ssl);
  GPR_ASSERT(strcmp(request->host, "www.googleapis.com") == 0);
  GPR_ASSERT(strcmp(request->http.path, "/oauth2/v3/certs") == 0);
  grpc_closure_sched(exec_ctx, on_done, GRPC_ERROR_NONE);
  return 1;
}

static int httpcli_get_openid_config(grpc_exec_ctx *exec_ctx,
                                     const grpc_httpcli_request *request,
                                     gpr_timespec deadline,
                                     grpc_closure *on_done,
                                     grpc_httpcli_response *response) {
  *response = http_response(200, gpr_strdup(good_openid_config));
  GPR_ASSERT(request->handshaker == &grpc_httpcli_ssl);
  GPR_ASSERT(strcmp(request->host, "accounts.google.com") == 0);
  GPR_ASSERT(strcmp(request->http.path, GRPC_OPENID_CONFIG_URL_SUFFIX) == 0);
  grpc_httpcli_set_override(httpcli_get_jwk_set,
                            httpcli_post_should_not_be_called);
  grpc_closure_sched(exec_ctx, on_done, GRPC_ERROR_NONE);
  return 1;
}

static void test_jwt_verifier_url_issuer_success(void) {
  grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  grpc_jwt_verifier *verifier = grpc_jwt_verifier_create(NULL, 0);
  char *jwt = NULL;
  char *key_str = json_key_str(json_key_str_part3_for_url_issuer);
  grpc_auth_json_key key = grpc_auth_json_key_create_from_string(key_str);
  gpr_free(key_str);
  GPR_ASSERT(grpc_auth_json_key_is_valid(&key));
  grpc_httpcli_set_override(httpcli_get_openid_config,
                            httpcli_post_should_not_be_called);
  jwt = grpc_jwt_encode_and_sign(&key, expected_audience, expected_lifetime,
                                 NULL);
  grpc_auth_json_key_destruct(&key);
  GPR_ASSERT(jwt != NULL);
  grpc_jwt_verifier_verify(&exec_ctx, verifier, NULL, jwt, expected_audience,
                           on_verification_success, (void *)expected_user_data);
  grpc_exec_ctx_finish(&exec_ctx);
  gpr_free(jwt);
  grpc_jwt_verifier_destroy(verifier);
  grpc_httpcli_set_override(NULL, NULL);
}

static void on_verification_key_retrieval_error(void *user_data,
                                                grpc_jwt_verifier_status status,
                                                grpc_jwt_claims *claims) {
  GPR_ASSERT(status == GRPC_JWT_VERIFIER_KEY_RETRIEVAL_ERROR);
  GPR_ASSERT(claims == NULL);
  GPR_ASSERT(user_data == (void *)expected_user_data);
}

static int httpcli_get_bad_json(grpc_exec_ctx *exec_ctx,
                                const grpc_httpcli_request *request,
                                gpr_timespec deadline, grpc_closure *on_done,
                                grpc_httpcli_response *response) {
  *response = http_response(200, gpr_strdup("{\"bad\": \"stuff\"}"));
  GPR_ASSERT(request->handshaker == &grpc_httpcli_ssl);
  grpc_closure_sched(exec_ctx, on_done, GRPC_ERROR_NONE);
  return 1;
}

static void test_jwt_verifier_url_issuer_bad_config(void) {
  grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  grpc_jwt_verifier *verifier = grpc_jwt_verifier_create(NULL, 0);
  char *jwt = NULL;
  char *key_str = json_key_str(json_key_str_part3_for_url_issuer);
  grpc_auth_json_key key = grpc_auth_json_key_create_from_string(key_str);
  gpr_free(key_str);
  GPR_ASSERT(grpc_auth_json_key_is_valid(&key));
  grpc_httpcli_set_override(httpcli_get_bad_json,
                            httpcli_post_should_not_be_called);
  jwt = grpc_jwt_encode_and_sign(&key, expected_audience, expected_lifetime,
                                 NULL);
  grpc_auth_json_key_destruct(&key);
  GPR_ASSERT(jwt != NULL);
  grpc_jwt_verifier_verify(&exec_ctx, verifier, NULL, jwt, expected_audience,
                           on_verification_key_retrieval_error,
                           (void *)expected_user_data);
  grpc_exec_ctx_finish(&exec_ctx);
  gpr_free(jwt);
  grpc_jwt_verifier_destroy(verifier);
  grpc_httpcli_set_override(NULL, NULL);
}

static void test_jwt_verifier_bad_json_key(void) {
  grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  grpc_jwt_verifier *verifier = grpc_jwt_verifier_create(NULL, 0);
  char *jwt = NULL;
  char *key_str = json_key_str(json_key_str_part3_for_google_email_issuer);
  grpc_auth_json_key key = grpc_auth_json_key_create_from_string(key_str);
  gpr_free(key_str);
  GPR_ASSERT(grpc_auth_json_key_is_valid(&key));
  grpc_httpcli_set_override(httpcli_get_bad_json,
                            httpcli_post_should_not_be_called);
  jwt = grpc_jwt_encode_and_sign(&key, expected_audience, expected_lifetime,
                                 NULL);
  grpc_auth_json_key_destruct(&key);
  GPR_ASSERT(jwt != NULL);
  grpc_jwt_verifier_verify(&exec_ctx, verifier, NULL, jwt, expected_audience,
                           on_verification_key_retrieval_error,
                           (void *)expected_user_data);
  grpc_exec_ctx_finish(&exec_ctx);
  gpr_free(jwt);
  grpc_jwt_verifier_destroy(verifier);
  grpc_httpcli_set_override(NULL, NULL);
}

static void corrupt_jwt_sig(char *jwt) {
  grpc_slice sig;
  char *bad_b64_sig;
  uint8_t *sig_bytes;
  char *last_dot = strrchr(jwt, '.');
  GPR_ASSERT(last_dot != NULL);
  sig = grpc_base64_decode(last_dot + 1, 1);
  GPR_ASSERT(!GRPC_SLICE_IS_EMPTY(sig));
  sig_bytes = GRPC_SLICE_START_PTR(sig);
  (*sig_bytes)++; /* Corrupt first byte. */
  bad_b64_sig = grpc_base64_encode(GRPC_SLICE_START_PTR(sig),
                                   GRPC_SLICE_LENGTH(sig), 1, 0);
  memcpy(last_dot + 1, bad_b64_sig, strlen(bad_b64_sig));
  gpr_free(bad_b64_sig);
  grpc_slice_unref(sig);
}

static void on_verification_bad_signature(void *user_data,
                                          grpc_jwt_verifier_status status,
                                          grpc_jwt_claims *claims) {
  GPR_ASSERT(status == GRPC_JWT_VERIFIER_BAD_SIGNATURE);
  GPR_ASSERT(claims == NULL);
  GPR_ASSERT(user_data == (void *)expected_user_data);
}

static void test_jwt_verifier_bad_signature(void) {
  grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  grpc_jwt_verifier *verifier = grpc_jwt_verifier_create(NULL, 0);
  char *jwt = NULL;
  char *key_str = json_key_str(json_key_str_part3_for_url_issuer);
  grpc_auth_json_key key = grpc_auth_json_key_create_from_string(key_str);
  gpr_free(key_str);
  GPR_ASSERT(grpc_auth_json_key_is_valid(&key));
  grpc_httpcli_set_override(httpcli_get_openid_config,
                            httpcli_post_should_not_be_called);
  jwt = grpc_jwt_encode_and_sign(&key, expected_audience, expected_lifetime,
                                 NULL);
  grpc_auth_json_key_destruct(&key);
  corrupt_jwt_sig(jwt);
  GPR_ASSERT(jwt != NULL);
  grpc_jwt_verifier_verify(&exec_ctx, verifier, NULL, jwt, expected_audience,
                           on_verification_bad_signature,
                           (void *)expected_user_data);
  grpc_exec_ctx_finish(&exec_ctx);
  gpr_free(jwt);
  grpc_jwt_verifier_destroy(verifier);
  grpc_httpcli_set_override(NULL, NULL);
}

static int httpcli_get_should_not_be_called(grpc_exec_ctx *exec_ctx,
                                            const grpc_httpcli_request *request,
                                            gpr_timespec deadline,
                                            grpc_closure *on_done,
                                            grpc_httpcli_response *response) {
  GPR_ASSERT(0);
  return 1;
}

static void on_verification_bad_format(void *user_data,
                                       grpc_jwt_verifier_status status,
                                       grpc_jwt_claims *claims) {
  GPR_ASSERT(status == GRPC_JWT_VERIFIER_BAD_FORMAT);
  GPR_ASSERT(claims == NULL);
  GPR_ASSERT(user_data == (void *)expected_user_data);
}

static void test_jwt_verifier_bad_format(void) {
  grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  grpc_jwt_verifier *verifier = grpc_jwt_verifier_create(NULL, 0);
  grpc_httpcli_set_override(httpcli_get_should_not_be_called,
                            httpcli_post_should_not_be_called);
  grpc_jwt_verifier_verify(&exec_ctx, verifier, NULL, "bad jwt",
                           expected_audience, on_verification_bad_format,
                           (void *)expected_user_data);
  grpc_exec_ctx_finish(&exec_ctx);
  grpc_jwt_verifier_destroy(verifier);
  grpc_httpcli_set_override(NULL, NULL);
}

/* find verification key: bad jks, cannot find key in jks */
/* bad signature custom provided email*/
/* bad key */

int main(int argc, char **argv) {
  grpc_test_init(argc, argv);
  grpc_init();
  test_jwt_issuer_email_domain();
  test_claims_success();
  test_expired_claims_failure();
  test_invalid_claims_failure();
  test_bad_audience_claims_failure();
  test_bad_subject_claims_failure();
  test_jwt_verifier_google_email_issuer_success();
  test_jwt_verifier_custom_email_issuer_success();
  test_jwt_verifier_url_issuer_success();
  test_jwt_verifier_url_issuer_bad_config();
  test_jwt_verifier_bad_json_key();
  test_jwt_verifier_bad_signature();
  test_jwt_verifier_bad_format();
  grpc_shutdown();
  return 0;
}