rune 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) Brett Buddin
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,26 @@
1
+ # Rune
2
+
3
+ Rune was written to provide a simple authentication signature generator for APIs.
4
+
5
+ Imagine you have a super-awesome API. Each user of your API has a secret `auth_token` which they will use to sign requests before sending them to you. When the API receives these requests, it performs the same process on the request ata and compares the resulting signature to the signature that was given by the client. If they match—huzzah!—they have entry.
6
+
7
+ The signature is generated using the following algorithm:
8
+
9
+ * Alphabetize all POST fields
10
+ * Concatinate the key and value of each POST field to eachother, concatinate each
11
+ field name and value to the end of the URL
12
+ * Using the string containing the URL, query string and POST fields, sign them using HMAC-SHA1 and the auth_token as the key.
13
+
14
+ *I suggest providing this signature to your API via an HTTP header like "X-YOUR_API_NAME-Signature".*
15
+
16
+ ## Example Usage
17
+
18
+ ``` ruby
19
+ rune = Rune.new(
20
+ 'http://localhost/people',
21
+ 'super_secret_auth_token',
22
+ {:person => {:name => "Name", :age => "29"}, :other => "dude"}
23
+ )
24
+
25
+ rune.generate #=> "BVuqfY28b69Bnt2Kiaj2CObOec0="
26
+ ```
@@ -0,0 +1,11 @@
1
+ begin
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec') do |t|
5
+ t.pattern = FileList['spec/**/*_spec.rb']
6
+ end
7
+
8
+ task :test do
9
+ Rake::Task['spec'].invoke
10
+ end
11
+ end
@@ -0,0 +1,57 @@
1
+ require 'openssl'
2
+ require 'base64'
3
+
4
+ class Rune
5
+ VERSION = Version = '0.1.1'
6
+
7
+ attr_accessor :url
8
+ attr_accessor :auth_token
9
+ attr_accessor :params
10
+
11
+ def initialize(url, auth_token, params = {})
12
+ @url = url
13
+ @auth_token = auth_token
14
+ @params = params
15
+ end
16
+
17
+ # Generate the signature.
18
+ #
19
+ # Returns the String signature.
20
+ def generate
21
+ data = flatten(stringify_keys(@params)).join
22
+ digest = OpenSSL::Digest::Digest.new("sha1")
23
+ Base64.encode64(OpenSSL::HMAC.digest(digest, @auth_token, "#{@url}#{data}")).strip
24
+ end
25
+
26
+ private
27
+
28
+ # Stringify the keys of a Hash.
29
+ #
30
+ # Returns the Hash that has strings for keys.
31
+ def stringify_keys(hash)
32
+ hash.keys.each do |key|
33
+ hash[key.to_s] = hash.delete(key)
34
+ end
35
+
36
+ hash.values.select{ |v| v.is_a?(Hash) }.each do |h|
37
+ stringify_keys(h)
38
+ end
39
+ hash
40
+ end
41
+
42
+ # Recursively flatten the hash. Each level of the hash is sorted
43
+ # alphabetically based on key name.
44
+ #
45
+ # Returns an Array of the hash, flattened. Mind fuck.
46
+ def flatten(value)
47
+ if value.is_a?(Array)
48
+ value.flatten.map{ |v| flatten(v) }
49
+ elsif value.is_a?(Hash)
50
+ value.to_a.sort_by do |k|
51
+ k[0].to_s
52
+ end.map { |v| flatten(v) }.flatten
53
+ else
54
+ value.to_s
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rune do
4
+ it "should generate a signature from instantiated Rune" do
5
+ rune = Rune.new(
6
+ 'http://localhost/people',
7
+ 'super_secret_auth_token',
8
+ {:person => {:name => "Name", :age => "29"}, :other => "dude"}
9
+ )
10
+ rune.generate.should == 'BVuqfY28b69Bnt2Kiaj2CObOec0='
11
+ end
12
+
13
+ it "should generate the same signature for hashes with symbols and string keys" do
14
+ rune_with_symbols = Rune.new(
15
+ 'http://localhost/people',
16
+ 'super_secret_auth_token',
17
+ {:person => {:name => "Name", :age => "29", :crazy => {:nested => "stuff"}}}
18
+ )
19
+ rune_with_strings = Rune.new(
20
+ 'http://localhost/people',
21
+ 'super_secret_auth_token',
22
+ {'person' => {'name' => "Name", 'age' => "29", 'crazy' => {'nested' => "stuff"}}}
23
+ )
24
+
25
+ rune_with_symbols.generate.should == rune_with_symbols.generate
26
+ end
27
+
28
+ it "should generate the same signature for two orderings of parameters" do
29
+ rune = Rune.new(
30
+ 'http://localhost/people',
31
+ 'super_secret_auth_token',
32
+ {:other => "dude", :person => {:age => "29", :name => "Name"}}
33
+ )
34
+ rune2 = Rune.new(
35
+ 'http://localhost/people',
36
+ 'super_secret_auth_token',
37
+ {"person" => {:name => "Name", :age => "29"}, :other => "dude"}
38
+ )
39
+
40
+ rune.generate.should == rune2.generate
41
+ end
42
+ end
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/../lib/rune'
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rune
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brett Buddin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-04-28 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Signature generation library for API authentication.
15
+ email: brett@intraspirit.net
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files:
19
+ - LICENSE
20
+ - README.md
21
+ files:
22
+ - LICENSE
23
+ - README.md
24
+ - Rakefile
25
+ - lib/rune.rb
26
+ - spec/rune_spec.rb
27
+ - spec/spec_helper.rb
28
+ homepage: http://github.com/brettbuddin/rune
29
+ licenses: []
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 1.7.2
49
+ signing_key:
50
+ specification_version: 3
51
+ summary: Signature generation library for API authentication.
52
+ test_files:
53
+ - spec/rune_spec.rb
54
+ - spec/spec_helper.rb