Debian Codegraph @

keystoneclient/v3/contrib/oauth1/request_tokens.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License");
2 # you may not use this file except in compliance with the License.
3 # You may obtain a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS,
9 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
10 # implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 import urllib.parse as urlparse
15
16 from keystoneauth1 import plugin
17
18 from keystoneclient import base
19 from keystoneclient.v3.contrib.oauth1 import utils
20
21 try:
22 from oauthlib import oauth1
23 except ImportError:
24 oauth1 = None
25
26
27 class RequestToken(base.Resource):
28 def authorize(self, roles):
29 try:
30 retval = self.manager.authorize(self.id, roles)
31 self = retval
32 except Exception:
33 retval = None
34
35 return retval
36
37
38 class RequestTokenManager(base.CrudManager):
39 """Manager class for manipulating identity OAuth request tokens."""
40
41 resource_class = RequestToken
42
43 def authorize(self, request_token, roles):
44 """Authorize a request token with specific roles.
45
46 Utilize Identity API operation:
47 PUT /OS-OAUTH1/authorize/$request_token_id
48
49 :param request_token: a request token that will be authorized, and
50 can be exchanged for an access token.
51 :param roles: a list of roles, that will be delegated to the user.
52 """
53 request_id = urlparse.quote(base.getid(request_token))
54 endpoint = utils.OAUTH_PATH + '/authorize/%s' % (request_id)
55 body = {'roles': [{'id': base.getid(r_id)} for r_id in roles]}
56 return self._put(endpoint, body, "token")
57
58 def create(self, consumer_key, consumer_secret, project):
59 endpoint = utils.OAUTH_PATH + '/request_token'
60 headers = {'requested-project-id': base.getid(project)}
61 oauth_client = oauth1.Client(consumer_key,
62 client_secret=consumer_secret,
63 signature_method=oauth1.SIGNATURE_HMAC,
64 callback_uri="oob")
65 url = self.client.get_endpoint(interface=plugin.AUTH_INTERFACE).rstrip(
66 "/")
67 url, headers, body = oauth_client.sign(url + endpoint,
68 http_method='POST',
69 headers=headers)
70 resp, body = self.client.post(endpoint, headers=headers)
71 token = utils.get_oauth_token_from_body(resp.content)
72 return self._prepare_return_value(resp,
73 self.resource_class(self, token))