[bootle] authnetifizierung mit authkit
Verfasst: Samstag 6. März 2010, 18:01
Hi,
ich spiele gerade ein wenig mit Bottle rum, und würde jetzt gerne per Authkit eine Authentifizierung mit einbauen. Ich hatte gehofft dass das damit schön easy geht.
Diese Code (geklaut von der authkit seite und leicht angepasst) funktioniert:
aber wie setzte ich dass mit bottle um? mein blauäugiger ansatz funktioniert jedenfalls nicht:
kann mir da jemand auf die sprünge helfen?
Danke!
ich spiele gerade ein wenig mit Bottle rum, und würde jetzt gerne per Authkit eine Authentifizierung mit einbauen. Ich hatte gehofft dass das damit schön easy geht.
Diese Code (geklaut von der authkit seite und leicht angepasst) funktioniert:
Code: Alles auswählen
#!/usr/bin/env python
from authkit.permissions import UserIn
from authkit.authorize import authorized, authorize, PermissionError
from authkit.authorize import middleware as authorize_middleware
from paste import httpexceptions
class NoSuchActionError(httpexceptions.HTTPNotFound):
pass
class AuthorizeExampleApp:
def __call__(self, environ, start_response):
if environ['PATH_INFO'] == '/':
method = 'index'
else:
method = environ['PATH_INFO'].split('/')[1]
if not hasattr(self, method):
raise NoSuchActionError('No such method')
app = getattr(self,method)
# This facilitates an alternative way you might want to check permisisons
# rather than using an authorize() decorator
if hasattr(app, 'permission'):
app = authorize_middleware(app, app.permission)
return app(environ, start_response)
def index(self, environ, start_response):
start_response('200 OK', [('Content-type','text/html')])
return ['''
<html>
<head>
<title>AuthKit Authorize Example</title>
</head>
<body>
<h1>Authorize Example</h1>
<p>Try the following links. You should only be able to sign
in as user <tt>james</tt> with the password the same as the
username.</p>
<ul>
<li><a href="/mid_method_test">Mid Method</a></li>
<li><a href="/decorator_test">Decorator</a></li>
<li><a href="/attribute_test">Attribute (middleware)</a></li>
</ul>
<p>Once you have signed in you will need to close your
browser to clear the authentication cache.</p>
</body>
</html>
''']
def mid_method_test(self, environ, start_response):
"""Authorize using a mid-method permissions check"""
if authorized(environ, UserIn(users=['james'])):
start_response('200 OK', [('Content-type','text/html')])
return ['Access granted to /mid_method_test']
else:
start_response('200 OK', [('Content-type','text/html')])
return ['User is not authorized']
@authorize(UserIn(users=['james']))
def decorator_test(self, environ, start_response):
"""Authorize using a decorator"""
start_response('200 OK', [('Content-type','text/html')])
return ['Access granted to /decorator_test']
def attribute_test(self, environ, start_response):
"""Authorize using a permission attribute"""
start_response('200 OK', [('Content-type','text/html')])
return ['Access granted to /attribute_test']
attribute_test.permission = UserIn(users=['james'])
if __name__ == '__main__':
from paste.httpserver import serve
from authkit.authenticate import middleware
def valid(environ, username, password):
"""
Sample, very insecure validation function
"""
return username == password
app = httpexceptions.make_middleware(AuthorizeExampleApp())
app = middleware(
app,
setup_method='form,cookie',
cookie_secret='uiuisecret',
form_authenticate_user_data = """
robert:robert
james:james
""",
cookie_signoutpath = '/signout'
)
serve(app, host='0.0.0.0', port=8080)
Code: Alles auswählen
from authkit.authorize import authorized, authorize, PermissionError
from authkit.authorize import middleware as authorize_middleware
from authkit.authenticate import middleware
from authkit.permissions import UserIn
from bottle import *
@route('/')
def hello():
if authorized(request.environ, UserIn(users=['james'])):
return "Hallo James"
else:
return "hello, stranger"
@authorize(UserIn(users=['james']))
@route('/secret', method='GET')
def secret():
return "secret"
# get the default bottle application
app = default_app()
app = middleware(app,
setup_method='form,cookie',
cookie_secret='secretstring',
form_authenticate_user_data = """
robert:robert
james:james
""",
cookie_signoutpath = '/signout'
)
# run the application
run(app=app, reloader=True)
Danke!