@@ -29,6 +29,7 @@ public class PushServiceClient
2929 private const string URGENCY_HEADER_NAME = "Urgency" ;
3030 private const string CRYPTO_KEY_HEADER_NAME = "Crypto-Key" ;
3131 private const string WEBPUSH_AUTHENTICATION_SCHEME = "WebPush" ;
32+ private const string VAPID_AUTHENTICATION_SCHEME = "vapid" ;
3233
3334 private const int DEFAULT_TIME_TO_LIVE = 2419200 ;
3435
@@ -68,6 +69,11 @@ public int DefaultTimeToLive
6869 /// Gets or sets the default authentication details.
6970 /// </summary>
7071 public VapidAuthentication DefaultAuthentication { get ; set ; }
72+
73+ /// <summary>
74+ /// Gets or sets the default <see cref="VapidAuthenticationScheme"/> to be used.
75+ /// </summary>
76+ public VapidAuthenticationScheme DefaultAuthenticationScheme { get ; set ; } = VapidAuthenticationScheme . WebPush ;
7177 #endregion
7278
7379 #region Methods
@@ -79,7 +85,7 @@ public int DefaultTimeToLive
7985 /// <returns>The task object representing the asynchronous operation.</returns>
8086 public Task RequestPushMessageDeliveryAsync ( PushSubscription subscription , PushMessage message )
8187 {
82- return RequestPushMessageDeliveryAsync ( subscription , message , null , CancellationToken . None ) ;
88+ return RequestPushMessageDeliveryAsync ( subscription , message , null , DefaultAuthenticationScheme , CancellationToken . None ) ;
8389 }
8490
8591 /// <summary>
@@ -91,7 +97,7 @@ public Task RequestPushMessageDeliveryAsync(PushSubscription subscription, PushM
9197 /// <returns>The task object representing the asynchronous operation.</returns>
9298 public Task RequestPushMessageDeliveryAsync ( PushSubscription subscription , PushMessage message , CancellationToken cancellationToken )
9399 {
94- return RequestPushMessageDeliveryAsync ( subscription , message , null , cancellationToken ) ;
100+ return RequestPushMessageDeliveryAsync ( subscription , message , null , DefaultAuthenticationScheme , cancellationToken ) ;
95101 }
96102
97103 /// <summary>
@@ -103,7 +109,20 @@ public Task RequestPushMessageDeliveryAsync(PushSubscription subscription, PushM
103109 /// <returns>The task object representing the asynchronous operation.</returns>
104110 public Task RequestPushMessageDeliveryAsync ( PushSubscription subscription , PushMessage message , VapidAuthentication authentication )
105111 {
106- return RequestPushMessageDeliveryAsync ( subscription , message , authentication , CancellationToken . None ) ;
112+ return RequestPushMessageDeliveryAsync ( subscription , message , authentication , DefaultAuthenticationScheme , CancellationToken . None ) ;
113+ }
114+
115+ /// <summary>
116+ /// Requests delivery of push message by push service as an asynchronous operation.
117+ /// </summary>
118+ /// <param name="subscription">The push service subscription.</param>
119+ /// <param name="message">The push message.</param>
120+ /// <param name="authentication">The authentication details.</param>
121+ /// <param name="authenticationScheme">The <see cref="VapidAuthenticationScheme"/> to use.</param>
122+ /// <returns>The task object representing the asynchronous operation.</returns>
123+ public Task RequestPushMessageDeliveryAsync ( PushSubscription subscription , PushMessage message , VapidAuthentication authentication , VapidAuthenticationScheme authenticationScheme )
124+ {
125+ return RequestPushMessageDeliveryAsync ( subscription , message , authentication , authenticationScheme , CancellationToken . None ) ;
107126 }
108127
109128 /// <summary>
@@ -114,16 +133,30 @@ public Task RequestPushMessageDeliveryAsync(PushSubscription subscription, PushM
114133 /// <param name="authentication">The authentication details.</param>
115134 /// <param name="cancellationToken">The cancellation token to cancel operation.</param>
116135 /// <returns>The task object representing the asynchronous operation.</returns>
117- public async Task RequestPushMessageDeliveryAsync ( PushSubscription subscription , PushMessage message , VapidAuthentication authentication , CancellationToken cancellationToken )
136+ public Task RequestPushMessageDeliveryAsync ( PushSubscription subscription , PushMessage message , VapidAuthentication authentication , CancellationToken cancellationToken )
118137 {
119- HttpRequestMessage pushMessageDeliveryRequest = PreparePushMessageDeliveryRequest ( subscription , message , authentication ) ;
138+ return RequestPushMessageDeliveryAsync ( subscription , message , authentication , DefaultAuthenticationScheme , cancellationToken ) ;
139+ }
140+
141+ /// <summary>
142+ /// Requests delivery of push message by push service as an asynchronous operation.
143+ /// </summary>
144+ /// <param name="subscription">The push service subscription.</param>
145+ /// <param name="message">The push message.</param>
146+ /// <param name="authentication">The authentication details.</param>
147+ /// <param name="authenticationScheme">The <see cref="VapidAuthenticationScheme"/> to use.</param>
148+ /// <param name="cancellationToken">The cancellation token to cancel operation.</param>
149+ /// <returns>The task object representing the asynchronous operation.</returns>
150+ public async Task RequestPushMessageDeliveryAsync ( PushSubscription subscription , PushMessage message , VapidAuthentication authentication , VapidAuthenticationScheme authenticationScheme , CancellationToken cancellationToken )
151+ {
152+ HttpRequestMessage pushMessageDeliveryRequest = PreparePushMessageDeliveryRequest ( subscription , message , authentication , authenticationScheme ) ;
120153
121154 HttpResponseMessage pushMessageDeliveryRequestResponse = await _httpClient . SendAsync ( pushMessageDeliveryRequest , HttpCompletionOption . ResponseHeadersRead , cancellationToken ) ;
122155
123156 HandlePushMessageDeliveryRequestResponse ( pushMessageDeliveryRequestResponse ) ;
124157 }
125158
126- private HttpRequestMessage PreparePushMessageDeliveryRequest ( PushSubscription subscription , PushMessage message , VapidAuthentication authentication )
159+ private HttpRequestMessage PreparePushMessageDeliveryRequest ( PushSubscription subscription , PushMessage message , VapidAuthentication authentication , VapidAuthenticationScheme authenticationScheme )
127160 {
128161 authentication = authentication ?? DefaultAuthentication ;
129162 if ( authentication == null )
@@ -138,21 +171,28 @@ private HttpRequestMessage PreparePushMessageDeliveryRequest(PushSubscription su
138171 { TTL_HEADER_NAME , ( message . TimeToLive ?? DefaultTimeToLive ) . ToString ( CultureInfo . InvariantCulture ) }
139172 }
140173 } ;
141- pushMessageDeliveryRequest = SetAuthentication ( pushMessageDeliveryRequest , subscription , authentication ) ;
174+ pushMessageDeliveryRequest = SetAuthentication ( pushMessageDeliveryRequest , subscription , authentication , authenticationScheme ) ;
142175 pushMessageDeliveryRequest = SetContent ( pushMessageDeliveryRequest , subscription , message ) ;
143176
144177 return pushMessageDeliveryRequest ;
145178 }
146179
147- private static HttpRequestMessage SetAuthentication ( HttpRequestMessage pushMessageDeliveryRequest , PushSubscription subscription , VapidAuthentication authentication )
180+ private static HttpRequestMessage SetAuthentication ( HttpRequestMessage pushMessageDeliveryRequest , PushSubscription subscription , VapidAuthentication authentication , VapidAuthenticationScheme authenticationScheme )
148181 {
149182 Uri endpointUri = new Uri ( subscription . Endpoint ) ;
150183 string audience = endpointUri . Scheme + @"://" + endpointUri . Host ;
151184
152- VapidAuthentication . WebPushSchemeHeadersValues webPushSchemeHeadersValues = authentication . GetWebPushSchemeHeadersValues ( audience ) ;
185+ if ( authenticationScheme == VapidAuthenticationScheme . WebPush )
186+ {
187+ VapidAuthentication . WebPushSchemeHeadersValues webPushSchemeHeadersValues = authentication . GetWebPushSchemeHeadersValues ( audience ) ;
153188
154- pushMessageDeliveryRequest . Headers . Authorization = new AuthenticationHeaderValue ( WEBPUSH_AUTHENTICATION_SCHEME , webPushSchemeHeadersValues . AuthenticationHeaderValueParameter ) ;
155- pushMessageDeliveryRequest . Headers . Add ( CRYPTO_KEY_HEADER_NAME , webPushSchemeHeadersValues . CryptoKeyHeaderValue ) ;
189+ pushMessageDeliveryRequest . Headers . Authorization = new AuthenticationHeaderValue ( WEBPUSH_AUTHENTICATION_SCHEME , webPushSchemeHeadersValues . AuthenticationHeaderValueParameter ) ;
190+ pushMessageDeliveryRequest . Headers . Add ( CRYPTO_KEY_HEADER_NAME , webPushSchemeHeadersValues . CryptoKeyHeaderValue ) ;
191+ }
192+ else
193+ {
194+ pushMessageDeliveryRequest . Headers . Authorization = new AuthenticationHeaderValue ( VAPID_AUTHENTICATION_SCHEME , authentication . GetVapidSchemeAuthenticationHeaderValueParameter ( audience ) ) ;
195+ }
156196
157197 return pushMessageDeliveryRequest ;
158198 }
0 commit comments