Last active
January 11, 2021 14:24
-
-
Save tom-henderson/3ac2e2ce05e4f5e77c2b to your computer and use it in GitHub Desktop.
Authenticating unifi wireless guest users.
This file contains 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
import json | |
import ssl | |
import requests | |
from requests_toolbelt import SSLAdapter | |
# URL or IP Address of the unifi controller: | |
PORTAL_URL = 'https://ubiquiti.example.com/' | |
# Username and password for the unifi controller: | |
PORTAL_LOGIN_PARAMS = { | |
'login': 'login', | |
'username': 'admin', | |
'password': 'hunter9', | |
} | |
def authorize_guest(client_mac, session_length): | |
login_url = '{}login'.format(PORTAL_URL) | |
logout_url = '{}logout'.format(PORTAL_URL) | |
api_url = '{}api/cmd/stamgr'.format(PORTAL_URL) | |
auth = { | |
'cmd': 'authorize-guest', | |
'mac': client_mac, | |
'minutes': session_length, | |
} | |
auth_params = { | |
'json': json.dumps(auth) | |
} | |
# Use a Session object to handle cookies. | |
# Use an SSLAdapter to work around SSL handshake issues. | |
session = requests.Session() | |
session.mount(PORTAL_URL, SSLAdapter(ssl.PROTOCOL_TLSv1)) | |
# Log in to the API | |
login_response = session.post(login_url, data=PORTAL_LOGIN_PARAMS, timeout=5) | |
if login_response.status_code != 200: | |
return False | |
# Authorize the guest | |
auth_response = session.post(api_url, data=auth_params, timeout=5) | |
if auth_response.status_code != 200: | |
return False | |
# Log out from the API | |
logout_response = session.get(logout_url, timeout=5) | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment