forked from Badgerati/Pode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeb-AuthForm.ps1
More file actions
116 lines (89 loc) · 4.02 KB
/
Web-AuthForm.ps1
File metadata and controls
116 lines (89 loc) · 4.02 KB
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
<#
.SYNOPSIS
Sample script demonstrating session persistent authentication using Pode.
.DESCRIPTION
This script sets up a Pode server that listens on localhost:8081 and uses session-based authentication
for user logins. The authentication is demonstrated using a simple form where users can log in with
predefined credentials (username: morty, password: pickle). Upon successful login, users are greeted
on the home page, and the view counter is incremented. Users can log out, which will purge the session
and redirect them to the login page.
.PARAMETER ScriptPath
Path of the script being executed.
.PARAMETER podePath
Path of the Pode module.
.EXAMPLE
To run the sample: ./Web-AuthForm.ps1
Run this script to start the Pode server and navigate to 'http://localhost:8081' in your browser.
You will be redirected to the login page, where you can log in with the credentials provided above.
.LINK
https://github.com/Badgerati/Pode/blob/develop/examples/Web-AuthForm.ps1
.NOTES
Author: Pode Team
License: MIT License
#>
try {
# Determine the script path and Pode module path
$ScriptPath = (Split-Path -Parent -Path $MyInvocation.MyCommand.Path)
$podePath = Split-Path -Parent -Path $ScriptPath
# Import the Pode module from the source path if it exists, otherwise from installed modules
if (Test-Path -Path "$($podePath)/src/Pode.psm1" -PathType Leaf) {
Import-Module "$($podePath)/src/Pode.psm1" -Force -ErrorAction Stop
}
else {
Import-Module -Name 'Pode' -MaximumVersion 2.99 -ErrorAction Stop
}
}
catch { throw }
# or just:
# Import-Module Pode
# create a server, and start listening on port 8081
Start-PodeServer -Threads 2 {
# listen on localhost:8081
Add-PodeEndpoint -Address localhost -Port 8081 -Protocol Http
# set the view engine
Set-PodeViewEngine -Type Pode
# enable error logging
New-PodeLoggingMethod -Terminal | Enable-PodeErrorLogging
# setup session details
Enable-PodeSessionMiddleware -Duration 120 -Extend
# setup form auth (<form> in HTML)
New-PodeAuthScheme -Form | Add-PodeAuth -Name 'Login' -FailureUrl '/login' -SuccessUrl '/' -ScriptBlock {
param($username, $password)
# here you'd check a real user storage, this is just for example
if ($username -eq 'morty' -and $password -eq 'pickle') {
return @{
User = @{
ID = 'M0R7Y302'
Name = 'Morty'
Type = 'Human'
}
}
}
return @{ Message = 'Invalid details supplied' }
}
# home page:
# redirects to login page if not authenticated
Add-PodeRoute -Method Get -Path '/' -Authentication Login -ScriptBlock {
$session:Views++
Write-PodeViewResponse -Path 'auth-home' -Data @{
Username = $WebEvent.Auth.User.Name
Views = $session:Views
Expiry = Get-PodeSessionExpiry
}
}
# login page:
# the login flag set below checks if there is already an authenticated session cookie. If there is, then
# the user is redirected to the home page. If there is no session then the login page will load without
# checking user authetication (to prevent a 401 status)
Add-PodeRoute -Method Get -Path '/login' -Authentication Login -Login -ScriptBlock {
Write-PodeViewResponse -Path 'auth-login' -FlashMessages
}
# login check:
# this is the endpoint the <form>'s action will invoke. If the user validates then they are set against
# the session as authenticated, and redirect to the home page. If they fail, then the login page reloads
Add-PodeRoute -Method Post -Path '/login' -Authentication Login -Login
# logout check:
# when the logout button is click, this endpoint is invoked. The logout flag set below informs this call
# to purge the currently authenticated session, and then redirect back to the login page
Add-PodeRoute -Method Post -Path '/logout' -Authentication Login -Logout
}