Skip to content

Commit 3f4ce2c

Browse files
authored
Merge pull request #179 from bepsoccer/release
New Function for OTP
2 parents 2aba246 + caca9ff commit 3f4ce2c

6 files changed

Lines changed: 253 additions & 20 deletions

File tree

docs/function-documentation/Connect-Verkada.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,26 @@ This will store the org_id 7cd47706-f51b-4419-8675-3b9f0ce7c12d with the public
4343

4444
### EXAMPLE 2
4545
```
46-
Connect-Verkada '7cd47706-f51b-4419-8675-3b9f0ce7c12d' -userName "admin.user@contoso.com" -Password
47-
This will authenticate user admin.user@contoso.com by prompting for the password(stored as a secure string) and upon success store the org_id 7cd47706-f51b-4419-8675-3b9f0ce7c12d and the returned tokens.
46+
Connect-Verkada '7cd47706-f51b-4419-8675-3b9f0ce7c12d' -userName "admin.user@contoso.com" -otp (Get-Otp (Get-Secret -Name myVerkadaOtp -AsPlainText)) -MyPwd (Get-Secret -Name myVerkadaPassword) -x_api_key 'myapiKey-dcwdskjnlnlkj'
47+
This will authenticate user admin.user@contoso.com with a otp token and a secure string variable stored password([secureString]$yourPwd) and upon success store the org_id 7cd47706-f51b-4419-8675-3b9f0ce7c12d and the returned tokens. This will also store the org_id 7cd47706-f51b-4419-8675-3b9f0ce7c12d with the public API key myapiKey-dcwdskjnlnlkj
4848
```
4949

5050
### EXAMPLE 3
5151
```
52-
Connect-Verkada '7cd47706-f51b-4419-8675-3b9f0ce7c12d' -userName "admin.user@contoso.com" -otp '123456' -MyPwd $yourPwd(seure string)
53-
This will authenticate user admin.user@contoso.com with a otp token and a secure string variable stored password([secureString]$yourPwd) and upon success store the org_id 7cd47706-f51b-4419-8675-3b9f0ce7c12d and the returned tokens.
52+
Connect-Verkada '7cd47706-f51b-4419-8675-3b9f0ce7c12d' -userName "admin.user@contoso.com" -Password
53+
This will authenticate user admin.user@contoso.com by prompting for the password(stored as a secure string) and upon success store the org_id 7cd47706-f51b-4419-8675-3b9f0ce7c12d and the returned tokens. This will no longer work for OrgAdmins due to the MFA requirement.
5454
```
5555

5656
### EXAMPLE 4
5757
```
58+
Connect-Verkada '7cd47706-f51b-4419-8675-3b9f0ce7c12d' -userName "admin.user@contoso.com" -otp '123456' -MyPwd $yourPwd(seure string)
59+
This will authenticate user admin.user@contoso.com with a otp token and a secure string variable stored password([secureString]$yourPwd) and upon success store the org_id 7cd47706-f51b-4419-8675-3b9f0ce7c12d and the returned tokens. This will no longer work for OrgAdmins due to the MFA requirement.
60+
```
61+
62+
### EXAMPLE 5
63+
```
5864
Connect-Verkada '7cd47706-f51b-4419-8675-3b9f0ce7c12d' -x_api_key 'myapiKey-dcwdskjnlnlkj' -userName "admin.user@contoso.com" -Password
59-
This will store the org_id 7cd47706-f51b-4419-8675-3b9f0ce7c12d with the public API key myapiKey-dcwdskjnlnlkj and will authenticate user admin.user@contoso.com by prompting for the password(stored as a secure string) and storing the returned tokens.
65+
This will store the org_id 7cd47706-f51b-4419-8675-3b9f0ce7c12d with the public API key myapiKey-dcwdskjnlnlkj and will authenticate user admin.user@contoso.com by prompting for the password(stored as a secure string) and storing the returned tokens. This will no longer work for OrgAdmins due to the MFA requirement.
6066
```
6167

