2020-02-24 21:24:54 -05:00
|
|
|
import json
|
|
|
|
|
import os
|
2023-11-30 09:55:48 -08:00
|
|
|
from openpilot.system.hardware.hw import Paths
|
2020-02-24 21:24:54 -05:00
|
|
|
|
2020-08-26 14:57:17 +02:00
|
|
|
|
2020-02-24 21:24:54 -05:00
|
|
|
class MissingAuthConfigError(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
2020-08-26 14:57:17 +02:00
|
|
|
|
2020-02-24 21:24:54 -05:00
|
|
|
def get_token():
|
|
|
|
|
try:
|
2023-11-30 09:55:48 -08:00
|
|
|
with open(os.path.join(Paths.config_root(), 'auth.json')) as f:
|
2020-02-24 21:24:54 -05:00
|
|
|
auth = json.load(f)
|
|
|
|
|
return auth['access_token']
|
2021-09-30 20:13:46 +02:00
|
|
|
except Exception:
|
|
|
|
|
return None
|
2020-02-24 21:24:54 -05:00
|
|
|
|
2020-08-26 14:57:17 +02:00
|
|
|
|
2020-02-24 21:24:54 -05:00
|
|
|
def set_token(token):
|
2023-12-06 09:55:29 -08:00
|
|
|
os.makedirs(Paths.config_root(), exist_ok=True)
|
2023-11-30 09:55:48 -08:00
|
|
|
with open(os.path.join(Paths.config_root(), 'auth.json'), 'w') as f:
|
2020-02-24 21:24:54 -05:00
|
|
|
json.dump({'access_token': token}, f)
|
|
|
|
|
|
2020-08-26 14:57:17 +02:00
|
|
|
|
2020-02-24 21:24:54 -05:00
|
|
|
def clear_token():
|
2023-09-12 11:00:18 -07:00
|
|
|
try:
|
2023-11-30 09:55:48 -08:00
|
|
|
os.unlink(os.path.join(Paths.config_root(), 'auth.json'))
|
2023-09-12 11:00:18 -07:00
|
|
|
except FileNotFoundError:
|
|
|
|
|
pass
|