Class: OauthToken

Inherits:
AccessToken show all
Defined in:
lib/authlogic_connect/oauth/tokens/oauth_token.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AccessToken

api_version, client, #config, #consumer, #service_name, service_name, #settings, settings

Class Method Details

.authorize_url(callback_url, &block) ⇒ Object

this is a cleaner method so we can access the authorize_url from oauth 1 or 2



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/authlogic_connect/oauth/tokens/oauth_token.rb', line 128

def authorize_url(callback_url, &block)
  consumer        = self.consumer # cached

  if oauth_version == 1.0
    request = get_request_token(callback_url, consumer)
    yield request if block_given?
    return request.authorize_url
  else
    options = {:redirect_uri => callback_url}

    unless consumer.nil? || consumer.options.empty? || consumer.options[:scope].nil?
      options[:scope] = consumer.options[:scope]
    else
      options[:scope] = self.config[:scope] unless self.config[:scope].blank?
    end
    return consumer.web_server.authorize_url(options)
  end
end

.configObject



70
71
72
# File 'lib/authlogic_connect/oauth/tokens/oauth_token.rb', line 70

def config
  super.merge(credentials[:options] || {})
end

.consumerObject



74
75
76
77
78
79
80
# File 'lib/authlogic_connect/oauth/tokens/oauth_token.rb', line 74

def consumer
  if oauth_version == 1.0
    OAuth::Consumer.new(credentials[:key], credentials[:secret], config)
  else
    OAuth2::Client.new(credentials[:key], credentials[:secret], config)
  end
end

.find_by_key_or_token(key, token, options = {}) ⇒ Object

if we’re lucky we can find it by the token.



83
84
85
86
87
88
89
90
91
# File 'lib/authlogic_connect/oauth/tokens/oauth_token.rb', line 83

def find_by_key_or_token(key, token, options = {})
  result = self.find_by_key(key, options) unless key.nil?
  unless result
    if !token.blank? && self.respond_to?(:find_by_token)
      result = self.find_by_token(token, options)
    end
  end
  result 
end

.get_access_token(oauth_verifier) ⇒ Object



159
160
161
# File 'lib/authlogic_connect/oauth/tokens/oauth_token.rb', line 159

def get_access_token(oauth_verifier)
  request_token.get_access_token(:oauth_verifier => oauth_verifier)
end

.get_request_token(callback_url, consumer = nil) ⇒ Object

if you pass a hash as the second parameter to consumer.get_request_token, ruby oauth will think this is a form and all sorts of bad things happen



153
154
155
156
157
# File 'lib/authlogic_connect/oauth/tokens/oauth_token.rb', line 153

def get_request_token(callback_url, consumer = nil)
  options = {:scope => config[:scope]} if config[:scope]
  consumer ||= self.consumer
  consumer.get_request_token({:oauth_callback => callback_url}, options)
end

.get_token_and_secret(options = {}) ⇒ Object

this is a wrapper around oauth 1 and 2. it looks obscure, but from the api point of view you won’t have to worry about it’s implementation. in oauth 1.0, key = oauth_token, secret = oauth_secret in oauth 2.0, key = code, secret = access_token



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/authlogic_connect/oauth/tokens/oauth_token.rb', line 98

def get_token_and_secret(options = {})
  oauth_verifier  = options[:oauth_verifier]
  redirect_uri    = options[:redirect_uri]
  token           = options[:token]
  secret          = options[:secret]
  consumer        = self.consumer # cached
  
  if oauth_version == 1.0
    access = request_token(token, secret).get_access_token(:oauth_verifier => oauth_verifier)
    result = {:token => access.token, :secret => access.secret, :key => nil}
    if self.oauth_key
      if oauth_key.is_a?(Proc)
        result[:key] = oauth_key.call(access)
      else
        result[:key] = access.params[self.oauth_key] || access.params[self.oauth_key.to_s] # try both
      end
    else
      puts "Access Token: #{access.inspect}"
      raise "please set an oauth key for #{service_name.to_s}"
    end
  else
    access = consumer.web_server.get_access_token(secret, :redirect_uri => redirect_uri)
    result = {:token => access.token, :secret => secret, :key => nil}
  end
  
  result
end

.key(value = nil, &block) ⇒ Object

unique key that we will use from the AccessToken response to identify the user by. in Twitter, its “user_id”. Twitter has “screen_name”, but that’s more subject to change than user_id. Pick whatever is least likely to change



58
59
60
61
62
63
64
# File 'lib/authlogic_connect/oauth/tokens/oauth_token.rb', line 58

def key(value = nil, &block)
  if block_given?
    @oauth_key = block
  else
    @oauth_key = value.is_a?(Symbol) ? value : value.to_sym
  end
end

.oauth_keyObject



66
67
68
# File 'lib/authlogic_connect/oauth/tokens/oauth_token.rb', line 66

def oauth_key
  @oauth_key
end

.oauth_versionObject



50
51
52
# File 'lib/authlogic_connect/oauth/tokens/oauth_token.rb', line 50

def oauth_version
  @oauth_version ||= 1.0
end

.request_token(token, secret) ⇒ Object



147
148
149
# File 'lib/authlogic_connect/oauth/tokens/oauth_token.rb', line 147

def request_token(token, secret)
  OAuth::RequestToken.new(consumer, token, secret)
end

.version(value) ⇒ Object

oauth version, 1.0 or 2.0



46
47
48
# File 'lib/authlogic_connect/oauth/tokens/oauth_token.rb', line 46

def version(value)
  @oauth_version = value
end

Instance Method Details

#clearObject



15
16
17
# File 'lib/authlogic_connect/oauth/tokens/oauth_token.rb', line 15

def clear
  @client = nil
end

#clientObject



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/authlogic_connect/oauth/tokens/oauth_token.rb', line 3

def client
  unless @client
    if oauth_version == 1.0
      @client = OAuth::AccessToken.new(self.consumer, self.token, self.secret)
    else
      @client = OAuth2::AccessToken.new(self.consumer, self.token)
    end
  end
  
  @client
end

#delete(path, headers = {}) ⇒ Object



39
40
41
# File 'lib/authlogic_connect/oauth/tokens/oauth_token.rb', line 39

def delete(path, headers = {})
  client.delete(path, headers)
end

#get(path, headers = {}) ⇒ Object



23
24
25
# File 'lib/authlogic_connect/oauth/tokens/oauth_token.rb', line 23

def get(path, headers = {})
  client.get(path, headers)
end

#head(path, headers = {}) ⇒ Object



31
32
33
# File 'lib/authlogic_connect/oauth/tokens/oauth_token.rb', line 31

def head(path, headers = {})
  client.head(path, headers)
end

#oauth_versionObject



19
20
21
# File 'lib/authlogic_connect/oauth/tokens/oauth_token.rb', line 19

def oauth_version
  self.class.oauth_version
end

#post(path, body = "", headers = {}) ⇒ Object



27
28
29
# File 'lib/authlogic_connect/oauth/tokens/oauth_token.rb', line 27

def post(path, body = "", headers = {})
  client.post(path, body, headers)
end

#put(path, body = "", headers = {}) ⇒ Object



35
36
37
# File 'lib/authlogic_connect/oauth/tokens/oauth_token.rb', line 35

def put(path, body = "", headers = {})
  client.put(path, body, headers)
end