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