summaryrefslogtreecommitdiff
path: root/src/leap/bonafide/provider.py
blob: 5b13d73b8b72936be0462eea0282c3391de92b47 (plain)
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
# -*- 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.
"""


class LeapProviderApi(object):
    # TODO when should the provider-api object be created?
    # TODO relate to a Provider object, with autoconf flag.

    # XXX separate in auth-needing actions?
    # doing that in LeapSession right now (with a decorator)
    # but probably it would be better if we can just gather that info in just
    # one place and decorate the methods programatically.

    # XXX version this mapping !!!

    actions = {
        'signup': ('users', 'POST'),
        'handshake': ('sessions', 'POST'),
        'authenticate': ('sessions/{login}', 'PUT'),
        'update_user': ('users/{uid}', 'PUT'),
        'logout': ('logout', 'DELETE'),
        'get_vpn_cert': ('cert', 'POST'),
        'get_smtp_cert': ('smtp_cert', 'POST'),
    }

    def __init__(self, uri, version):
        self.uri = uri
        self.version = version

    @property
    def base_url(self):
        return "https://{0}/{1}".format(self.uri, self.version)

    # XXX split in two different methods?
    def get_uri_and_method(self, action_name, **extra_params):
        action = self.actions.get(action_name, None)
        if not action:
            raise ValueError("Requested a non-existent action for this API")
        resource, method = action

        uri = '{0}/{1}'.format(bytes(self.base_url), bytes(resource)).format(
            **extra_params)
        return uri, method

    # XXX add a provider_domain property, just to check if it's the right
    # provider domain?