Fix file permissions and remove shebang's
[cascardo/ipsilon.git] / ipsilon / providers / common.py
1 # Copyright (C) 2014  Simo Sorce <simo@redhat.com>
2 #
3 # see file 'COPYING' for use and warranty information
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 from ipsilon.util.log import Log
19 from ipsilon.util.plugin import PluginInstaller, PluginLoader
20 from ipsilon.util.plugin import PluginObject, PluginConfig
21 from ipsilon.util.page import Page
22 import cherrypy
23
24
25 class ProviderException(Exception, Log):
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
35 class AuthenticationError(ProviderException):
36
37     def __init__(self, message, code):
38         super(AuthenticationError, self).__init__(message)
39         self.code = code
40         self._debug('%s [%s]' % (message, code))
41
42
43 class InvalidRequest(ProviderException):
44
45     def __init__(self, message):
46         super(InvalidRequest, self).__init__(message)
47         self._debug(message)
48
49
50 class ProviderBase(PluginConfig, PluginObject):
51
52     def __init__(self, name, path, *pargs):
53         PluginConfig.__init__(self)
54         PluginObject.__init__(self, *pargs)
55         self.name = name
56         self._root = None
57         self.path = path
58         self.tree = None
59
60     def get_tree(self, site):
61         raise NotImplementedError
62
63     def register(self, root, site):
64
65         self._root = root
66         # init pages and admin interfaces
67         self.tree = self.get_tree(site)
68         self._debug('IdP Provider registered: %s' % self.name)
69
70     def on_enable(self):
71         self._root.add_subtree(self.name, self.tree)
72
73     def on_disable(self):
74         self._root.del_subtree(self.name)
75
76
77 class ProviderPageBase(Page):
78
79     def __init__(self, site, config):
80         super(ProviderPageBase, self).__init__(site)
81         self.plugin_name = config.name
82         self.cfg = config
83
84     def GET(self, *args, **kwargs):
85         raise cherrypy.HTTPError(501)
86
87     def POST(self, *args, **kwargs):
88         raise cherrypy.HTTPError(501)
89
90     def root(self, *args, **kwargs):
91         method = cherrypy.request.method
92
93         preop = getattr(self, 'pre_%s' % method, None)
94         if preop and callable(preop):
95             preop(*args, **kwargs)
96
97         op = getattr(self, method, self.GET)
98         if callable(op):
99             return op(*args, **kwargs)
100         else:
101             raise cherrypy.HTTPError(405)
102
103     def _debug(self, fact):
104         superfact = '%s: %s' % (self.plugin_name, fact)
105         super(ProviderPageBase, self)._debug(superfact)
106
107     def _audit(self, fact):
108         cherrypy.log('%s: %s' % (self.plugin_name, fact))
109
110
111 FACILITY = 'provider_config'
112
113
114 class LoadProviders(Log):
115
116     def __init__(self, root, site):
117         plugins = PluginLoader(LoadProviders, FACILITY, 'IdpProvider')
118         plugins.get_plugin_data()
119         site[FACILITY] = plugins
120
121         available = plugins.available.keys()
122         self._debug('Available providers: %s' % str(available))
123
124         for item in plugins.available:
125             plugin = plugins.available[item]
126             plugin.register(root, site)
127
128         for item in plugins.enabled:
129             self._debug('Provider plugin in enabled list: %s' % item)
130             if item not in plugins.available:
131                 continue
132             plugins.available[item].enable()
133
134
135 class ProvidersInstall(object):
136
137     def __init__(self):
138         pi = PluginInstaller(ProvidersInstall, FACILITY)
139         self.plugins = pi.get_plugins()