Skip to content

Commit b05caae

Browse files
committed
Web Push Protocol primitives
1 parent 994a500 commit b05caae

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace Lib.Net.Http.WebPush
2+
{
3+
/// <summary>
4+
/// The client keys shared as part of subscription.
5+
/// </summary>
6+
public enum PushEncryptionKeyName
7+
{
8+
/// <summary>
9+
/// The client P-256 public key for use in ECDH.
10+
/// </summary>
11+
P256DH,
12+
/// <summary>
13+
/// The client authentication secret.
14+
/// </summary>
15+
Auth
16+
}
17+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
3+
namespace Lib.Net.Http.WebPush
4+
{
5+
/// <summary>
6+
/// Class representing a push message.
7+
/// </summary>
8+
public class PushMessage
9+
{
10+
#region Fields
11+
private int? _timeToLive;
12+
#endregion
13+
14+
#region Properties
15+
/// <summary>
16+
/// Gets or sets the content.
17+
/// </summary>
18+
public string Content { get; set; }
19+
20+
/// <summary>
21+
/// Gets or sets the time (in seconds) for which the message should be retained by push service.
22+
/// </summary>
23+
public int? TimeToLive
24+
{
25+
get { return _timeToLive; }
26+
27+
set
28+
{
29+
if (value.HasValue && (value.Value < 0))
30+
{
31+
throw new ArgumentOutOfRangeException(nameof(TimeToLive), "The TTL must be a non-negative integer");
32+
}
33+
34+
_timeToLive = value;
35+
}
36+
}
37+
#endregion
38+
39+
#region Constructors
40+
/// <summary>
41+
/// Creates new instance of <see cref="PushMessage"/> class.
42+
/// </summary>
43+
/// <param name="content">The content.</param>
44+
public PushMessage(string content)
45+
{
46+
Content = content;
47+
}
48+
#endregion
49+
}
50+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System.Collections.Generic;
2+
3+
namespace Lib.Net.Http.WebPush
4+
{
5+
/// <summary>
6+
/// Class representing a push subscription
7+
/// </summary>
8+
public class PushSubscription
9+
{
10+
#region Properties
11+
/// <summary>
12+
/// Gets or sets the subscription endpoint.
13+
/// </summary>
14+
public string Endpoint { get; set; }
15+
16+
/// <summary>
17+
/// Gets or sets client keys shared as part of subscription.
18+
/// </summary>
19+
public IDictionary<string, string> Keys { get; set; }
20+
#endregion
21+
22+
#region Methods
23+
/// <summary>
24+
/// Gets specific client key shared as part of subscription.
25+
/// </summary>
26+
/// <param name="keyName">The key name.</param>
27+
/// <returns>The key.</returns>
28+
public string GetKey(PushEncryptionKeyName keyName)
29+
{
30+
string key = null;
31+
32+
if (Keys != null)
33+
{
34+
string keyNameStringified = StringifyKeyName(keyName);
35+
36+
if (Keys.ContainsKey(keyNameStringified))
37+
{
38+
key = Keys[keyNameStringified];
39+
}
40+
}
41+
42+
return key;
43+
}
44+
45+
/// <summary>
46+
/// Sets specific client key shared as part of subscription.
47+
/// </summary>
48+
/// <param name="keyName">The key name.</param>
49+
/// <param name="key">The key.</param>
50+
public void SetKey(PushEncryptionKeyName keyName, string key)
51+
{
52+
if (Keys == null)
53+
{
54+
Keys = new Dictionary<string, string>();
55+
}
56+
57+
Keys[StringifyKeyName(keyName)] = key;
58+
}
59+
60+
private string StringifyKeyName(PushEncryptionKeyName keyName)
61+
{
62+
return keyName.ToString().ToLowerInvariant();
63+
}
64+
#endregion
65+
}
66+
}

0 commit comments

Comments
 (0)