Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Use OAuth tokens as XMPP passwords given an OAuth Provider.
#! /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