Module: Oauth::Controllers::ConsumerController

Included in:
OauthConsumersController
Defined in:
lib/oauth/controllers/consumer_controller.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(controller) ⇒ Object



4
5
6
7
8
9
# File 'lib/oauth/controllers/consumer_controller.rb', line 4

def self.included(controller)
  controller.class_eval do
    before_filter :load_consumer, :except=>:index
    skip_before_filter :verify_authenticity_token,:only=>:callback
  end
end

Instance Method Details

#callbackObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/oauth/controllers/consumer_controller.rb', line 65

def callback
  logger.info "CALLBACK"
  @request_token_secret=session[params[:oauth_token]]
  if @request_token_secret
    @token=@consumer.find_or_create_from_request_token(current_user,params[:oauth_token],@request_token_secret,params[:oauth_verifier])
    session[params[:oauth_token]] = nil
    if @token
      # Log user in
      if logged_in?
        flash[:notice] = "#{params[:id].humanize} was successfully connected to your account"
      else
        self.current_user = @token.user
        flash[:notice] = "You logged in with #{params[:id].humanize}"
      end
      go_back
    else
      flash[:error] = "An error happened, please try connecting again"
      redirect_to oauth_consumer_url(params[:id])
    end
  end

end

#callback2Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/oauth/controllers/consumer_controller.rb', line 47

def callback2
  @token = @consumer.access_token(current_user,params[:code], callback2_oauth_consumer_url)
  if @token
    # Log user in
    if logged_in?
      flash[:notice] = "#{params[:id].humanize} was successfully connected to your account"
    else
      self.current_user = @token.user
      flash[:notice] = "You logged in with #{params[:id].humanize}"
    end
    go_back
  else
    flash[:error] = "An error happened, please try connecting again"
    redirect_to oauth_consumer_url(params[:id])
  end

end

#callback2_oauth_consumer_urlObject



120
121
122
# File 'lib/oauth/controllers/consumer_controller.rb', line 120

def callback2_oauth_consumer_url
  @consumer.consumer.options[:redirect_uri]
end

#callback2_querystringObject



43
44
45
# File 'lib/oauth/controllers/consumer_controller.rb', line 43

def callback2_querystring
  request.query_string.blank? ? '' : '?' + request.query_string
end

#clientObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/oauth/controllers/consumer_controller.rb', line 88

def client
  method = request.method.downcase.to_sym
  path = "/#{params[:endpoint]}?#{request.query_string}"
  if consumer_credentials[:expose]
    if @token
      oauth_response = @token.client.send(method, path)
      if oauth_response.is_a? Net::HTTPRedirection
        # follow redirect
        oauth_response = @token.client.send(method, oauth_response['Location'])
      end

      render :text => oauth_response.body
    else
      render :text => "Token needed.", :status => 403
    end
  else
    render :text => "Not allowed", :status => 403
  end
end

#destroyObject



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/oauth/controllers/consumer_controller.rb', line 108

def destroy
  throw RecordNotFound unless @token
  @token.destroy
  if params[:commit]=="Reconnect"
    redirect_to oauth_consumer_url(params[:id])
  else
    flash[:notice] = "#{params[:id].humanize} was successfully disconnected from your account"

    go_back
  end
end

#indexObject



11
12
13
14
15
# File 'lib/oauth/controllers/consumer_controller.rb', line 11

def index
  @consumer_tokens=ConsumerToken.all :conditions=>{:user_id=>current_user.id}
  # The services the user hasn't already connected to
  @services=OAUTH_CREDENTIALS.keys-@consumer_tokens.collect{|c| c.class.service_name}
end

#showObject

If the user has no token or force is set as a param, creates request token and redirects on to oauth provider’s auth page. Otherwise it displays a page with an option to disconnect and redo



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/oauth/controllers/consumer_controller.rb', line 20

def show
  if @token && params[:force]
    @token.destroy
    @token = nil
  end

  unless @token
    if @consumer.ancestors.include?(Oauth2Token)
      request_url = callback2_oauth_consumer_url + callback2_querystring
      redirect_to @consumer.authorize_url(request_url)
    else
      request_url = callback_oauth_consumer_url(params[:id]) + callback2_querystring
      @request_token = @consumer.get_request_token(request_url)
      session[@request_token.token]=@request_token.secret
      if @request_token.callback_confirmed?
        redirect_to @request_token.authorize_url
      else
        redirect_to(@request_token.authorize_url + "&oauth_callback=#{callback_oauth_consumer_url(params[:id])}")
      end
    end
  end
end