Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PowerShell Script to validate YubiKey using Single Factor
# Client ID obtained after registering here https://upgrade.yubico.com/getapikey/
$clientID = '12345'
# Read in the key
$getKey = Read-Host 'Insert your YubiKey and touch it until the OTP is received (~2 seconds)'
$credentialID = $getKey.Substring(0,12)
# Generate a Nonce
$nonce = -join ((65..90) + (97..122) | Get-Random -Count 32 | % {[char]$_})
$nonce = $nonce.ToLower()
# Build the URI
$uri = "http://api.yubico.com/wsapi/2.0/verify?id=$($clientID)&nonce=$($nonce)&otp=$($getKey)"
# Invoke Web Request
$response = Invoke-WebRequest -Method GET -Uri $uri
# Split the response into an array
$lines = $response.ParsedHtml.body.innerText -split " "
# Check to the response and output
if ($response.ParsedHtml.body.innerText.Contains("status=OK")){
# SUCCESSFUL
write-host "***********SUCCESS****************"
write-host "Credential ID $($credentialID)"
$i=0
foreach ($line in $lines){
$linenumber = $i
switch ($linenumber) {
0 {write-host "Signature $($line)"}
1 {write-host "Time $($line)"}
2 {write-host "$($line)"}
3 {write-host "$($line)"}
4 {write-host "% $($line)"}
5 {write-host "$($line)"}
}
$i++
}
} else {
$i=0
# ERROR
write-host "============ERROR==============="
write-host "Credential ID $($credentialID)"
foreach ($line in $lines){
$linenumber = $i
switch ($linenumber) {
4 {write-host "$($line)"}
5 {write-host "$($line)"}
}
$i++
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment