Last active
September 10, 2024 07:05
-
-
Save luke-z/f3c8d95637a5f7009835ac9935ca2e77 to your computer and use it in GitHub Desktop.
Basic filter example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ------------------------------------------------------------------------------ # | |
# EXAMPLE: v9 Auth | |
Add-Type -Path "dll_v9\PsrApi.dll" | |
$psrApi = New-Object PsrApi.PsrApi("url/api") | |
$authenticationFlow = $psrApi.AuthenticationManagerV2.StartNewAuthentication("db", "user") | |
$startLoginTask = $authenticationFlow.StartLogin() | |
$startLoginTask.Wait() | |
$requirement = $authenticationFlow.GetNextRequirement() | |
$selectedRequirement = $requirement.PossibleRequirements[0] | |
foreach ($field in $selectedRequirement.Fields) { | |
$field.Value = "password" | |
} | |
$authenticateTask = $authenticationFlow.Authenticate($selectedRequirement) | |
try { | |
$authenticateTask.Wait() | |
} | |
catch { | |
Write-Error "Login failed. Please check the credentials and try again." | |
return | |
} | |
# ------------------------------------------------------------------------------ # | |
# EXAMPLE: v8 Auth | |
Add-Type -Path "dll_v8\PsrApi.dll" | |
$psrApi = New-Object PsrApi.PsrApi("url/api") | |
$loginTask = $psrApi.AuthenticationManager.Login("db", "username", "password") | |
try { | |
$loginTask.Wait() | |
} | |
catch { | |
Write-Error "Login failed. Please check the credentials and try again." | |
return | |
} | |
# ------------------------------------------------------------------------------ # | |
# EXAMPLE: Get container by ID | |
$containerTask = $psrApi.ContainerManager.GetContainer("containerid") | |
$containerTask.Wait() | |
Write-Host ($containerTask.Result | Format-List | Out-String) | |
# ------------------------------------------------------------------------------ # | |
# EXAMPLE: Get all password containers | |
$defaultContainerFilter1 = New-Object PsrApi.Data.PsrContainerListFilter | |
$getAllPasswordsTask = $psrApi.ContainerManager.GetContainerList([PsrApi.Data.Enums.PsrContainerType]::Password, $defaultContainerFilter1) | |
$getAllPasswordsTask.Wait() | |
Write-Host ($getAllPasswordsTask.Result | Format-List | Out-String) | |
# ------------------------------------------------------------------------------ # | |
# EXAMPLE: Filtering | |
# Link for clarification (class documentation): https://help.passwordsafe.de/api/v9/html/a8bd7b37-84b1-d8ab-e6b5-1ded000de5f7.htm | |
# Create base filter | |
$defaultContainerFilter2 = New-Object PsrApi.Data.PsrContainerListFilter | |
# Filter by content | |
$listFilterObject = New-Object PsrApi.Data.PsrListFilterObjectContent | |
$listFilterObject.Search = "search term" | |
$listFilterObject.FilterActive = $true | |
# Add filter object to filter group | |
$filterGroup = New-Object PsrApi.Data.PsrListFilterGroupContent | |
$filterGroup.SearchList += $listFilterObject | |
# Add filter group to the base filter | |
$defaultContainerFilter2.FilterGroups += $filterGroup | |
# Get all password containers based on the filter | |
$findPasswordsTask = $psrApi.ContainerManager.GetContainerList([PsrApi.Data.Enums.PsrContainerType]::Password, $defaultContainerFilter2) | |
$findPasswordsTask.Wait() | |
Write-Host ($findPasswordsTask.Result | Format-List | Out-String) | |
# ------------------------------------------------------------------------------ # | |
# EXAMPLE: Add Password Container | |
# Text item (at least one is required) | |
$containerItemText = New-Object PsrApi.Data.PsrContainerItem | |
$containerItemText.Name = "Description" | |
$containerItemText.Value = "This is a test description" | |
$containerItemText.ContainerItemType = [PsrApi.Data.Enums.PsrContainerItemType]::ContainerItemText | |
# Password item (value must be "PlainTextValue", will be encrypted automatically) | |
$containerItemPassword = New-Object PsrApi.Data.PsrContainerItem | |
$containerItemPassword.Name = "Password" | |
$containerItemPassword.PlainTextValue = "password123" | |
$containerItemPassword.ContainerItemType = [PsrApi.Data.Enums.PsrContainerItemType]::ContainerItemPassword | |
$itemsList = [System.Collections.Generic.List[PsrApi.Data.PsrContainerItem]]::new() | |
$itemsList.Add($containerItemText) | |
$itemsList.Add($containerItemPassword) | |
$passwordContainer = @{ | |
ContainerType = [PsrApi.Data.Enums.PsrContainerType]::Password | |
Items = $itemsList | |
} | |
$ouId = "0ec4cf8a-b987-ee11-810e-xxxxxxxxxxxx" # Specific OU ID | |
$containerTask = $psrApi.ContainerManager.AddContainer($passwordContainer, $ouId) | |
$containerTask.Wait() | |
Write-Host ($containerTask.Result | Format-List | Out-String) | |
# ------------------------------------------------------------------------------ # | |
# EXAMPLE: Add user to role | |
$roleId = "17db08cd-2d69-ef11-811d-fd7e0d268a42" | |
$getRoleTask = $psrApi.RoleManager.GetRole($roleId) | |
$getRoleTask.Wait() | |
$role = $getRoleTask.Result | |
$userId = "2c6cf506-138d-ee11-810e-ee70fdafccf2" | |
$getUserTask = $psrApi.OrganisationUnitManager.GetOrganisationUnitUser($userId) | |
$getUserTask.Wait() | |
$user = $getUserTask.Result | |
$newRight = New-Object -TypeName PsrApi.Data.PsrDataRight | |
$newRight.DataId = $role.Id | |
$newRight.LegitimateId = $user.Id | |
$newRight.Legitimate = $user | |
$newRight.IncludeDataRightKey = $true | |
$newRight.Rights = [PsrApi.Data.Enums.PsrRights]::RightRead | |
$getRightsTask = $psrApi.RightManager.GetLegitimateDataRights($role.Id) | |
$getRightsTask.Wait() | |
$rights = $getRightsTask.Result | |
$rights.Add($newRight) | |
$roles = [System.Collections.Generic.List[PsrApi.Data.PsrData]]::new() | |
$roles.Add($role) | |
$saveRightsTask = $psrApi.GenericRightManager.SaveRights($roles, $rights, $false, $false) | |
$saveRightsTask.Wait() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment