Add persistent Inbox and RFC 8291/8292 Web Push notification system

This commit is contained in:
tyuXX 2026-10-31 15:41:51 +03:00
parent 4714d25724
commit 7cc9ba7f4d
27 changed files with 3573 additions and 17 deletions

View file

@ -113,4 +113,55 @@ public class CryptoTests
string? wrongPassword = E2eeCrypto.UnwrapPrivateKey(wrapped, "WrongPassword", "Alice");
Assert.Null(wrongPassword);
}
[Fact]
public void WebPushCrypto_GenerateVapidKeys_And_SignVerifyJwt_ShouldSucceed()
{
var (publicKey, privateKey) = WebPushCrypto.GenerateVapidKeys();
Assert.False(string.IsNullOrWhiteSpace(publicKey));
Assert.False(string.IsNullOrWhiteSpace(privateKey));
var pubBytes = WebPushCrypto.Base64UrlDecode(publicKey);
var privBytes = WebPushCrypto.Base64UrlDecode(privateKey);
Assert.Equal(65, pubBytes.Length);
Assert.Equal(0x04, pubBytes[0]);
Assert.Equal(32, privBytes.Length);
string authHeader = WebPushCrypto.CreateVapidAuthorizationHeader(
"https://fcm.googleapis.com/fcm/send/example123",
"mailto:admin@gsdchat.local",
publicKey,
privateKey);
Assert.StartsWith("vapid t=", authHeader);
Assert.Contains($", k={publicKey}", authHeader);
// Extract JWT token and verify ES256 signature
int tStart = authHeader.IndexOf("t=", StringComparison.Ordinal) + 2;
int tEnd = authHeader.IndexOf(", k=", StringComparison.Ordinal);
string jwt = authHeader.Substring(tStart, tEnd - tStart);
Assert.True(WebPushCrypto.VerifyVapidJwt(jwt, publicKey));
}
[Fact]
public void WebPushCrypto_Rfc8291Aes128GcmPayloadEncryption_ShouldRoundTrip()
{
// Generate simulated browser push subscription keypair (p256dh) and 16-byte auth secret
var (subPub, subPriv) = WebPushCrypto.GenerateVapidKeys();
byte[] authSecretBytes = new byte[16];
System.Security.Cryptography.RandomNumberGenerator.Fill(authSecretBytes);
string authSecret = WebPushCrypto.Base64UrlEncode(authSecretBytes);
string jsonPayload = "{\"title\":\"GsDChat (@Alice)\",\"body\":\"Hey @Bob check #announcements!\",\"channelId\":\"ch_1\"}";
byte[] encryptedBody = WebPushCrypto.EncryptPayload(System.Text.Encoding.UTF8.GetBytes(jsonPayload), subPub, authSecret);
Assert.NotNull(encryptedBody);
// Header is 86 bytes (16 salt + 4 rs + 1 idlen + 65 pubkey) + ciphertext + 16 tag
Assert.True(encryptedBody.Length > 86 + 16);
byte[] decryptedBytes = WebPushCrypto.DecryptPayload(encryptedBody, subPub, subPriv, authSecret);
string decrypted = System.Text.Encoding.UTF8.GetString(decryptedBytes);
Assert.Equal(jsonPayload, decrypted);
}
}