Easy to use on command line too:
$ curl \
--data '{"title":"Foo", ...' \
http://localhost:8080\
/api/v1/poems
--user 'username:pass'
def GET( self, urlid ): user = authenticate_user( self.db ) ...
def authenticate_user( db ): auth = web.ctx.env.get( "HTTP_AUTHORIZATION" ) if auth is None: return None user, pw = extract_user_pw( auth ) ...
def extract_user_pw( auth ): auth = re.sub( "^Basic ", "", auth ) auth = base64.decodestring( auth ) return auth.split( ":" )
def authenticate_user( db ): ... if ( user in known_users and known_users[user] == pw ): return user else: raise unauthorized()
def POST( self, urlid ): user = require_authenticated_user( self.db ) ...
def require_authenticated_user( db ): user = authenticate_user( db ) if user is None: raise unathorized() return user
def is_valid_user( db, user ): return ( user is not None ) def amendpoem( ..., user ): if not is_valid_user( db, user ): raise InvalidRequest( 401 ) ...
def amendpoem( ..., user ): ... if doc["contributor"] != user: raise InvalidRequest( 403 )
Videos | youtube.com/user/ajbalaam |
---|---|
@andybalaam | |
Blog | artificialworlds.net/blog |
Projects | artificialworlds.net |