]> git.openstreetmap.org Git - rails.git/blob - app/controllers/oauth_controller.rb
Use symbols consistently for names of content blocks
[rails.git] / app / controllers / oauth_controller.rb
1 class OauthController < ApplicationController
2   layout 'slim'
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     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         @token.write_attribute(pref, true)
21       end
22       @token.save!
23
24       render :text => @token.to_query
25     else
26       render :nothing => true, :status => 401
27     end
28   end
29
30   def access_token
31     @token = current_token && current_token.exchange!
32     if @token
33       render :text => @token.to_query
34     else
35       render :nothing => true, :status => 401
36     end
37   end
38
39   def oauthorize
40     @token = RequestToken.find_by_token params[:oauth_token]
41     unless @token.nil? or @token.invalidated? 
42       if request.post?
43         any_auth = false
44         @token.client_application.permissions.each do |pref|
45           if params[pref]
46             @token.write_attribute(pref, true)
47             any_auth ||= true
48           else
49             @token.write_attribute(pref, false)
50           end
51         end
52
53         if any_auth
54           @token.authorize!(@user)
55           if @token.oauth10?
56             redirect_url = params[:oauth_callback] || @token.client_application.callback_url
57           else
58             redirect_url = @token.oob? ? @token.client_application.callback_url : @token.callback_url
59           end
60           if redirect_url and not redirect_url.empty?
61             if @token.oauth10?
62               redirect_to "#{redirect_url}?oauth_token=#{@token.token}"
63             else
64               redirect_to "#{redirect_url}?oauth_token=#{@token.token}&oauth_verifier=#{@token.verifier}"
65             end
66           else
67             render :action => "authorize_success"
68           end
69         else
70           @token.invalidate!
71           render :action => "authorize_failure"
72         end
73       end
74     else
75       render :action => "authorize_failure"
76     end
77   end
78
79   def revoke
80     @token = @user.oauth_tokens.find_by_token params[:token]
81     if @token
82       @token.invalidate!
83       flash[:notice] = t('oauth.revoke.flash', :application => @token.client_application.name)
84     end
85     redirect_to :controller => 'oauth_clients', :action => 'index'
86   end
87 end