David Tulloh
2016-04-20 d7df42ae13a2a9bfb73a76ed96997dad88a794a9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
""" Simple BFG application demonstrating use of repoze.who in "hybrid" mode.
 
- repoze.who middleware intercepts and validates existing request credentials,
  leaving 'REMOTE_USER' in the WSGI environ if they are OK.
 
- Application handles login / logout directly, using the repoze.who API
  to validate credentials and set headers.
"""
import logging
import os
import sys
from StringIO import StringIO
 
from paste.httpserver import serve
from repoze.bfg.authentication import RemoteUserAuthenticationPolicy
from repoze.bfg.authorization import ACLAuthorizationPolicy
from repoze.bfg.configuration import Configurator
from repoze.bfg.security import Allow
from repoze.bfg.security import Authenticated
from repoze.bfg.security import DENY_ALL
from repoze.bfg.security import Everyone
from repoze.who.api import get_api
from repoze.who.interfaces import IChallenger
from repoze.who.middleware import PluggableAuthenticationMiddleware as PAM
from repoze.who.plugins.basicauth import BasicAuthPlugin
from repoze.who.plugins.auth_tkt import AuthTktCookiePlugin
from repoze.who.plugins.redirector import RedirectorPlugin
from repoze.who.plugins.htpasswd import HTPasswdPlugin
from repoze.who.classifiers import default_request_classifier
from repoze.who.classifiers import default_challenge_decider
from webob import Response
from webob.exc import HTTPFound
 
LINK = ' <p><a href="%(url)s">%(title)s</a></p>'
 
ACTIONS = {
    'root': {'url': '/', 'title': 'Root'},   
    'protected': {'url': '/protected.html', 'title': 'Protected'},   
    'login': {'url': '/login.html', 'title': 'Login'},   
    'logout': {'url': '/logout.html', 'title': 'Logout'},   
}
 
def _actions(request):
    names = ['root']
    if 'REMOTE_USER' in request.environ:
        names.append('protected')
        names.append('logout')
    else:
        names.append('login')
    return '\n'.join([LINK % ACTIONS[x] for x in names])
    
 
PAGE = """\
<html>
<body>
 <h1>%(page_title)s</h1>
%(actions)s
</body>
</html>
"""
 
def unprotected(request):
    return Response(PAGE % {'page_title': 'Unprotected Page',
                            'actions': _actions(request),
                           })
 
def protected(request):
    return Response(PAGE % {'page_title': 'protected Page',
                            'actions': _actions(request),
                           })
 
 
LOGIN_FORM = """\
<html>
<body>
 <h1> Log In </h1>
 <form method="POST">
 %(came_from)s
 <p>%(message)s</p>
 <p>Login name: <input type="text" name="login" /></p>
 <p>Password: <input type="password" name="password" /></p>
 <p><input type="submit" name="form.login" value="Log In"/></p>
 </form>
</body>
</html>
"""
 
def login(request):
    message = ''
    info = {}
 
    # Remember any 'came_from', for redirection on succcesful login.
    came_from = request.params.get('came_from')
    if came_from is not None:
        info['came_from'] = (
        '<input type="hidden" name="came_from" value="%s" />' % came_from)
    else:
        info['came_from'] = ''
 
    who_api = get_api(request.environ)
    if 'form.login' in request.POST:
        # Validate credentials.
        creds = {}
        creds['login'] = request.POST['login']
        creds['password'] = request.POST['password']
        authenticated, headers = who_api.login(creds)
 
        if authenticated:
            # Redirect to 'came_from', or to root.
            # headers here are "remember" headers, setting the
            # auth_tkt cookies.
            return HTTPFound(location=came_from or '/',
                             headers=headers)
 
        else:
            message = 'Invalid login.'
    else:
        # Forcefully forget any existing credentials.
        _, headers = who_api.login({})
 
    # Headers here are "forget" headers, clearing the auth_tkt cookies.
    request.response_headerlist = headers
 
    if 'REMOTE_USER' in request.environ:
        del request.environ['REMOTE_USER']
 
    info['message'] = message
 
    return Response(LOGIN_FORM % info)
 
 
def logout(request):
    # Use repoze.who API to get "forget" headers.
    who_api = get_api(request.environ)
    return HTTPFound(location='/', headers=who_api.logout())
 
 
class Root(object):
    __acl__ = [(Allow, Authenticated, ('view_protected',)),
               (Allow, Everyone, ('view',)),
               DENY_ALL,
              ]
 
def get_root(*args, **kw):
    return Root()
 
 
if __name__ == '__main__':
    # Configure the BFG application
 
    ## Set up security policies, root object, etc.
    authentication_policy=RemoteUserAuthenticationPolicy()
    authorization_policy=ACLAuthorizationPolicy()
    config = Configurator(
                root_factory=get_root,
                default_permission='view',
                authentication_policy=authentication_policy,
                authorization_policy=authorization_policy,
               )
    config.begin()
 
    ## Configure views
    config.add_view(unprotected)
    config.add_view(protected, 'protected.html', permission='view_protected')
    config.add_view(login, 'login.html')
    config.add_view(logout, 'logout.html')
    config.end()
 
    ## Create the app object.
    app = config.make_wsgi_app()
 
    # Configure the repoze.who middleware:
 
    ## fake .htpasswd authentication source
    io = StringIO()
    for name, password in [('admin', 'admin'),
                           ('user', 'user')]:
        io.write('%s:%s\n' % (name, password))
    io.seek(0)
    def cleartext_check(password, hashed):
        return password == hashed
    htpasswd = HTPasswdPlugin(io, cleartext_check)
 
    ## other plugins
    basicauth = BasicAuthPlugin('repoze.who')
    auth_tkt = AuthTktCookiePlugin('secret', 'auth_tkt', digest_algo="sha512")
    redirector = RedirectorPlugin(login_url='/login.html')
    redirector.classifications = {IChallenger:['browser'] } # only for browser
 
    ## group / order plugins by function
    identifiers = [('auth_tkt', auth_tkt),
                   ('basicauth', basicauth)]
    authenticators = [('auth_tkt', auth_tkt),
                      ('htpasswd', htpasswd)]
    challengers = [('redirector', redirector),
                   ('basicauth', basicauth)]
    mdproviders = []
 
    ## set up who logging, if desired
    log_stream = None
    if os.environ.get('WHO_LOG'):
        log_stream = sys.stdout
 
    # Wrap the middleware around the application.
    middleware = PAM(app,
                     identifiers,
                     authenticators,
                     challengers,
                     mdproviders,
                     default_request_classifier,
                     default_challenge_decider,
                     log_stream = log_stream,
                     log_level = logging.DEBUG
                    )
 
    # Serve up the WSGI stack.
    serve(middleware, host='0.0.0.0')