d5ab42896922ff178eac7399354d3a966f754cdb
[cascardo/ipsilon.git] / ipsilon / login / authpam.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2013  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 import pam
23
24
25 class Pam(LoginPageBase):
26
27     def _authenticate(self, username, password):
28         if self.lm.service_name:
29             ok = pam.authenticate(username, password, self.lm.service_name)
30         else:
31             ok = pam.authenticate(username, password)
32
33         if ok:
34             cherrypy.log("User %s successfully authenticated." % username)
35             return username
36
37         cherrypy.log("User %s failed authentication." % username)
38         return None
39
40     def GET(self, *args, **kwargs):
41         return self._template('login/pam.html', title='Login',
42                               action='%s/login/pam' % self.basepath,
43                               service_name=self.lm.service_name,
44                               help_text=self.lm.help_text,
45                               username_text=self.lm.username_text,
46                               password_text=self.lm.password_text)
47
48     def POST(self, *args, **kwargs):
49         username = None
50         password = None
51         user = None
52         for key, value in kwargs.iteritems():
53             if key == 'login_name':
54                 username = value
55             elif key == 'login_password':
56                 password = value
57         if username is not None and password is not None:
58             user = self._authenticate(username, password)
59         else:
60             cherrypy.log.error("Error: Username or password is missing")
61
62         if user:
63             return self.lm.auth_successful(user)
64         else:
65             return self.lm.auth_failed()
66
67     def root(self, *args, **kwargs):
68         op = getattr(self, cherrypy.request.method, self.GET)
69         if callable(op):
70             return op(*args, **kwargs)
71
72
73 class LoginManager(LoginManagerBase):
74
75     def __init__(self, *args, **kwargs):
76         super(LoginManager, self).__init__(*args, **kwargs)
77         self.name = 'pam'
78         self.path = 'pam'
79         self.page = None
80         self.description = """
81 Form based login Manager that uses the system's PAM infrastructure
82 for authentication. """
83         self._options = {
84             'service name': [
85                 """ The name of the PAM service used to authenticate. """,
86                 'string',
87                 'remote'
88             ],
89             'help text': [
90                 """ The text shown to guide the user at login time. """,
91                 'string',
92                 'Insert your Username and Password and then submit.'
93             ],
94             'username text': [
95                 """ The text shown to ask for the username in the form. """,
96                 'string',
97                 'Username'
98             ],
99             'password text': [
100                 """ The text shown to ask for the password in the form. """,
101                 'string',
102                 'Password'
103             ],
104         }
105
106     @property
107     def service_name(self):
108         return self.get_config_value('service name')
109
110     @property
111     def help_text(self):
112         return self.get_config_value('help text')
113
114     @property
115     def username_text(self):
116         return self.get_config_value('username text')
117
118     @property
119     def password_text(self):
120         return self.get_config_value('password text')
121
122     def get_tree(self, site):
123         self.page = Pam(site, self)
124         return self.page