Issues 351 - Making Identity static and unique

pull/420/head
Jonathan D'Orleans 2016-09-07 20:50:45 -04:00
parent 5fd8342868
commit b1a4a9c22b
7 changed files with 45 additions and 37 deletions

View File

@ -14,14 +14,14 @@ class Api(object):
config_server = config.get("server") config_server = config.get("server")
self.url = config_server.get("url") self.url = config_server.get("url")
self.version = config_server.get("version") self.version = config_server.get("version")
self.identity = IdentityManager().get() self.identity = IdentityManager.get()
def request(self, params): def request(self, params):
method = params.get("method", "GET") method = params.get("method", "GET")
headers = self.build_headers(params) headers = self.build_headers(params)
body = self.build_body(params) body = self.build_body(params)
url = self.build_url(params) url = self.build_url(params)
response = requests.request(method, url, headers=headers, data=body) response = requests.request(method, url, headers=headers, json=body)
return self.get_response(response) return self.get_response(response)
@staticmethod @staticmethod
@ -54,7 +54,7 @@ class Api(object):
def build_body(self, params): def build_body(self, params):
body = params.get("body") body = params.get("body")
if body and params["headers"]["Content-Type"] == "application/json": if body and params["headers"]["Content-Type"] == "application/json":
for k, v in body: for k, v in body.iteritems():
if v == "": if v == "":
body[k] = None body[k] = None
params["body"] = body params["body"] = body
@ -77,6 +77,7 @@ class DeviceApi(Api):
def activate(self, state, token): def activate(self, state, token):
return self.request({ return self.request({
"method": "POST",
"path": "/activate", "path": "/activate",
"body": {"state": state, "token": token} "body": {"state": state, "token": token}
}) })

View File

@ -71,15 +71,19 @@ class IBMRecognizerWrapper(object):
audio, username, password, language=language, show_all=show_all) audio, username, password, language=language, show_all=show_all)
class CerberusGoogleProxy(object): class MycroftRecognizer(object):
def __init__(self, _): def __init__(self, _):
self.version = get_version() self.version = get_version()
def transcribe( def transcribe(
self, audio, language="en-US", show_all=False, metrics=None): self, audio, language="en-US", show_all=False, metrics=None):
# FIXME - Refactor
raise CerberusAccessDenied()
timer = Stopwatch() timer = Stopwatch()
timer.start() timer.start()
identity = IdentityManager().get() identity = IdentityManager.get()
headers = {'Authorization': 'Bearer ' + identity.token} headers = {'Authorization': 'Bearer ' + identity.token}
url = ConfigurationManager.get().get("server").get("url") url = ConfigurationManager.get().get("server").get("url")
response = requests.post(url + response = requests.post(url +
@ -128,7 +132,7 @@ class CerberusGoogleProxy(object):
RECOGNIZER_IMPLS = { RECOGNIZER_IMPLS = {
'mycroft': CerberusGoogleProxy, 'mycroft': MycroftRecognizer,
'google': GoogleRecognizerWrapper, 'google': GoogleRecognizerWrapper,
'wit': WitRecognizerWrapper, 'wit': WitRecognizerWrapper,
'ibm': IBMRecognizerWrapper 'ibm': IBMRecognizerWrapper

View File

@ -28,21 +28,25 @@ class DeviceIdentity(object):
class IdentityManager(object): class IdentityManager(object):
FILE = 'identity.json' __identity = None
def __init__(self): @staticmethod
self.file_system = FileSystemAccess('identity') def load():
self.identity = DeviceIdentity() try:
self.load() with FileSystemAccess('identity').open('identity.json', 'r') as f:
IdentityManager.__identity = DeviceIdentity(**json.load(f))
except:
IdentityManager.__identity = DeviceIdentity()
def load(self): @staticmethod
with self.file_system.open(self.FILE, 'r') as f: def save(uuid, token):
self.identity = DeviceIdentity(**json.load(f)) IdentityManager.__identity.uuid = uuid
IdentityManager.__identity.token = token
with FileSystemAccess('identity').open('identity.json', 'w') as f:
json.dump(IdentityManager.__identity.__dict__, f)
def save(self, identity): @staticmethod
self.identity = identity def get():
with self.file_system.open(self.FILE, 'w') as f: if not IdentityManager.__identity:
json.dump(self.identity, f) IdentityManager.load()
return IdentityManager.__identity
def get(self):
return self.identity

View File

@ -36,8 +36,8 @@ class CerberusConfigSkill(MycroftSkill):
self.register_intent(intent, self.handle_update_intent) self.register_intent(intent, self.handle_update_intent)
def handle_update_intent(self, message): def handle_update_intent(self, message):
identity = IdentityManager().get() identity = IdentityManager.get()
if identity: if identity.token:
self.speak_dialog("not.paired") self.speak_dialog("not.paired")
else: else:
ConfigurationManager.load_remote() ConfigurationManager.load_remote()

View File

@ -21,6 +21,7 @@ from adapt.intent import IntentBuilder
from os.path import dirname from os.path import dirname
from mycroft.api import DeviceApi from mycroft.api import DeviceApi
from mycroft.identity import IdentityManager
from mycroft.skills.core import MycroftSkill from mycroft.skills.core import MycroftSkill
@ -54,8 +55,10 @@ class PairingSkill(MycroftSkill):
def activate(self): def activate(self):
try: try:
self.api.activate(self.state, self.data.get("token")) token = self.data.get("token")
uuid = self.api.activate(self.state, token)
self.speak_dialog("pairing.paired") self.speak_dialog("pairing.paired")
IdentityManager.save(uuid.get("uuid"), token)
except: except:
self.data["expiration"] -= self.delay self.data["expiration"] -= self.delay
@ -64,6 +67,7 @@ class PairingSkill(MycroftSkill):
self.handle_pairing() self.handle_pairing()
else: else:
self.activator = Timer(self.delay, self.activate) self.activator = Timer(self.delay, self.activate)
self.activator.start()
def is_paired(self): def is_paired(self):
try: try:

View File

@ -50,7 +50,7 @@ class WeatherSkill(MycroftSkill):
@property @property
def owm(self): def owm(self):
return OWM(API_key=self.config.get('api_key', ''), return OWM(API_key=self.config.get('api_key', ''),
identity=IdentityManager().get()) identity=IdentityManager.get())
def initialize(self): def initialize(self):
self.load_data_files(dirname(__file__)) self.load_data_files(dirname(__file__))

View File

@ -82,15 +82,10 @@ class CerberusWolframAlphaClient(object):
""" """
Query Wolfram|Alpha with query using the v2.0 API Query Wolfram|Alpha with query using the v2.0 API
""" """
response = {} identity = IdentityManager.get()
identity = IdentityManager().get()
if identity:
bearer_token = 'Bearer %s:%s' % \
(identity.device_id, identity.token)
query = urllib.parse.urlencode(dict(input=query)) query = urllib.parse.urlencode(dict(input=query))
url = 'https://cerberus.mycroft.ai/wolframalpha/v2/query?' + query url = 'https://cerberus.mycroft.ai/wolframalpha/v2/query?' + query
headers = {'Authorization': bearer_token} headers = {'Authorization': 'Bearer ' + identity.token}
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers)
if response.status_code == 401: if response.status_code == 401:
raise CerberusAccessDenied() raise CerberusAccessDenied()