Skip to content

Commit b7e6e8d

Browse files
authored
Making client keys handling case insensitivity. Resolves #26
2 parents eeceb58 + b2b9eeb commit b7e6e8d

File tree

4 files changed

+209
-6
lines changed

4 files changed

+209
-6
lines changed

src/Lib.Net.Http.WebPush/PushServiceClient.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,18 @@ private static HttpRequestMessage SetContent(HttpRequestMessage pushMessageDeliv
291291
}
292292
else
293293
{
294+
string p256dhKey = subscription.GetKey(PushEncryptionKeyName.P256DH);
295+
string authKey = subscription.GetKey(PushEncryptionKeyName.Auth);
296+
297+
if (String.IsNullOrWhiteSpace(p256dhKey) || String.IsNullOrWhiteSpace(authKey))
298+
{
299+
throw new InvalidOperationException("The client P-256 public key or authentication secret is not available");
300+
}
301+
294302
ECDHAgreement keyAgreement = ECDHAgreementCalculator.CalculateAgreement
295303
(
296-
UrlBase64Converter.FromUrlBase64String(subscription.GetKey(PushEncryptionKeyName.P256DH)),
297-
UrlBase64Converter.FromUrlBase64String(subscription.GetKey(PushEncryptionKeyName.Auth))
304+
UrlBase64Converter.FromUrlBase64String(p256dhKey),
305+
UrlBase64Converter.FromUrlBase64String(authKey)
298306
);
299307

300308
pushMessageDeliveryRequest.Content = new Aes128GcmEncodedContent(

src/Lib.Net.Http.WebPush/PushSubscription.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Linq;
3+
using System.Collections.Generic;
24

35
namespace Lib.Net.Http.WebPush
46
{
@@ -37,6 +39,10 @@ public string GetKey(PushEncryptionKeyName keyName)
3739
{
3840
key = Keys[keyNameStringified];
3941
}
42+
else
43+
{
44+
key = Keys.SingleOrDefault(x => String.Equals(x.Key, keyNameStringified, StringComparison.OrdinalIgnoreCase)).Value;
45+
}
4046
}
4147

4248
return key;

test/Test.Lib.Net.Http.WebPush/Functional/PushMessageDeliveryTests.cs

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Net;
32
using System.Threading.Tasks;
43
using System.Collections.Generic;
54
using Xunit;
@@ -19,12 +18,15 @@ public class PushMessageDeliveryTests : IClassFixture<FakePushServiceApplication
1918

2019
private const string WALRUS_CONTENT = "I am the walrus";
2120

21+
private const string PUSH_SUBSCRIPTION_AUTH_KEY = "n5mG_PyMSKALsjsU542E6g";
22+
private const string PUSH_SUBSCRIPTION_P256DH_KEY = "BDS52l6tfaf6ZEqhyDa0cScvCi4WXNYPIwmfas-7nKLIQex-DVKXB9gUxDExaZEOiwovl6LbWXZBZ9AT-GWT6eQ";
23+
2224
private readonly PushSubscription _pushSubscription = new PushSubscription
2325
{
2426
Keys = new Dictionary<string, string>
2527
{
26-
{ "auth", "n5mG_PyMSKALsjsU542E6g" },
27-
{ "p256dh", "BDS52l6tfaf6ZEqhyDa0cScvCi4WXNYPIwmfas-7nKLIQex-DVKXB9gUxDExaZEOiwovl6LbWXZBZ9AT-GWT6eQ" }
28+
{ "auth", PUSH_SUBSCRIPTION_AUTH_KEY },
29+
{ "p256dh", PUSH_SUBSCRIPTION_P256DH_KEY }
2830
}
2931
};
3032

@@ -204,6 +206,81 @@ public async Task PushService_OtherClientError_PushServiceClientExceptionContain
204206

205207
Assert.Equal(_pushSubscription, pushMessageDeliveryException.PushSubscription);
206208
}
209+
210+
[Fact]
211+
public async Task PushService_PushEncryptionKeysNamesLowercase_DeliversPushMessage()
212+
{
213+
PushSubscription pushSubscription = new PushSubscription
214+
{
215+
Keys = new Dictionary<string, string>
216+
{
217+
{ "auth", PUSH_SUBSCRIPTION_AUTH_KEY },
218+
{ "p256dh", PUSH_SUBSCRIPTION_P256DH_KEY }
219+
},
220+
Endpoint = CREATED_ENDPOINT
221+
};
222+
223+
PushMessage pushMessage = new PushMessage(WALRUS_CONTENT);
224+
225+
PushServiceClient pushClient = PreparePushServiceClient();
226+
227+
Exception pushMessageDeliveryException = await Record.ExceptionAsync(async () =>
228+
{
229+
await pushClient.RequestPushMessageDeliveryAsync(pushSubscription, pushMessage);
230+
});
231+
232+
Assert.Null(pushMessageDeliveryException);
233+
}
234+
235+
[Fact]
236+
public async Task PushService_PushEncryptionKeysNamesUppercase_DeliversPushMessage()
237+
{
238+
PushSubscription pushSubscription = new PushSubscription
239+
{
240+
Keys = new Dictionary<string, string>
241+
{
242+
{ "AUTH", PUSH_SUBSCRIPTION_AUTH_KEY },
243+
{ "P256DH", PUSH_SUBSCRIPTION_P256DH_KEY }
244+
},
245+
Endpoint = CREATED_ENDPOINT
246+
};
247+
248+
PushMessage pushMessage = new PushMessage(WALRUS_CONTENT);
249+
250+
PushServiceClient pushClient = PreparePushServiceClient();
251+
252+
Exception pushMessageDeliveryException = await Record.ExceptionAsync(async () =>
253+
{
254+
await pushClient.RequestPushMessageDeliveryAsync(pushSubscription, pushMessage);
255+
});
256+
257+
Assert.Null(pushMessageDeliveryException);
258+
}
259+
260+
[Fact]
261+
public async Task PushService_PushEncryptionKeysNamesMixedCase_DeliversPushMessage()
262+
{
263+
PushSubscription pushSubscription = new PushSubscription
264+
{
265+
Keys = new Dictionary<string, string>
266+
{
267+
{ "AuTh", PUSH_SUBSCRIPTION_AUTH_KEY },
268+
{ "P256dH", PUSH_SUBSCRIPTION_P256DH_KEY }
269+
},
270+
Endpoint = CREATED_ENDPOINT
271+
};
272+
273+
PushMessage pushMessage = new PushMessage(WALRUS_CONTENT);
274+
275+
PushServiceClient pushClient = PreparePushServiceClient();
276+
277+
Exception pushMessageDeliveryException = await Record.ExceptionAsync(async () =>
278+
{
279+
await pushClient.RequestPushMessageDeliveryAsync(pushSubscription, pushMessage);
280+
});
281+
282+
Assert.Null(pushMessageDeliveryException);
283+
}
207284
#endregion
208285
}
209286
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using System.Collections.Generic;
2+
using Xunit;
3+
using Lib.Net.Http.WebPush;
4+
5+
namespace Test.Lib.Net.Http.WebPush.Unit
6+
{
7+
public class PushSubscriptionTests
8+
{
9+
#region Fields
10+
private const string AUTH_KEY = "n5mG_PyMSKALsjsU542E6g";
11+
private const string P256DH_KEY = "BDS52l6tfaf6ZEqhyDa0cScvCi4WXNYPIwmfas-7nKLIQex-DVKXB9gUxDExaZEOiwovl6LbWXZBZ9AT-GWT6eQ";
12+
#endregion
13+
14+
#region Tests
15+
[Fact]
16+
public void GetKey_AuthKeyNameLowercase_ReturnsKey()
17+
{
18+
PushSubscription pushSubscription = new PushSubscription
19+
{
20+
Keys = new Dictionary<string, string>
21+
{
22+
{ "auth", AUTH_KEY }
23+
}
24+
};
25+
26+
string authKey = pushSubscription.GetKey(PushEncryptionKeyName.Auth);
27+
28+
Assert.Equal(AUTH_KEY, authKey);
29+
}
30+
31+
[Fact]
32+
public void GetKey_AuthKeyNameUppercase_ReturnsKey()
33+
{
34+
PushSubscription pushSubscription = new PushSubscription
35+
{
36+
Keys = new Dictionary<string, string>
37+
{
38+
{ "AUTH", AUTH_KEY }
39+
}
40+
};
41+
42+
string authKey = pushSubscription.GetKey(PushEncryptionKeyName.Auth);
43+
44+
Assert.Equal(AUTH_KEY, authKey);
45+
}
46+
47+
[Fact]
48+
public void GetKey_AuthKeyNameMixedCase_ReturnsKey()
49+
{
50+
PushSubscription pushSubscription = new PushSubscription
51+
{
52+
Keys = new Dictionary<string, string>
53+
{
54+
{ "AuTh", AUTH_KEY }
55+
}
56+
};
57+
58+
string authKey = pushSubscription.GetKey(PushEncryptionKeyName.Auth);
59+
60+
Assert.Equal(AUTH_KEY, authKey);
61+
}
62+
63+
[Fact]
64+
public void GetKey_P256DHKeyNameLowercase_ReturnsKey()
65+
{
66+
PushSubscription pushSubscription = new PushSubscription
67+
{
68+
Keys = new Dictionary<string, string>
69+
{
70+
{ "p256dh", P256DH_KEY }
71+
}
72+
};
73+
74+
string p256dhKey = pushSubscription.GetKey(PushEncryptionKeyName.P256DH);
75+
76+
Assert.Equal(P256DH_KEY, p256dhKey);
77+
}
78+
79+
[Fact]
80+
public void GetKey_P256DHKeyNameUppercase_ReturnsKey()
81+
{
82+
PushSubscription pushSubscription = new PushSubscription
83+
{
84+
Keys = new Dictionary<string, string>
85+
{
86+
{ "P256DH", P256DH_KEY }
87+
}
88+
};
89+
90+
string p256dhKey = pushSubscription.GetKey(PushEncryptionKeyName.P256DH);
91+
92+
Assert.Equal(P256DH_KEY, p256dhKey);
93+
}
94+
95+
[Fact]
96+
public void GetKey_P256DHKeyNameMixedCase_ReturnsKey()
97+
{
98+
PushSubscription pushSubscription = new PushSubscription
99+
{
100+
Keys = new Dictionary<string, string>
101+
{
102+
{ "P256dH", P256DH_KEY }
103+
}
104+
};
105+
106+
string p256dhKey = pushSubscription.GetKey(PushEncryptionKeyName.P256DH);
107+
108+
Assert.Equal(P256DH_KEY, p256dhKey);
109+
}
110+
#endregion
111+
}
112+
}

0 commit comments

Comments
 (0)