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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/oauth/controllers/consumer_controller.rb', line 56

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/oauth/controllers/consumer_controller.rb', line 37

def callback2
  @token = @consumer.access_token(current_user,params[:code], callback2_oauth_consumer_url(params[:id]))
  logger.info @token.inspect
  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

#clientObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/oauth/controllers/consumer_controller.rb', line 79

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



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/oauth/controllers/consumer_controller.rb', line 99

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

creates request token and redirects on to oauth provider's auth page If user is already connected it displays a page with an option to disconnect and redo



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

def show
  unless @token
    if @consumer.ancestors.include?(Oauth2Token)
      request_url = callback2_oauth_consumer_url(params[:id]) + '?' + request.query_string
      redirect_to @consumer.authorize_url(request_url)
    else
      request_url = callback_oauth_consumer_url(params[:id]) + '?' + request.query_string
      @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