Created
October 24, 2017 22:05
-
-
Save Sonictherocketman/aefd78575280978af0562a0b9841a903 to your computer and use it in GitHub Desktop.
Use OAuth tokens as XMPP passwords given an OAuth Provider.
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
#! /usr/bin/python | |
# https://docs.ejabberd.im/developer/guide/#toc_8 | |
import sys, os | |
from struct import * | |
from requests import get | |
BASE_URL = os.environ['BASE_URL'] | |
DJANGO_AUTH = os.environ['DJANGO_AUTH'] | |
DJANGO_IS_USER = os.environ['DJANGO_IS_USER'] | |
def from_ejabberd(): | |
input_length = sys.stdin.read(2) | |
(size,) = unpack('>h', input_length) | |
return sys.stdin.read(size).split(':') | |
def to_ejabberd(bool): | |
answer = 0 | |
if bool: | |
answer = 1 | |
token = pack('>hh', 2, answer) | |
sys.stdout.write(token) | |
sys.stdout.flush() | |
class Authenticator(object): | |
@staticmethod | |
def auth(username, server, password): | |
""" Verifies that OAuth token is authorized. """ | |
url = '{base_url}{api}'.format(base_url=BASE_URL, api=DJANGO_AUTH) | |
headers = { | |
'Host': 'app.adventurerscodex.com', | |
'Authorization': 'Bearer {}'.format(password) | |
} | |
response = get(url, headers=headers) | |
return response.status_code == 200 | |
@staticmethod | |
def isuser(username, server): | |
""" Verifies if the given username exists. """ | |
url = '{base_url}{api}?username={username}'.format(base_url=BASE_URL, api=DJANGO_IS_USER, username=username) | |
headers = {'Host': 'app.adventurerscodex.com'} | |
response = get(url, headers=headers) | |
return response.status_code == 200 | |
def main(): | |
while True: | |
args = from_ejabberd() | |
method = args[0] | |
success = False | |
if method == "auth": | |
success = Authenticator.auth(args[1], args[2], args[3]) | |
elif method == "isuser": | |
success = Authenticator.isuser(args[1], args[2]) | |
to_ejabberd(success) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment