End-user UI for SP Portal
[cascardo/ipsilon.git] / ipsilon / providers / common.py
1 # Copyright (C) 2014 Ipsilon project Contributors, for license see COPYING
2
3 from ipsilon.util.log import Log
4 from ipsilon.util.plugin import PluginInstaller, PluginLoader
5 from ipsilon.util.plugin import PluginObject
6 from ipsilon.util.config import ConfigHelper
7 from ipsilon.util.page import Page
8 from ipsilon.util.page import admin_protect
9 from ipsilon.rest.common import RestPage
10 import cherrypy
11
12
13 class ProviderException(Exception, Log):
14
15     def __init__(self, message):
16         super(ProviderException, self).__init__(message)
17         self.message = message
18
19     def __str__(self):
20         return repr(self.message)
21
22
23 class AuthenticationError(ProviderException):
24
25     def __init__(self, message, code):
26         super(AuthenticationError, self).__init__(message)
27         self.code = code
28         self.debug('%s [%s]' % (message, code))
29
30
31 class InvalidRequest(ProviderException):
32
33     def __init__(self, message):
34         super(InvalidRequest, self).__init__(message)
35         self.debug(message)
36
37
38 class ProviderBase(ConfigHelper, PluginObject):
39
40     def __init__(self, name, path, *pargs):
41         ConfigHelper.__init__(self)
42         PluginObject.__init__(self, *pargs)
43         self.name = name
44         self._root = None
45         self.path = path
46         self.tree = None
47
48     def get_tree(self, site):
49         raise NotImplementedError
50
51     def register(self, root, site):
52
53         self._root = root
54         # init pages and admin interfaces
55         self.tree = self.get_tree(site)
56         self.debug('IdP Provider registered: %s' % self.name)
57
58     def on_enable(self):
59         self._root.add_subtree(self.name, self.tree)
60
61     def on_disable(self):
62         self._root.del_subtree(self.name)
63
64     def get_providers(self):
65         return []
66
67
68 class ProviderPageBase(Page):
69
70     def __init__(self, site, config):
71         super(ProviderPageBase, self).__init__(site)
72         self.plugin_name = config.name
73         self.cfg = config
74
75     def GET(self, *args, **kwargs):
76         raise cherrypy.HTTPError(501)
77
78     def POST(self, *args, **kwargs):
79         raise cherrypy.HTTPError(501)
80
81     def root(self, *args, **kwargs):
82         method = cherrypy.request.method
83
84         preop = getattr(self, 'pre_%s' % method, None)
85         if preop and callable(preop):
86             preop(*args, **kwargs)
87
88         op = getattr(self, method, self.GET)
89         if callable(op):
90             return op(*args, **kwargs)
91         else:
92             raise cherrypy.HTTPError(405)
93
94     def debug(self, fact):
95         superfact = '%s: %s' % (self.plugin_name, fact)
96         super(ProviderPageBase, self).debug(superfact)
97
98     def _audit(self, fact):
99         cherrypy.log('%s: %s' % (self.plugin_name, fact))
100
101
102 FACILITY = 'provider_config'
103
104
105 class ProviderInstaller(object):
106     def __init__(self):
107         self.facility = FACILITY
108         self.ptype = 'provider'
109         self.name = None
110
111     def unconfigure(self, opts, changes):
112         return
113
114     def install_args(self, group):
115         raise NotImplementedError
116
117     def validate_args(self, args):
118         return
119
120     def configure(self, opts, changes):
121         raise NotImplementedError
122
123
124 class LoadProviders(Log):
125
126     def __init__(self, root, site):
127         plugins = PluginLoader(LoadProviders, FACILITY, 'IdpProvider')
128         plugins.get_plugin_data()
129         site[FACILITY] = plugins
130
131         available = plugins.available.keys()
132         self.debug('Available providers: %s' % str(available))
133
134         for item in plugins.available:
135             plugin = plugins.available[item]
136             plugin.register(root, site)
137
138         for item in plugins.enabled:
139             self.debug('Provider plugin in enabled list: %s' % item)
140             if item not in plugins.available:
141                 continue
142             plugins.available[item].enable()
143
144
145 class ProvidersInstall(object):
146
147     def __init__(self):
148         pi = PluginInstaller(ProvidersInstall, FACILITY)
149         self.plugins = pi.get_plugins()
150
151
152 class RestProviderBase(RestPage):
153
154     def __init__(self, site, config):
155         super(RestProviderBase, self).__init__(site)
156         self.plugin_name = config.name
157         self.cfg = config
158
159     @admin_protect
160     def GET(self, *args, **kwargs):
161         raise cherrypy.HTTPError(501)
162
163     @admin_protect
164     def POST(self, *args, **kwargs):
165         raise cherrypy.HTTPError(501)
166
167     @admin_protect
168     def DELETE(self, *args, **kwargs):
169         raise cherrypy.HTTPError(501)
170
171     @admin_protect
172     def PUT(self, *args, **kwargs):
173         raise cherrypy.HTTPError(501)
174
175     def root(self, *args, **kwargs):
176         method = cherrypy.request.method
177
178         preop = getattr(self, 'pre_%s' % method, None)
179         if preop and callable(preop):
180             preop(*args, **kwargs)
181
182         op = getattr(self, method, self.GET)
183         if callable(op):
184             return op(*args, **kwargs)
185         else:
186             raise cherrypy.HTTPError(405)
187
188     def debug(self, fact):
189         superfact = '%s: %s' % (self.plugin_name, fact)
190         super(RestProviderBase, self).debug(superfact)
191
192     def _audit(self, fact):
193         cherrypy.log('%s: %s' % (self.plugin_name, fact))