Skip to content

Commit

Permalink
Merge pull request #5 from bepsoccer/accessApiWork
Browse files Browse the repository at this point in the history
Start of Access user functions
  • Loading branch information
bepsoccer authored Feb 22, 2023
2 parents 835f5ec + 21e850d commit dd1bcd0
Show file tree
Hide file tree
Showing 12 changed files with 576 additions and 16 deletions.
53 changes: 53 additions & 0 deletions Private/Invoke-VerkadaFormCall.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
function Invoke-VerkadaFormCall
{
<#
.SYNOPSIS
Used to build an Invoke-RestMethod call for Verkada's AC API
.DESCRIPTION
.NOTES
.EXAMPLE
.LINK
#>

[CmdletBinding(PositionalBinding = $true)]
Param(
[Parameter(Mandatory = $true, Position = 0)]
[String]$url,
[Parameter(Mandatory = $true, Position = 1)]
[String]$org_id,
[Parameter(Mandatory = $true,Position = 3)]
[Object]$form_params,
[Parameter()]
[String]$method = 'POST',
[Parameter()]
[string]$x_verkada_token = $Global:verkadaConnection.csrfToken,
[Parameter()]
[string]$x_verkada_auth = $Global:verkadaConnection.userToken

)

Begin {
#if (!($Global:verkadaConnection)){Write-Warning 'Missing auth token which is required'; return}
#if ($Global:verkadaConnection.authType -ne 'UnPwd'){Write-Warning 'Un/Pwd auth is required'; return}
}

Process {
#$form = @{}
#$form_params.psobject.properties | Foreach { $form[$_.Name] = $_.Value }

$headers=@{
'x-verkada-token' = $x_verkada_token
'X-Verkada-Auth' = $x_verkada_auth
}

$uri = $url

$response = Invoke-RestMethod -Uri $uri -Form $form_params -Headers $headers -Method $method -ContentType 'multipart/form-data' -MaximumRetryCount 3 -TimeoutSec 30 -RetryIntervalSec 5
return $response

} #end process
} #end function
71 changes: 71 additions & 0 deletions Private/Invoke-VerkadaGraphqlCall.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
function Invoke-VerkadaGraphqlCall
{
<#
.SYNOPSIS
Used to build an Invoke-RestMethod call for Verkada's Graphql enpoint
.DESCRIPTION
.NOTES
.EXAMPLE
.LINK
#>

[CmdletBinding(PositionalBinding = $true)]
Param(
[Parameter(Mandatory = $true, Position = 0)]
[String]$url,
[Parameter(Mandatory = $true, Position = 1, ParameterSetName = 'body')]
[Object]$body,
[Parameter()]
[String]$method = 'GET',
[Parameter()]
[int]$page_size = 100,
[Parameter(Mandatory = $true)]
[String]$propertyName,
[Parameter(Mandatory = $true, Position = 1, ParameterSetName = 'query')]
[object]$query,
[Parameter(Mandatory = $true, Position = 2, ParameterSetName = 'query')]
[object]$qlVariables

)

Process {
if (!($Global:verkadaConnection)){Write-Warning 'Missing auth token which is required'; return}
if ($Global:verkadaConnection.authType -ne 'UnPwd'){Write-Warning 'Un/Pwd auth is required'; return}

if ($query) {
$body = @{
'query' = $query
'variables' = $variables
}
}

$body.variables.pagination.pageSize = $page_size
$body.variables.pagination.pageToken = $null

$cookies = @{
'auth' = $Global:verkadaConnection.userToken
'org' = $Global:verkadaConnection.org_id
'token' = $Global:verkadaConnection.csrfToken
'usr' = $Global:verkadaConnection.usr = $response.userId
}

$session = New-WebSession $cookies $url

$uri = $url
$records = @()

Do {
$bodyJson = $body | ConvertTo-Json -depth 100 -Compress
$response = Invoke-RestMethod -Uri $uri -Body $bodyJson -ContentType 'application/json' -WebSession $session -Method $method -MaximumRetryCount 3 -TimeoutSec 30 -RetryIntervalSec 5
$records += $response.data.($propertyName).($propertyName)
$body.variables.pagination.pageToken = $response.data.($propertyName).nextPageToken
} While ($body.variables.pagination.pageToken)

return $records

} #end process
} #end function
40 changes: 31 additions & 9 deletions Private/Invoke-VerkadaRestMethod.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ function Invoke-VerkadaRestMethod
Param(
[Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'Default')]
[Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'Pagination')]
[Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'UnPwd')]
[String]$url,
[Parameter(Mandatory = $true, Position = 1, ParameterSetName = 'Default')]
[Parameter(Mandatory = $true, Position = 1, ParameterSetName = 'Pagination')]
[Parameter(Mandatory = $true, Position = 1, ParameterSetName = 'UnPwd')]
[String]$org_id,
[Parameter(Mandatory = $true, Position = 2, ParameterSetName = 'Default')]
[Parameter(Mandatory = $true, Position = 2, ParameterSetName = 'Pagination')]
Expand All @@ -29,16 +31,24 @@ function Invoke-VerkadaRestMethod
[Object]$query_params,
[Parameter(Position = 4, ParameterSetName = 'Default')]
[Parameter(Position = 4, ParameterSetName = 'Pagination')]
[Parameter(Position = 2, ParameterSetName = 'UnPwd')]
[Object]$body_params,
[Parameter(ParameterSetName = 'Default')]
[Parameter(ParameterSetName = 'Pagination')]
[Parameter(ParameterSetName = 'UnPwd')]
[String]$method = 'GET',
[Parameter(Mandatory = $true, ParameterSetName = 'Pagination')]
[switch]$pagination,
[Parameter(Mandatory = $true, ParameterSetName = 'Pagination')]
[String]$page_size,
[Parameter(Mandatory = $true, ParameterSetName = 'Pagination')]
[String]$propertyName
[String]$propertyName,
[Parameter(Mandatory = $true,ParameterSetName = 'UnPwd')]
[string]$x_verkada_token = $Global:verkadaConnection.csrfToken,
[Parameter(Mandatory = $true,ParameterSetName = 'UnPwd')]
[string]$x_verkada_auth = $Global:verkadaConnection.userToken,
[Parameter(ParameterSetName = 'UnPwd')]
[Switch]$UnPwd

)

