+ ##
+ # sets up the current_user for use by other methods. this is mostly called
+ # from the authorize method, but can be called elsewhere if authorisation
+ # is optional.
+ def setup_user_auth
+ # try and setup using OAuth
+ unless Authenticator.new(self, [:token]).allow?
+ username, passwd = get_auth_data # parse from headers
+ # authenticate per-scheme
+ self.current_user = if username.nil?
+ nil # no authentication provided - perhaps first connect (client should retry after 401)
+ elsif username == "token"
+ User.authenticate(:token => passwd) # preferred - random token for user from db, passed in basic auth
+ else
+ User.authenticate(:username => username, :password => passwd) # basic auth
+ end
+ end