Permalink
Browse files

First release

  • Loading branch information...
matsumotory committed Dec 31, 2012
1 parent 0d0e4ad commit 9c4750e1d6721ff7536b1340269ca3dbb8dfc23e
Showing with 157 additions and 0 deletions.
  1. +9 −0 Makefile
  2. 0 README
  3. +40 −0 README.md
  4. +17 −0 example/example.rb
  5. BIN mrblib/.oauth.rb.swp
  6. +91 −0 mrblib/oauth.rb
View
@@ -0,0 +1,9 @@
+GEM := mruby-oauth
+
+include $(MAKEFILE_4_GEM)
+
+GEM_RB_FILES := $(wildcard $(MRB_DIR)/*.rb)
+
+gem-all : gem-rb-files
+
+gem-clean : gem-clean-rb-files
View
0 README
No changes.
View
@@ -0,0 +1,40 @@
+# oauth Class for mruby
+refactored oauth of iij/mruby using mruby-uv and mruby-http
+
+## install by mrbgems
+```bash
+git clone git://github.com/matsumoto-r/mruby-oauth.git
+cp -pr mruby-oauth ${MRUBY_ROOT}/mrbgems/g/.
+echo mruby-oauth >> ${MRUBY_ROOT}/mrbgems/GEMS.active
+cd ${MRUBY_ROOT}
+make
+./bin/mruby ${MRUBY_ROOT}/mrbgems/g/mruby-oauth/example/example.rb
+```
+
+## example
+
+```ruby
+CONSUMER_KEY = ''
+CONSUMER_SECRET = ''
+ACCESS_TOKEN = ''
+ACCESS_TOKEN_SECRET = ''
+API_URL = 'http://api.twitter.com/1.1/statuses/update.json'
+
+tweet = "hello twitter world from mruby-oauht"
+ex_headers = {'Content-Type' => 'application/x-www-form-urlencoded'}
+twitter = OAuth.new(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
+response = twitter.post(API_URL, {:status => msg}, ex_headers)
+
+if response.code.to_i == 200
+ puts "tweet success: #{msg}"
+else
+ puts "tweet failed: #{msg}: bellow response"
+ p response
+end
+```
+
+# License
+under the MIT License:
+
+* http://www.opensource.org/licenses/mit-license.php
+
View
@@ -0,0 +1,17 @@
+CONSUMER_KEY = ''
+CONSUMER_SECRET = ''
+ACCESS_TOKEN = ''
+ACCESS_TOKEN_SECRET = ''
+API_URL = 'http://api.twitter.com/1.1/statuses/update.json'
+
+tweet = "hello twitter world from mruby-oauht"
+ex_headers = {'Content-Type' => 'application/x-www-form-urlencoded'}
+twitter = OAuth.new(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
+response = twitter.post(API_URL, {:status => msg}, ex_headers)
+
+if response.code.to_i == 200
+ puts "tweet success: #{msg}"
+else
+ puts "tweet failed: #{msg}: bellow response"
+ p response
+end
View
Binary file not shown.
View
@@ -0,0 +1,91 @@
+class OAuth < HttpRequest
+ VERSION = '0.1'
+ USER_AGENT = "mruby_oauth/" + VERSION
+ def initialize(consumer_key, consumer_secret, token, token_secret)
+ @consumer_key = consumer_key
+ @consumer_secret = consumer_secret
+ @token = token
+ @token_secret = token_secret
+ @signature_method = 'HMAC-SHA1'
+ self
+ end
+ def request(method, url, body = nil, headers = {})
+ parser = HTTP::Parser.new()
+ url = parser.parse_url(url)
+ request = create_http_request(method, body, headers)
+ host = url.host.to_sym.to_s
+ if url.query
+ request_uri = url.path + "?" + url.query
+ else
+ request_uri = url.path
+ end
+ request['Authorization'] = auth_header(method, url, request["body"])
+ request['User-Agent'] = USER_AGENT
+ host = url.host.to_sym.to_s
+ p request_uri, request
+ SimpleHttp.new(host, url.port).request(method, request_uri, request)
+ end
+ def auth_header(method, url, body)
+ parameters = oauth_parameters
+ parameters["oauth_signature"] = signature(method, url, body, parameters)
+ p 'OAuth ' + encode_parameters(parameters, ', ', '"')
+ end
+
+ OAUTH_VERSION = '1.0'
+
+ def oauth_parameters
+ {
+ "oauth_consumer_key" => @consumer_key,
+ "oauth_token" => @token,
+ "oauth_signature_method" => @signature_method,
+ "oauth_timestamp" => timestamp,
+ "oauth_nonce" => nonce,
+ "oauth_version" => OAUTH_VERSION
+ }
+ end
+ def timestamp
+ Time.now.to_i.to_s
+ end
+ def nonce
+ Digest::MD5.hexdigest(timestamp)
+ #MD5::md5_hex(timestamp)
+ end
+ def signature(*args)
+ base64(digest_hmac_sha1(signature_base_string(*args)))
+ end
+ def base64(value)
+ # [ value ].pack('m').gsub(/\n/, '')
+ r = [ value ].pack('m')
+ r.include?("\n") ? r.split("\n").join("") : r
+ end
+ def digest_hmac_sha1(value)
+ Digest::HMAC.digest(value, secret, Digest::SHA1)
+ #h.digest
+ #SHA1::sha1_hex(value)
+ end
+ def secret
+ escape(@consumer_secret) + '&' + escape(@token_secret)
+ end
+ def signature_base_string(method, url, body, parameters)
+ method = method.upcase
+ base_url = signature_base_url(url)
+ parameters = normalize_parameters(parameters, body, url.query)
+ p ['signature_base_string', method, base_url, parameters]
+ p encode_parameters([ method, base_url, parameters ])
+ end
+ def signature_base_url(url)
+ str = url.schema + "://"
+ #str += url.userinfo + "@" if url.userinfo
+ str += url.host
+ str += ":" + url.port if url.port
+ str += url.path if url.path
+ str += "?" + url.query if url.query
+ p str
+ end
+ def normalize_parameters(parameters, body, query)
+ parameters = encode_parameters(parameters, nil)
+ parameters += body.split('&') if body
+ parameters += query.split('&') if query
+ parameters.sort.join('&')
+ end
+end

0 comments on commit 9c4750e

Please sign in to comment.