forked from Badgerati/Pode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCookies.ps1
459 lines (360 loc) · 10 KB
/
Cookies.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<#
.SYNOPSIS
Sets a cookie on the Response.
.DESCRIPTION
Sets a cookie on the Response using the "Set-Cookie" header. You can also set cookies to expire, or being signed.
.PARAMETER Name
The name of the cookie.
.PARAMETER Value
The value of the cookie.
.PARAMETER Secret
If supplied, the secret with which to sign the cookie.
.PARAMETER Duration
The duration, in seconds, before the cookie is expired.
.PARAMETER ExpiryDate
An explicit expiry date for the cookie.
.PARAMETER HttpOnly
Only allow the cookie to be used in browsers.
.PARAMETER Discard
Inform browsers to remove the cookie.
.PARAMETER Secure
Only allow the cookie on secure (HTTPS) connections.
.EXAMPLE
Set-PodeCookie -Name 'Views' -Value 2
.EXAMPLE
Set-PodeCookie -Name 'Views' -Value 2 -Secret 'hunter2'
.EXAMPLE
Set-PodeCookie -Name 'Views' -Value 2 -Duration 3600
#>
function Set-PodeCookie
{
[CmdletBinding(DefaultParameterSetName='Duration')]
[OutputType([hashtable])]
param(
[Parameter(Mandatory=$true)]
[string]
$Name,
[Parameter()]
[string]
$Value,
[Parameter()]
[string]
$Secret,
[Parameter(ParameterSetName='Duration')]
[int]
$Duration = 0,
[Parameter(ParameterSetName='ExpiryDate')]
[datetime]
$ExpiryDate,
[switch]
$HttpOnly,
[switch]
$Discard,
[switch]
$Secure
)
# sign the value if we have a secret
if (![string]::IsNullOrWhiteSpace($Secret)) {
$Value = (Invoke-PodeValueSign -Value $Value -Secret $Secret)
}
# create a new cookie
$cookie = [System.Net.Cookie]::new($Name, $Value)
$cookie.Secure = $Secure
$cookie.Discard = $Discard
$cookie.HttpOnly = $HttpOnly
$cookie.Path = '/'
if ($null -ne $ExpiryDate) {
if ($ExpiryDate.Kind -eq [System.DateTimeKind]::Local) {
$ExpiryDate = $ExpiryDate.ToUniversalTime()
}
$cookie.Expires = $ExpiryDate
}
elseif ($Duration -gt 0) {
$cookie.Expires = [datetime]::UtcNow.AddSeconds($Duration)
}
# sets the cookie on the the response
$WebEvent.PendingCookies[$cookie.Name] = $cookie
Add-PodeHeader -Name 'Set-Cookie' -Value (ConvertTo-PodeCookieString -Cookie $cookie)
return (ConvertTo-PodeCookie -Cookie $cookie)
}
<#
.SYNOPSIS
Retrieves a cookie from the Request.
.DESCRIPTION
Retrieves a cookie from the Request, with the option to supply a secret to unsign the cookie's value.
.PARAMETER Name
The name of the cookie to retrieve.
.PARAMETER Secret
The secret used to unsign the cookie's value.
.PARAMETER Raw
If supplied, the cookie returned will be the raw .NET Cookie object for manipulation.
.EXAMPLE
Get-PodeCookie -Name 'Views'
.EXAMPLE
Get-PodeCookie -Name 'Views' -Secret 'hunter2'
#>
function Get-PodeCookie
{
[CmdletBinding()]
[OutputType([hashtable])]
param(
[Parameter(Mandatory=$true)]
[string]
$Name,
[Parameter()]
[string]
$Secret,
[switch]
$Raw
)
# get the cookie from the request
$cookie = $WebEvent.Cookies[$Name]
if (!$Raw) {
$cookie = (ConvertTo-PodeCookie -Cookie $cookie)
}
if (($null -eq $cookie) -or [string]::IsNullOrWhiteSpace($cookie.Value)) {
return $null
}
# if a secret was supplied, attempt to unsign the cookie
if (![string]::IsNullOrWhiteSpace($Secret)) {
$value = (Invoke-PodeValueUnsign -Value $cookie.Value -Secret $Secret)
if (![string]::IsNullOrWhiteSpace($value)) {
$cookie.Value = $value
}
}
return $cookie
}
<#
.SYNOPSIS
Retrieves the value of a cookie from the Request.
.DESCRIPTION
Retrieves the value of a cookie from the Request, with the option to supply a secret to unsign the cookie's value.
.PARAMETER Name
The name of the cookie to retrieve.
.PARAMETER Secret
The secret used to unsign the cookie's value.
.EXAMPLE
Get-PodeCookieValue -Name 'Views'
.EXAMPLE
Get-PodeCookieValue -Name 'Views' -Secret 'hunter2'
#>
function Get-PodeCookieValue
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]
$Name,
[Parameter()]
[string]
$Secret
)
$cookie = Get-PodeCookie -Name $Name -Secret $Secret
if ($null -eq $cookie) {
return $null
}
return $cookie.Value
}
<#
.SYNOPSIS
Tests if a cookie exists on the Request.
.DESCRIPTION
Tests if a cookie exists on the Request.
.PARAMETER Name
The name of the cookie to test for on the Request.
.EXAMPLE
Test-PodeCookie -Name 'Views'
#>
function Test-PodeCookie
{
[CmdletBinding()]
[OutputType([bool])]
param(
[Parameter(Mandatory=$true)]
[string]
$Name
)
$cookie = $WebEvent.Cookies[$Name]
return (($null -ne $cookie) -and ![string]::IsNullOrWhiteSpace($cookie.Value))
}
<#
.SYNOPSIS
Removes a cookie from the Response.
.DESCRIPTION
Removes a cookie from the Response, this is done by immediately expiring the cookie and flagging it for discard.
.PARAMETER Name
The name of the cookie to be removed.
.EXAMPLE
Remove-PodeCookie -Name 'Views'
#>
function Remove-PodeCookie
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]
$Name
)
# get the cookie from the response - if it's not found, get it from the request
$cookie = $WebEvent.PendingCookies[$Name]
if ($null -eq $cookie) {
$cookie = Get-PodeCookie -Name $Name -Raw
}
# remove the cookie from the response, and reset it to expire
if ($null -ne $cookie) {
$cookie.Discard = $true
$cookie.Expires = [DateTime]::UtcNow.AddDays(-2)
$cookie.Path = '/'
$WebEvent.PendingCookies[$cookie.Name] = $cookie
Add-PodeHeader -Name 'Set-Cookie' -Value (ConvertTo-PodeCookieString -Cookie $cookie)
}
}
<#
.SYNOPSIS
Tests if a cookie on the Request is validly signed.
.DESCRIPTION
Tests if a cookie on the Request is validly signed, by attempting to unsign it using some secret.
.PARAMETER Name
The name of the cookie to test.
.PARAMETER Secret
A secret to use for attempting to unsign the cookie's value.
.EXAMPLE
Test-PodeCookieSigned -Name 'Views' -Secret 'hunter2'
#>
function Test-PodeCookieSigned
{
[CmdletBinding()]
[OutputType([bool])]
param(
[Parameter(Mandatory=$true)]
[string]
$Name,
[Parameter()]
[string]
$Secret
)
$cookie = $WebEvent.Cookies[$Name]
if (($null -eq $cookie) -or [string]::IsNullOrWhiteSpace($cookie.Value)) {
return $false
}
$value = (Invoke-PodeValueUnsign -Value $cookie.Value -Secret $Secret)
return (![string]::IsNullOrWhiteSpace($value))
}
<#
.SYNOPSIS
Updates the exipry date of a cookie on the Response.
.DESCRIPTION
Updates the exipry date of a cookie on the Response. This can either be done by suppling a duration, or and explicit expiry date.
.PARAMETER Name
The name of the cookie to extend.
.PARAMETER Duration
The duration, in seconds, to extend the cookie's expiry.
.PARAMETER ExpiryDate
An explicit expiry date for the cookie.
.EXAMPLE
Update-PodeCookieExpiry -Name 'Views' -Duration 1800
.EXAMPLE
Update-PodeCookieExpiry -Name 'Views' -ExpiryDate ([datetime]::UtcNow.AddSeconds(1800))
#>
function Update-PodeCookieExpiry
{
[CmdletBinding(DefaultParameterSetName='Duration')]
[OutputType([hashtable])]
param(
[Parameter(Mandatory=$true)]
[string]
$Name,
[Parameter(ParameterSetName='Duration')]
[int]
$Duration = 0,
[Parameter(ParameterSetName='ExpiryDate')]
[datetime]
$ExpiryDate
)
# get the cookie from the response - if it's not found, get it from the request
$cookie = $WebEvent.PendingCookies[$Name]
if ($null -eq $cookie) {
$cookie = Get-PodeCookie -Name $Name -Raw
}
# extends the expiry on the cookie
if ($null -ne $ExpiryDate) {
if ($ExpiryDate.Kind -eq [System.DateTimeKind]::Local) {
$ExpiryDate = $ExpiryDate.ToUniversalTime()
}
$cookie.Expires = $ExpiryDate
}
elseif ($Duration -gt 0) {
$cookie.Expires = [datetime]::UtcNow.AddSeconds($Duration)
}
$cookie.Path = '/'
# sets the cookie on the the response
$WebEvent.PendingCookies[$cookie.Name] = $cookie
Add-PodeHeader -Name 'Set-Cookie' -Value (ConvertTo-PodeCookieString -Cookie $cookie)
return (ConvertTo-PodeCookie -Cookie $cookie)
}
<#
.SYNOPSIS
Stores secrets that can be used to sign cookies.
.DESCRIPTION
Stores secrets that can be used to sign cookies. A global secret can be set for easier retrieval.
.PARAMETER Name
The name of the secret to store.
.PARAMETER Value
The value of the secret to store.
.PARAMETER Global
If flagged, the secret being stored will be set as the global secret.
.EXAMPLE
Set-PodeCookieSecret -Name 'my-secret' -Value 'shhhh!'
.EXAMPLE
Set-PodeCookieSecret -Value 'hunter2' -Global
#>
function Set-PodeCookieSecret
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, ParameterSetName='General')]
[string]
$Name,
[Parameter(Mandatory=$true)]
[string]
$Value,
[Parameter(ParameterSetName='Global')]
[switch]
$Global
)
if ($Global) {
$Name = 'global'
}
$PodeContext.Server.Cookies.Secrets[$Name] = $Value
}
<#
.SYNOPSIS
Retrieves a stored secret value.
.DESCRIPTION
Retrieves a stored secret value.
.PARAMETER Name
The name of the secret to retrieve.
.PARAMETER Global
If flagged, will return the current global secret value.
.EXAMPLE
Get-PodeCookieSecret -Name 'my-secret'
.EXAMPLE
Get-PodeCookieSecret -Global
#>
function Get-PodeCookieSecret
{
[CmdletBinding()]
[OutputType([string])]
param(
[Parameter(Mandatory=$true, ParameterSetName='General')]
[string]
$Name,
[Parameter(ParameterSetName='Global')]
[switch]
$Global
)
if ($Global) {
return ($PodeContext.Server.Cookies.Secrets['global'])
}
return ($PodeContext.Server.Cookies.Secrets[$Name])
}