Expand All @@ -54,28 +64,40 @@ function Invoke-VerkadaRestMethod
$body += $body_params
$body = $body | ConvertTo-Json
}
$headers=@{
'x-api-key' = $x_api_key
if ($UnPwd){
$headers=@{
'x-verkada-token' = $x_verkada_token
'X-Verkada-Auth' = $x_verkada_auth
}
} else {
$headers=@{
'x-api-key' = $x_api_key
}
}

if ($pagination){
if ($pagination.IsPresent){
$query.add('page_size', $page_size)
$query.add('page_token', '1')
$uri = [System.UriBuilder]"$url"
$uri.Query = $query.ToString()
$uri = $uri.Uri.OriginalString
$records = @()
Do {
$response = Invoke-RestMethod -Uri $uri -Body $body -Headers $headers -ContentType 'application/json'
$response = Invoke-RestMethod -Uri $uri -Body $body -Headers $headers -ContentType 'application/json' -MaximumRetryCount 3 -TimeoutSec 30 -RetryIntervalSec 5
$records += $response.($propertyName)
$body.page_token = $response.next_page_token
} While ($body.page_token)
return $records
} else {
$uri = [System.UriBuilder]"$url"
$uri.Query = $query.ToString()
$uri = $uri.Uri.OriginalString
$response = Invoke-RestMethod -Uri $uri -Body $body -Headers $headers -Method $method -ContentType 'application/json'
if ($UnPwd.IsPresent) {
$uri = $url
} else {
$uri = [System.UriBuilder]"$url"
$uri.Query = $query.ToString()
$uri = $uri.Uri.OriginalString
}

$response = Invoke-RestMethod -Uri $uri -Body $body -Headers $headers -Method $method -ContentType 'application/json' -MaximumRetryCount 3 -TimeoutSec 30 -RetryIntervalSec 5
return $response
}

Expand Down
20 changes: 20 additions & 0 deletions Private/New-WebSession.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function New-WebSession {
param(
[hashtable]$Cookies,
[Uri]$For
)

$newSession = [Microsoft.PowerShell.Commands.WebRequestSession]::new()

foreach($entry in $Cookies.GetEnumerator()){
$cookie = [System.Net.Cookie]::new($entry.Name, $entry.Value)
if($For){
$newSession.Cookies.Add([uri]::new($For, '/'), $cookie)
}
else{
$newSession.Cookies.Add($cookie)
}
}

return $newSession
}
69 changes: 69 additions & 0 deletions Public/Add-VerkadaAccessBadgeToUser.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
function Add-VerkadaAccessBadgeToUser
{
<#
.SYNOPSIS
Adds a badger to an Access User in an organization
.DESCRIPTION
.NOTES
.EXAMPLE
.LINK
#>

[CmdletBinding(PositionalBinding = $true, DefaultParameterSetName = 'email')]
Param(
[Parameter(ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[String]$org_id = $Global:verkadaConnection.org_id,
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[String]$userId,
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[String]$cardType,
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName = 'cardNumber')]
[String]$cardNumber,
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName = 'cardNumberHex')]
[String]$cardNumberHex,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[String]$facilityCode,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$x_verkada_token = $Global:verkadaConnection.csrfToken,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$x_verkada_auth = $Global:verkadaConnection.userToken,
[Parameter()]
[int]$threads=$null

)

Begin {
#if (!($org_id)){Write-Warning 'Missing org_id which is required'; return}
#if (!($Global:verkadaConnection)){Write-Warning 'Missing auth token which is required'; return}
#if ($Global:verkadaConnection.authType -ne 'UnPwd'){Write-Warning 'Un/Pwd auth is required'; return}

$url = "https://vcerberus.command.verkada.com/user/access_card/add"
} #end begin

Process {
$body_params = @{
"userId" = $userId
"organizationId" = $org_id
"cardType" = $cardType
}
$body_params.cardParams = @{}
if (!([string]::IsNullOrEmpty($cardNumber))){$body_params.cardParams.cardNumber = $cardNumber}
if (!([string]::IsNullOrEmpty($cardNumberHex))){$body_params.cardParams.cardNumberHex = $cardNumberHex}
if (!([string]::IsNullOrEmpty($facilityCode))){$body_params.cardParams.facilityCode = $facilityCode}

Invoke-VerkadaRestMethod $url $org_id $body_params -x_verkada_token $x_verkada_token -x_verkada_auth $x_verkada_auth -Method 'POST' -UnPwd

} #end process

End {

}
} #end function
Loading

0 comments on commit dd1bcd0

Please sign in to comment.