From f3543569f4afd4db26656df2b52cab4e8919806f Mon Sep 17 00:00:00 2001 From: Robert Hafner Date: Sun, 18 Feb 2018 13:13:01 -0800 Subject: [PATCH] Add auth token support --- README.md | 2 + requirements.txt | 2 +- screeps_console/console.py | 10 ++++- screeps_console/interactive.py | 20 +++++----- screeps_console/settings.py | 70 +++++++++++++++++++++++++++------- 5 files changed, 79 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 4de29ba..36db216 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,8 @@ Typically there is little reason to edit it manually. When you attempt to connec to a server for the first time it will ask for your credentials (and if it's a private server for the host), which will then be saved for future use. +When using the "screeps.com" server the console will automatically create a +token and use that instead of storing your credentials. ## Launching diff --git a/requirements.txt b/requirements.txt index eb58d4f..c5d7219 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,7 @@ nose==1.3.7 PyYAML>=3.12,<4 requests>=2.18.3,<3 Requires==0.0.3 -screepsapi>=0.4.2 +screepsapi>=0.5.1 six>=1.10.0,<2 urwid==1.3.1 websocket-client==0.44.0 diff --git a/screeps_console/console.py b/screeps_console/console.py index 660a7c9..03807fd 100755 --- a/screeps_console/console.py +++ b/screeps_console/console.py @@ -116,7 +116,15 @@ def start(self): server = sys.argv[1] config = settings.getConnection(server) - screepsconsole = ScreepsConsole(user=config['username'], password=config['password'], secure=config['secure'], host=config['host']) + + if server == 'main' and 'token' not in config: + settings.addConnection('main', config['username'], config['password']) + config = settings.getConnection(server) + + if 'token' in config: + screepsconsole = ScreepsConsole(token=config['token'], secure=config['secure'], host=config['host']) + else: + screepsconsole = ScreepsConsole(user=config['username'], password=config['password'], secure=config['secure'], host=config['host']) if len(sys.argv) > 2: if sys.argv[2] == 'interactive': diff --git a/screeps_console/interactive.py b/screeps_console/interactive.py index a5e2f80..4c2fe4f 100755 --- a/screeps_console/interactive.py +++ b/screeps_console/interactive.py @@ -391,15 +391,14 @@ def __del__(self): if server == 'main' or server == 'ptr': legacyConfig = settings.getLegacySettings() if legacyConfig: - if input("Upgrade settings file to the new format? (y/n) ") != "y": - sys.exit(-1) - settings.addConnection('main', legacyConfig['screeps_username'], legacyConfig['screeps_password']) - config = settings.getSettings() - config['smooth_scroll'] = legacyConfig['smooth_scroll'] - config['max_scroll'] = legacyConfig['max_scroll'] - config['max_history'] = legacyConfig['max_history'] - settings.saveSettings(config) - connectionSettings = settings.getConnection(server) + if input("Upgrade settings file to the new format? (y/n) ") == "y": + settings.addConnection('main', legacyConfig['screeps_username'], legacyConfig['screeps_password']) + config = settings.getSettings() + config['smooth_scroll'] = legacyConfig['smooth_scroll'] + config['max_scroll'] = legacyConfig['max_scroll'] + config['max_history'] = legacyConfig['max_history'] + settings.saveSettings(config) + connectionSettings = settings.getConnection(server) if not connectionSettings: if server is 'main': @@ -412,5 +411,8 @@ def __del__(self): password = input("Password: ") settings.addConnection(server, username, password, host, secure) + if server == 'main' and 'token' not in connectionSettings: + settings.addConnection('main', connectionSettings['username'], connectionSettings['password']) + connectionSettings = settings.getConnection(server) ScreepsInteractiveConsole(server) diff --git a/screeps_console/settings.py b/screeps_console/settings.py index 03c993a..7ba7f87 100644 --- a/screeps_console/settings.py +++ b/screeps_console/settings.py @@ -1,8 +1,10 @@ +from collections import OrderedDict +import json import os from os.path import expanduser -import sys import yaml import screepsapi +import requests userhome = expanduser('~') @@ -43,7 +45,7 @@ def addConnection(name, username, password, host=False, secure=False): if name == 'main': secure = True host = 'screeps.com' - addConnection('ptr', username, password) + #addConnection('ptr', username, password) if name == 'ptr': secure = True host = 'screeps.com/ptr' @@ -55,16 +57,49 @@ def addConnection(name, username, password, host=False, secure=False): if 'connections' not in settings: settings['connections'] = {} - settings['connections'][name] = { - 'host': host, - 'secure': secure, - 'username': username, - 'password': password - } + if name == 'main' or name == 'ptr': + token = getToken(username, password, host, secure) + settings['connections'][name] = { + 'host': host, + 'secure': secure, + 'token': token + } + else: + settings['connections'][name] = { + 'host': host, + 'secure': secure, + 'username': username, + 'password': password + } saveSettings(settings) +def getToken(username, password, host, secure): + if secure: + apiurl = 'https://%s/api/user/auth-token' % (host) + else: + apiurl = 'http://%s/api/user/auth-token' % (host) + + authtype = {"type": "full", + "endpoints": { + "GET /api/user/name": False, + "GET /api/user/money-history": False, + "GET /api/market/my-orders": False, + "GET /api/user/memory": False, + "GET /api/user/memory-segment": False, + "POST /api/user/memory-segment": False}, + "websockets": { + "console": False, + "rooms": False}, + "memorySegments": "" + } + r = requests.post(apiurl, data=authtype, auth=(username, password)) + r.raise_for_status() + apiret = json.loads(r.text, object_pairs_hook=OrderedDict) + return apiret['token'] + + def removeConnection(name): if name == 'main': removeConnection('ptr') @@ -82,12 +117,19 @@ def saveSettings(settings): def getApiClient(name): settings = getConnection(name) - return screepsapi.API( - u=settings['username'], - p=settings['password'], - host=settings['host'], - secure=settings['secure'], - ) + if 'token' in settings: + return screepsapi.API( + token=settings['token'], + host=settings['host'], + secure=settings['secure'], + ) + else: + return screepsapi.API( + u=settings['username'], + p=settings['password'], + host=settings['host'], + secure=settings['secure'], + )