def get_requesttoken(cached):
    logging.info("Requesting a request token.")
    path = ""
    url = ""
    if len(microgear.gearalias):
        verifier = microgear.gearalias
        path = '/oauth2/authorize?' + "response_type=code&client_id=" + microgear.gearkey + "&scope=appid:" + microgear.appid + " " + "alias:" + microgear.gearalias + "&state=mgrev:" + microgear.mgrev
    else:
        verifier = microgear.mgrev
        path = '/oauth2/authorize?' + "response_type=code&client_id=" + microgear.gearkey + "&scope=appid:" + microgear.appid + "&state=mgrev:" + microgear.mgrev

    if microgear.securemode:
        url = "https://" + microgear.gearauthsite + ":" + microgear.gearapisecureport + path
    else:
        url = "http://" + microgear.gearauthsite + ":" + microgear.gearapiport + path
    response = requests.get(url)
    response = response.url.split("code=")
    if len(response) == 2:
        cached["requesttoken"] = {
            "token": response[1],
            "secret": None,
            "verifier": verifier
        }
        cache.set_item("microgear-" + microgear.gearkey + ".cache", cached)
        microgear.requesttoken = cached["requesttoken"]
        get_accesstoken(cached)
    else:
        on_error(
            "Request token is not issued, please check your appkey and appsecret."
        )
        logging.error(
            "Request token is not issued, please check your appkey and appsecret."
        )
Exemple #2
0
def get_requesttoken(cached):
    logging.debug("Requesting a request token.")
    consumer = oauth.Consumer(key=microgear.gearkey, secret=microgear.gearsecret)
    client = oauth.Client(consumer)
    if len(microgear.gearalias):
        verifier = microgear.gearalias
    else:
        verifier = microgear.mgrev
    headers = {}
    method = "POST"
    params = {'oauth_callback': "scope=%s&mgrev=%s&appid=%s&verifier=%s" % (microgear.scope, microgear.mgrev, microgear.appid, verifier)}
    req = oauth.Request.from_consumer_and_token(consumer, http_method=method,
            http_url=microgear.gearauthrequesttokenendpoint, parameters=params)
    req.sign_request(oauth.SignatureMethod_HMAC_SHA1(), consumer, None)
    headers.update(req.to_header(realm="NETPIE"))
    h = httplib2.Http(os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])),".cache"))
    resp, content = h.request(microgear.gearauthrequesttokenendpoint, method=method,
            headers=headers)
    parsed_resp = parse_qs(content.decode(encoding='UTF-8'))
    if resp.status == 200:
        cached["requesttoken"] = {
            "token": parsed_resp['oauth_token'][0],
            "secret": parsed_resp['oauth_token_secret'][0],
            "verifier": verifier
        }
        cache.set_item("microgear-"+microgear.gearkey+".cache", cached)
        microgear.requesttoken = cached["requesttoken"]
        get_accesstoken(cached)
    else:
        on_error("Request token is not issued, please check your appkey and appsecret.")
        logging.error("Request token is not issued, please check your appkey and appsecret.")
Exemple #3
0
def get_accesstoken(cached):
    microgear.requesttoken = cached.get("requesttoken")
    logging.debug("Already has request token.")
    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)
    client = oauth.Client(consumer, token)
    params = {"oauth_verifier": microgear.requesttoken["verifier"]}
    headers = {}
    method = "POST"
    req = oauth.Request.from_consumer_and_token(
        consumer,
        token=token,
        http_method=method,
        http_url=microgear.gearauthaccesstokenendpoint,
        parameters=params)
    req.sign_request(oauth.SignatureMethod_HMAC_SHA1(), consumer, token)
    headers.update(req.to_header(realm="NETPIE"))
    h = httplib2.Http(
        os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])), ".cache"))
    resp, content = h.request(microgear.gearauthaccesstokenendpoint,
                              method=method,
                              headers=headers)
    parsed_resp = parse_qs(content.decode(encoding='UTF-8'))
    if resp.status == 200:
        revokecode = hmac(
            parsed_resp['oauth_token_secret'][0] + "&" + microgear.gearsecret,
            parsed_resp['oauth_token'][0]).replace('/', '_')
        cached["requesttoken"] = None
        cached["accesstoken"] = {
            "token": parsed_resp['oauth_token'][0],
            "secret": parsed_resp['oauth_token_secret'][0],
            "endpoint": parsed_resp['endpoint'][0],
            "revokecode": revokecode
        }
        flag = parsed_resp.get('flag', ["P"])
        if flag[0] == "P":
            cache.set_item("microgear-" + microgear.gearkey + ".cache", cached)
        elif flag[0] == "S":
            cache.set_item("microgear-" + microgear.gearkey + ".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."
        )