6268
## PARAMETERS
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
---
2+
external help file: verkadaModule-help.xml
3+
Module Name: verkadaModule
4+
online version: https://github.com/bepsoccer/verkadaModule/blob/master/docs/function-documentation/Get-Otp.md
5+
schema: 2.0.0
6+
---
7+
8+
# Get-Otp
9+
10+
## SYNOPSIS
11+
Time-base One-Time Password Algorithm (RFC 6238)
12+
13+
## SYNTAX
14+
15+
```
16+
Get-Otp [-SECRET] <Object> [[-LENGTH] <Object>] [[-WINDOW] <Object>] [-ProgressAction <ActionPreference>]
17+
[<CommonParameters>]
18+
```
19+
20+
## DESCRIPTION
21+
This is an implementation of the RFC 6238 Time-Based One-Time Password Algorithm draft based upon the HMAC-based One-Time Password (HOTP) algorithm (RFC 4226).
22+
This is a time based variant of the HOTP algorithm providing short-lived OTP values.
23+
24+
## EXAMPLES
25+
26+
### EXAMPLE 1
27+
```
28+
Get-Otp MySecretTotpKey
29+
```
30+
31+
## PARAMETERS
32+
33+
### -SECRET
34+
{{ Fill SECRET Description }}
35+
36+
```yaml
37+
Type: Object
38+
Parameter Sets: (All)
39+
Aliases:
40+
41+
Required: True
42+
Position: 1
43+
Default value: None
44+
Accept pipeline input: False
45+
Accept wildcard characters: False
46+
```
47+
48+
### -LENGTH
49+
{{ Fill LENGTH Description }}
50+
51+
```yaml
52+
Type: Object
53+
Parameter Sets: (All)
54+
Aliases:
55+
56+
Required: False
57+
Position: 2
58+
Default value: 6
59+
Accept pipeline input: False
60+
Accept wildcard characters: False
61+
```
62+
63+
### -WINDOW
64+
{{ Fill WINDOW Description }}
65+
66+
```yaml
67+
Type: Object
68+
Parameter Sets: (All)
69+
Aliases:
70+
71+
Required: False
72+
Position: 3
73+
Default value: 30
74+
Accept pipeline input: False
75+
Accept wildcard characters: False
76+
```
77+
78+
### -ProgressAction
79+
{{ Fill ProgressAction Description }}
80+
81+
```yaml
82+
Type: ActionPreference
83+
Parameter Sets: (All)
84+
Aliases: proga
85+
86+
Required: False
87+
Position: Named
88+
Default value: None
89+
Accept pipeline input: False
90+
Accept wildcard characters: False
91+
```
92+
93+
### CommonParameters
94+
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
95+
96+
## INPUTS
97+
98+
## OUTPUTS
99+
100+
## NOTES
101+
Version: 1.0
102+
Author: Jon Friesen
103+
Creation Date: May 7, 2015
104+
Purpose/Change: Provide an easy way of generating OTPs
105+
106+
## RELATED LINKS
107+
108+
[https://github.com/bepsoccer/verkadaModule/blob/master/docs/function-documentation/Get-Otp.md](https://github.com/bepsoccer/verkadaModule/blob/master/docs/function-documentation/Get-Otp.md)
109+

docs/reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* [Export-VerkadaAccessUserProfilePicture](function-documentation/Export-VerkadaAccessUserProfilePicture.md)
2323
* [Find-VerkadaCommandUser](function-documentation/Find-VerkadaCommandUser.md)
2424
* [Find-VerkadaUserId](function-documentation/Find-VerkadaUserId.md)
25+
* [Get-Otp](function-documentation/Get-Otp.md)
2526
* [Get-VerkadaAccessCredential](function-documentation/Get-VerkadaAccessCredential.md)
2627
* [Get-VerkadaAccessDoorConfigReport](function-documentation/Get-VerkadaAccessDoorConfigReport.md)
2728
* [Get-VerkadaAccessDoors](function-documentation/Get-VerkadaAccessDoors.md)

verkadaModule/Public/Connect-Verkada.ps1

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,22 @@ function Connect-Verkada
1313
.EXAMPLE
1414
Connect-Verkada '7cd47706-f51b-4419-8675-3b9f0ce7c12d' 'myapiKey-dcwdskjnlnlkj'
1515
This will store the org_id 7cd47706-f51b-4419-8675-3b9f0ce7c12d with the public API key myapiKey-dcwdskjnlnlkj.
16+
17+
.EXAMPLE
18+
Connect-Verkada '7cd47706-f51b-4419-8675-3b9f0ce7c12d' -userName "admin.user@contoso.com" -otp (Get-Otp (Get-Secret -Name myVerkadaOtp -AsPlainText)) -MyPwd (Get-Secret -Name myVerkadaPassword) -x_api_key 'myapiKey-dcwdskjnlnlkj'
19+
This will authenticate user admin.user@contoso.com with a otp token and a secure string variable stored password([secureString]$yourPwd) and upon success store the org_id 7cd47706-f51b-4419-8675-3b9f0ce7c12d and the returned tokens. This will also store the org_id 7cd47706-f51b-4419-8675-3b9f0ce7c12d with the public API key myapiKey-dcwdskjnlnlkj
1620
1721
.EXAMPLE
1822
Connect-Verkada '7cd47706-f51b-4419-8675-3b9f0ce7c12d' -userName "admin.user@contoso.com" -Password
19-
This will authenticate user admin.user@contoso.com by prompting for the password(stored as a secure string) and upon success store the org_id 7cd47706-f51b-4419-8675-3b9f0ce7c12d and the returned tokens.
23+
This will authenticate user admin.user@contoso.com by prompting for the password(stored as a secure string) and upon success store the org_id 7cd47706-f51b-4419-8675-3b9f0ce7c12d and the returned tokens. This will no longer work for OrgAdmins due to the MFA requirement.
2024
2125
.EXAMPLE
2226
Connect-Verkada '7cd47706-f51b-4419-8675-3b9f0ce7c12d' -userName "admin.user@contoso.com" -otp '123456' -MyPwd $yourPwd(seure string)
23-
This will authenticate user admin.user@contoso.com with a otp token and a secure string variable stored password([secureString]$yourPwd) and upon success store the org_id 7cd47706-f51b-4419-8675-3b9f0ce7c12d and the returned tokens.
27+
This will authenticate user admin.user@contoso.com with a otp token and a secure string variable stored password([secureString]$yourPwd) and upon success store the org_id 7cd47706-f51b-4419-8675-3b9f0ce7c12d and the returned tokens. This will no longer work for OrgAdmins due to the MFA requirement.
2428
2529
.EXAMPLE
2630
Connect-Verkada '7cd47706-f51b-4419-8675-3b9f0ce7c12d' -x_api_key 'myapiKey-dcwdskjnlnlkj' -userName "admin.user@contoso.com" -Password
27-
This will store the org_id 7cd47706-f51b-4419-8675-3b9f0ce7c12d with the public API key myapiKey-dcwdskjnlnlkj and will authenticate user admin.user@contoso.com by prompting for the password(stored as a secure string) and storing the returned tokens.
31+
This will store the org_id 7cd47706-f51b-4419-8675-3b9f0ce7c12d with the public API key myapiKey-dcwdskjnlnlkj and will authenticate user admin.user@contoso.com by prompting for the password(stored as a secure string) and storing the returned tokens. This will no longer work for OrgAdmins due to the MFA requirement.
2832
#>
2933

3034
[CmdletBinding(PositionalBinding = $true,DefaultParameterSetName='apiToken')]

verkadaModule/Public/Get-Otp.ps1

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<#
2+
.SYNOPSIS
3+
Time-base One-Time Password Algorithm (RFC 6238)
4+
5+
.DESCRIPTION
6+
This is an implementation of the RFC 6238 Time-Based One-Time Password Algorithm draft based upon the HMAC-based One-Time Password (HOTP) algorithm (RFC 4226). This is a time based variant of the HOTP algorithm providing short-lived OTP values.
7+
8+
.LINK
9+
https://github.com/bepsoccer/verkadaModule/blob/master/docs/function-documentation/Get-Otp.md
10+
11+
.EXAMPLE
12+
Get-Otp MySecretTotpKey
13+
14+
.NOTES
15+
Version: 1.0
16+
Author: Jon Friesen
17+
Creation Date: May 7, 2015
18+
Purpose/Change: Provide an easy way of generating OTPs
19+
20+
#>
21+
22+
function Get-Otp(){
23+
[Alias("otp")]
24+
param(
25+
[Parameter(Mandatory=$true)]$SECRET,
26+
$LENGTH = 6,
27+
$WINDOW = 30
28+
)
29+
#$enc = [System.Text.Encoding]::UTF8
30+
$hmac = New-Object -TypeName System.Security.Cryptography.HMACSHA1
31+
$hmac.key = Convert-HexToByteArray(Convert-Base32ToHex(($SECRET.ToUpper())))
32+
$timeBytes = Get-TimeByteArray $WINDOW
33+
$randHash = $hmac.ComputeHash($timeBytes)
34+
35+
$offset = $randhash[($randHash.Length-1)] -band 0xf
36+
$fullOTP = ($randhash[$offset] -band 0x7f) * [math]::pow(2, 24)
37+
$fullOTP += ($randHash[$offset + 1] -band 0xff) * [math]::pow(2, 16)
38+
$fullOTP += ($randHash[$offset + 2] -band 0xff) * [math]::pow(2, 8)
39+
$fullOTP += ($randHash[$offset + 3] -band 0xff)
40+
41+
$modNumber = [math]::pow(10, $LENGTH)
42+
$otp = $fullOTP % $modNumber
43+
$otp = $otp.ToString("0" * $LENGTH)
44+
return $otp
45+
}
46+
47+
# Get-OTPRemainingSeconds returns how many seconds are left in the current TOTP window. In a script that needs to wait until the next code is generated, use like $RetryDelayInSeconds = Get-OTPRemainingSeconds; Start-Sleep -Seconds $RetryDelayInSeconds
48+
function Get-OTPRemainingSeconds ([int32]$WINDOW = 30) {
49+
$EPOCH = Get-Date -Year 1970 -Month 1 -Day 1 -Hour 0 -Minute 0 -Second 0
50+
51+
$span = New-TimeSpan -Start $EPOCH -End (Get-Date).ToUniversalTime()
52+
$seconds = [math]::floor($span.TotalSeconds)
53+
$counter = [math]::floor($seconds / $WINDOW)
54+
55+
$nextTimeStep = ($counter + 1)*$WINDOW
56+
$difference = $nextTimeStep - $seconds
57+
58+
return $difference
59+
}
60+
61+
function Get-TimeByteArray($WINDOW) {
62+
$span = (New-TimeSpan -Start (Get-Date -Year 1970 -Month 1 -Day 1 -Hour 0 -Minute 0 -Second 0) -End (Get-Date).ToUniversalTime()).TotalSeconds
63+
$unixTime = [Convert]::ToInt64([Math]::Floor($span/$WINDOW))
64+
$byteArray = [BitConverter]::GetBytes($unixTime)
65+
[array]::Reverse($byteArray)
66+
return $byteArray
67+
}
68+
69+
function Convert-HexToByteArray($hexString) {
70+
$byteArray = $hexString -replace '^0x', '' -split "(?<=\G\w{2})(?=\w{2})" | %{ [Convert]::ToByte( $_, 16 ) }
71+
return $byteArray
72+
}
73+
74+
function Convert-Base32ToHex($base32) {
75+
$base32chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
76+
$bits = "";
77+
$hex = "";
78+
79+
for ($i = 0; $i -lt $base32.Length; $i++) {
80+
$val = $base32chars.IndexOf($base32.Chars($i));
81+
$binary = [Convert]::ToString($val, 2)
82+
$staticLen = 5
83+
$padder = '0'
84+
# Write-Host $binary
85+
$bits += Add-LeftPad $binary.ToString() $staticLen $padder
86+
}
87+
88+
89+
for ($i = 0; $i+4 -le $bits.Length; $i+=4) {
90+
$chunk = $bits.Substring($i, 4)
91+
# Write-Host $chunk
92+
$intChunk = [Convert]::ToInt32($chunk, 2)
93+
$hexChunk = Convert-IntToHex($intChunk)
94+
# Write-Host $hexChunk
95+
$hex = $hex + $hexChunk
96+
}
97+
return $hex;
98+
99+
}
100+
101+
function Convert-IntToHex([int]$num) {
102+
return ('{0:x}' -f $num)
103+
}
104+
105+
function Add-LeftPad($str, $len, $pad) {
106+
if(($len + 1) -ge $str.Length) {
107+
while (($len - 1) -ge $str.Length) {
108+
$str = ($pad + $str)
109+
}
110+
}
111+
return $str;
112+
}

verkadaModule/verkadaModule.psd1

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# Generated by: Verkada SE Community
55
#
6-
# Generated on: 03/31/2024
6+
# Generated on: 06/28/2024
77
#
88

99
@{
@@ -12,7 +12,7 @@
1212
RootModule = 'verkadaModule.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '0.7.1'
15+
ModuleVersion = '0.7.2'
1616

1717
# Supported PSEditions
1818
CompatiblePSEditions = 'Desktop', 'Core'
@@ -81,7 +81,7 @@ FunctionsToExport = 'Add-VerkadaAccessBadgeToUser', 'Add-VerkadaAccessGroup',
8181
'Enable-VerkadaAccessUserCard',
8282
'Enable-VerkadaAccessUserLicensePlate',
8383
'Export-VerkadaAccessUserProfilePicture', 'Find-VerkadaCommandUser',
84-
'Find-VerkadaUserId', 'Get-VerkadaAccessCredential',
84+
'Find-VerkadaUserId', 'Get-Otp', 'Get-VerkadaAccessCredential',
8585
'Get-VerkadaAccessDoorConfigReport', 'Get-VerkadaAccessDoors',
8686
'Get-VerkadaAccessDoorSchedules', 'Get-VerkadaAccessGroup',
8787
'Get-VerkadaAccessLevels', 'Get-VerkadaAccessSite',
@@ -134,15 +134,16 @@ AliasesToExport = 'a-VrkdaAcGrp', 'Add-VrkdaAcGrp', 'a-VrkdaAcUsrCrd',
134134
'Add-VrkdaWrkEmp', 'd-VrkdaAcUsrCrd', 'Disable-VrkdaAcUsrCrd',
135135
'd-VrkdaAcUsrLPR', 'Disable-VrkdaAcUsrLPR', 'e-VrkdaAcUsrCrd',
136136
'Enable-VrkdaAcUsrCrd', 'e-VrkdaAcUsrLPR', 'Enable-VrkdaAcUsrLPR',
137-
'ep-VrkdaAcUsrPrflPic', 'Export-VrkdaAcUsrPrflPic', 'Get-VrkdaAcGrp',
138-
'gt-VrkdaAcGrp', 'Get-VrkdaAcUsr', 'gt-VrkdaAcUsr', 'g-VrkdAlrmDevs',
139-
'Get-VrkdAlrmDevs', 'Get-VerkadaCameraSite', 'Get-VrkdaCmdUsr',
140-
'gt-VrkdaCmdUsr', 'Get-VerkadaLPoI', 'Get-VrkdaWrkEmp',
141-
'gt-VrkdaWrkEmp', 'rd-VrkdaAcGrps', 'Read-VrkdaAcGrps',
142-
'rd-VrkdaCamArchv', 'Read-VrkdaCamArchv', 'rd-VrkdaGstSte',
143-
'Read-VrkdaGstSte', 'rd-VrkdaWrkEmp', 'Read-VrkdaWrkEmp',
144-
'Remove-VrkdaAcGrp', 'rm-VrkdaAcGrp', 'Remove-VrkdaAcUsrBtUnlk',
145-
'rm-VrkdaAcUsrBtUnlk', 'Remove-VrkdaAcUsrCrd', 'rm-VrkdaAcUsrCrd',
137+
'ep-VrkdaAcUsrPrflPic', 'Export-VrkdaAcUsrPrflPic', 'otp',
138+
'Get-VrkdaAcGrp', 'gt-VrkdaAcGrp', 'Get-VrkdaAcUsr', 'gt-VrkdaAcUsr',
139+
'g-VrkdAlrmDevs', 'Get-VrkdAlrmDevs', 'Get-VerkadaCameraSite',
140+
'Get-VrkdaCmdUsr', 'gt-VrkdaCmdUsr', 'Get-VerkadaLPoI',
141+
'Get-VrkdaWrkEmp', 'gt-VrkdaWrkEmp', 'rd-VrkdaAcGrps',
142+
'Read-VrkdaAcGrps', 'rd-VrkdaCamArchv', 'Read-VrkdaCamArchv',
143+
'rd-VrkdaGstSte', 'Read-VrkdaGstSte', 'rd-VrkdaWrkEmp',
144+
'Read-VrkdaWrkEmp', 'Remove-VrkdaAcGrp', 'rm-VrkdaAcGrp',
145+
'Remove-VrkdaAcUsrBtUnlk', 'rm-VrkdaAcUsrBtUnlk',
146+
'Remove-VrkdaAcUsrCrd', 'rm-VrkdaAcUsrCrd',
146147
'Remove-VrkdaAcUsrEntryCo', 'rm-VrkdaAcUsrEntryCo',
147148
'Remove-VrkdaAcUsrFrGrp', 'rm-VrkdaAcUsrFrGrp',
148149
'Remove-VrkdaAcUsrLPR', 'rm-VrkdaAcUsrLPR',

0 commit comments

Comments
 (0)