/usr/lib/python2.7/dist-packages/pypump/client.py is in python-pypump 0.7-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | ##
# Copyright (C) 2013 Jessica T. (Tsyesika) <xray7224@googlemail.com>
#
# This program 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.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
##
from __future__ import absolute_import
import json
import logging
import requests
from pypump.exceptions import ClientException
_log = logging.getLogger(__name__)
class Client(object):
"""This represents a client/application which is using the Pump API.
:param webfinger: webfinger id of this user, ie "mary@example.org"
This is probably your username @ the domain of the pump instance
you're using.
:param type: whether this is a "web" or "native" client. Unless you're
using pypump as part of a web service, you should choose "native".
:param name: The name of this client, ie your application's name. For
example, if you were using PyPump to write CoolCommunicator, you
would put "CoolCommunicator" here.
:param contacts: Who wrote this application? List of email addresses
of those who authored this client.
:param redirect: a list of URIs as callbacks for the authorization
server
:param logo: URI of your application's logo.
Additionally, the following init args should only be supplied if
you've already registered this client with the server. If not,
these will be filled in during the `self.register()` step.
:param key: If This is the token (the `client_id`) we got back that
identifies our client, assuming we've already registered our
client.
:param secret: This is your secret token that authorizes you to connect
to the pump instance (the `client_secret`).
:param expirey: When our token expires (`espires_at`)
Note that the above three are not the same as the oauth
permissions verifying the user has access to post, this is related
to permissions and identification of the client software to post.
For the premission related to the access of the account, see
PyPump.key and PyPump.secret.
"""
ENDPOINT = "api/client/register"
def __init__(self, webfinger, type, name=None, contacts=None, redirect=None,
logo=None, key=None, secret=None, expirey=None):
self.webfinger = webfinger
self.name = name
self.type = type
self.logo = logo
self.contacts = contacts or []
self.redirect = redirect or []
self.key = key
self.secret = secret
self.expirey = expirey
self._pump = None
@property
def server(self):
return self.webfinger.split("@", 1)[1]
@property
def nickname(self):
return self.webfinger.split("@", 1)[0]
def set_pump(self, pump):
self._pump = pump
@property
def context(self):
""" Provides request context """
type = "client_associate" if self.key is None else "client_update"
data = {
"type": type,
"application_type": self.type,
}
# is this an update?
if self.key:
data["client_id"] = self.key
data["client_secret"] = self.secret
# Add optional params
if self.name:
data["application_name"] = self.name
if self.logo:
data["logo_url"] = self.logo
if self.contacts:
# space seporated list
data["contacts"] = " ".join(self.contacts)
if self.redirect:
data["redirect_uri"] = " ".join(self.redirect)
# Convert to JSON and send
return json.dumps(data)
def request(self, server=None):
""" Sends the request """
request = {
"headers": {"Content-Type": "application/json"},
"timeout": self._pump.timeout,
"data": self.context,
}
url = "{proto}://{server}/{endpoint}".format(
proto=self._pump.protocol,
server=server or self.server,
endpoint=self.ENDPOINT,
)
response = self._pump._requester(requests.post, url, **request)
try:
server_data = response.json()
except ValueError:
raise ClientException(response.content)
if "error" in server_data:
raise ClientException(server_data["error"], self.context)
_log.debug("Client registration recieved: %(id)s %(secret)s %(expire)s", {
"id": server_data["client_id"],
"secret": server_data["client_secret"],
"expire": server_data["expires_at"],
})
return server_data
def register(self, server=None):
""" Registers the client with the Pump API retrieving the id and secret """
if (self.key or self.secret):
return self.update()
server_data = self.request(server)
self.key = server_data["client_id"]
self.secret = server_data["client_secret"]
self.expirey = server_data["expires_at"]
def update(self):
""" Updates the information the Pump server has about the client """
error = ""
if self.key is None:
error = "To update a client you need to provide a key"
if self.secret is None:
error = "To update a client you need to provide the secret"
if error:
raise ClientException(error)
self.request()
return True
def __repr__(self):
if self.key:
return "<Client {0} ({1})>".format(self.server, self.key)
return "<Client {0}>".format(self.server)
def __str__(self):
return repr(self)
|