Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Session Authentication, and Refactor Authorisation #1166

Merged
merged 2 commits into from
Oct 12, 2023
Merged

Conversation

Badgerati
Copy link
Owner

Description of the Change

  • Adds a new "Session Authentication" method, which can be added using the new Add-PodeAuthSession
  • Refactors the Authorisation functions to be in their own space, away from Auth. For example, Add-PodeAuthAccess is now Add-PodeAccess
  • Access is not set at the Route level, not the Authentication level, making even more dynamic scenarios possible
  • Authorisation is now its own separate middleware, away from Authentication
  • Fixes Authorisation not working appropriately when sessions are being used
  • Splits Add-PodeAccess into New-PodeAccessScheme and Add-PodeAccess - this works similar to the Auth New/Add functions

Related Issue

Resolves #1163

Examples

Start-PodeServer {
    Add-PodeEndpoint -Address * -Port 8085 -Protocol Http

    # setup access
    New-PodeAccessScheme -Type Role | Add-PodeAccess -Name 'TestRbac'
    New-PodeAccessScheme -Type Group | Add-PodeAccess -Name 'TestGbac'

    # merge access
    Merge-PodeAccess -Name 'TestMergedAll' -Access 'TestRbac', 'TestGbac' -Valid All
    Merge-PodeAccess -Name 'TestMergedOne' -Access 'TestRbac', 'TestGbac' -Valid One

    # setup basic auth (base64> username:password in header)
    New-PodeAuthScheme -Basic -Realm 'Pode Example Page' | Add-PodeAuth -Name 'Validate' -Sessionless -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 = @{
                    Name = 'Morty'
                    Roles = @('Developer')
                    Groups = @('Software', 'Admins')
                }
            }
        }

        return @{ Message = 'Invalid details supplied' }
    }

    # POST request to get list of users - there's no Access, so any auth'd user can access
    Add-PodeRoute -Method Post -Path '/users-all' -Authentication 'Validate' -ScriptBlock {
        Write-PodeJsonResponse -Value @{
            Users = @(@{ Name = 'Deep Thought' })
        }
    }

    # POST request to get list of users - only Developer roles can access
    Add-PodeRoute -Method Post -Path '/users-dev' -Authentication 'Validate' -Access 'TestRbac' -Role Developer -ScriptBlock {
        Write-PodeJsonResponse -Value @{
            Users = @(@{ Name = 'Leeroy Jenkins' })
        }
    }

    # POST request to get list of users - only users in the SOftware group can access
    Add-PodeRoute -Method Post -Path '/users-soft' -Authentication 'Validate' -Access 'TestGbac' -Group Software -ScriptBlock {
        Write-PodeJsonResponse -Value @{
            Users = @(@{ Name = 'Smooth McGroove' })
        }
    }

    # POST request to get list of users - only Developer role in the Admins group can access
    Add-PodeRoute -Method Post -Path '/users-dev-admin' -Authentication 'Validate' -Access 'TestMergedAll' -Role Developer -Group Admins -ScriptBlock {
        Write-PodeJsonResponse -Value @{
            Users = @(@{ Name = 'Arthur Dent' })
        }
    }

    # POST request to get list of users - either DevOps role or Admins group can access
    Add-PodeRoute -Method Post -Path '/users-devop-admin' -Authentication 'Validate' -Access 'TestMergedOne' -Role DevOps -Group Admins -ScriptBlock {
        Write-PodeJsonResponse -Value @{
            Users = @(@{ Name = 'Monkey D. Luffy' })
        }
    }
}

@Badgerati Badgerati added this to the 2.9.0 milestone Oct 12, 2023
@Badgerati Badgerati self-assigned this Oct 12, 2023
@Badgerati Badgerati merged commit 1ca29ee into develop Oct 12, 2023
8 checks passed
@Badgerati Badgerati deleted the Issue-1163 branch October 12, 2023 20:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a new "Session" Authentication type
1 participant