| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 gRPC authors. |
| 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 |
*/ |
| 18 |
|
| 19 |
#include "src/core/lib/security/credentials/jwt/jwt_verifier.h" |
| 20 |
|
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
#include <gtest/gtest.h> |
| 24 |
|
| 25 |
#include <grpc/grpc.h> |
| 26 |
#include <grpc/slice.h> |
| 27 |
#include <grpc/support/alloc.h> |
| 28 |
#include <grpc/support/log.h> |
| 29 |
#include <grpc/support/string_util.h> |
| 30 |
|
| 31 |
#include "src/core/lib/http/httpcli.h" |
| 32 |
#include "src/core/lib/security/credentials/jwt/json_token.h" |
| 33 |
#include "src/core/lib/slice/b64.h" |
| 34 |
#include "test/core/util/test_config.h" |
| 35 |
|
| 36 |
using grpc_core::Json; |
| 37 |
|
| 38 |
/* This JSON key was generated with the GCE console and revoked immediately. |
| 39 |
The identifiers have been changed as well. |
| 40 |
Maximum size for a string literal is 509 chars in C89, yay! */ |
| 41 |
static const char json_key_str_part1[] = |
| 42 |
"{ \"private_key\": \"-----BEGIN PRIVATE KEY-----" |
| 43 |
"\\nMIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAOEvJsnoHnyHkXcp\\n7mJE" |
| 44 |
"qg" |
| 45 |
"WGjiw71NfXByguekSKho65FxaGbsnSM9SMQAqVk7Q2rG+I0OpsT0LrWQtZ\\nyjSeg/" |
| 46 |
"rWBQvS4hle4LfijkP3J5BG+" |
| 47 |
"IXDMP8RfziNRQsenAXDNPkY4kJCvKux2xdD\\nOnVF6N7dL3nTYZg+" |
| 48 |
"uQrNsMTz9UxVAgMBAAECgYEAzbLewe1xe9vy+2GoSsfib+28\\nDZgSE6Bu/" |
| 49 |
"zuFoPrRc6qL9p2SsnV7txrunTyJkkOnPLND9ABAXybRTlcVKP/sGgza\\n/" |
| 50 |
"8HpCqFYM9V8f34SBWfD4fRFT+n/" |
| 51 |
"73cfRUtGXdXpseva2lh8RilIQfPhNZAncenU\\ngqXjDvpkypEusgXAykECQQD+"; |
| 52 |
static const char json_key_str_part2[] = |
| 53 |
"53XxNVnxBHsYb+AYEfklR96yVi8HywjVHP34+OQZ\\nCslxoHQM8s+" |
| 54 |
"dBnjfScLu22JqkPv04xyxmt0QAKm9+vTdAkEA4ib7YvEAn2jXzcCI\\nEkoy2L/" |
| 55 |
"XydR1GCHoacdfdAwiL2npOdnbvi4ZmdYRPY1LSTO058tQHKVXV7NLeCa3\\nAARh2QJBAMKeDA" |
| 56 |
"G" |
| 57 |
"W303SQv2cZTdbeaLKJbB5drz3eo3j7dDKjrTD9JupixFbzcGw\\n8FZi5c8idxiwC36kbAL6Hz" |
| 58 |
"A" |
| 59 |
"ZoX+ofI0CQE6KCzPJTtYNqyShgKAZdJ8hwOcvCZtf\\n6z8RJm0+" |
| 60 |
"6YBd38lfh5j8mZd7aHFf6I17j5AQY7oPEc47TjJj/" |
| 61 |
"5nZ68ECQQDvYuI3\\nLyK5fS8g0SYbmPOL9TlcHDOqwG0mrX9qpg5DC2fniXNSrrZ64GTDKdzZ" |
| 62 |
"Y" |
| 63 |
"Ap6LI9W\\nIqv4vr6y38N79TTC\\n-----END PRIVATE KEY-----\\n\", "; |
| 64 |
static const char json_key_str_part3_for_google_email_issuer[] = |
| 65 |
"\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", " |
| 66 |
"\"client_email\": " |
| 67 |
"\"[email protected]." |
| 68 |
"com\", \"client_id\": " |
| 69 |
"\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent." |
| 70 |
"com\", \"type\": \"service_account\" }"; |
| 71 |
/* Trick our JWT library into issuing a JWT with iss=accounts.google.com. */ |
| 72 |
static const char json_key_str_part3_for_url_issuer[] = |
| 73 |
"\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", " |
| 74 |
"\"client_email\": \"accounts.google.com\", " |
| 75 |
"\"client_id\": " |
| 76 |
"\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent." |
| 77 |
"com\", \"type\": \"service_account\" }"; |
| 78 |
static const char json_key_str_part3_for_custom_email_issuer[] = |
| 79 |
"\"private_key_id\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\", " |
| 80 |
"\"client_email\": " |
| 81 |
"\"[email protected]\", \"client_id\": " |
| 82 |
"\"777-abaslkan11hlb6nmim3bpspl31ud.apps.googleusercontent." |
| 83 |
"com\", \"type\": \"service_account\" }"; |
| 84 |
|
| 85 |
static grpc_jwt_verifier_email_domain_key_url_mapping custom_mapping = { |
| 86 |
"bar.com", "keys.bar.com/jwk"}; |
| 87 |
|
| 88 |
static const char expected_user_data[] = "user data"; |
| 89 |
|
| 90 |
static const char good_jwk_set[] = |
| 91 |
"{" |
| 92 |
" \"keys\": [" |
| 93 |
" {" |
| 94 |
" \"kty\": \"RSA\"," |
| 95 |
" \"alg\": \"RS256\"," |
| 96 |
" \"use\": \"sig\"," |
| 97 |
" \"kid\": \"e6b5137873db8d2ef81e06a47289e6434ec8a165\"," |
| 98 |
" \"n\": " |
| 99 |
"\"4S8myegefIeRdynuYkSqBYaOLDvU19cHKC56RIqGjrkXFoZuydIz1IxACpWTtDasb4jQ6mxP" |
| 100 |
"QutZC1nKNJ6D-tYFC9LiGV7gt-KOQ_cnkEb4hcMw_xF_OI1FCx6cBcM0-" |
| 101 |
"RjiQkK8q7HbF0M6dUXo3t0vedNhmD65Cs2wxPP1TFU=\"," |
| 102 |
" \"e\": \"AQAB\"" |
| 103 |
" }" |
| 104 |
" ]" |
| 105 |
"}"; |
| 106 |
|
| 107 |
static gpr_timespec expected_lifetime = {3600, 0, GPR_TIMESPAN}; |
| 108 |
|
| 109 |
static const char good_google_email_keys_part1[] = |
| 110 |
"{\"e6b5137873db8d2ef81e06a47289e6434ec8a165\": \"-----BEGIN " |
| 111 |
"CERTIFICATE-----" |
| 112 |
"\\nMIICATCCAWoCCQDEywLhxvHjnDANBgkqhkiG9w0BAQsFADBFMQswCQYDVQQGEwJB\\nVTET" |
| 113 |
"MBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0\\ncyBQdHkgTHR" |
| 114 |
"kMB4XDTE1MDYyOTA4Mzk1MFoXDTI1MDYyNjA4Mzk1MFowRTELMAkG\\nA1UEBhMCQVUxEzARBg" |
| 115 |
"NVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0\\nIFdpZGdpdHMgUHR5IEx0ZDCBn" |
| 116 |
"zANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA4S8m\\nyegefIeRdynuYkSqBYaOLDvU19cHKC56" |
| 117 |
"RIqGjrkXFoZuydIz1IxACpWTtDasb4jQ\\n6mxPQutZC1nKNJ6D+tYFC9LiGV7gt+KOQ/"; |
| 118 |
|
| 119 |
static const char good_google_email_keys_part2[] = |
| 120 |
"cnkEb4hcMw/xF/OI1FCx6cBcM0+" |
| 121 |
"Rji\\nQkK8q7HbF0M6dUXo3t0vedNhmD65Cs2wxPP1TFUCAwEAATANBgkqhkiG9w0BAQsF\\nA" |
| 122 |
"AOBgQBfu69FkPmBknbKNFgurPz78kbs3VNN+k/" |
| 123 |
"PUgO5DHKskJmgK2TbtvX2VMpx\\nkftmHGzgzMzUlOtigCaGMgHWjfqjpP9uuDbahXrZBJzB8c" |
| 124 |
"Oq7MrQF8r17qVvo3Ue\\nPjTKQMAsU8uxTEMmeuz9L6yExs0rfd6bPOrQkAoVfFfiYB3/" |
| 125 |
"pA==\\n-----END CERTIFICATE-----\\n\"}"; |
| 126 |
|
| 127 |
static const char expected_audience[] = "https://foo.com"; |
| 128 |
|
| 129 |
static const char good_openid_config[] = |
| 130 |
"{" |
| 131 |
" \"issuer\": \"https://accounts.google.com\"," |
| 132 |
" \"authorization_endpoint\": " |
| 133 |
"\"https://accounts.google.com/o/oauth2/v2/auth\"," |
| 134 |
" \"token_endpoint\": \"https://oauth2.googleapis.com/token\"," |
| 135 |
" \"userinfo_endpoint\": \"https://www.googleapis.com/oauth2/v3/userinfo\"," |
| 136 |
" \"revocation_endpoint\": \"https://oauth2.googleapis.com/revoke\"," |
| 137 |
" \"jwks_uri\": \"https://www.googleapis.com/oauth2/v3/certs\"" |
| 138 |
"}"; |
| 139 |
|
| 140 |
static const char expired_claims[] = |
| 141 |
"{ \"aud\": \"https://foo.com\"," |
| 142 |
" \"iss\": \"blah.foo.com\"," |
| 143 |
" \"sub\": \"[email protected]\"," |
| 144 |
" \"jti\": \"jwtuniqueid\"," |
| 145 |
" \"iat\": 100," /* Way back in the past... */ |
| 146 |
" \"exp\": 120," |
| 147 |
" \"nbf\": 60," |
| 148 |
" \"foo\": \"bar\"}"; |
| 149 |
|
| 150 |
static const char claims_without_time_constraint[] = |
| 151 |
"{ \"aud\": \"https://foo.com\"," |
| 152 |
" \"iss\": \"blah.foo.com\"," |
| 153 |
" \"sub\": \"[email protected]\"," |
| 154 |
" \"jti\": \"jwtuniqueid\"," |
| 155 |
" \"foo\": \"bar\"}"; |
| 156 |
|
| 157 |
static const char claims_with_bad_subject[] = |
| 158 |
"{ \"aud\": \"https://foo.com\"," |
| 159 |
" \"iss\": \"[email protected]\"," |
| 160 |
" \"sub\": \"[email protected]\"," |
| 161 |
" \"jti\": \"jwtuniqueid\"," |
| 162 |
" \"foo\": \"bar\"}"; |
| 163 |
|
| 164 |
static const char invalid_claims[] = |
| 165 |
"{ \"aud\": \"https://foo.com\"," |
| 166 |
" \"iss\": 46," /* Issuer cannot be a number. */ |
| 167 |
" \"sub\": \"[email protected]\"," |
| 168 |
" \"jti\": \"jwtuniqueid\"," |
| 169 |
" \"foo\": \"bar\"}"; |
| 170 |
|
| 171 |
typedef struct { |
| 172 |
grpc_jwt_verifier_status expected_status; |
| 173 |
const char* expected_issuer; |
| 174 |
const char* expected_subject; |
| 175 |
} verifier_test_config; |
| 176 |
|
| 177 |
TEST(JwtVerifierTest, JwtIssuerEmailDomain) { |
| 178 |
const char* d = grpc_jwt_issuer_email_domain("https://foo.com"); |
| 179 |
ASSERT_EQ(d, nullptr); |
| 180 |
d = grpc_jwt_issuer_email_domain("foo.com"); |
| 181 |
ASSERT_EQ(d, nullptr); |
| 182 |
d = grpc_jwt_issuer_email_domain(""); |
| 183 |
ASSERT_EQ(d, nullptr); |
| 184 |
d = grpc_jwt_issuer_email_domain("@"); |
| 185 |
ASSERT_EQ(d, nullptr); |
| 186 |
d = grpc_jwt_issuer_email_domain("bar@foo"); |
| 187 |
ASSERT_STREQ(d, "foo"); |
| 188 |
d = grpc_jwt_issuer_email_domain("[email protected]"); |
| 189 |
ASSERT_STREQ(d, "foo.com"); |
| 190 |
d = grpc_jwt_issuer_email_domain("[email protected]"); |
| 191 |
ASSERT_STREQ(d, "foo.com"); |
| 192 |
d = grpc_jwt_issuer_email_domain("[email protected]"); |
| 193 |
ASSERT_STREQ(d, "foo.com"); |
| 194 |
d = grpc_jwt_issuer_email_domain("[email protected]"); |
| 195 |
ASSERT_STREQ(d, "foo.com"); |
| 196 |
|
| 197 |
/* This is not a very good parser but make sure we do not crash on these weird |
| 198 |
inputs. */ |
| 199 |
d = grpc_jwt_issuer_email_domain("@foo"); |
| 200 |
ASSERT_STREQ(d, "foo"); |
| 201 |
d = grpc_jwt_issuer_email_domain("bar@."); |
| 202 |
ASSERT_NE(d, nullptr); |
| 203 |
d = grpc_jwt_issuer_email_domain("bar@.."); |
| 204 |
ASSERT_NE(d, nullptr); |
| 205 |
d = grpc_jwt_issuer_email_domain("bar@..."); |
| 206 |
ASSERT_NE(d, nullptr); |
| 207 |
} |
| 208 |
|
| 209 |
TEST(JwtVerifierTest, ClaimsSuccess) { |
| 210 |
grpc_jwt_claims* claims; |
| 211 |
auto json = Json::Parse(claims_without_time_constraint); |
| 212 |
ASSERT_TRUE(json.ok()) << json.status(); |
| 213 |
ASSERT_EQ(json->type(), Json::Type::OBJECT); |
| 214 |
grpc_core::ExecCtx exec_ctx; |
| 215 |
claims = grpc_jwt_claims_from_json(*json); |
| 216 |
ASSERT_NE(claims, nullptr); |
| 217 |
ASSERT_EQ(*grpc_jwt_claims_json(claims), *json); |
| 218 |
ASSERT_STREQ(grpc_jwt_claims_audience(claims), "https://foo.com"); |
| 219 |
ASSERT_STREQ(grpc_jwt_claims_issuer(claims), "blah.foo.com"); |
| 220 |
ASSERT_STREQ(grpc_jwt_claims_subject(claims), "[email protected]"); |
| 221 |
ASSERT_STREQ(grpc_jwt_claims_id(claims), "jwtuniqueid"); |
| 222 |
ASSERT_EQ(grpc_jwt_claims_check(claims, "https://foo.com"), |
| 223 |
GRPC_JWT_VERIFIER_OK); |
| 224 |
grpc_jwt_claims_destroy(claims); |
| 225 |
} |
| 226 |
|
| 227 |
TEST(JwtVerifierTest, ExpiredClaimsFailure) { |
| 228 |
grpc_jwt_claims* claims; |
| 229 |
auto json = Json::Parse(expired_claims); |
| 230 |
ASSERT_TRUE(json.ok()) << json.status(); |
| 231 |
ASSERT_EQ(json->type(), Json::Type::OBJECT); |
| 232 |
gpr_timespec exp_iat = {100, 0, GPR_CLOCK_REALTIME}; |
| 233 |
gpr_timespec exp_exp = {120, 0, GPR_CLOCK_REALTIME}; |
| 234 |
gpr_timespec exp_nbf = {60, 0, GPR_CLOCK_REALTIME}; |
| 235 |
grpc_core::ExecCtx exec_ctx; |
| 236 |
claims = grpc_jwt_claims_from_json(*json); |
| 237 |
ASSERT_NE(claims, nullptr); |
| 238 |
ASSERT_EQ(*grpc_jwt_claims_json(claims), *json); |
| 239 |
ASSERT_STREQ(grpc_jwt_claims_audience(claims), "https://foo.com"); |
| 240 |
ASSERT_STREQ(grpc_jwt_claims_issuer(claims), "blah.foo.com"); |
| 241 |
ASSERT_STREQ(grpc_jwt_claims_subject(claims), "[email protected]"); |
| 242 |
ASSERT_STREQ(grpc_jwt_claims_id(claims), "jwtuniqueid"); |
| 243 |
ASSERT_EQ(gpr_time_cmp(grpc_jwt_claims_issued_at(claims), exp_iat), 0); |
| 244 |
ASSERT_EQ(gpr_time_cmp(grpc_jwt_claims_expires_at(claims), exp_exp), 0); |
| 245 |
ASSERT_EQ(gpr_time_cmp(grpc_jwt_claims_not_before(claims), exp_nbf), 0); |
| 246 |
ASSERT_EQ(grpc_jwt_claims_check(claims, "https://foo.com"), |
| 247 |
GRPC_JWT_VERIFIER_TIME_CONSTRAINT_FAILURE); |
| 248 |
grpc_jwt_claims_destroy(claims); |
| 249 |
} |
| 250 |
|
| 251 |
TEST(JwtVerifierTest, InvalidClaimsFailure) { |
| 252 |
auto json = Json::Parse(invalid_claims); |
| 253 |
ASSERT_TRUE(json.ok()) << json.status(); |
| 254 |
ASSERT_EQ(json->type(), Json::Type::OBJECT); |
| 255 |
grpc_core::ExecCtx exec_ctx; |
| 256 |
ASSERT_EQ(grpc_jwt_claims_from_json(*json), nullptr); |
| 257 |
} |
| 258 |
|
| 259 |
TEST(JwtVerifierTest, BadAudienceClaimsFailure) { |
| 260 |
grpc_jwt_claims* claims; |
| 261 |
auto json = Json::Parse(claims_without_time_constraint); |
| 262 |
ASSERT_TRUE(json.ok()) << json.status(); |
| 263 |
ASSERT_EQ(json->type(), Json::Type::OBJECT); |
| 264 |
grpc_core::ExecCtx exec_ctx; |
| 265 |
claims = grpc_jwt_claims_from_json(*json); |
| 266 |
ASSERT_NE(claims, nullptr); |
| 267 |
ASSERT_EQ(grpc_jwt_claims_check(claims, "https://bar.com"), |
| 268 |
GRPC_JWT_VERIFIER_BAD_AUDIENCE); |
| 269 |
grpc_jwt_claims_destroy(claims); |
| 270 |
} |
| 271 |
|
| 272 |
TEST(JwtVerifierTest, BadSubjectClaimsFailure) { |
| 273 |
grpc_jwt_claims* claims; |
| 274 |
auto json = Json::Parse(claims_with_bad_subject); |
| 275 |
ASSERT_TRUE(json.ok()) << json.status(); |
| 276 |
ASSERT_EQ(json->type(), Json::Type::OBJECT); |
| 277 |
grpc_core::ExecCtx exec_ctx; |
| 278 |
claims = grpc_jwt_claims_from_json(*json); |
| 279 |
ASSERT_NE(claims, nullptr); |
| 280 |
ASSERT_EQ(grpc_jwt_claims_check(claims, "https://foo.com"), |
| 281 |
GRPC_JWT_VERIFIER_BAD_SUBJECT); |
| 282 |
grpc_jwt_claims_destroy(claims); |
| 283 |
} |
| 284 |
|
| 285 |
static char* json_key_str(const char* last_part) { |
| 286 |
size_t result_len = strlen(json_key_str_part1) + strlen(json_key_str_part2) + |
| 287 |
strlen(last_part); |
| 288 |
char* result = static_cast<char*>(gpr_malloc(result_len + 1)); |
| 289 |
char* current = result; |
| 290 |
strcpy(result, json_key_str_part1); |
| 291 |
current += strlen(json_key_str_part1); |
| 292 |
strcpy(current, json_key_str_part2); |
| 293 |
current += strlen(json_key_str_part2); |
| 294 |
strcpy(current, last_part); |
| 295 |
return result; |
| 296 |
} |
| 297 |
|
| 298 |
static char* good_google_email_keys(void) { |
| 299 |
size_t result_len = strlen(good_google_email_keys_part1) + |
| 300 |
strlen(good_google_email_keys_part2); |
| 301 |
char* result = static_cast<char*>(gpr_malloc(result_len + 1)); |
| 302 |
char* current = result; |
| 303 |
strcpy(result, good_google_email_keys_part1); |
| 304 |
current += strlen(good_google_email_keys_part1); |
| 305 |
strcpy(current, good_google_email_keys_part2); |
| 306 |
return result; |
| 307 |
} |
| 308 |
|
| 309 |
static grpc_http_response http_response(int status, char* body) { |
| 310 |
grpc_http_response response; |
| 311 |
response = {}; |
| 312 |
response.status = status; |
| 313 |
response.body = body; |
| 314 |
response.body_length = strlen(body); |
| 315 |
return response; |
| 316 |
} |
| 317 |
|
| 318 |
static int httpcli_post_should_not_be_called( |
| 319 |
const grpc_http_request* /*request*/, const char* /*host*/, |
| 320 |
const char* /*path*/, const char* /*body_bytes*/, size_t /*body_size*/, |
| 321 |
grpc_core::Timestamp /*deadline*/, grpc_closure* /*on_done*/, |
| 322 |
grpc_http_response* /*response*/) { |
| 323 |
EXPECT_EQ("HTTP POST should not be called", nullptr); |
| 324 |
return 1; |
| 325 |
} |
| 326 |
|
| 327 |
static int httpcli_put_should_not_be_called( |
| 328 |
const grpc_http_request* /*request*/, const char* /*host*/, |
| 329 |
const char* /*path*/, const char* /*body_bytes*/, size_t /*body_size*/, |
| 330 |
grpc_core::Timestamp /*deadline*/, grpc_closure* /*on_done*/, |
| 331 |
grpc_http_response* /*response*/) { |
| 332 |
EXPECT_EQ("HTTP PUT should not be called", nullptr); |
| 333 |
return 1; |
| 334 |
} |
| 335 |
|
| 336 |
static int httpcli_get_google_keys_for_email( |
| 337 |
const grpc_http_request* /*request*/, const char* host, const char* path, |
| 338 |
grpc_core::Timestamp /*deadline*/, grpc_closure* on_done, |
| 339 |
grpc_http_response* response) { |
| 340 |
*response = http_response(200, good_google_email_keys()); |
| 341 |
EXPECT_STREQ(host, "www.googleapis.com"); |
| 342 |
EXPECT_STREQ(path, |
| 343 |
"/robot/v1/metadata/x509/" |
| 344 |
"777-abaslkan11hlb6nmim3bpspl31ud@developer." |
| 345 |
"gserviceaccount.com"); |
| 346 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, on_done, absl::OkStatus()); |
| 347 |
return 1; |
| 348 |
} |
| 349 |
|
| 350 |
static void on_verification_success(void* user_data, |
| 351 |
grpc_jwt_verifier_status status, |
| 352 |
grpc_jwt_claims* claims) { |
| 353 |
ASSERT_EQ(status, GRPC_JWT_VERIFIER_OK); |
| 354 |
ASSERT_NE(claims, nullptr); |
| 355 |
ASSERT_EQ(user_data, (void*)expected_user_data); |
| 356 |
ASSERT_STREQ(grpc_jwt_claims_audience(claims), expected_audience); |
| 357 |
grpc_jwt_claims_destroy(claims); |
| 358 |
} |
| 359 |
|
| 360 |
TEST(JwtVerifierTest, JwtVerifierGoogleEmailIssuerSuccess) { |
| 361 |
grpc_core::ExecCtx exec_ctx; |
| 362 |
grpc_jwt_verifier* verifier = grpc_jwt_verifier_create(nullptr, 0); |
| 363 |
char* jwt = nullptr; |
| 364 |
char* key_str = json_key_str(json_key_str_part3_for_google_email_issuer); |
| 365 |
grpc_auth_json_key key = grpc_auth_json_key_create_from_string(key_str); |
| 366 |
gpr_free(key_str); |
| 367 |
ASSERT_TRUE(grpc_auth_json_key_is_valid(&key)); |
| 368 |
grpc_core::HttpRequest::SetOverride(httpcli_get_google_keys_for_email, |
| 369 |
httpcli_post_should_not_be_called, |
| 370 |
httpcli_put_should_not_be_called); |
| 371 |
jwt = grpc_jwt_encode_and_sign(&key, expected_audience, expected_lifetime, |
| 372 |
nullptr); |
| 373 |
grpc_auth_json_key_destruct(&key); |
| 374 |
ASSERT_NE(jwt, nullptr); |
| 375 |
grpc_jwt_verifier_verify(verifier, nullptr, jwt, expected_audience, |
| 376 |
on_verification_success, |
| 377 |
const_cast<char*>(expected_user_data)); |
| 378 |
grpc_jwt_verifier_destroy(verifier); |
| 379 |
grpc_core::ExecCtx::Get()->Flush(); |
| 380 |
gpr_free(jwt); |
| 381 |
grpc_core::HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 382 |
} |
| 383 |
|
| 384 |
static int httpcli_get_custom_keys_for_email( |
| 385 |
const grpc_http_request* /*request*/, const char* host, const char* path, |
| 386 |
grpc_core::Timestamp /*deadline*/, grpc_closure* on_done, |
| 387 |
grpc_http_response* response) { |
| 388 |
*response = http_response(200, gpr_strdup(good_jwk_set)); |
| 389 |
EXPECT_STREQ(host, "keys.bar.com"); |
| 390 |
EXPECT_STREQ(path, "/jwk/[email protected]"); |
| 391 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, on_done, absl::OkStatus()); |
| 392 |
return 1; |
| 393 |
} |
| 394 |
|
| 395 |
TEST(JwtVerifierTest, JwtVerifierCustomEmailIssuerSuccess) { |
| 396 |
grpc_core::ExecCtx exec_ctx; |
| 397 |
grpc_jwt_verifier* verifier = grpc_jwt_verifier_create(&custom_mapping, 1); |
| 398 |
char* jwt = nullptr; |
| 399 |
char* key_str = json_key_str(json_key_str_part3_for_custom_email_issuer); |
| 400 |
grpc_auth_json_key key = grpc_auth_json_key_create_from_string(key_str); |
| 401 |
gpr_free(key_str); |
| 402 |
ASSERT_TRUE(grpc_auth_json_key_is_valid(&key)); |
| 403 |
grpc_core::HttpRequest::SetOverride(httpcli_get_custom_keys_for_email, |
| 404 |
httpcli_post_should_not_be_called, |
| 405 |
httpcli_put_should_not_be_called); |
| 406 |
jwt = grpc_jwt_encode_and_sign(&key, expected_audience, expected_lifetime, |
| 407 |
nullptr); |
| 408 |
grpc_auth_json_key_destruct(&key); |
| 409 |
ASSERT_NE(jwt, nullptr); |
| 410 |
grpc_jwt_verifier_verify(verifier, nullptr, jwt, expected_audience, |
| 411 |
on_verification_success, |
| 412 |
const_cast<char*>(expected_user_data)); |
| 413 |
grpc_jwt_verifier_destroy(verifier); |
| 414 |
grpc_core::ExecCtx::Get()->Flush(); |
| 415 |
gpr_free(jwt); |
| 416 |
grpc_core::HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 417 |
} |
| 418 |
|
| 419 |
static int httpcli_get_jwk_set(const grpc_http_request* /*request*/, |
| 420 |
const char* host, const char* path, |
| 421 |
grpc_core::Timestamp /*deadline*/, |
| 422 |
grpc_closure* on_done, |
| 423 |
grpc_http_response* response) { |
| 424 |
*response = http_response(200, gpr_strdup(good_jwk_set)); |
| 425 |
EXPECT_STREQ(host, "www.googleapis.com"); |
| 426 |
EXPECT_STREQ(path, "/oauth2/v3/certs"); |
| 427 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, on_done, absl::OkStatus()); |
| 428 |
return 1; |
| 429 |
} |
| 430 |
|
| 431 |
static int httpcli_get_openid_config(const grpc_http_request* /*request*/, |
| 432 |
const char* host, const char* path, |
| 433 |
grpc_core::Timestamp /*deadline*/, |
| 434 |
grpc_closure* on_done, |
| 435 |
grpc_http_response* response) { |
| 436 |
*response = http_response(200, gpr_strdup(good_openid_config)); |
| 437 |
EXPECT_STREQ(host, "accounts.google.com"); |
| 438 |
EXPECT_STREQ(path, GRPC_OPENID_CONFIG_URL_SUFFIX); |
| 439 |
grpc_core::HttpRequest::SetOverride(httpcli_get_jwk_set, |
| 440 |
httpcli_post_should_not_be_called, |
| 441 |
httpcli_put_should_not_be_called); |
| 442 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, on_done, absl::OkStatus()); |
| 443 |
return 1; |
| 444 |
} |
| 445 |
|
| 446 |
TEST(JwtVerifierTest, JwtVerifierUrlIssuerSuccess) { |
| 447 |
grpc_core::ExecCtx exec_ctx; |
| 448 |
grpc_jwt_verifier* verifier = grpc_jwt_verifier_create(nullptr, 0); |
| 449 |
char* jwt = nullptr; |
| 450 |
char* key_str = json_key_str(json_key_str_part3_for_url_issuer); |
| 451 |
grpc_auth_json_key key = grpc_auth_json_key_create_from_string(key_str); |
| 452 |
gpr_free(key_str); |
| 453 |
ASSERT_TRUE(grpc_auth_json_key_is_valid(&key)); |
| 454 |
grpc_core::HttpRequest::SetOverride(httpcli_get_openid_config, |
| 455 |
httpcli_post_should_not_be_called, |
| 456 |
httpcli_put_should_not_be_called); |
| 457 |
jwt = grpc_jwt_encode_and_sign(&key, expected_audience, expected_lifetime, |
| 458 |
nullptr); |
| 459 |
grpc_auth_json_key_destruct(&key); |
| 460 |
ASSERT_NE(jwt, nullptr); |
| 461 |
grpc_jwt_verifier_verify(verifier, nullptr, jwt, expected_audience, |
| 462 |
on_verification_success, |
| 463 |
const_cast<char*>(expected_user_data)); |
| 464 |
grpc_jwt_verifier_destroy(verifier); |
| 465 |
grpc_core::ExecCtx::Get()->Flush(); |
| 466 |
gpr_free(jwt); |
| 467 |
grpc_core::HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 468 |
} |
| 469 |
|
| 470 |
static void on_verification_key_retrieval_error(void* user_data, |
| 471 |
grpc_jwt_verifier_status status, |
| 472 |
grpc_jwt_claims* claims) { |
| 473 |
ASSERT_EQ(status, GRPC_JWT_VERIFIER_KEY_RETRIEVAL_ERROR); |
| 474 |
ASSERT_EQ(claims, nullptr); |
| 475 |
ASSERT_EQ(user_data, (void*)expected_user_data); |
| 476 |
} |
| 477 |
|
| 478 |
static int httpcli_get_bad_json(const grpc_http_request* /* request */, |
| 479 |
const char* /*host*/, const char* /*path*/, |
| 480 |
grpc_core::Timestamp /*deadline*/, |
| 481 |
grpc_closure* on_done, |
| 482 |
grpc_http_response* response) { |
| 483 |
*response = http_response(200, gpr_strdup("{\"bad\": \"stuff\"}")); |
| 484 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, on_done, absl::OkStatus()); |
| 485 |
return 1; |
| 486 |
} |
| 487 |
|
| 488 |
TEST(JwtVerifierTest, JwtVerifierUrlIssuerBadConfig) { |
| 489 |
grpc_core::ExecCtx exec_ctx; |
| 490 |
grpc_jwt_verifier* verifier = grpc_jwt_verifier_create(nullptr, 0); |
| 491 |
char* jwt = nullptr; |
| 492 |
char* key_str = json_key_str(json_key_str_part3_for_url_issuer); |
| 493 |
grpc_auth_json_key key = grpc_auth_json_key_create_from_string(key_str); |
| 494 |
gpr_free(key_str); |
| 495 |
ASSERT_TRUE(grpc_auth_json_key_is_valid(&key)); |
| 496 |
grpc_core::HttpRequest::SetOverride(httpcli_get_bad_json, |
| 497 |
httpcli_post_should_not_be_called, |
| 498 |
httpcli_put_should_not_be_called); |
| 499 |
jwt = grpc_jwt_encode_and_sign(&key, expected_audience, expected_lifetime, |
| 500 |
nullptr); |
| 501 |
grpc_auth_json_key_destruct(&key); |
| 502 |
ASSERT_NE(jwt, nullptr); |
| 503 |
grpc_jwt_verifier_verify(verifier, nullptr, jwt, expected_audience, |
| 504 |
on_verification_key_retrieval_error, |
| 505 |
const_cast<char*>(expected_user_data)); |
| 506 |
grpc_jwt_verifier_destroy(verifier); |
| 507 |
grpc_core::ExecCtx::Get()->Flush(); |
| 508 |
gpr_free(jwt); |
| 509 |
grpc_core::HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 510 |
} |
| 511 |
|
| 512 |
TEST(JwtVerifierTest, JwtVerifierBadJsonKey) { |
| 513 |
grpc_core::ExecCtx exec_ctx; |
| 514 |
grpc_jwt_verifier* verifier = grpc_jwt_verifier_create(nullptr, 0); |
| 515 |
char* jwt = nullptr; |
| 516 |
char* key_str = json_key_str(json_key_str_part3_for_google_email_issuer); |
| 517 |
grpc_auth_json_key key = grpc_auth_json_key_create_from_string(key_str); |
| 518 |
gpr_free(key_str); |
| 519 |
ASSERT_TRUE(grpc_auth_json_key_is_valid(&key)); |
| 520 |
grpc_core::HttpRequest::SetOverride(httpcli_get_bad_json, |
| 521 |
httpcli_post_should_not_be_called, |
| 522 |
httpcli_put_should_not_be_called); |
| 523 |
jwt = grpc_jwt_encode_and_sign(&key, expected_audience, expected_lifetime, |
| 524 |
nullptr); |
| 525 |
grpc_auth_json_key_destruct(&key); |
| 526 |
ASSERT_NE(jwt, nullptr); |
| 527 |
grpc_jwt_verifier_verify(verifier, nullptr, jwt, expected_audience, |
| 528 |
on_verification_key_retrieval_error, |
| 529 |
const_cast<char*>(expected_user_data)); |
| 530 |
grpc_jwt_verifier_destroy(verifier); |
| 531 |
grpc_core::ExecCtx::Get()->Flush(); |
| 532 |
gpr_free(jwt); |
| 533 |
grpc_core::HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 534 |
} |
| 535 |
|
| 536 |
static void corrupt_jwt_sig(char* jwt) { |
| 537 |
grpc_slice sig; |
| 538 |
char* bad_b64_sig; |
| 539 |
uint8_t* sig_bytes; |
| 540 |
char* last_dot = strrchr(jwt, '.'); |
| 541 |
ASSERT_NE(last_dot, nullptr); |
| 542 |
{ |
| 543 |
grpc_core::ExecCtx exec_ctx; |
| 544 |
sig = grpc_base64_decode(last_dot + 1, 1); |
| 545 |
} |
| 546 |
ASSERT_FALSE(GRPC_SLICE_IS_EMPTY(sig)); |
| 547 |
sig_bytes = GRPC_SLICE_START_PTR(sig); |
| 548 |
(*sig_bytes)++; /* Corrupt first byte. */ |
| 549 |
bad_b64_sig = grpc_base64_encode(GRPC_SLICE_START_PTR(sig), |
| 550 |
GRPC_SLICE_LENGTH(sig), 1, 0); |
| 551 |
memcpy(last_dot + 1, bad_b64_sig, strlen(bad_b64_sig)); |
| 552 |
gpr_free(bad_b64_sig); |
| 553 |
grpc_slice_unref(sig); |
| 554 |
} |
| 555 |
|
| 556 |
static void on_verification_bad_signature(void* user_data, |
| 557 |
grpc_jwt_verifier_status status, |
| 558 |
grpc_jwt_claims* claims) { |
| 559 |
ASSERT_EQ(status, GRPC_JWT_VERIFIER_BAD_SIGNATURE); |
| 560 |
ASSERT_EQ(claims, nullptr); |
| 561 |
ASSERT_EQ(user_data, (void*)expected_user_data); |
| 562 |
} |
| 563 |
|
| 564 |
TEST(JwtVerifierTest, JwtVerifierBadSignature) { |
| 565 |
grpc_core::ExecCtx exec_ctx; |
| 566 |
grpc_jwt_verifier* verifier = grpc_jwt_verifier_create(nullptr, 0); |
| 567 |
char* jwt = nullptr; |
| 568 |
char* key_str = json_key_str(json_key_str_part3_for_url_issuer); |
| 569 |
grpc_auth_json_key key = grpc_auth_json_key_create_from_string(key_str); |
| 570 |
gpr_free(key_str); |
| 571 |
ASSERT_TRUE(grpc_auth_json_key_is_valid(&key)); |
| 572 |
grpc_core::HttpRequest::SetOverride(httpcli_get_openid_config, |
| 573 |
httpcli_post_should_not_be_called, |
| 574 |
httpcli_put_should_not_be_called); |
| 575 |
jwt = grpc_jwt_encode_and_sign(&key, expected_audience, expected_lifetime, |
| 576 |
nullptr); |
| 577 |
grpc_auth_json_key_destruct(&key); |
| 578 |
corrupt_jwt_sig(jwt); |
| 579 |
ASSERT_NE(jwt, nullptr); |
| 580 |
grpc_jwt_verifier_verify(verifier, nullptr, jwt, expected_audience, |
| 581 |
on_verification_bad_signature, |
| 582 |
const_cast<char*>(expected_user_data)); |
| 583 |
gpr_free(jwt); |
| 584 |
grpc_jwt_verifier_destroy(verifier); |
| 585 |
grpc_core::ExecCtx::Get()->Flush(); |
| 586 |
grpc_core::HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 587 |
} |
| 588 |
|
| 589 |
static int httpcli_get_should_not_be_called( |
| 590 |
const grpc_http_request* /*request*/, const char* /*host*/, |
| 591 |
const char* /*path*/, grpc_core::Timestamp /*deadline*/, |
| 592 |
grpc_closure* /*on_done*/, grpc_http_response* /*response*/) { |
| 593 |
EXPECT_TRUE(0); |
| 594 |
return 1; |
| 595 |
} |
| 596 |
|
| 597 |
static void on_verification_bad_format(void* user_data, |
| 598 |
grpc_jwt_verifier_status status, |
| 599 |
grpc_jwt_claims* claims) { |
| 600 |
ASSERT_EQ(status, GRPC_JWT_VERIFIER_BAD_FORMAT); |
| 601 |
ASSERT_EQ(claims, nullptr); |
| 602 |
ASSERT_EQ(user_data, (void*)expected_user_data); |
| 603 |
} |
| 604 |
|
| 605 |
TEST(JwtVerifierTest, JwtVerifierBadFormat) { |
| 606 |
grpc_core::ExecCtx exec_ctx; |
| 607 |
grpc_jwt_verifier* verifier = grpc_jwt_verifier_create(nullptr, 0); |
| 608 |
grpc_core::HttpRequest::SetOverride(httpcli_get_should_not_be_called, |
| 609 |
httpcli_post_should_not_be_called, |
| 610 |
httpcli_put_should_not_be_called); |
| 611 |
grpc_jwt_verifier_verify(verifier, nullptr, "bad jwt", expected_audience, |
| 612 |
on_verification_bad_format, |
| 613 |
const_cast<char*>(expected_user_data)); |
| 614 |
grpc_jwt_verifier_destroy(verifier); |
| 615 |
grpc_core::ExecCtx::Get()->Flush(); |
| 616 |
grpc_core::HttpRequest::SetOverride(nullptr, nullptr, nullptr); |
| 617 |
} |
| 618 |
|
| 619 |
/* find verification key: bad jks, cannot find key in jks */ |
| 620 |
/* bad signature custom provided email*/ |
| 621 |
/* bad key */ |
| 622 |
|
| 623 |
int main(int argc, char** argv) { |
| 624 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 625 |
::testing::InitGoogleTest(&argc, argv); |
| 626 |
grpc::testing::TestGrpcScope grpc_scope; |
| 627 |
return RUN_ALL_TESTS(); |
| 628 |
} |