| 1 |
from authkit.authenticate import middleware |
|---|
| 2 |
|
|---|
| 3 |
def sample_app(environ, start_response): |
|---|
| 4 |
""" |
|---|
| 5 |
A sample WSGI application that returns a 401 status code when the path |
|---|
| 6 |
``/private`` is entered, triggering the authenticate middleware to |
|---|
| 7 |
forward to ``/signin`` where the user is prompted to sign in. |
|---|
| 8 |
|
|---|
| 9 |
If the sign in is successful a cookie is set and the user can visit |
|---|
| 10 |
the ``/private`` path. |
|---|
| 11 |
|
|---|
| 12 |
The path ``/signout`` will display a signed out message if |
|---|
| 13 |
and sign the user out if cookie_signout = '/signout' is specified in |
|---|
| 14 |
the middelware config. |
|---|
| 15 |
|
|---|
| 16 |
The path ``/`` always displays the environment. |
|---|
| 17 |
""" |
|---|
| 18 |
if environ['PATH_INFO']=='/private' and not environ.has_key('REMOTE_USER'): |
|---|
| 19 |
start_response('401 Not signed in', []) |
|---|
| 20 |
elif environ['PATH_INFO'] == '/signout': |
|---|
| 21 |
start_response('200 OK', [('Content-type', 'text/plain')]) |
|---|
| 22 |
if environ.has_key('REMOTE_USER'): |
|---|
| 23 |
return ["Signed Out"] |
|---|
| 24 |
else: |
|---|
| 25 |
return ["Not signed in"] |
|---|
| 26 |
elif environ['PATH_INFO'] == '/signin': |
|---|
| 27 |
page = """ |
|---|
| 28 |
<html> |
|---|
| 29 |
<body> |
|---|
| 30 |
%s |
|---|
| 31 |
<form action="/signin"> |
|---|
| 32 |
Username: <input type="text" name="username" /> |
|---|
| 33 |
Password: <input type="password" name="password" /> |
|---|
| 34 |
<br /> |
|---|
| 35 |
<input type="submit" value="Sign in" /> |
|---|
| 36 |
</body> |
|---|
| 37 |
</html> |
|---|
| 38 |
""" |
|---|
| 39 |
if not environ.get('QUERY_STRING'): |
|---|
| 40 |
start_response( |
|---|
| 41 |
'200 Sign in required', |
|---|
| 42 |
[('Content-type', 'text/html')] |
|---|
| 43 |
) |
|---|
| 44 |
return [page%'<p>Please Sign In</p>'] |
|---|
| 45 |
else: |
|---|
| 46 |
# Quick and dirty sign in check, do it properly in your code |
|---|
| 47 |
params = {} |
|---|
| 48 |
for part in environ['QUERY_STRING'].split('&'): |
|---|
| 49 |
params[part.split("=")[0]] = part.split('=')[1] |
|---|
| 50 |
if params['username'] and params['username'] == params['password']: |
|---|
| 51 |
start_response('200 OK', [('Content-type', 'text/html')]) |
|---|
| 52 |
environ['paste.auth_tkt.set_user'](params['username']) |
|---|
| 53 |
return ["Signed in."] |
|---|
| 54 |
else: |
|---|
| 55 |
start_response('200 OK', [('Content-type', 'text/html')]) |
|---|
| 56 |
return [page%'<p>Invalid details</p>'] |
|---|
| 57 |
|
|---|
| 58 |
start_response('200 OK', [('Content-type', 'text/plain')]) |
|---|
| 59 |
result = ['You Have Access To This Page.\n\nHere is the environment...\n\n'] |
|---|
| 60 |
for k,v in environ.items(): |
|---|
| 61 |
result.append('%s: %s\n'%(k,v)) |
|---|
| 62 |
return result |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
app = middleware( |
|---|
| 66 |
sample_app, |
|---|
| 67 |
setup_method='forward,cookie', |
|---|
| 68 |
forward_signinpath = '/signin', |
|---|
| 69 |
cookie_signoutpath = '/signout', |
|---|
| 70 |
cookie_secret = 'somesecret', |
|---|
| 71 |
) |
|---|
| 72 |
|
|---|
| 73 |
if __name__ == '__main__': |
|---|
| 74 |
from paste.httpserver import serve |
|---|
| 75 |
serve(app, host='0.0.0.0', port=8080) |
|---|
| 76 |
|
|---|