1 # Filters added to this controller will be run for all controllers in the application.
2 # Likewise, all the methods added will be available for all controllers.
3 class ApplicationController < ActionController::Base
6 @user = User.find_by_token(session[:token])
9 def authorize(realm='Web Password', errormessage="Could't authenticate you")
10 username, passwd = get_auth_data
13 if @user = User.authenticate(username, passwd)
14 # user exists and password is correct ... horray!
15 if @user.methods.include? 'lastlogin'
17 @session['lastlogin'] = user.lastlogin
18 @user.last.login = Time.now
20 @session["User.id"] = @user.id
23 # the user does not exist or the password was wrong
24 @response.headers["Status"] = "Unauthorized"
25 @response.headers["WWW-Authenticate"] = "Basic realm=\"#{realm}\""
26 render_text(errormessage, 401)
31 doc = XML::Document.new
32 doc.encoding = 'UTF-8'
33 root = XML::Node.new 'osm'
34 root['version'] = API_VERSION
35 root['generator'] = 'OpenStreetMap server'
43 # extract authorisation credentials
44 if request.env.has_key? 'X-HTTP_AUTHORIZATION'
45 # try to get it where mod_rewrite might have put it
46 authdata = @request.env['X-HTTP_AUTHORIZATION'].to_s.split
47 elsif request.env.has_key? 'HTTP_AUTHORIZATION'
48 # this is the regular location
49 authdata = @request.env['HTTP_AUTHORIZATION'].to_s.split
52 # at the moment we only support basic authentication
53 if authdata and authdata[0] == 'Basic'
54 user, pass = Base64.decode64(authdata[1]).split(':')[0..1]