Exemple #4
0
def get_requesttoken(cached):
    logging.debug("Requesting a request token.")
    consumer = oauth.Consumer(key=microgear.gearkey,
                              secret=microgear.gearsecret)
    client = oauth.Client(consumer)
    if len(microgear.gearalias):
        verifier = microgear.gearalias
    else:
        verifier = microgear.mgrev
    headers = {}
    method = "POST"
    params = {
        'oauth_callback':
        "scope=%s&mgrev=%s&appid=%s&verifier=%s" %
        (microgear.scope, microgear.mgrev, microgear.appid, verifier)
    }
    req = oauth.Request.from_consumer_and_token(
        consumer,
        http_method=method,
        http_url=microgear.gearauthrequesttokenendpoint,
        parameters=params)
    req.sign_request(oauth.SignatureMethod_HMAC_SHA1(), consumer, None)
    headers.update(req.to_header(realm="NETPIE"))
    h = httplib2.Http(
        os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])), ".cache"))
    resp, content = h.request(microgear.gearauthrequesttokenendpoint,
                              method=method,
                              headers=headers)
    parsed_resp = parse_qs(content.decode(encoding='UTF-8'))
    if resp.status == 200:
        cached["requesttoken"] = {
            "token": parsed_resp['oauth_token'][0],
            "secret": parsed_resp['oauth_token_secret'][0],
            "verifier": verifier
        }
        cache.set_item("microgear-" + microgear.gearkey + ".cache", cached)
        microgear.requesttoken = cached["requesttoken"]
        get_accesstoken(cached)
    else:
        on_error(
            "Request token is not issued, please check your appkey and appsecret."
        )
        logging.error(
            "Request token is not issued, please check your appkey and appsecret."
        )
Exemple #5
0
def get_token():
    logging.debug("Check stored token.")
    cached = cache.get_item("microgear-"+microgear.gearkey+".cache")
    if cached:
        if cached.get("accesstoken"):
            if not cached.get("key"):
                cached["key"] = microgear.gearkey
                cache.set_item("microgear-"+microgear.gearkey+".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-"+microgear.gearkey+".cache", {"key": microgear.gearkey})
def get_accesstoken(cached):
    microgear.requesttoken = cached.get("requesttoken")
    logging.info("Already has request token.")
    logging.info("Requesting an access token.")
    verifier = ""
    url = ""
    path = "/oauth2/token?grant_type=authorization_code&code=" + microgear.requesttoken.get(
        "token"
    ) + "&client_id=" + microgear.gearkey + "&client_secret=" + microgear.gearsecret + "&state=mgrev:" + microgear.mgrev
    if microgear.securemode:
        url = "https://" + microgear.gearauthsite + ":" + microgear.gearapisecureport + path
    else:
        url = "http://" + microgear.gearauthsite + ":" + microgear.gearapiport + path
    response = requests.post(url)
    if response.status_code == 200:
        datajson = json.loads(response.text)
        token = datajson['access_token'].split(':')
        revokecode = hmac(token[1] + "&" + microgear.gearsecret,
                          token[0]).replace('/', '_')
        cached["requesttoken"] = None
        cached["accesstoken"] = {
            "token": token[0],
            "secret": token[1],
            "endpoint": datajson['endpoint'],
            "revokecode": revokecode
        }
        flag = datajson['flag']
        if flag == "P":
            cache.set_item("microgear-" + microgear.gearkey + ".cache", cached)
        elif flag == "S":
            cache.set_item("microgear-" + microgear.gearkey + ".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."
        )
Exemple #7
0
def get_token():
    logging.debug("Check stored token.")
    cached = cache.get_item("microgear-" + microgear.gearkey + ".cache")
    if cached:
        if cached.get("accesstoken"):
            if not cached.get("key"):
                cached["key"] = microgear.gearkey
                cache.set_item("microgear-" + microgear.gearkey + ".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-" + microgear.gearkey + ".cache",
                                {"key": microgear.gearkey})
Exemple #8
0
def get_accesstoken(cached):
    microgear.requesttoken = cached.get("requesttoken")
    logging.debug("Already has request token.")
    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)
    client = oauth.Client(consumer, token)
    params = { "oauth_verifier": microgear.requesttoken["verifier"]}
    headers = {}
    method = "POST"
    req = oauth.Request.from_consumer_and_token(consumer, token=token, http_method=method,
            http_url=microgear.gearauthaccesstokenendpoint, parameters=params)
    req.sign_request(oauth.SignatureMethod_HMAC_SHA1(), consumer, token)
    headers.update(req.to_header(realm="NETPIE"))
    h = httplib2.Http(os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])),".cache"))
    resp, content = h.request(microgear.gearauthaccesstokenendpoint, method=method,
            headers=headers)
    parsed_resp = parse_qs(content.decode(encoding='UTF-8'))
    if resp.status == 200:
        revokecode = hmac(parsed_resp['oauth_token_secret'][0]+"&"+microgear.gearsecret,parsed_resp['oauth_token'][0]).replace('/','_')
        cached["requesttoken"] = None
        cached["accesstoken"] = {
            "token": parsed_resp['oauth_token'][0],
            "secret": parsed_resp['oauth_token_secret'][0],
            "endpoint": parsed_resp['endpoint'][0],
            "revokecode": revokecode
        }
        flag = parsed_resp.get('flag',["P"])
        if flag[0] == "P":
            cache.set_item("microgear-"+microgear.gearkey+".cache", cached)
        elif flag[0] == "S":
            cache.set_item("microgear-"+microgear.gearkey+".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.")