Import-GreenRadiusYubikeyToken.ps1
|
function Import-GreenRadiusYubikeyToken { <# .NOTES Version 2026.3.22.2155 .SYNOPSIS Creates a YubiKey OTP token in GreenRADIUS. .DESCRIPTION Utilizes the GreenRADIUS API to import a YubiKey OTP token into GreenRADIUS. Reqires Connect-GreenRadiusApi to have been called at some point earlier in the PowerShell session. .OUTPUTS $true if import was successful, $false if not. If not successful, an explanation of the failure will also be passed to Write-Error. .PARAMETER Make The make/manufacturer information for this token. At this time, the only supported value for this parameter is "Yubico OTP" which is also the default value for this parameter. .PARAMETER SerialNumber The serial number of the YubiKey. .PARAMETER PublicIdentity The public ID of the OTP, which presents itself in actual OTP codes as the first twelve characters, which do not change. .PARAMETER PrivateIdentity The key's private ID. .PARAMETER SecretKey The key's AES-128 secret. .PARAMETER Server Hostname of the server to which to send the API query, or an array of strings containing such hostnames. This parameter can be used to override the server which was set in Connect-GreenRadiusApi or Set-GreenRadiusApi. #> [CmdletBinding()] param ( [Parameter(Mandatory=$true)] [string]$Make="Yubico OTP", [Parameter(Mandatory=$true)] [int]$SerialNumber, [Parameter(Mandatory=$true)] [ValidatePattern('^[lnrtuvcbdefghijk]+$')] [string]$PublicIdentity, [Parameter(Mandatory=$true)] [ValidatePattern('^[0-9A-Fa-f]+$')] [string]$PrivateIdentity, [Parameter(Mandatory=$true)] [ValidatePattern('^[0-9A-Fa-f]+$')] [string]$SecretKey, [string]$Server ) if (-not $global:GreenRadiusApiSession) { Write-Error "You forgot to run Connect-GreenRadiusApi!" return $false; } $Body = @{ yubikeys = @( @{ make = $Make serialno = $SerialNumber publicname = $PublicIdentity internalname = $PrivateIdentity aeskey = $SecretKey } ) } $apiResult = SubmitGreenRadiusApiCall -Servers $Server -Body $Body ` -Method Post -Uri "/gras-api/v2/mgmt/import_token/yubikey" if ($apiResult.records_imported.Count -gt 0) { return $true } else { if ($apiResult.records_skipped.Count -gt 0) { Write-Error $apiResult.records_skipped.description } elseif ($apiResult.records_invalid.Count -gt 0) { Write-Error $apiResult.records_invalid.description } return $false } } <# LICENSE Copyright (c) 2026 Derek Howard and the City of Eureka, California Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, and/or distribute copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software, and the original author shall be credited as having created the original work. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #> |