2016-05-26 16:16:13 +00:00
|
|
|
# Copyright 2016 Mycroft AI, Inc.
|
|
|
|
#
|
|
|
|
# This file is part of Mycroft Core.
|
|
|
|
#
|
|
|
|
# Mycroft Core is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Mycroft Core is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Mycroft Core. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
2016-05-20 14:16:01 +00:00
|
|
|
import json
|
2016-09-30 13:29:19 +00:00
|
|
|
import time
|
2016-08-31 02:55:35 +00:00
|
|
|
|
2016-05-20 14:16:01 +00:00
|
|
|
from mycroft.filesystem import FileSystemAccess
|
|
|
|
|
|
|
|
|
|
|
|
class DeviceIdentity(object):
|
|
|
|
def __init__(self, **kwargs):
|
2016-09-28 03:56:51 +00:00
|
|
|
self.uuid = kwargs.get("uuid", "")
|
|
|
|
self.access = kwargs.get("access", "")
|
|
|
|
self.refresh = kwargs.get("refresh", "")
|
2016-10-18 21:39:30 +00:00
|
|
|
self.expires_at = kwargs.get("expires_at", 0)
|
2016-05-20 14:16:01 +00:00
|
|
|
|
2016-10-18 23:41:55 +00:00
|
|
|
def is_expired(self):
|
|
|
|
return self.refresh and self.expires_at <= time.time()
|
|
|
|
|
2016-05-20 14:16:01 +00:00
|
|
|
|
|
|
|
class IdentityManager(object):
|
2016-09-08 00:50:45 +00:00
|
|
|
__identity = None
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def load():
|
|
|
|
try:
|
|
|
|
with FileSystemAccess('identity').open('identity.json', 'r') as f:
|
|
|
|
IdentityManager.__identity = DeviceIdentity(**json.load(f))
|
|
|
|
except:
|
|
|
|
IdentityManager.__identity = DeviceIdentity()
|
2016-10-18 23:41:55 +00:00
|
|
|
return IdentityManager.__identity
|
2016-09-08 00:50:45 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2016-09-30 13:29:19 +00:00
|
|
|
def save(login=None):
|
|
|
|
if login:
|
|
|
|
IdentityManager.update(login)
|
2016-09-08 00:50:45 +00:00
|
|
|
with FileSystemAccess('identity').open('identity.json', 'w') as f:
|
|
|
|
json.dump(IdentityManager.__identity.__dict__, f)
|
|
|
|
|
2016-09-30 13:29:19 +00:00
|
|
|
@staticmethod
|
2016-11-17 05:25:06 +00:00
|
|
|
def update(login={}):
|
2016-10-19 00:11:27 +00:00
|
|
|
expiration = login.get("expiration", 0)
|
2016-11-17 05:25:06 +00:00
|
|
|
IdentityManager.__identity.uuid = login.get("uuid", "")
|
|
|
|
IdentityManager.__identity.access = login.get("accessToken", "")
|
|
|
|
IdentityManager.__identity.refresh = login.get("refreshToken", "")
|
2016-10-18 21:39:30 +00:00
|
|
|
IdentityManager.__identity.expires_at = time.time() + expiration
|
2016-09-30 13:29:19 +00:00
|
|
|
|
2016-09-08 00:50:45 +00:00
|
|
|
@staticmethod
|
|
|
|
def get():
|
|
|
|
if not IdentityManager.__identity:
|
|
|
|
IdentityManager.load()
|
|
|
|
return IdentityManager.__identity
|