Skip to content

Commit

Permalink
Change endpoint, label support
Browse files Browse the repository at this point in the history
  • Loading branch information
ridnarong committed Dec 4, 2015
1 parent f580917 commit 2970da1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 33 deletions.
10 changes: 6 additions & 4 deletions microgear/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import logging
__version__ = '1.1.3'
__version__ = '1.1.8'

gearauthsite = "http://gearauth.netpie.io:8080"
gearauthrequesttokenendpoint = gearauthsite+"/oauth/request_token"
gearauthaccesstokenendpoint = gearauthsite+"/oauth/access_token"
gearauthrequesttokenendpoint = gearauthsite+"/api/rtoken"
gearauthaccesstokenendpoint = gearauthsite+"/api/atoken"

mgrv = "PY11h;"
gearkey = None
gearsecret = None
gearlabel = None
appid = None
gearname = None
accesstoken = None
Expand All @@ -16,4 +18,4 @@
gearexaddress = None
gearexport = None
mqtt_client = None
logger = logging.getLogger("python-microgear")
logger = logging.getLogger("python-microgear")
67 changes: 38 additions & 29 deletions microgear/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def create(gearkey,gearsecret, appid="", args = {}):
format='%(asctime)s %(levelname)-8s %(message)s',
datefmt='%d/%m/%Y %I:%M:%S %p',
)

microgear.gearlabel = args.get('label')
if 'scope' in args:
matchScope = re.match( r'^(\w+:[a-zA-Z\/]+,*)+$', args['scope'])
if matchScope:
Expand Down Expand Up @@ -92,6 +92,9 @@ def client_on_message(client, userdata, msg):
on_present(str(msg.payload))
elif topics[2] == "&absent":
on_absent(str(msg.payload))
elif '&id' in topic[2]:
#controll message
pass
else:
on_message(msg.topic,str(msg.payload))

Expand All @@ -105,7 +108,7 @@ def client_on_disconnect(client, userdata, rc):
logging.debug("Diconnected with result code "+str(rc))

def connect(block=False):
global block_loop
global block_loop
block_loop = block
global current_subscribe_list
global current_id
Expand Down Expand Up @@ -178,7 +181,7 @@ def subscribe(topic):
t.start()
else:
subscribe_list.append("/"+microgear.appid+topic)


def unsubscribe(topic):
global current_subscribe_list
Expand Down Expand Up @@ -228,30 +231,35 @@ def writestream(stream,data):
def get_token():
logging.debug("Check stored token.")
cached = cache.get_item("microgear.cache")
if cached and cached.get("accesstoken"):
microgear.accesstoken = cached.get("accesstoken")
for name,value in microgear.accesstoken.items():
microgear.accesstoken[name] = str(value)
if cached:
if cached.get("accesstoken"):
if not cached.get("key"):
cached["key"] = microgear.gearkey
cache.set_item("microgear.cache", cached)
microgear.accesstoken = cached.get("accesstoken")
for name,value in microgear.accesstoken.items():
microgear.accesstoken[name] = str(value)
endpoint = microgear.accesstoken.get("endpoint").split("//")[1].split(":")
microgear.gearexaddress = endpoint[0]
microgear.gearexport = endpoint[1]
elif cached.get("requesttoken"):
get_accesstoken(cached)
else:
get_requesttoken(cached)
else:
cached = cache.set_item("microgear.cache", {})
cached["key"] = microgear.gearkey
cache.set_item("microgear.cache", cached)

if microgear.accesstoken:
endpoint = microgear.accesstoken.get("endpoint").split("//")[1].split(":")
microgear.gearexaddress = endpoint[0]
microgear.gearexport = endpoint[1]
else:
if cached.get("requesttoken"):
get_accesstoken(cached)
else:
get_requesttoken(cached)

def get_requesttoken(cached):
logging.debug("Requesting a request token.")
consumer = oauth.Consumer(key=microgear.gearkey, secret=microgear.gearsecret)
client = oauth.Client(consumer)
verifier = ''.join(random.sample(string.ascii_lowercase+string.digits,8))
verifier = microgear.mgrv
if microgear.gearlabel:
verifier+= microgear.gearlabel
else:
verifier += "_"+''.join(random.sample(string.ascii_lowercase+string.digits,8))
headers = {}
method = "POST"
params = {'oauth_callback': "scope=%s&appid=%s&verifier=%s" % (microgear.scope, microgear.appid, verifier)}
Expand Down Expand Up @@ -280,9 +288,7 @@ def get_requesttoken(cached):

def get_accesstoken(cached):
microgear.requesttoken = cached.get("requesttoken")
#send requesttoken to obtain accesstoken
logging.debug("Already has request token.")
#logging.debug(json.dumps(microgear.requesttoken))
logging.debug("Requesting an access token.")
token = oauth.Token(key=microgear.requesttoken.get("token"), secret=microgear.requesttoken.get("secret"))
consumer = oauth.Consumer(key=microgear.gearkey, secret=microgear.gearsecret)
Expand All @@ -297,21 +303,24 @@ def get_accesstoken(cached):
h = httplib2.Http(".cache")
resp, content = h.request(microgear.gearauthaccesstokenendpoint, method=method,
headers=headers)
content = content.decode('UTF-8')
matchContent = re.match( r'endpoint=(.*?)&oauth_token=(.*?)&oauth_token_secret=(.*?).*', content)
if matchContent:
content = unquote(content.decode('UTF-8'))
if resp.status == 200:
contents = content.split("&")
revokecode = hmac(contents[2].split("=")[1]+"&"+microgear.gearsecret,contents[1].split("=")[1]).replace('/','_')
revokecode = hmac(contents[3].split("=")[1]+"&"+microgear.gearsecret,contents[2].split("=")[1]).replace('/','_')
cached["requesttoken"] = None
cached["accesstoken"] = {
"token": contents[1].split("=")[1],
"secret": contents[2].split("=")[1],
"endpoint": unquote(contents[0].split("=")[1]),
"token": contents[2].split("=")[1],
"secret": contents[3].split("=")[1],
"endpoint": contents[1].split("=")[1],
"revokecode": revokecode
}
cache.set_item("microgear.cache", cached)
if contents[0].split("=")[1] == "P":
cache.set_item("microgear.cache", cached)
elif contents[0].split("=")[1] == "S":
cache.set_item("microgear.cache", {})
microgear.accesstoken = cached["accesstoken"]
else:
resettoken()
on_error("Access token is not issued, please check your consumerkey and consumersecret.")
logging.error("Access token is not issued, please check your consumerkey and consumersecret.")

Expand All @@ -330,7 +339,7 @@ def hmac(key, message):
def resettoken():
cached = cache.get_item("microgear.cache")
if cached :
microgear.accesstoken = cached["accesstoken"]
microgear.accesstoken = cached.get("accesstoken",{})
if "revokecode" in microgear.accesstoken :
path = "/api/revoke/"+microgear.accesstoken["token"]+"/"+microgear.accesstoken["revokecode"]
h = httplib2.Http(".cache")
Expand Down

0 comments on commit 2970da1

Please sign in to comment.