Improve exceptions for saml2 providers
[cascardo/ipsilon.git] / ipsilon / providers / common.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2014  Simo Sorce <simo@redhat.com>
4 #
5 # see file 'COPYING' for use and warranty information
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 from ipsilon.util.plugin import PluginLoader, PluginObject
21 from ipsilon.util.page import Page
22 import cherrypy
23
24
25 class ProviderException(Exception):
26
27     def __init__(self, message):
28         super(ProviderException, self).__init__(message)
29         self.message = message
30
31     def __str__(self):
32         return repr(self.message)
33
34     def _debug(self, fact):
35         if cherrypy.config.get('debug', False):
36             cherrypy.log('%s: %s' % (self.__class__.__name__, fact))
37
38
39 class ProviderBase(PluginObject):
40
41     def __init__(self, name, path):
42         super(ProviderBase, self).__init__()
43         self.name = name
44         self.path = path
45
46
47 class ProviderPageBase(Page):
48
49     def __init__(self, site, config):
50         super(ProviderPageBase, self).__init__(site)
51         self.plugin_name = config.name
52         self.cfg = config
53
54     def GET(self, *args, **kwargs):
55         raise cherrypy.HTTPError(501)
56
57     def POST(self, *args, **kwargs):
58         raise cherrypy.HTTPError(501)
59
60     def root(self, *args, **kwargs):
61         op = getattr(self, cherrypy.request.method, self.GET)
62         if callable(op):
63             return op(*args, **kwargs)
64         else:
65             raise cherrypy.HTTPError(405)
66
67     def _debug(self, fact):
68         superfact = '%s: %s' % (self.plugin_name, fact)
69         super(ProviderPageBase, self)._debug(superfact)
70
71     def _audit(self, fact):
72         cherrypy.log('%s: %s' % (self.plugin_name, fact))
73
74
75 FACILITY = 'provider_config'
76
77
78 class LoadProviders(object):
79
80     def __init__(self, root, site):
81         loader = PluginLoader(LoadProviders, FACILITY, 'IdpProvider')
82         site[FACILITY] = loader.get_plugin_data()
83         providers = site[FACILITY]
84
85         available = providers['available'].keys()
86         self._debug('Available providers: %s' % str(available))
87
88         for item in providers['whitelist']:
89             self._debug('IdP Provider in whitelist: %s' % item)
90             if item not in providers['available']:
91                 continue
92             self._debug('IdP Provider enabled: %s' % item)
93             providers['enabled'].append(item)
94             provider = providers['available'][item]
95             if item in providers['config']:
96                 provider.set_config(providers['config'][item])
97             root.__dict__[item] = provider.get_tree(site)
98
99     def _debug(self, fact):
100         if cherrypy.config.get('debug', False):
101             cherrypy.log(fact)