Last active
March 22, 2023 19:43
-
-
Save cgmartin/466bd2d3724de6c04743d61cf0de2066 to your computer and use it in GitHub Desktop.
Vera Auth Sessions 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
#!/bin/bash | |
set -e | |
command -v jq >/dev/null 2>&1 || { echo >&2 "I require jq but it's not installed (see: https://stedolan.github.io/jq/). Aborting."; exit 1; } | |
command -v curl >/dev/null 2>&1 || { echo >&2 "I require curl but it's not installed (see: https://curl.haxx.se/). Aborting."; exit 1; } | |
# Auth API Notes: http://forum.micasaverde.com/index.php/topic,24942.45.html | |
# New Server domains listing: http://forum.micasaverde.com/index.php/topic,25859.0.html | |
# Example implementations: | |
# https://github.com/rickbassham/vera/blob/master/vera_test.py | |
# https://github.com/amg0/ALTUI/blob/master/Remote/VeraloginAction.php | |
# Usage: | |
# ./vera_auth_test.sh {userId} {password} | |
userId=$1 | |
userPassword=$2 | |
[ -z "$userId" ] && { echo >&2 "Error: A userId parameter is required"; exit 1; } | |
[ -z "$userPassword" ] && { echo >&2 "Error: A password parameter is required"; exit 1; } | |
authUrl="https://vera-us-oem-autha.mios.com" # "https://us-autha11.mios.com" # "https://us-autha.mios.com" | |
passwordSeed=oZ7QE6LcLJp6fiWzdqZc | |
shaPassword=$(echo -n "${userId}${userPassword}${passwordSeed}" | openssl dgst -sha1) | |
# Get Identity tokens | |
authRequest="${authUrl}/autha/auth/username/${userId}?SHA1Password=${shaPassword}&PK_Oem=1" | |
identityJson=$(curl -s "$authRequest") | |
identityToken=$(echo "$identityJson" | jq -r '.Identity') | |
identitySignature=$(echo "$identityJson" | jq -r '.IdentitySignature') | |
serverAccount=$(echo "$identityJson" | jq -r '.Server_Account') | |
echo | |
echo "1. Authenticate" | |
echo " $authRequest" | |
echo "--------------------------------------------------------------------------------------" | |
echo "$identityJson" | jq | |
identityTokenJson=$(echo "$identityToken" | base64 --decode) | |
accountId=$(echo "$identityTokenJson" | jq -r '.PK_Account') | |
echo | |
echo "1a. Identity" | |
echo "------------" | |
echo "$identityTokenJson" | jq | |
# Get Session token | |
sessionRequest="https://${serverAccount}/info/session/token" | |
sessionToken=$(curl -s -H "MMSAuth: ${identityToken}" -H "MMSAuthSig: ${identitySignature}" "$sessionRequest") | |
echo | |
echo "2. Session" | |
echo " $sessionRequest" | |
echo "--------------------------------------------------------------------------------------" | |
echo "Token: ${sessionToken}" | |
# Get Account devices (hubs) | |
devicesRequest="https://${serverAccount}/account/account/account/${accountId}/devices" | |
devicesJson=$(curl -s -H "MMSSession: ${sessionToken}" "$devicesRequest") | |
deviceId=$(echo "$devicesJson" | jq -r '.Devices[0].PK_Device') | |
deviceRelay=$(echo "$devicesJson" | jq -r '.Devices[0].Server_Device') | |
echo | |
echo "3. Account Devices" | |
echo " $devicesRequest" | |
echo "--------------------------------------------------------------------------------------" | |
echo "$devicesJson" | jq | |
# Get info for a device hub | |
deviceInfoRequest="https://${deviceRelay}/device/device/device/${deviceId}" | |
hubJson=$(curl -s -H "MMSSession: ${sessionToken}" "$deviceInfoRequest") | |
hubInternalIp=$(echo "$hubJson" | jq -r '.InternalIP') | |
hubExternalIp=$(echo "$hubJson" | jq -r '.ExternalIP') | |
serverRelay=$(echo "$hubJson" | jq -r '.Server_Relay') | |
echo | |
echo "4. Device Info" | |
echo " $deviceInfoRequest" | |
echo "--------------------------------------------------------------------------------------" | |
echo "$hubJson" | jq | |
# Get Session token for Relay Server (need to request a new session for each server) | |
deviceSessionRequest="https://${serverRelay}/info/session/token" | |
relaySession=$(curl -s -H "MMSAuth: ${identityToken}" -H "MMSAuthSig: ${identitySignature}" "$deviceSessionRequest") | |
echo | |
echo "5. Device Relay Session" | |
echo " $deviceSessionRequest" | |
echo "--------------------------------------------------------------------------------------" | |
echo "Token: ${relaySession}" | |
# Get SData from Hub through Relay Server | |
sdataRequest="https://${serverRelay}/relay/relay/relay/device/${deviceId}/port_3480/data_request?id=sdata" | |
sdataJson=$(curl -s -H "MMSSession: ${relaySession}" "$sdataRequest") | |
echo | |
echo "6. SData" | |
echo " $sdataRequest" | |
echo "--------------------------------------------------------------------------------------" | |
echo "$sdataJson" | jq | |
# For other types of requests, see: http://wiki.micasaverde.com/index.php/Luup_Requests | |
echo | |
echo "curl -s \"https://${serverRelay}/relay/relay/relay/device/${deviceId}/session/${relaySession}/port_3480/data_request?id=sdata\"" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this!
Here's a fork for Bali Blinds (based on vera) that also runs a routine/scene: https://gist.github.com/bostrt/f8189ce83e6fa6fb573aea2f1e76b723