3 # Copyright (C) 2013 Simo Sorce <simo@redhat.com>
5 # see file 'COPYING' for use and warranty information
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.
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.
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/>.
20 from ipsilon.login.common import LoginPageBase, LoginManagerBase
25 class Pam(LoginPageBase):
27 def _authenticate(self, username, password):
28 if self.lm.service_name:
29 ok = pam.authenticate(username, password, self.lm.service_name)
31 ok = pam.authenticate(username, password)
34 cherrypy.log("User %s successfully authenticated." % username)
37 cherrypy.log("User %s failed authentication." % username)
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)
45 def POST(self, *args, **kwargs):
46 username = kwargs.get("login_name")
47 password = kwargs.get("login_password")
51 if username and password:
52 user = self._authenticate(username, password)
54 return self.lm.auth_successful(user)
56 error = "Authentication failed"
57 cherrypy.log.error(error)
59 error = "Username or password is missing"
60 cherrypy.log.error("Error: " + error)
62 context = self.create_tmpl_context(
65 error_password=not password,
66 error_username=not username
68 # pylint: disable=star-args
69 return self._template('login/pam.html', **context)
71 def root(self, *args, **kwargs):
72 op = getattr(self, cherrypy.request.method, self.GET)
74 return op(*args, **kwargs)
76 def create_tmpl_context(self, **kwargs):
78 if self.lm.next_login is not None:
79 next_url = self.lm.next_login.path
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,
90 context.update(kwargs)
94 class LoginManager(LoginManagerBase):
96 def __init__(self, *args, **kwargs):
97 super(LoginManager, self).__init__(*args, **kwargs)
101 self.description = """
102 Form based login Manager that uses the system's PAM infrastructure
103 for authentication. """
106 """ The name of the PAM service used to authenticate. """,
111 """ The text shown to guide the user at login time. """,
113 'Insert your Username and Password and then submit.'
116 """ The text shown to ask for the username in the form. """,
121 """ The text shown to ask for the password in the form. """,
128 def service_name(self):
129 return self.get_config_value('service name')
133 return self.get_config_value('help text')
136 def username_text(self):
137 return self.get_config_value('username text')
140 def password_text(self):
141 return self.get_config_value('password text')
143 def get_tree(self, site):
144 self.page = Pam(site, self)