autopen 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in autopen.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 J-P Teti
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Autopen
2
+
3
+ Autopen writes your (OAuth) signatures for you. That's all it does. Simple.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'autopen'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install autopen
18
+
19
+ ## Usage
20
+
21
+ require 'autopen'
22
+ signature = Autopen::Signature.new(HTTP METHOD, URL, POST_PARAMS, {:consumer_key => "YOUR CONSUMER KEY", :consumer_secret => "YOUR CONSUMER SECRET", :version => "1.0 (probably)", :timestamp => Time.now.to_i, :nonce => "YOUR NONCE HERE", :token => "YOUR TOKEN, IF YOU HAVE ONE. OTHERWISE DON'T INCLUDE THIS VALUE AT ALL.", :token_secret => "YOUR TOKEN SECRET, IF YOU HAVE ONE. OTHERWISE DON'T INCLUDE THIS VALUE AT ALL."})
23
+ request_signature = s.generate
24
+
25
+ That's a little... generic, so let's try an example. To generate the signature the Twitter API example documented [here](https://dev.twitter.com/docs/auth/authorizing-request) and [here](https://dev.twitter.com/docs/auth/creating-signature), do this:
26
+
27
+ require 'autopen'
28
+ s = Autopen::Signature.new("post", "https://api.twitter.com/1/statuses/update.json?include_entities=true", {"status" => "Hello Ladies + Gentlemen, a signed OAuth request!"}, {:consumer_key => "xvz1evFS4wEEPTGEFPHBog", :consumer_secret => "kAcSOqF21Fu85e7zjz7ZN2U4ZRhfV3WpwPAoE3Z7kBw", :version => "1.0", :timestamp => 1318622958, :nonce => "kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg", :token => "370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb", :token_secret => "LswwdoUaIvS8ltyTt5jkRh4J50vUPVVHtR2YPi5kE"})
29
+ request_signature = s.generate # returns "tnnArxj06cWHq44gCs1OSKk%2FjLY%3D"
30
+
31
+ There's two other methods on an `Autopen::Signature` that might be useful. One is `oauth_params`, which will return an alphabetized Hash of all the OAuth params. The other is `url`, which will return the url you entered along with the OAuth params.
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/autopen.gemspec ADDED
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/autopen/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["J-P Teti"]
6
+ gem.email = ["roboteti@me.com"]
7
+ gem.description = %q{Autopen writes your (OAuth) signatures for you. That's all it does. Simple.}
8
+ gem.summary = %q{Give Autopen your request data and URL, and Autopen returns an OAuth signature. Write your requests however you like. Use Autopen to generate the signature.}
9
+ gem.homepage = "https://github.com/roboteti/autopen"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "autopen"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Autopen::VERSION
17
+ end
@@ -0,0 +1,3 @@
1
+ module Autopen
2
+ RESERVED_CHARACTERS = /[^a-zA-Z0-9\-\.\_\~]/
3
+ end
@@ -0,0 +1,102 @@
1
+ module Autopen
2
+ class Signature
3
+ attr_reader :method, :url, :post_params, :consumer, :oauth_details
4
+
5
+ def initialize(method, url, post_params, oauth_details)
6
+ @method = method
7
+ @url = url
8
+ @post_params = post_params
9
+ @oauth_details = oauth_details
10
+ end
11
+
12
+ def sign(key, base_string)
13
+ digest = OpenSSL::Digest::Digest.new('sha1')
14
+ hmac = OpenSSL::HMAC.digest(digest, key, base_string)
15
+ Base64.encode64(hmac).chomp.gsub(/\n/, '')
16
+ end
17
+
18
+ def escape(value)
19
+ # This code is stolen, er, borrowed from the oAuth gem
20
+ URI::escape(value.to_s, Autopen::RESERVED_CHARACTERS)
21
+ rescue ArgumentError
22
+ URI::escape(value.to_s.force_encoding(Encoding::UTF_8), Autopen::RESERVED_CHARACTERS)
23
+ end
24
+
25
+ def generate_param_string
26
+ if @post_params.nil?
27
+ all_params = {}
28
+ else
29
+ all_params = @post_params
30
+ end
31
+
32
+ uri_from_url = URI(@url)
33
+ unless uri_from_url.query.nil?
34
+ query_string_params = uri_from_url.query.split("&")
35
+ query_string_params.each do |param|
36
+ a = param.split("=")
37
+ all_params[a[0]] = a[1]
38
+ end
39
+ end
40
+
41
+ all_params["oauth_consumer_key"] = @oauth_details[:consumer_key]
42
+ all_params["oauth_nonce"] = @oauth_details[:nonce]
43
+ all_params["oauth_signature_method"] = "HMAC-SHA1"
44
+ all_params["oauth_timestamp"] = @oauth_details[:timestamp]
45
+ all_params["oauth_version"] = @oauth_details[:version]
46
+
47
+ unless @oauth_details[:token].nil?
48
+ all_params["oauth_token"] = @oauth_details[:token]
49
+ end
50
+
51
+ all_params = Hash[all_params.sort]
52
+ new_params = []
53
+ all_params.each do |key, value|
54
+ new_params<<("#{escape(key)}=#{escape(value)}")
55
+ end
56
+ new_params.join("&")
57
+ end
58
+
59
+ def base_string
60
+ uri_from_url = URI(@url)
61
+ url_string = uri_from_url.scheme + "://" + uri_from_url.host + uri_from_url.path
62
+ @method.upcase + "&" + escape(url_string) + "&" + escape(generate_param_string)
63
+ end
64
+
65
+ def generate
66
+ if @oauth_details[:token_secret].nil?
67
+ signing_key = @oauth_details[:consumer_secret] + "&"
68
+ else
69
+ signing_key = @oauth_details[:consumer_secret] + "&" + @oauth_details[:token_secret]
70
+ end
71
+ escape(sign(signing_key, base_string))
72
+ end
73
+
74
+ def oauth_params
75
+ keys = {
76
+ "oauth_consumer_key" => @oauth_details[:consumer_key],
77
+ "oauth_version" => @oauth_details[:version],
78
+ "oauth_timestamp" => @oauth_details[:timestamp],
79
+ "oauth_signature_method" => "HMAC-SHA1",
80
+ "oauth_nonce" => @oauth_details[:nonce],
81
+ "oauth_signature" => generate
82
+ }
83
+ Hash[keys.sort]
84
+ end
85
+
86
+ def url
87
+ get_params = oauth_params
88
+ uri_from_url = URI(@url)
89
+ if uri_from_url.query.nil?
90
+ base = @url + "?"
91
+ else
92
+ base = @url
93
+ end
94
+ params = []
95
+ get_params.each do |key, value|
96
+ params<<("#{key}=#{value}")
97
+ end
98
+ query_string = params.join("&")
99
+ base + query_string
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,3 @@
1
+ module Autopen
2
+ VERSION = "0.0.1"
3
+ end
data/lib/autopen.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "CGI"
2
+ require "openssl"
3
+ require "base64"
4
+ require "autopen/version"
5
+ require "autopen/definitions"
6
+ require "autopen/signature"
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: autopen
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - J-P Teti
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-13 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Autopen writes your (OAuth) signatures for you. That's all it does. Simple.
15
+ email:
16
+ - roboteti@me.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - autopen.gemspec
27
+ - lib/autopen.rb
28
+ - lib/autopen/definitions.rb
29
+ - lib/autopen/signature.rb
30
+ - lib/autopen/version.rb
31
+ homepage: https://github.com/roboteti/autopen
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.23
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: Give Autopen your request data and URL, and Autopen returns an OAuth signature.
55
+ Write your requests however you like. Use Autopen to generate the signature.
56
+ test_files: []