|
4 | 4 | * for more information concerning the license and the contributors participating to this project.
|
5 | 5 | */
|
6 | 6 |
|
| 7 | +using System.IdentityModel.Tokens.Jwt; |
| 8 | +using System.Security.Cryptography; |
| 9 | +using System.Text.Json; |
7 | 10 | using Microsoft.AspNetCore.WebUtilities;
|
8 | 11 | using Microsoft.Extensions.DependencyInjection.Extensions;
|
9 | 12 | using Microsoft.IdentityModel.Logging;
|
@@ -465,6 +468,91 @@ public async Task BuildChallengeUrl_Generates_Correct_Url(bool usePkce)
|
465 | 468 | }
|
466 | 469 | }
|
467 | 470 |
|
| 471 | + [Fact] |
| 472 | + public void Regenerate_Test_Jwts() |
| 473 | + { |
| 474 | + using var rsa = RSA.Create(); |
| 475 | + var parameters = rsa.ExportParameters(true); |
| 476 | + |
| 477 | + var webKey = new |
| 478 | + { |
| 479 | + kty = JsonWebAlgorithmsKeyTypes.RSA, |
| 480 | + kid = "AIDOPK1", |
| 481 | + use = "sig", |
| 482 | + alg = SecurityAlgorithms.RsaSha256, |
| 483 | + n = Base64UrlEncoder.Encode(parameters.Modulus), |
| 484 | + e = Base64UrlEncoder.Encode(parameters.Exponent), |
| 485 | + }; |
| 486 | + |
| 487 | + var signingCredentials = new SigningCredentials(new RsaSecurityKey(rsa), SecurityAlgorithms.RsaSha256) |
| 488 | + { |
| 489 | + CryptoProviderFactory = new CryptoProviderFactory { CacheSignatureProviders = false } |
| 490 | + }; |
| 491 | + |
| 492 | + var audience = "com.martincostello.signinwithapple.test.client"; |
| 493 | + var issuer = "https://appleid.apple.com"; |
| 494 | + var expires = DateTimeOffset.FromUnixTimeSeconds(1587212159).UtcDateTime; |
| 495 | + |
| 496 | + var iat = new Claim(JwtRegisteredClaimNames.Iat, "1587211559"); |
| 497 | + var sub = new Claim(JwtRegisteredClaimNames.Sub, "001883.fcc77ba97500402389df96821ad9c790.1517"); |
| 498 | + var atHash = new Claim(JwtRegisteredClaimNames.AtHash, "eOy0y7XVexdkzc7uuDZiCQ"); |
| 499 | + var emailVerified = new Claim("email_verified", "true"); |
| 500 | + var authTime = new Claim(JwtRegisteredClaimNames.AuthTime, "1587211556"); |
| 501 | + var nonceSupported = new Claim("nonce_supported", "true"); |
| 502 | + |
| 503 | + var claimsForPublicEmail = new Claim[] |
| 504 | + { |
| 505 | + iat, |
| 506 | + sub, |
| 507 | + atHash, |
| 508 | + new Claim(JwtRegisteredClaimNames.Email, "[email protected]"), |
| 509 | + emailVerified, |
| 510 | + authTime, |
| 511 | + nonceSupported, |
| 512 | + }; |
| 513 | + |
| 514 | + var publicEmailToken = new JwtSecurityToken( |
| 515 | + issuer, |
| 516 | + audience, |
| 517 | + claimsForPublicEmail, |
| 518 | + expires: expires, |
| 519 | + signingCredentials: signingCredentials); |
| 520 | + |
| 521 | + var claimsForPrivateEmail = new Claim[] |
| 522 | + { |
| 523 | + iat, |
| 524 | + sub, |
| 525 | + atHash, |
| 526 | + new Claim(JwtRegisteredClaimNames.Email, "[email protected]"), |
| 527 | + emailVerified, |
| 528 | + authTime, |
| 529 | + nonceSupported, |
| 530 | + new Claim("is_private_email", "true"), |
| 531 | + }; |
| 532 | + |
| 533 | + var privateEmailToken = new JwtSecurityToken( |
| 534 | + issuer, |
| 535 | + audience, |
| 536 | + claimsForPrivateEmail, |
| 537 | + expires: expires, |
| 538 | + signingCredentials: signingCredentials); |
| 539 | + |
| 540 | + var publicEmailIdToken = new JwtSecurityTokenHandler().WriteToken(publicEmailToken); |
| 541 | + var privateEmailIdToken = new JwtSecurityTokenHandler().WriteToken(privateEmailToken); |
| 542 | + var serializedRsaPublicKey = JsonSerializer.Serialize(webKey, new JsonSerializerOptions() { WriteIndented = true }); |
| 543 | + |
| 544 | + // Copy the values from the test output to bundles.json if you need to regenerate the JWTs to edit the claims |
| 545 | + |
| 546 | + // For https://appleid.apple.com/auth/keys |
| 547 | + OutputHelper!.WriteLine($"RSA key: {serializedRsaPublicKey}"); |
| 548 | + |
| 549 | + // For https://appleid.apple.com/auth/token |
| 550 | + OutputHelper!.WriteLine($"Public email JWT: {publicEmailIdToken}"); |
| 551 | + |
| 552 | + // For https://appleid.apple.local/auth/token/email |
| 553 | + OutputHelper!.WriteLine($"Private email JWT: {privateEmailIdToken}"); |
| 554 | + } |
| 555 | + |
468 | 556 | private sealed class CustomAppleAuthenticationEvents : AppleAuthenticationEvents
|
469 | 557 | {
|
470 | 558 | }
|
|
0 commit comments