2020-02-24 19:24:54 -07:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
from common.file_helpers import mkdirs_exists_ok
|
2020-12-16 22:30:23 -07:00
|
|
|
from selfdrive.hardware import PC
|
2020-02-24 19:24:54 -07:00
|
|
|
|
2020-08-26 06:57:17 -06:00
|
|
|
|
2020-02-24 19:24:54 -07:00
|
|
|
class MissingAuthConfigError(Exception):
|
|
|
|
pass
|
|
|
|
|
2020-08-26 06:57:17 -06:00
|
|
|
|
|
|
|
if PC:
|
2020-06-04 19:57:25 -06:00
|
|
|
CONFIG_DIR = os.path.expanduser('~/.comma')
|
2020-08-26 06:57:17 -06:00
|
|
|
else:
|
|
|
|
CONFIG_DIR = "/tmp/.comma"
|
|
|
|
|
2020-02-24 19:24:54 -07:00
|
|
|
mkdirs_exists_ok(CONFIG_DIR)
|
|
|
|
|
2020-08-26 06:57:17 -06:00
|
|
|
|
2020-02-24 19:24:54 -07:00
|
|
|
def get_token():
|
|
|
|
try:
|
|
|
|
with open(os.path.join(CONFIG_DIR, 'auth.json')) as f:
|
|
|
|
auth = json.load(f)
|
|
|
|
return auth['access_token']
|
2020-09-03 19:46:13 -06:00
|
|
|
except Exception as e:
|
|
|
|
raise MissingAuthConfigError('Authenticate with tools/lib/auth.py') from e
|
2020-02-24 19:24:54 -07:00
|
|
|
|
2020-08-26 06:57:17 -06:00
|
|
|
|
2020-02-24 19:24:54 -07:00
|
|
|
def set_token(token):
|
|
|
|
with open(os.path.join(CONFIG_DIR, 'auth.json'), 'w') as f:
|
|
|
|
json.dump({'access_token': token}, f)
|
|
|
|
|
2020-08-26 06:57:17 -06:00
|
|
|
|
2020-02-24 19:24:54 -07:00
|
|
|
def clear_token():
|
|
|
|
os.unlink(os.path.join(CONFIG_DIR, 'auth.json'))
|