PAM page
[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         context = self.create_tmpl_context()
42         # pylint: disable=star-args
43         return self._template('login/pam.html', **context)
44
45     def POST(self, *args, **kwargs):
46         username = kwargs.get("login_name")
47         password = kwargs.get("login_password")
48         user = None
49         error = None
50
51         if username and password:
52             user = self._authenticate(username, password)
53             if user:
54                 return self.lm.auth_successful(user)
55             else:
56                 error = "Authentication failed"
57                 cherrypy.log.error(error)
58         else:
59             error = "Username or password is missing"
60             cherrypy.log.error("Error: " + error)
61
62         context = self.create_tmpl_context(
63             username=username,
64             error=error,
65             error_password=not password,
66             error_username=not username
67         )
68         # pylint: disable=star-args
69         return self._template('login/pam.html', **context)
70
71     def root(self, *args, **kwargs):
72         op = getattr(self, cherrypy.request.method, self.GET)
73         if callable(op):
74             return op(*args, **kwargs)
75
76     def create_tmpl_context(self, **kwargs):
77         next_url = None
78         if self.lm.next_login is not None:
79             next_url = self.lm.next_login.path
80
81         context = {
82             "title": 'Login',
83             "action": '%s/login/pam' % self.basepath,
84             "service_name": self.lm.service_name,
85             "username_text": self.lm.username_text,
86             "password_text": self.lm.password_text,
87             "description": self.lm.help_text,
88             "next_url": next_url,
89         }
90         context.update(kwargs)
91         return context
92
93
94 class LoginManager(LoginManagerBase):
95
96     def __init__(self, *args, **kwargs):
97         super(LoginManager, self).__init__(*args, **kwargs)
98         self.name = 'pam'
99         self.path = 'pam'
100         self.page = None
101         self.description = """
102 Form based login Manager that uses the system's PAM infrastructure
103 for authentication. """
104         self._options = {
105             'service name': [
106                 """ The name of the PAM service used to authenticate. """,
107                 'string',
108                 'remote'
109             ],
110             'help text': [
111                 """ The text shown to guide the user at login time. """,
112                 'string',
113                 'Insert your Username and Password and then submit.'
114             ],
115             'username text': [
116                 """ The text shown to ask for the username in the form. """,
117                 'string',
118                 'Username'
119             ],
120             'password text': [
121                 """ The text shown to ask for the password in the form. """,
122                 'string',
123                 'Password'
124             ],
125         }
126
127     @property
128     def service_name(self):
129         return self.get_config_value('service name')
130
131     @property
132     def help_text(self):
133         return self.get_config_value('help text')
134
135     @property
136     def username_text(self):
137         return self.get_config_value('username text')
138
139     @property
140     def password_text(self):
141         return self.get_config_value('password text')
142
143     def get_tree(self, site):
144         self.page = Pam(site, self)
145         return self.page