Add sample pam based login plugin
[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                               username_text=self.lm.username_text,
45                               password_text=self.lm.password_text)
46
47     def POST(self, *args, **kwargs):
48         username = None
49         password = None
50         user = None
51         for key, value in kwargs.iteritems():
52             if key == 'login_name':
53                 username = value
54             elif key == 'login_password':
55                 password = value
56         if username is not None and password is not None:
57             user = self._authenticate(username, password)
58         else:
59             cherrypy.log.error("Error: Username or password is missing")
60
61         if user:
62             return self.lm.auth_successful(user)
63         else:
64             return self.lm.auth_failed()
65
66     def root(self, *args, **kwargs):
67         op = getattr(self, cherrypy.request.method, self.GET)
68         if callable(op):
69             return op(*args, **kwargs)
70
71
72 class LoginManager(LoginManagerBase):
73
74     def __init__(self, *args, **kwargs):
75         super(LoginManager, self).__init__(*args, **kwargs)
76         self.name = 'pam'
77         self.path = 'pam'
78         self.page = None
79         self.description = """
80 Form based login Manager that uses the system's PAM infrastructure
81 for authentication. """
82         self._options = {
83             'service name': [
84                 """ The name of the PAM service used to authenticate. """,
85                 'string',
86                 'remote'
87             ],
88             'username text': [
89                 """ The text shown to ask for the username in the form. """,
90                 'string',
91                 'Username'
92             ],
93             'password text': [
94                 """ The text shown to ask for the password in the form. """,
95                 'string',
96                 'Password'
97             ],
98         }
99
100     @property
101     def service_name(self):
102         return self.get_config_value('service name')
103
104     @property
105     def username_text(self):
106         return self.get_config_value('username text')
107
108     @property
109     def password_text(self):
110         return self.get_config_value('password text')
111
112     def get_tree(self, site):
113         self.page = Pam(site, self)
114         return self.page