]> git.openstreetmap.org Git - rails.git/blob - app/controllers/oauth_controller.rb
Enable image compression utils for image_optim
[rails.git] / app / controllers / oauth_controller.rb
1 require "oauth/controllers/provider_controller"
2
3 class OauthController < ApplicationController
4   include OAuth::Controllers::ProviderController
5
6   layout "site"
7
8   def login_required
9     authorize_web
10     set_locale
11     require_user
12   end
13
14   def user_authorizes_token?
15     any_auth = false
16
17     @token.client_application.permissions.each do |pref|
18       if params[pref]
19         @token.write_attribute(pref, true)
20         any_auth ||= true
21       else
22         @token.write_attribute(pref, false)
23       end
24     end
25
26     any_auth
27   end
28
29   def revoke
30     @token = current_user.oauth_tokens.find_by :token => params[:token]
31     if @token
32       @token.invalidate!
33       flash[:notice] = t("oauth.revoke.flash", :application => @token.client_application.name)
34     end
35     redirect_to oauth_clients_url(:display_name => @token.user.display_name)
36   end
37
38   protected
39
40   def oauth1_authorize
41     append_content_security_policy_directives(:form_action => %w[*])
42
43     if @token.invalidated?
44       @message = t "oauth.oauthorize_failure.invalid"
45       render :action => "authorize_failure"
46     elsif request.post?
47       if user_authorizes_token?
48         @token.authorize!(current_user)
49         callback_url = if @token.oauth10?
50                          params[:oauth_callback] || @token.client_application.callback_url
51                        else
52                          @token.oob? ? @token.client_application.callback_url : @token.callback_url
53                        end
54         @redirect_url = URI.parse(callback_url) if callback_url.present?
55
56         if @redirect_url.to_s.blank?
57           render :action => "authorize_success"
58         else
59           @redirect_url.query = if @redirect_url.query.blank?
60                                   "oauth_token=#{@token.token}"
61                                 else
62                                   @redirect_url.query +
63                                     "&oauth_token=#{@token.token}"
64                                 end
65
66           unless @token.oauth10?
67             @redirect_url.query += "&oauth_verifier=#{@token.verifier}"
68           end
69
70           redirect_to @redirect_url.to_s
71         end
72       else
73         @token.invalidate!
74         @message = t("oauth.oauthorize_failure.denied", :app_name => @token.client_application.name)
75         render :action => "authorize_failure"
76       end
77     end
78   end
79 end