Use transactions throughout the code
[cascardo/ipsilon.git] / ipsilon / login / authfas.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2014 Ipsilon contributors, see COPYING file for license
4
5
6 from ipsilon.login.common import LoginFormBase, LoginManagerBase
7 from ipsilon.login.common import FACILITY
8 from ipsilon.util.plugin import PluginObject
9 import cherrypy
10
11 from fedora.client.fasproxy import FasProxyClient
12 from fedora.client import AuthError
13
14
15 class FAS(LoginFormBase):
16
17     def POST(self, *args, **kwargs):
18         username = kwargs.get("login_name")
19         password = kwargs.get("login_password")
20         error = None
21
22         if username and password:
23             data = None
24             try:
25                 _, data = self.lm.fpc.login(username, password)
26             except AuthError, e:
27                 cherrypy.log.error("Authentication error [%s]" % str(e))
28             except Exception, e:  # pylint: disable=broad-except
29                 cherrypy.log.error("Unknown Error [%s]" % str(e))
30             if data and data.user:
31                 return self.lm.auth_successful(self.trans,
32                                                data.user['username'],
33                                                userdata={'fas': data.user})
34             else:
35                 error = "Authentication failed"
36                 cherrypy.log.error(error)
37         else:
38             error = "Username or password is missing"
39             cherrypy.log.error("Error: " + error)
40
41         context = self.create_tmpl_context(
42             username=username,
43             error=error,
44             error_password=not password,
45             error_username=not username
46         )
47         # pylint: disable=star-args
48         return self._template(self.formtemplate, **context)
49
50
51 class LoginManager(LoginManagerBase):
52
53     def __init__(self, *args, **kwargs):
54         super(LoginManager, self).__init__(*args, **kwargs)
55         self.name = 'fas'
56         self.path = 'fas'
57         self.service_name = 'fas'
58         self.page = None
59         self.fpc = None
60         self.description = """
61 Form based login Manager that uses the Fedora Authentication Server
62 """
63         self._options = {
64             'help text': [
65                 """ The text shown to guide the user at login time. """,
66                 'string',
67                 'Login wth your FAS credentials'
68             ],
69             'username text': [
70                 """ The text shown to ask for the username in the form. """,
71                 'string',
72                 'FAS Username'
73             ],
74             'password text': [
75                 """ The text shown to ask for the password in the form. """,
76                 'string',
77                 'Password'
78             ],
79             'FAS url': [
80                 """ The FAS Url. """,
81                 'string',
82                 'https://admin.fedoraproject.org/accounts/'
83             ],
84             'FAS Proxy client user Agent': [
85                 """ The User Agent presented to the FAS Server. """,
86                 'string',
87                 'Ipsilon v1.0'
88             ],
89             'FAS Insecure Auth': [
90                 """ If 'YES' skips FAS server cert verification. """,
91                 'string',
92                 ''
93             ],
94         }
95         self.conf_opt_order = ['FAS url', 'FAS Proxy client user Agent',
96                                'FAS Insecure Auth', 'username text',
97                                'password text', 'help text']
98
99     @property
100     def help_text(self):
101         return self.get_config_value('help text')
102
103     @property
104     def username_text(self):
105         return self.get_config_value('username text')
106
107     @property
108     def password_text(self):
109         return self.get_config_value('password text')
110
111     @property
112     def fas_url(self):
113         return self.get_config_value('FAS url')
114
115     @property
116     def user_agent(self):
117         return self.get_config_value('FAS Proxy client user Agent')
118
119     @property
120     def insecure(self):
121         return self.get_config_value('FAS Insecure Auth')
122
123     def get_tree(self, site):
124         self.fpc = FasProxyClient(base_url=self.fas_url,
125                                   useragent=self.user_agent,
126                                   insecure=(self.insecure == 'YES'))
127         self.page = FAS(site, self, 'login/fas', 'login/fas.html')
128         return self.page
129
130
131 class Installer(object):
132
133     def __init__(self):
134         self.name = 'fas'
135         self.ptype = 'login'
136
137     def install_args(self, group):
138         group.add_argument('--fas', choices=['yes', 'no'], default='no',
139                            help='Configure FAS authentication')
140
141     def configure(self, opts):
142         if opts['fas'] != 'yes':
143             return
144
145         # Add configuration data to database
146         po = PluginObject()
147         po.name = 'fas'
148         po.wipe_data()
149
150         po.wipe_config_values(FACILITY)
151
152         # Update global config to add login plugin
153         po = PluginObject()
154         po.name = 'global'
155         globalconf = po.get_plugin_config(FACILITY)
156         if 'order' in globalconf:
157             order = globalconf['order'].split(',')
158         else:
159             order = []
160         order.append('fas')
161         globalconf['order'] = ','.join(order)
162         po.set_config(globalconf)
163         po.save_plugin_config(FACILITY)