Skip to content

Commit

Permalink
digest issue
Browse files Browse the repository at this point in the history
  • Loading branch information
mdaneri committed Jan 23, 2025
1 parent e577132 commit fe397f8
Show file tree
Hide file tree
Showing 5 changed files with 5,303 additions and 7 deletions.
31 changes: 27 additions & 4 deletions examples/Web-AuthDigest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,29 @@
.EXAMPLE
To run the sample: ./Web-AuthDigest.ps1
Invoke-RestMethod -Uri http://localhost:8081/users -Method Get
# Define the URI and credentials
$uri = [System.Uri]::new("http://localhost:8081/users")
$username = "morty"
$password = "pickle"
# Create a credential cache and add Digest authentication
$credentialCache = [System.Net.CredentialCache]::new()
$networkCredential = [System.Net.NetworkCredential]::new($username, $password)
$credentialCache.Add($uri, "Digest", $networkCredential)
# Create the HTTP client handler with the credential cache
$handler = [System.Net.Http.HttpClientHandler]::new()
$handler.Credentials = $credentialCache
# Create the HTTP client
$httpClient = [System.Net.Http.HttpClient]::new($handler)
# Send the GET request and capture the response
$response = $httpClient.GetStringAsync($uri).Result
# Display the response
$response
.LINK
https://github.com/Badgerati/Pode/blob/develop/examples/Web-AuthDigest.ps1
Expand Down Expand Up @@ -45,7 +67,7 @@ Start-PodeServer -Threads 2 {
# setup digest auth
New-PodeAuthScheme -Digest | Add-PodeAuth -Name 'Validate' -Sessionless -ScriptBlock {
param($username, $params)

write-podehost "username=$username"
# here you'd check a real user storage, this is just for example
if ($username -ieq 'morty') {
return @{
Expand All @@ -57,12 +79,13 @@ Start-PodeServer -Threads 2 {
Password = 'pickle'
}
}

write-podehost 'no auth'
return $null
}

# GET request to get list of users (since there's no session, authentication will always happen)
Add-PodeRoute -Method Get -Path '/users' -Authentication 'Validate' -ScriptBlock {
Add-PodeRoute -Method Get -Path '/users' -Authentication 'Validate' -ScriptBlock {
write-podehsot '1'
Write-PodeJsonResponse -Value @{
Users = @(
@{
Expand Down
40 changes: 39 additions & 1 deletion examples/Web-AuthManualErrorHandling.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,19 @@ Start-PodeServer {
return @{ Success = $false; User = $key; Reason = 'Not existing user' }
}


New-PodeAuthScheme -ApiKey | Add-PodeAuth -Name 'APIKey_standard' -Sessionless -ScriptBlock {
param($key)

# Validate API key
if ($key -eq 'test_user') {
return @{ Success = $true; User = 'test_user'; UserId = 1 }
}

}

# Define an API route with manual authentication error handling
Add-PodeRoute -PassThru -Method 'Get' -Path '/api/v3/' -Authentication 'APIKey' -NoMiddlewareAuthentication -ScriptBlock {
Add-PodeRoute -PassThru -Method 'Get' -Path '/api/v3/whoami' -Authentication 'APIKey' -NoMiddlewareAuthentication -ScriptBlock {
# Manually invoke authentication
$auth = Invoke-PodeAuth -Name 'APIKey'

Expand All @@ -95,4 +106,31 @@ Start-PodeServer {
} | Set-PodeOARouteInfo -Summary 'Who am I' -Tags 'auth' -OperationId 'whoami' -PassThru |
Add-PodeOAResponse -StatusCode 200 -Description 'Successful operation' -Content @{ 'application/json' = (New-PodeOABoolProperty -Name 'Success' -Default $true | New-PodeOAStringProperty -Name 'Username' | New-PodeOAIntProperty -Name 'UserId' | New-PodeOAObjectProperty ) } -PassThru |
Add-PodeOAResponse -StatusCode 401 -Description 'Authentication failure' -Content @{ 'application/json' = (New-PodeOABoolProperty -Name 'Success' -Default $false | New-PodeOAStringProperty -Name 'Username' | New-PodeOAStringProperty -Name 'Message' | New-PodeOAObjectProperty ) }

Add-PodeRoute -PassThru -Method 'Get' -Path '/api/v3/whoami_standard' -Authentication 'APIKey_standard' -ErrorContentType 'application/json' -ScriptBlock {
# Manually invoke authentication
# $auth = Invoke-PodeAuth -Name 'APIKey'

# Log authentication details for debugging
Write-PodeHost $auth -Explode

# If authentication succeeds, return user details
if ($auth.Success) {
Write-PodeJsonResponse -StatusCode 200 -Value @{
Success = $true
Username = $auth.User
UserId = $auth.UserId
}
}
else {
# Handle authentication failures with a custom error response
Write-PodeJsonResponse -StatusCode 401 -Value @{
Success = $false
Message = $auth.Reason
Username = $auth.User
}
}
} | Set-PodeOARouteInfo -Summary 'Who am I (default auth)' -Tags 'auth' -OperationId 'whoami_standard' -PassThru |
Add-PodeOAResponse -StatusCode 200 -Description 'Successful operation' -Content @{ 'application/json' = (New-PodeOABoolProperty -Name 'Success' -Default $true | New-PodeOAStringProperty -Name 'Username' | New-PodeOAIntProperty -Name 'UserId' | New-PodeOAObjectProperty ) } -PassThru |
Add-PodeOAResponse -StatusCode 401 -Description 'Authentication failure' -Content @{ 'application/json' = (New-PodeOABoolProperty -Name 'Success' -Default $false | New-PodeOAStringProperty -Name 'Username' | New-PodeOAStringProperty -Name 'Message' | New-PodeOAObjectProperty ) }
}
Loading

0 comments on commit fe397f8

Please sign in to comment.