예제 #1
0
파일: pixiv.py 프로젝트: shaneschulte/code
 def get_member_info(self, member_id):
     Core.trace("calling api.users(member_id=%r)", member_id)
     resp = self.api.users(member_id)
     if resp["status"] == "success":
         return resp["response"][0]
     else:
         raise PixivApiError("API call failed: %r" % resp)
예제 #2
0
파일: pixiv.py 프로젝트: shaneschulte/code
 def get_illust_info(self, illust_id):
     Core.trace("calling api.works(illust_id=%r)", illust_id)
     resp = self.api.works(illust_id)
     if resp["status"] == "success":
         return resp["response"][0]
     else:
         raise PixivApiError("API call failed: %r" % resp)
예제 #3
0
def clear_libsecret(attributes):
    Core.trace("libsecret clear: %r", attributes)
    cmd = ["secret-tool", "clear"]
    for k, v in attributes.items():
        cmd += [str(k), str(v)]

    r = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    if r.returncode != 0:
        raise KeyError("libsecret clear failed: %r" % r.stderr.decode())
예제 #4
0
 def _get_json(self, *args, **kwargs):
     resp = self.get(*args, **kwargs)
     resp.raise_for_status()
     data = json.loads(resp.text, object_hook=ObjectDict)
     Core.trace("JSON (%r) = %r", args, data)
     if data.get("error"):
         raise Exception("API error: %r", data.get("message") or data["error"])
     else:
         return data["body"]
예제 #5
0
def get_libsecret(attributes):
    Core.trace("libsecret query: %r", attributes)
    cmd = ["secret-tool", "lookup"]
    for k, v in attributes.items():
        cmd += [str(k), str(v)]

    r = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    if r.returncode != 0:
        raise KeyError("libsecret lookup failed: %r" % r.stderr.decode())

    return r.stdout.decode()
예제 #6
0
def store_libsecret(label, secret, attributes):
    Core.trace("libsecret store: %r %r", label, attributes)
    cmd = ["secret-tool", "store", "--label=%s" % label]
    for k, v in attributes.items():
        cmd += [str(k), str(v)]

    r = subprocess.run(cmd,
                       input=secret.encode(),
                       stdout=subprocess.PIPE,
                       stderr=subprocess.PIPE)
    if r.returncode != 0:
        raise IOError("libsecret store failed: (%r, %r)" %
                      (r.returncode, r.stderr.decode()))
예제 #7
0
 def _validate(self):
     Core.debug("verifying session status")
     resp = self.get("https://www.pixiv.net/member.php", allow_redirects=False)
     if resp.is_redirect:
         Core.trace("member.php redirects to %r", resp.next.url)
         url = requests.utils.urlparse(resp.next.url)
         if url.path == "/member.php":
             query = parse_query_string(url.query)
             self.user_id = int(query["id"])
             Core.debug("session is valid, userid %r", self.user_id)
             return True
     Core.debug("session is not valid")
     return False
예제 #8
0
파일: pixiv.py 프로젝트: shaneschulte/code
    def _authenticate(self):
        if self.api.user_id:
            Core.warn("BUG: _authenticate() called twice")
            return True

        data = self._load_token()
        if data:
            Core.trace("loaded token: %r", data)
            if os.environ.get("FORCE_TOKEN_REFRESH"):
                token_valid = False
            else:
                token_valid = data["expires_at"] > time.time()

            if token_valid:
                Core.debug("access token within expiry time, using as-is")
                self.api.user_id = data["user_id"]
                self.api.access_token = data["access_token"]
                self.api.refresh_token = data["refresh_token"]
                return True
            else:
                Core.debug("access token has expired, renewing")
                try:
                    token = self.api.auth(refresh_token=data["refresh_token"])
                    Core.trace("retrieved token: %r", token)
                except Exception as e:
                    Core.warn("could not refresh access token: %r", e)
                    #self._forget_token()
                else:
                    self._store_token(token)
                    return True

        data = self._load_creds()
        if data:
            Core.info("logging in to Pixiv as %r", data["login"])
            try:
                token = self.api.auth(username=data["login"],
                                      password=data["password"])
            except Exception as e:
                Core.warn("could not log in using username & password: %r", e)
            else:
                self._store_token(token)
                return True

        Core.die("could not log in to Pixiv (no credentials)")
        return False
예제 #9
0
    hash_algo = "sha512"
    data = SshsigWrapper(namespace=namespace.encode(),
                         hash_algo=hash_algo.encode(),
                         hash=hash_data(hash_algo, data)).to_bytes()

    # Sign the packet.

    agent = SshAgentConnection()
    agentkey = agent.get_key_by_fprint(args.fingerprint)

    flags = 0
    if agentkey.keyalgo == "ssh-rsa":
        flags |= SignRequestFlags.RSA_SHA2_256
    sigblob = agentkey.sign_data(data, flags)
    Core.trace("raw signature blob: %r", sigblob)

    # Show information.

    keydata = ssh_parse_publickey(agentkey.keyblob)
    sigdata = ssh_parse_signature(sigblob)
    Core.trace("parsed publickey blob: %r", keydata)
    Core.trace("parsed signature blob: %r", sigdata)

    hash_algo = sigalgo_to_digest[sigdata["algo"]]
    Core.info("Signed using %s (%s)", hash_algo, sigdata["algo"])

    sshsig = SshsigSignature(public_key=agentkey.keyblob,
                             namespace=namespace.encode(),
                             hash_algo=hash_algo.encode(),
                             signature=sigblob)