Skip to content

Commit

Permalink
Add auth token support
Browse files Browse the repository at this point in the history
  • Loading branch information
tedivm committed Feb 18, 2018
1 parent 4fc0ac0 commit f354356
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 25 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 9 additions & 1 deletion screeps_console/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
20 changes: 11 additions & 9 deletions screeps_console/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand All @@ -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)
70 changes: 56 additions & 14 deletions screeps_console/settings.py
Original file line number Diff line number Diff line change
@@ -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('~')
Expand Down Expand Up @@ -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'
Expand All @@ -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')
Expand All @@ -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'],
)



Expand Down

0 comments on commit f354356

Please sign in to comment.