Add transactions support
[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(data.user['username'],
32                                                userdata={'fas': data.user})
33             else:
34                 error = "Authentication failed"
35                 cherrypy.log.error(error)
36         else:
37             error = "Username or password is missing"
38             cherrypy.log.error("Error: " + error)
39
40         context = self.create_tmpl_context(
41             username=username,
42             error=error,
43             error_password=not password,
44             error_username=not username
45         )
46         # pylint: disable=star-args
47         return self._template(self.formtemplate, **context)
48
49
50 class LoginManager(LoginManagerBase):
51
52     def __init__(self, *args, **kwargs):
53         super(LoginManager, self).__init__(*args, **kwargs)
54         self.name = 'fas'
55         self.path = 'fas'
56         self.service_name = 'fas'
57         self.page = None
58         self.fpc = None
59         self.description = """
60 Form based login Manager that uses the Fedora Authentication Server
61 """
62         self._options = {
63             'help text': [
64                 """ The text shown to guide the user at login time. """,
65                 'string',
66                 'Login wth your FAS credentials'
67             ],
68             'username text': [
69                 """ The text shown to ask for the username in the form. """,
70                 'string',
71                 'FAS Username'
72             ],
73             'password text': [
74                 """ The text shown to ask for the password in the form. """,
75                 'string',
76                 'Password'
77             ],
78             'FAS url': [
79                 """ The FAS Url. """,
80                 'string',
81                 'https://admin.fedoraproject.org/accounts/'
82             ],
83             'FAS Proxy client user Agent': [
84                 """ The User Agent presented to the FAS Server. """,
85                 'string',
86                 'Ipsilon v1.0'
87             ],
88             'FAS Insecure Auth': [
89                 """ If 'YES' skips FAS server cert verification. """,
90                 'string',
91                 ''
92             ],
93         }
94         self.conf_opt_order = ['FAS url', 'FAS Proxy client user Agent',
95                                'FAS Insecure Auth', 'username text',
96                                'password text', 'help text']
97
98     @property
99     def help_text(self):
100         return self.get_config_value('help text')
101
102     @property
103     def username_text(self):
104         return self.get_config_value('username text')
105
106     @property
107     def password_text(self):
108         return self.get_config_value('password text')
109
110     @property
111     def fas_url(self):
112         return self.get_config_value('FAS url')
113
114     @property
115     def user_agent(self):
116         return self.get_config_value('FAS Proxy client user Agent')
117
118     @property
119     def insecure(self):
120         return self.get_config_value('FAS Insecure Auth')
121
122     def get_tree(self, site):
123         self.fpc = FasProxyClient(base_url=self.fas_url,
124                                   useragent=self.user_agent,
125                                   insecure=(self.insecure == 'YES'))
126         self.page = FAS(site, self, 'login/fas', 'login/fas.html')
127         return self.page
128
129
130 class Installer(object):
131
132     def __init__(self):
133         self.name = 'fas'
134         self.ptype = 'login'
135
136     def install_args(self, group):
137         group.add_argument('--fas', choices=['yes', 'no'], default='no',
138                            help='Configure FAS authentication')
139
140     def configure(self, opts):
141         if opts['fas'] != 'yes':
142             return
143
144         # Add configuration data to database
145         po = PluginObject()
146         po.name = 'fas'
147         po.wipe_data()
148
149         po.wipe_config_values(FACILITY)
150
151         # Update global config to add login plugin
152         po = PluginObject()
153         po.name = 'global'
154         globalconf = po.get_plugin_config(FACILITY)
155         if 'order' in globalconf:
156             order = globalconf['order'].split(',')
157         else:
158             order = []
159         order.append('fas')
160         globalconf['order'] = ','.join(order)
161         po.set_config(globalconf)
162         po.save_plugin_config(FACILITY)