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
|
# -*- coding: utf-8 -*-
# provier.py
# Copyright (C) 2015 LEAP
#
# 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/>.
"""
LEAP Provider API.
"""
from copy import deepcopy
import re
from urlparse import urlparse
class _MetaActionDispatcher(type):
"""
A metaclass that will create dispatcher methods dynamically for each
action made available by the LEAP provider API.
The new methods will be created according to the values contained in an
`_actions` dictionary, with the following format::
{'action_name': (uri_template, method)}
where `uri_template` is a string that will be formatted with an arbitrary
number of keyword arguments.
Any class that uses this one as its metaclass needs to implement two
private methods::
_get_uri(self, action_name, **extra_params)
_get_method(self, action_name)
Beware that currently they cannot be inherited from bases.
"""
def __new__(meta, name, bases, dct):
def _generate_action_funs(dct):
_get_uri = dct['_get_uri']
_get_method = dct['_get_method']
newdct = deepcopy(dct)
actions = dct['_actions']
def create_uri_fun(action_name):
return lambda self, **kw: _get_uri(
self, action_name=action_name, **kw)
def create_met_fun(action_name):
return lambda self: _get_method(
self, action_name=action_name)
for action in actions:
uri, method = actions[action]
_action_uri = 'get_%s_uri' % action
_action_met = 'get_%s_method' % action
newdct[_action_uri] = create_uri_fun(action)
newdct[_action_met] = create_met_fun(action)
return newdct
newdct = _generate_action_funs(dct)
return super(_MetaActionDispatcher, meta).__new__(
meta, name, bases, newdct)
class BaseProvider(object):
def __init__(self, netloc, version=1):
parsed = urlparse(netloc)
if parsed.scheme != 'https':
raise ValueError(
'ProviderApi needs to be passed a url with https scheme')
self.netloc = parsed.netloc
self.version = version
def get_hostname(self):
return urlparse(self._get_base_url()).hostname
def _get_base_url(self):
return "https://{0}/{1}".format(self.netloc, self.version)
class Api(BaseProvider):
"""
An object that has all the information that a client needs to communicate
with the remote methods exposed by the web API of a LEAP provider.
The actions are described in https://leap.se/bonafide
By using the _MetaActionDispatcher as a metaclass, the _actions dict will
be translated dynamically into a set of instance methods that will allow
getting the uri and method for each action.
The keyword arguments specified in the format string will automatically
raise a KeyError if the needed keyword arguments are not passed to the
dynamically created methods.
"""
# TODO when should the provider-api object be created?
# TODO pass a Provider object to constructor, with autoconf flag.
# TODO make the actions attribute depend on the api version
# TODO missing UPDATE USER RECORD
__metaclass__ = _MetaActionDispatcher
_actions = {
'signup': ('users', 'POST'),
'update_user': ('users/{uid}', 'PUT'),
'handshake': ('sessions', 'POST'),
'authenticate': ('sessions/{login}', 'PUT'),
'logout': ('logout', 'DELETE'),
'vpn_cert': ('cert', 'POST'),
'smtp_cert': ('smtp_cert', 'POST'),
}
# Methods expected by the dispatcher metaclass
def _get_uri(self, action_name, **extra_params):
resource, _ = self._actions.get(action_name)
uri = '{0}/{1}'.format(
bytes(self._get_base_url()),
bytes(resource)).format(**extra_params)
return uri
def _get_method(self, action_name):
_, method = self._actions.get(action_name)
return method
class Discovery(BaseProvider):
"""
Discover basic information about a provider, including the provided
services.
"""
__metaclass__ = _MetaActionDispatcher
_actions = {
'provider_info': ('provider.json', 'GET'),
'configs': ('1/configs.json', 'GET'),
}
api_uri = None
api_port = None
def _get_base_url(self):
if self.api_uri:
base = self.api_uri
else:
base = self.netloc
uri = "https://{0}".format(base)
if self.api_port:
uri = uri + ':%s' % self.api_port
return uri
def get_base_uri(self):
return self._get_base_url()
# Methods expected by the dispatcher metaclass
def _get_uri(self, action_name, **extra_params):
resource, _ = self._actions.get(action_name)
uri = '{0}/{1}'.format(
bytes(self._get_base_url()),
bytes(resource)).format(**extra_params)
return uri
def _get_method(self, action_name):
_, method = self._actions.get(action_name)
return method
def validate_username(username):
accepted_characters = '^[a-z0-9\-\_\.]*$'
if not re.match(accepted_characters, username):
raise ValueError('Only lowercase letters, digits, . - and _ allowed.')
|