Class: FlickRaw::OAuthClient
- Inherits:
-
Object
- Object
- FlickRaw::OAuthClient
show all
- Defined in:
- lib/flickraw/oauth.rb
Defined Under Namespace
Classes: FailedResponse, UnknownSignatureMethod
Instance Attribute Summary collapse
Class Method Summary
collapse
-
.authorization_header(url, params) ⇒ Object
-
.encode_value(v) ⇒ Object
-
.escape(s) ⇒ Object
-
.gen_default_params ⇒ Object
-
.gen_nonce ⇒ Object
-
.gen_timestamp ⇒ Object
-
.parse_response(text) ⇒ Object
-
.sign_hmac_sha1(method, url, params, token_secret, consumer_secret) ⇒ Object
-
.sign_plaintext(method, url, params, token_secret, consumer_secret) ⇒ Object
-
.sign_rsa_sha1(method, url, params, token_secret, consumer_secret) ⇒ Object
-
.signature_base_string(method, url, params) ⇒ Object
Instance Method Summary
collapse
-
#access_token(url, token_secret, oauth_params = {}) ⇒ Object
-
#authorize_url(url, oauth_params = {}) ⇒ Object
-
#initialize(consumer_key, consumer_secret) ⇒ OAuthClient
constructor
A new instance of OAuthClient.
-
#post_form(url, token_secret, oauth_params = {}, params = {}) ⇒ Object
-
#post_multipart(url, token_secret, oauth_params = {}, params = {}) ⇒ Object
-
#request_token(url, oauth_params = {}) ⇒ Object
Constructor Details
#initialize(consumer_key, consumer_secret) ⇒ OAuthClient
Returns a new instance of OAuthClient.
71
72
73
74
|
# File 'lib/flickraw/oauth.rb', line 71
def initialize(consumer_key, consumer_secret)
@consumer_key, @consumer_secret = consumer_key, consumer_secret
self.proxy = nil
end
|
Instance Attribute Details
#ca_file ⇒ Object
Returns the value of attribute ca_file.
67
68
69
|
# File 'lib/flickraw/oauth.rb', line 67
def ca_file
@ca_file
end
|
#ca_path ⇒ Object
Returns the value of attribute ca_path.
68
69
70
|
# File 'lib/flickraw/oauth.rb', line 68
def ca_path
@ca_path
end
|
#check_certificate ⇒ Object
Returns the value of attribute check_certificate.
66
67
68
|
# File 'lib/flickraw/oauth.rb', line 66
def check_certificate
@check_certificate
end
|
#proxy ⇒ Object
Returns the value of attribute proxy.
65
66
67
|
# File 'lib/flickraw/oauth.rb', line 65
def proxy
@proxy
end
|
#user_agent ⇒ Object
Returns the value of attribute user_agent.
64
65
66
|
# File 'lib/flickraw/oauth.rb', line 64
def user_agent
@user_agent
end
|
Class Method Details
58
59
60
61
|
# File 'lib/flickraw/oauth.rb', line 58
def (url, params)
params_norm = params.map {|k,v| %(#{escape(k)}="#{escape(v)}")}.sort.join(", ")
%(OAuth realm="#{url.to_s}", #{params_norm})
end
|
.encode_value(v) ⇒ Object
15
16
17
18
|
# File 'lib/flickraw/oauth.rb', line 15
def encode_value(v)
v = v.to_s.encode("utf-8").force_encoding("ascii-8bit") if RUBY_VERSION >= "1.9"
v.to_s
end
|
.escape(s) ⇒ Object
20
21
22
23
24
|
# File 'lib/flickraw/oauth.rb', line 20
def escape(s)
encode_value(s).gsub(/[^a-zA-Z0-9\-\.\_\~]/) { |special|
special.unpack("C*").map{|i| sprintf("%%%02X", i) }.join
}
end
|
.gen_default_params ⇒ Object
53
54
55
56
|
# File 'lib/flickraw/oauth.rb', line 53
def gen_default_params
{ :oauth_version => "1.0", :oauth_signature_method => "HMAC-SHA1",
:oauth_nonce => gen_nonce, :oauth_timestamp => gen_timestamp }
end
|
.gen_nonce ⇒ Object
52
|
# File 'lib/flickraw/oauth.rb', line 52
def gen_nonce; [OpenSSL::Random.random_bytes(32)].pack('m0').gsub(/\n$/,'') end
|
.gen_timestamp ⇒ Object
51
|
# File 'lib/flickraw/oauth.rb', line 51
def gen_timestamp; Time.now.to_i end
|
.parse_response(text) ⇒ Object
26
|
# File 'lib/flickraw/oauth.rb', line 26
def parse_response(text); Hash[text.split("&").map {|s| s.split("=")}] end
|
.sign_hmac_sha1(method, url, params, token_secret, consumer_secret) ⇒ Object
44
45
46
47
48
49
|
# File 'lib/flickraw/oauth.rb', line 44
def sign_hmac_sha1(method, url, params, token_secret, consumer_secret)
text = signature_base_string(method, url, params)
key = escape(consumer_secret) + "&" + escape(token_secret)
digest = OpenSSL::Digest::SHA1.new
[OpenSSL::HMAC.digest(digest, key, text)].pack('m0').gsub(/\n$/,'')
end
|
.sign_plaintext(method, url, params, token_secret, consumer_secret) ⇒ Object
33
34
35
|
# File 'lib/flickraw/oauth.rb', line 33
def sign_plaintext(method, url, params, token_secret, consumer_secret)
escape(consumer_secret) + "&" + escape(token_secret)
end
|
.sign_rsa_sha1(method, url, params, token_secret, consumer_secret) ⇒ Object
37
38
39
40
41
42
|
# File 'lib/flickraw/oauth.rb', line 37
def sign_rsa_sha1(method, url, params, token_secret, consumer_secret)
text = signature_base_string(method, url, params)
key = OpenSSL::PKey::RSA.new(consumer_secret)
digest = OpenSSL::Digest::SHA1.new
[key.sign(digest, text)].pack('m0').gsub(/\n$/,'')
end
|
.signature_base_string(method, url, params) ⇒ Object
28
29
30
31
|
# File 'lib/flickraw/oauth.rb', line 28
def signature_base_string(method, url, params)
params_norm = params.map {|k,v| escape(k) + "=" + escape(v)}.sort.join("&")
method.to_s.upcase + "&" + escape(url) + "&" + escape(params_norm)
end
|
Instance Method Details
#access_token(url, token_secret, oauth_params = {}) ⇒ Object
88
89
90
91
|
# File 'lib/flickraw/oauth.rb', line 88
def access_token(url, token_secret, oauth_params = {})
r = post_form(url, token_secret, oauth_params)
OAuthClient.parse_response(r.body)
end
|
#authorize_url(url, oauth_params = {}) ⇒ Object
81
82
83
84
85
86
|
# File 'lib/flickraw/oauth.rb', line 81
def authorize_url(url, oauth_params = {})
params_norm = oauth_params.map {|k,v| OAuthClient.escape(k) + "=" + OAuthClient.escape(v)}.sort.join("&")
url = URI.parse(url)
url.query = url.query ? url.query + "&" + params_norm : params_norm
url.to_s
end
|
#post_form(url, token_secret, oauth_params = {}, params = {}) ⇒ Object
93
94
95
96
|
# File 'lib/flickraw/oauth.rb', line 93
def post_form(url, token_secret, oauth_params = {}, params = {})
encoded_params = Hash[*params.map {|k,v| [OAuthClient.encode_value(k), OAuthClient.encode_value(v)]}.flatten]
post(url, token_secret, oauth_params, params) {|request| request.form_data = encoded_params}
end
|
#post_multipart(url, token_secret, oauth_params = {}, params = {}) ⇒ Object
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
|
# File 'lib/flickraw/oauth.rb', line 98
def post_multipart(url, token_secret, oauth_params = {}, params = {})
post(url, token_secret, oauth_params, params) {|request|
boundary = "FlickRaw#{OAuthClient.gen_nonce}"
request['Content-type'] = "multipart/form-data, boundary=#{boundary}"
request.body = ''
params.each { |k, v|
if v.respond_to? :read
basename = File.basename(v.path.to_s) if v.respond_to? :path
basename ||= File.basename(v.base_uri.to_s) if v.respond_to? :base_uri
basename ||= "unknown"
request.body << "--#{boundary}\r\n" <<
"Content-Disposition: form-data; name=\"#{OAuthClient.encode_value(k)}\"; filename=\"#{OAuthClient.encode_value(basename)}\"\r\n" <<
"Content-Transfer-Encoding: binary\r\n" <<
"Content-Type: image/jpeg\r\n\r\n" <<
v.read << "\r\n"
else
request.body << "--#{boundary}\r\n" <<
"Content-Disposition: form-data; name=\"#{OAuthClient.encode_value(k)}\"\r\n\r\n" <<
"#{OAuthClient.encode_value(v)}\r\n"
end
}
request.body << "--#{boundary}--"
}
end
|
#request_token(url, oauth_params = {}) ⇒ Object
76
77
78
79
|
# File 'lib/flickraw/oauth.rb', line 76
def request_token(url, oauth_params = {})
r = post_form(url, nil, {:oauth_callback => "oob"}.merge(oauth_params))
OAuthClient.parse_response(r.body)
end
|