Last active
June 14, 2020 20:46
-
-
Save msafadieh/d78e23b976af5a2863273d068c36d9a9 to your computer and use it in GitHub Desktop.
authenticate prosody using pleroma
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
#! /usr/bin/env python3 | |
from urllib.error import URLError | |
from urllib.parse import urlencode | |
from urllib.request import urlopen | |
PLEROMA_SERVER = "https://otherground.party" | |
DELIMITER = ":" | |
API = { | |
"auth": ("/check_password", {"user": 0, "pass": 2}), | |
"isuser": ("/user_exists", {"user": 0}), | |
} | |
def get_response(endpoint, params): | |
query = urlencode(params) | |
url = "{}{}?{}".format(PLEROMA_SERVER, endpoint, query) | |
try: | |
response = urlopen(url).read() | |
except URLError as e: | |
# /user_exists returns 404 on failure | |
# /check_password returns 403 on failure | |
if e.code in (403, 404): | |
return 0 | |
raise | |
return int(response.decode() == "true") | |
def process_input(line): | |
command, args = line.split(DELIMITER, 1) | |
args = args.split(DELIMITER) | |
response = 0 | |
if command in API: | |
endpoint, params = API[command] | |
params = {key: args[index] for key, index in params.items()} | |
response = get_response(endpoint, params) | |
return response | |
def main(): | |
try: | |
while True: | |
response = process_input(input()) | |
print(response, flush=True) | |
# input() throws EOFError at the end causing problems with prosody | |
except EOFError: | |
pass | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment