0b05e0a6eeef8fd80a74d464edfc01cb7d114cf4
[cascardo/ipsilon.git] / ipsilon / login / authtest.py
1 # Copyright (C) 2014 Ipsilon project Contributors, for license see COPYING
2
3 from ipsilon.login.common import LoginFormBase, LoginManagerBase, \
4     LoginManagerInstaller
5 from ipsilon.util.plugin import PluginObject
6 from ipsilon.util import config as pconfig
7 import cherrypy
8 import logging
9
10
11 class TestAuth(LoginFormBase):
12
13     def POST(self, *args, **kwargs):
14         username = kwargs.get("login_name")
15         password = kwargs.get("login_password")
16         error = None
17
18         if username and password:
19             if password == 'ipsilon':
20                 cherrypy.log("User %s successfully authenticated." % username)
21                 testdata = {
22                     'givenname': 'Test User',
23                     'surname': username,
24                     'fullname': 'Test User %s' % username,
25                     'email': '%s@example.com' % username,
26                     '_groups': [username]
27                 }
28                 return self.lm.auth_successful(self.trans,
29                                                username, 'password', testdata)
30             else:
31                 cherrypy.log("User %s failed authentication." % username)
32                 error = "Authentication failed"
33         else:
34             error = "Username or password is missing"
35             cherrypy.log.error("Error: " + error, severity=logging.ERROR)
36
37         context = self.create_tmpl_context(
38             username=username,
39             error=error,
40             error_password=not password,
41             error_username=not username
42         )
43         self.lm.set_auth_error()
44         return self._template('login/form.html', **context)
45
46
47 class LoginManager(LoginManagerBase):
48
49     def __init__(self, *args, **kwargs):
50         super(LoginManager, self).__init__(*args, **kwargs)
51         self.name = 'testauth'
52         self.service_name = 'testauth'
53         self.path = 'testauth'
54         self.page = None
55         self.description = """
56 Form based TEST login Manager, DO NOT EVER ACTIVATE IN PRODUCTION """
57         self.new_config(
58             self.name,
59             pconfig.String(
60                 'username text',
61                 'Text used to ask for the username at login time.',
62                 'Username'),
63             pconfig.String(
64                 'password text',
65                 'Text used to ask for the password at login time.',
66                 'Password'),
67             pconfig.String(
68                 'help text',
69                 'Text used to guide the user at login time.',
70                 'DISABLE IN PRODUCTION, USE ONLY FOR TEST ' +
71                 'Use any username they are all valid, "admin" gives ' +
72                 'administrative powers. ' +
73                 'Use the fixed password "ipsilon" for any user')
74         )
75
76     @property
77     def help_text(self):
78         return self.get_config_value('help text')
79
80     @property
81     def username_text(self):
82         return self.get_config_value('username text')
83
84     @property
85     def password_text(self):
86         return self.get_config_value('password text')
87
88     def get_tree(self, site):
89         self.page = TestAuth(site, self, 'login/testauth')
90         return self.page
91
92
93 class Installer(LoginManagerInstaller):
94
95     def __init__(self, *pargs):
96         super(Installer, self).__init__()
97         self.name = 'testauth'
98         self.pargs = pargs
99
100     def install_args(self, group):
101         group.add_argument('--testauth', choices=['yes', 'no'], default='no',
102                            help='Configure PAM authentication')
103
104     def configure(self, opts):
105         if opts['testauth'] != 'yes':
106             return
107
108         logging.debug(self.pargs)
109         # Add configuration data to database
110         po = PluginObject(*self.pargs)
111         po.name = 'testauth'
112         po.wipe_data()
113
114         # Update global config to add login plugin
115         po.is_enabled = True
116         po.save_enabled_state()