Authentication

In this tutorial, I'll run through how to get a remoting server setup that deals with authentication. I'll assume that you already have the latest version of PyAMF installed (at the time of writing, 0.1b is the most recent version).

I am going to build a WSGI remoting gateway, but you can use the principles applied here to any of the other supported gateway implementations (Django or Twisted). Check out the tutorials for more info.

from wsgiref import simple_server from pyamf.remoting.gateway.wsgi import WSGIGateway class CalcService(object): def add(a, b): return a + b def auth(username, password): if username == 'jane' and password == 'doe': return True return False httpd = simple_server.WSGIServer(('localhost', 8080), simple_server.WSGIRequestHandler) gateway = WSGIGateway({'calc': CalcService}, authenticator=auth) httpd.set_app(gateway) httpd.serve_forever()

Lets dissect this code and work out what is going on:

from wsgiref import simple_server

Imports the pure Python implementation of the WSGI framework, that we are going to use to serve our gateway.

from pyamf.remoting.gateway.wsgi import WSGIGateway

Imports the WSGI compliant remoting gateway that we are going to use to service AMF requests.

class CalcService(object): def add(a, b): return a + b

Our one and only service that will be attached to the remoting gateway.

def auth(username, password): if username == 'jane' and password == 'doe': return True return False

This function will be used as our authenticator. It is over simplified for the purposes of this tutorial as you would usually connect to a database to determine the validity of the supplied credentials (or something similar). Only the user 'jane', using the password 'doe' will be allowed to access any service attached the gateway.

httpd = simple_server.WSGIServer(('localhost', 8080), simple_server.WSGIRequestHandler)

Initialises our http server, serving to 'http://localhost:8080/'. Change these values as appropriate.

gateway = WSGIGateway({'calc': CalcService}, authenticator=auth)

This is the important bit. This attaches the CalcService to the gateway, using the name 'calc' and sets up the auth function we defined earlier as our authenticator.

httpd.set_app(gateway)

This attaches our gateway to the web server.

httpd.serve_forever()

And finally, we run the webserver.

So, all thats left is to show you a working example! Luckily for you we set up a demo authentication remoting server at http://demo.pyamf.org/gateway/authentication (check the source of this gateway for more info). We've also created a piece of flash that authenticates the supplied credentials against the server:

If you enter the username: jane and the password: doe, the sum of 1 + 2 is calculated for you. Find the source of the flash example here. If you use any other username or password, then you will get an Authentication Error.

Comments

blog comments powered by Disqus