Commit 677277c9 authored by Torsten Grote's avatar Torsten Grote

Add support for Authorization Token

parent cdb4b463
......@@ -2,6 +2,7 @@
import asyncio
import json
import sys
import threading
from datetime import datetime
......@@ -18,33 +19,41 @@ PROMPT = '$> '
def main():
load_history()
if len(sys.argv) != 2:
print("Please provide auth token: %s <token>" % sys.argv[0])
sys.exit(1)
threading.Thread(target=get_message_stdin).start()
auth_token = sys.argv[1]
load_history(auth_token)
threading.Thread(target=get_message_stdin, args=[auth_token]).start()
loop = asyncio.get_event_loop()
loop.create_task(get_message_websocket())
loop.create_task(get_message_websocket(auth_token))
loop.run_forever()
loop.close()
async def get_message_websocket():
async with websockets.connect(URL_WS) as ws:
message = await ws.recv()
m = json.loads(message)
if m['name'] == 'org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent':
print() # line-break
print_message(m['data'])
print(PROMPT, end='', flush=True)
async def get_message_websocket(token):
url = URL_WS.replace('ws://', 'ws://%s@' % token)
async with websockets.connect(url) as ws:
while not ws.closed and not asyncio.get_event_loop().is_closed():
message = await ws.recv()
m = json.loads(message)
if m['name'] == 'org.briarproject.briar.api.messaging' + \
'.event.PrivateMessageReceivedEvent':
print() # line-break
print_message(m['data'])
print(PROMPT, end='', flush=True)
if not asyncio.get_event_loop().is_closed():
asyncio.get_event_loop().create_task(get_message_websocket())
asyncio.get_event_loop().create_task(get_message_websocket(token))
def get_message_stdin():
def get_message_stdin(token):
while True:
body = input(PROMPT)
if len(body) > 0:
print_message(requests.post(URL, data={'message': body}).json())
res = requests.post(URL, headers=get_auth_header(token), data={'message': body}).json()
print_message(res)
def print_message(message):
......@@ -57,11 +66,15 @@ def get_timestamp(date):
return date.strftime('%Y-%m-%dT%H:%M:%S')
def load_history():
history = requests.get(URL).json()
def load_history(token):
history = requests.get(URL, headers=get_auth_header(token)).json()
for message in history:
print_message(message)
def get_auth_header(token):
return {'Authorization': 'Bearer %s' % token}
if __name__ == "__main__":
main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment