tags:

views:

14

answers:

1

I'm new to python . I want to make a basic auth in a web project , is there any way get HTTP authentication variables like the '$_SERVER['php_auth_user']' in php ? I'm using the FF's torando server.

A: 

There doesn't seem to be any particular support for Basic auth in Tornado, so you'd have to do it yourself by base64-decoding the Authorization header.

Probably something like:

import base64

class BasicAuthHandler(tornado.web.RequestHandler):
    def get_current_user(self):
        scheme, _, token= self.request.headers.get('Authorization', '').partition(' ')
        if scheme.lower()=='basic':
            user, _, pwd= base64.decodestring(token).partition(':')
            # if pwd matches user:
                return user
        return None

    def get(self):
        if not self.current_user:
            self.set_status(401)
            self.set_header('WWW-Authenticate: basic realm="Example site"')
            # produce error/login page for user to see if they press escape to
            # cancel authorisation
            return

(not tested as I don't run Tornado.)

bobince
thank you , it's very helpful
Sospartan