]> git.openstreetmap.org Git - rails.git/blob - app/controllers/oauth_controller.rb
8af8af5e7bd38302778f4a5d64d85d46e8ecbcdf
[rails.git] / app / controllers / oauth_controller.rb
1 class OauthController < ApplicationController
2   layout 'site'
3
4   before_filter :authorize_web, :only => [:oauthorize, :revoke]
5   before_filter :set_locale, :only => [:oauthorize, :revoke]
6   before_filter :require_user, :only => [:oauthorize]
7   before_filter :verify_oauth_consumer_signature, :only => [:request_token]
8   before_filter :verify_oauth_request_token, :only => [:access_token]
9   # Uncomment the following if you are using restful_open_id_authentication
10   # skip_before_filter :verify_authenticity_token
11
12   def request_token
13     @token = current_client_application.create_request_token
14
15     logger.info "in REQUEST TOKEN"
16     if @token
17       logger.info "request token params: #{params.inspect}"
18       # request tokens indicate what permissions the client *wants*, not
19       # necessarily the same as those which the user allows.
20       current_client_application.permissions.each do |pref|
21         logger.info "PARAMS found #{pref}"
22         @token.write_attribute(pref, true)
23       end
24       @token.save!
25
26       render :text => @token.to_query
27     else
28       render :nothing => true, :status => 401
29     end
30   end
31
32   def access_token
33     @token = current_token && current_token.exchange!
34     if @token
35       render :text => @token.to_query
36     else
37       render :nothing => true, :status => 401
38     end
39   end
40
41   def oauthorize
42     @token = RequestToken.find_by_token params[:oauth_token]
43     unless @token.invalidated?
44       if request.post?
45         any_auth = false
46         @token.client_application.permissions.each do |pref|
47           if params[pref]
48             logger.info "OAUTHORIZE PARAMS found #{pref}"
49             @token.write_attribute(pref, true)
50             any_auth ||= true
51           else
52             @token.write_attribute(pref, false)
53           end
54         end
55
56         if any_auth
57           @token.authorize!(@user)
58           redirect_url = params[:oauth_callback] || @token.client_application.callback_url
59           if redirect_url
60             redirect_to "#{redirect_url}?oauth_token=#{@token.token}"
61           else
62             render :action => "authorize_success"
63           end
64         else
65           @token.invalidate!
66           render :action => "authorize_failure"
67         end
68       end
69     else
70       render :action => "authorize_failure"
71     end
72   end
73
74   def revoke
75     @token = @user.oauth_tokens.find_by_token params[:token]
76     if @token
77       @token.invalidate!
78       flash[:notice] = t('oauth.revoke.flash', :application => @token.client_application.name)
79     end
80     logger.info "about to redirect"
81     redirect_to :controller => 'oauth_clients', :action => 'index'
82   end
83 end