]> git.openstreetmap.org Git - rails.git/blob - vendor/plugins/oauth-plugin/generators/oauth_provider/templates/controller.rb
58ac213910058450a5debda4b4328866e01d46f0
[rails.git] / vendor / plugins / oauth-plugin / generators / oauth_provider / templates / controller.rb
1 class OauthController < ApplicationController
2   before_filter :login_required, :except => [:request_token, :access_token, :test_request]
3   before_filter :login_or_oauth_required, :only => [:test_request]
4   before_filter :verify_oauth_consumer_signature, :only => [:request_token]
5   before_filter :verify_oauth_request_token, :only => [:access_token]
6   # Uncomment the following if you are using restful_open_id_authentication
7   # skip_before_filter :verify_authenticity_token
8
9   def request_token
10     @token = current_client_application.create_request_token
11     if @token
12       render :text => @token.to_query
13     else
14       render :nothing => true, :status => 401
15     end
16   end 
17   
18   def access_token
19     @token = current_token && current_token.exchange!
20     if @token
21       render :text => @token.to_query
22     else
23       render :nothing => true, :status => 401
24     end
25   end
26
27   def test_request
28     render :text => params.collect{|k,v|"#{k}=#{v}"}.join("&")
29   end
30   
31   def authorize
32     @token = RequestToken.find_by_token params[:oauth_token]
33     unless @token.invalidated?    
34       if request.post? 
35         if params[:authorize] == '1'
36           @token.authorize!(current_user)
37           redirect_url = params[:oauth_callback] || @token.client_application.callback_url
38           if redirect_url
39             redirect_to "#{redirect_url}?oauth_token=#{@token.token}"
40           else
41             render :action => "authorize_success"
42           end
43         elsif params[:authorize] == "0"
44           @token.invalidate!
45           render :action => "authorize_failure"
46         end
47       end
48     else
49       render :action => "authorize_failure"
50     end
51   end
52   
53   def revoke
54     @token = current_user.tokens.find_by_token params[:token]
55     if @token
56       @token.invalidate!
57       flash[:notice] = "You've revoked the token for #{@token.client_application.name}"
58     end
59     redirect_to oauth_clients_url
60   end
61   
62 end