8069c6ad4019dd6ab8b483e1922e9a49445e69a3
[cascardo/ipsilon.git] / ipsilon / login / authkrb.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.login.common import LoginPageBase, LoginManagerBase
21 import cherrypy
22
23
24 class Krb(LoginPageBase):
25
26     def root(self, *args, **kwargs):
27         # Someone typed manually or a robot is walking th tree.
28         # Redirect to default page
29         return self.lm.redirect_to_path(self.lm.path)
30
31
32 class KrbAuth(LoginPageBase):
33
34     def root(self, *args, **kwargs):
35         # If we can get here, we must be authenticated and remote_user
36         # was set. Check the session has a user set already or error.
37         if self.user and self.user.name:
38             userdata = {'krb_principal_name': self.user.name}
39             return self.lm.auth_successful(self.user.name, userdata)
40         else:
41             return self.lm.auth_failed()
42
43
44 class KrbError(LoginPageBase):
45
46     def root(self, *args, **kwargs):
47         cherrypy.log.error('REQUEST: %s' % cherrypy.request.headers)
48         # If we have no negotiate header return whatever mod_auth_kerb
49         # generated and wait for the next request
50
51         if not 'WWW-Authenticate' in cherrypy.request.headers:
52             cherrypy.response.status = 401
53
54             if self.lm.next_login:
55                 return self.lm.next_login.page.root(*args, **kwargs)
56
57             conturl = '%s/login' % self.basepath
58             return self._template('login/krb.html',
59                                   title='Kerberos Login',
60                                   cont=conturl)
61
62         # If we get here, negotiate failed
63         return self.lm.auth_failed()
64
65
66 class LoginManager(LoginManagerBase):
67
68     def __init__(self, *args, **kwargs):
69         super(LoginManager, self).__init__(*args, **kwargs)
70         self.name = 'krb'
71         self.path = 'krb/negotiate'
72         self.page = None
73         self.description = """
74 Kereros Negotiate authentication plugin. Relies on the mod_auth_kerb apache
75 plugin for actual authentication. """
76
77     def get_tree(self, site):
78         self.page = Krb(site, self)
79         self.page.__dict__['negotiate'] = KrbAuth(site, self)
80         self.page.__dict__['unauthorized'] = KrbError(site, self)
81         return self.page