Skip to content

Commit 1d26fae

Browse files
committed
Improved SSE/WebSocket connection handling, additional events for SSE/WebSockets/Auth, manual upgrade control for WebSockets
1 parent f940490 commit 1d26fae

File tree

98 files changed

+39802
-36300
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+39802
-36300
lines changed

examples/Web-AuthForm.ps1

Lines changed: 115 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,116 @@
1-
<#
2-
.SYNOPSIS
3-
Sample script demonstrating session persistent authentication using Pode.
4-
5-
.DESCRIPTION
6-
This script sets up a Pode server that listens on localhost:8081 and uses session-based authentication
7-
for user logins. The authentication is demonstrated using a simple form where users can log in with
8-
predefined credentials (username: morty, password: pickle). Upon successful login, users are greeted
9-
on the home page, and the view counter is incremented. Users can log out, which will purge the session
10-
and redirect them to the login page.
11-
12-
.PARAMETER ScriptPath
13-
Path of the script being executed.
14-
15-
.PARAMETER podePath
16-
Path of the Pode module.
17-
18-
.EXAMPLE
19-
To run the sample: ./Web-AuthForm.ps1
20-
21-
Run this script to start the Pode server and navigate to 'http://localhost:8081' in your browser.
22-
You will be redirected to the login page, where you can log in with the credentials provided above.
23-
24-
.LINK
25-
https://github.com/Badgerati/Pode/blob/develop/examples/Web-AuthForm.ps1
26-
27-
.NOTES
28-
Author: Pode Team
29-
License: MIT License
30-
#>
31-
32-
try {
33-
# Determine the script path and Pode module path
34-
$ScriptPath = (Split-Path -Parent -Path $MyInvocation.MyCommand.Path)
35-
$podePath = Split-Path -Parent -Path $ScriptPath
36-
37-
# Import the Pode module from the source path if it exists, otherwise from installed modules
38-
if (Test-Path -Path "$($podePath)/src/Pode.psm1" -PathType Leaf) {
39-
Import-Module "$($podePath)/src/Pode.psm1" -Force -ErrorAction Stop
40-
}
41-
else {
42-
Import-Module -Name 'Pode' -MaximumVersion 2.99 -ErrorAction Stop
43-
}
44-
}
45-
catch { throw }
46-
47-
# or just:
48-
# Import-Module Pode
49-
50-
# create a server, and start listening on port 8081
51-
Start-PodeServer -Threads 2 {
52-
53-
# listen on localhost:8081
54-
Add-PodeEndpoint -Address localhost -Port 8081 -Protocol Http
55-
56-
# set the view engine
57-
Set-PodeViewEngine -Type Pode
58-
59-
# enable error logging
60-
New-PodeLoggingMethod -Terminal | Enable-PodeErrorLogging
61-
62-
# setup session details
63-
Enable-PodeSessionMiddleware -Duration 120 -Extend
64-
65-
# setup form auth (<form> in HTML)
66-
New-PodeAuthScheme -Form | Add-PodeAuth -Name 'Login' -FailureUrl '/login' -SuccessUrl '/' -ScriptBlock {
67-
param($username, $password)
68-
69-
# here you'd check a real user storage, this is just for example
70-
if ($username -eq 'morty' -and $password -eq 'pickle') {
71-
return @{
72-
User = @{
73-
ID = 'M0R7Y302'
74-
Name = 'Morty'
75-
Type = 'Human'
76-
}
77-
}
78-
}
79-
80-
return @{ Message = 'Invalid details supplied' }
81-
}
82-
83-
84-
# home page:
85-
# redirects to login page if not authenticated
86-
Add-PodeRoute -Method Get -Path '/' -Authentication Login -ScriptBlock {
87-
$session:Views++
88-
89-
Write-PodeViewResponse -Path 'auth-home' -Data @{
90-
Username = $WebEvent.Auth.User.Name
91-
Views = $session:Views
92-
Expiry = Get-PodeSessionExpiry
93-
}
94-
}
95-
96-
97-
# login page:
98-
# the login flag set below checks if there is already an authenticated session cookie. If there is, then
99-
# the user is redirected to the home page. If there is no session then the login page will load without
100-
# checking user authetication (to prevent a 401 status)
101-
Add-PodeRoute -Method Get -Path '/login' -Authentication Login -Login -ScriptBlock {
102-
Write-PodeViewResponse -Path 'auth-login' -FlashMessages
103-
}
104-
105-
106-
# login check:
107-
# this is the endpoint the <form>'s action will invoke. If the user validates then they are set against
108-
# the session as authenticated, and redirect to the home page. If they fail, then the login page reloads
109-
Add-PodeRoute -Method Post -Path '/login' -Authentication Login -Login
110-
111-
112-
# logout check:
113-
# when the logout button is click, this endpoint is invoked. The logout flag set below informs this call
114-
# to purge the currently authenticated session, and then redirect back to the login page
115-
Add-PodeRoute -Method Post -Path '/logout' -Authentication Login -Logout
1+
<#
2+
.SYNOPSIS
3+
Sample script demonstrating session persistent authentication using Pode.
4+
5+
.DESCRIPTION
6+
This script sets up a Pode server that listens on localhost:8081 and uses session-based authentication
7+
for user logins. The authentication is demonstrated using a simple form where users can log in with
8+
predefined credentials (username: morty, password: pickle). Upon successful login, users are greeted
9+
on the home page, and the view counter is incremented. Users can log out, which will purge the session
10+
and redirect them to the login page.
11+
12+
.PARAMETER ScriptPath
13+
Path of the script being executed.
14+
15+
.PARAMETER podePath
16+
Path of the Pode module.
17+
18+
.EXAMPLE
19+
To run the sample: ./Web-AuthForm.ps1
20+
21+
Run this script to start the Pode server and navigate to 'http://localhost:8081' in your browser.
22+
You will be redirected to the login page, where you can log in with the credentials provided above.
23+
24+
.LINK
25+
https://github.com/Badgerati/Pode/blob/develop/examples/Web-AuthForm.ps1
26+
27+
.NOTES
28+
Author: Pode Team
29+
License: MIT License
30+
#>
31+
32+
try {
33+
# Determine the script path and Pode module path
34+
$ScriptPath = (Split-Path -Parent -Path $MyInvocation.MyCommand.Path)
35+
$podePath = Split-Path -Parent -Path $ScriptPath
36+
37+
# Import the Pode module from the source path if it exists, otherwise from installed modules
38+
if (Test-Path -Path "$($podePath)/src/Pode.psm1" -PathType Leaf) {
39+
Import-Module "$($podePath)/src/Pode.psm1" -Force -ErrorAction Stop
40+
}
41+
else {
42+
Import-Module -Name 'Pode' -MaximumVersion 2.99 -ErrorAction Stop
43+
}
44+
}
45+
catch { throw }
46+
47+
# or just:
48+
# Import-Module Pode
49+
50+
# create a server, and start listening on port 8081
51+
Start-PodeServer -Threads 2 {
52+
53+
# listen on localhost:8081
54+
Add-PodeEndpoint -Address localhost -Port 8081 -Protocol Http
55+
56+
# set the view engine
57+
Set-PodeViewEngine -Type Pode
58+
59+
# enable error logging
60+
New-PodeLoggingMethod -Terminal | Enable-PodeErrorLogging
61+
62+
# setup session details
63+
Enable-PodeSessionMiddleware -Duration 120 -Extend
64+
65+
# setup form auth (<form> in HTML)
66+
New-PodeAuthScheme -Form | Add-PodeAuth -Name 'Login' -FailureUrl '/login' -SuccessUrl '/' -ScriptBlock {
67+
param($username, $password)
68+
69+
# here you'd check a real user storage, this is just for example
70+
if ($username -eq 'morty' -and $password -eq 'pickle') {
71+
return @{
72+
User = @{
73+
ID = 'M0R7Y302'
74+
Name = 'Morty'
75+
Type = 'Human'
76+
}
77+
}
78+
}
79+
80+
return @{ Message = 'Invalid details supplied' }
81+
}
82+
83+
84+
# home page:
85+
# redirects to login page if not authenticated
86+
Add-PodeRoute -Method Get -Path '/' -Authentication Login -ScriptBlock {
87+
$session:Views++
88+
89+
Write-PodeViewResponse -Path 'auth-home' -Data @{
90+
Username = $WebEvent.Auth.User.Name
91+
Views = $session:Views
92+
Expiry = Get-PodeSessionExpiry
93+
}
94+
}
95+
96+
97+
# login page:
98+
# the login flag set below checks if there is already an authenticated session cookie. If there is, then
99+
# the user is redirected to the home page. If there is no session then the login page will load without
100+
# checking user authentication (to prevent a 401 status)
101+
Add-PodeRoute -Method Get -Path '/login' -Authentication Login -Login -ScriptBlock {
102+
Write-PodeViewResponse -Path 'auth-login' -FlashMessages
103+
}
104+
105+
106+
# login check:
107+
# this is the endpoint the <form>'s action will invoke. If the user validates then they are set against
108+
# the session as authenticated, and redirect to the home page. If they fail, then the login page reloads
109+
Add-PodeRoute -Method Post -Path '/login' -Authentication Login -Login
110+
111+
112+
# logout check:
113+
# when the logout button is click, this endpoint is invoked. The logout flag set below informs this call
114+
# to purge the currently authenticated session, and then redirect back to the login page
115+
Add-PodeRoute -Method Post -Path '/logout' -Authentication Login -Logout
116116
}

0 commit comments

Comments
 (0)