| 335 | } |
| 336 | |
| 337 | bool parseHeaderPart(Arena& arena, TokenRef& token, StringRef b64urlHeader) { |
| 338 | auto tmpArena = Arena(); |
| 339 | auto optHeader = base64url::decode(tmpArena, b64urlHeader); |
| 340 | if (!optHeader.present()) |
| 341 | return false; |
| 342 | auto header = optHeader.get(); |
| 343 | auto d = rapidjson::Document(); |
| 344 | d.Parse(reinterpret_cast<const char*>(header.begin()), header.size()); |
| 345 | if (d.HasParseError()) { |
| 346 | TraceEvent(SevWarnAlways, "TokenHeaderJsonParseError") |
| 347 | .suppressFor(10) |
| 348 | .detail("Header", header.toString()) |
| 349 | .detail("Message", GetParseError_En(d.GetParseError())) |
| 350 | .detail("Offset", d.GetErrorOffset()); |
| 351 | return false; |
| 352 | } |
| 353 | if (!d.IsObject()) |
| 354 | return false; |
| 355 | auto typItr = d.FindMember("typ"); |
| 356 | if (typItr == d.MemberEnd() || !typItr->value.IsString()) |
| 357 | return false; |
| 358 | auto algItr = d.FindMember("alg"); |
| 359 | if (algItr == d.MemberEnd() || !algItr->value.IsString()) |
| 360 | return false; |
| 361 | auto kidItr = d.FindMember("kid"); |
| 362 | if (kidItr == d.MemberEnd() || !kidItr->value.IsString()) |
| 363 | return false; |
| 364 | auto const& typ = typItr->value; |
| 365 | auto const& alg = algItr->value; |
| 366 | auto const& kid = kidItr->value; |
| 367 | auto typValue = StringRef(reinterpret_cast<const uint8_t*>(typ.GetString()), typ.GetStringLength()); |
| 368 | if (typValue != "JWT"_sr) |
| 369 | return false; |
| 370 | auto algValue = StringRef(reinterpret_cast<const uint8_t*>(alg.GetString()), alg.GetStringLength()); |
| 371 | auto algType = algorithmFromString(algValue); |
| 372 | if (algType == Algorithm::UNKNOWN) |
| 373 | return false; |
| 374 | token.algorithm = algType; |
| 375 | token.keyId = StringRef(arena, reinterpret_cast<const uint8_t*>(kid.GetString()), kid.GetStringLength()); |
| 376 | return true; |
| 377 | } |
| 378 | |
| 379 | template <class FieldType> |
| 380 | bool parseField(Arena& arena, Optional<FieldType>& out, const rapidjson::Document& d, const char* fieldName) { |
no test coverage detected