-
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathWeb-AuthBasicHeader.ps1
More file actions
105 lines (83 loc) · 3.41 KB
/
Web-AuthBasicHeader.ps1
File metadata and controls
105 lines (83 loc) · 3.41 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
<#
.SYNOPSIS
A PowerShell script to set up a Pode server with session-based Basic authentication for REST APIs.
.DESCRIPTION
This script sets up a Pode server that listens on a specified port, enables session-based authentication
using headers, and provides login and logout functionality. Authenticated users can access a REST API endpoint
to retrieve user information.
.PARAMETER Location
The location where the API key is expected. Valid values are 'Header', 'Query', and 'Cookie'. Default is 'Header'.
.EXAMPLE
To run the sample: ./Web-AuthBasicHeader.ps1
This example shows how to use session authentication on REST APIs using Headers.
The example used here is Basic authentication.
Login:
$session = (Invoke-WebRequest -Uri http://localhost:8081/login -Method Post -Headers @{ Authorization = 'Basic bW9ydHk6cGlja2xl' }).Headers['pode.sid']
Users:
Invoke-RestMethod -Uri http://localhost:8081/users -Method Post -Headers @{ 'pode.sid' = "$session" }
Logout:
Invoke-WebRequest -Uri http://localhost:8081/logout -Method Post -Headers @{ 'pode.sid' = "$session" }
.LINK
https://github.com/Badgerati/Pode/blob/develop/examples/Web-AuthBasicHeader.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
# enable error logging
New-PodeLoggingMethod -Terminal | Enable-PodeErrorLogging
# setup session details
Enable-PodeSessionMiddleware -Duration 120 -Extend -UseHeaders -Strict
# setup basic auth (base64> username:password in header)
New-PodeAuthScheme -Basic | Add-PodeAuth -Name 'Login' -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' }
}
# POST request to login
Add-PodeRoute -Method Post -Path '/login' -Authentication 'Login'
# POST request to logout
Add-PodeRoute -Method Post -Path '/logout' -Authentication 'Login' -Logout
# POST request to get list of users - the "pode.sid" header is expected
Add-PodeRoute -Method Post -Path '/users' -Authentication 'Login' -ScriptBlock {
Write-PodeJsonResponse -Value @{
Users = @(
@{
Name = 'Deep Thought'
Age = 42
},
@{
Name = 'Leeroy Jenkins'
Age = 1337
}
)
}
}
}