sigil 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "activesupport", ">= 3.0.0"
4
+ gem "i18n"
5
+
6
+ group :development do
7
+ gem "rspec", "~> 2.1.0"
8
+ gem "bundler", "~> 1.0.0"
9
+ gem "jeweler", "~> 1.5.1"
10
+ gem "rcov", ">= 0"
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.0.3)
5
+ diff-lcs (1.1.2)
6
+ git (1.2.5)
7
+ i18n (0.5.0)
8
+ jeweler (1.5.1)
9
+ bundler (~> 1.0.0)
10
+ git (>= 1.2.5)
11
+ rake
12
+ rake (0.8.7)
13
+ rcov (0.9.9)
14
+ rspec (2.1.0)
15
+ rspec-core (~> 2.1.0)
16
+ rspec-expectations (~> 2.1.0)
17
+ rspec-mocks (~> 2.1.0)
18
+ rspec-core (2.1.0)
19
+ rspec-expectations (2.1.0)
20
+ diff-lcs (~> 1.1.2)
21
+ rspec-mocks (2.1.0)
22
+
23
+ PLATFORMS
24
+ ruby
25
+
26
+ DEPENDENCIES
27
+ activesupport (>= 3.0.0)
28
+ bundler (~> 1.0.0)
29
+ i18n
30
+ jeweler (~> 1.5.1)
31
+ rcov
32
+ rspec (~> 2.1.0)
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "sigil"
16
+ gem.homepage = "http://github.com/steveh/sigil"
17
+ gem.license = "MIT"
18
+ gem.summary = "Signs and verifies a set of parameters"
19
+ gem.description = "Signs and verifies a set of parameters"
20
+ gem.email = "steve@seven.net.nz"
21
+ gem.authors = ["Steve Hoeksema"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ gem.add_development_dependency 'rspec', '>= 2.1.0'
26
+ gem.add_development_dependency 'activesupport', '>= 3.0.0'
27
+ end
28
+ Jeweler::RubygemsDotOrgTasks.new
29
+
30
+ require 'rspec/core'
31
+ require 'rspec/core/rake_task'
32
+ RSpec::Core::RakeTask.new(:spec) do |spec|
33
+ spec.pattern = FileList['spec/**/*_spec.rb']
34
+ end
35
+
36
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
37
+ spec.pattern = 'spec/**/*_spec.rb'
38
+ spec.rcov = true
39
+ end
40
+
41
+ task :default => :spec
42
+
43
+ require 'rake/rdoctask'
44
+ Rake::RDocTask.new do |rdoc|
45
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
46
+
47
+ rdoc.rdoc_dir = 'rdoc'
48
+ rdoc.title = "sigil #{version}"
49
+ rdoc.rdoc_files.include('README*')
50
+ rdoc.rdoc_files.include('lib/**/*.rb')
51
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/lib/sigil.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "active_support/core_ext/hash"
2
+ require "active_support/core_ext/object/conversions"
3
+ require "openssl"
4
+
5
+ module Sigil
6
+ class Error < StandardError; end
7
+ end
8
+
9
+ require "sigil/base"
data/lib/sigil/base.rb ADDED
@@ -0,0 +1,35 @@
1
+ module Sigil
2
+ class Base
3
+ attr_reader :params, :key, :signature
4
+
5
+ def initialize(params, key)
6
+ raise Sigil::Error, "Params must be a Hash" unless params.kind_of?(Hash)
7
+ @params = params.to_options!
8
+ @key = key.to_s
9
+ @signature = nil
10
+ end
11
+
12
+ def to_query
13
+ @params.to_query
14
+ end
15
+
16
+ def signature
17
+ unless @signature
18
+ @signature = self.class.sign(self.to_query, @key).to_s
19
+ end
20
+ @signature
21
+ end
22
+
23
+ def self.sign(string, key)
24
+ hmacd = OpenSSL::HMAC.new(key, OpenSSL::Digest::SHA1.new)
25
+ hmacd.update(string)
26
+ end
27
+
28
+ def verify(provided_signature)
29
+ raise Sigil::Error, "Params not set" if params.empty?
30
+ raise Sigil::Error, "Signature not set" if provided_signature.blank?
31
+
32
+ signature == provided_signature
33
+ end
34
+ end
35
+ end
data/sigil.gemspec ADDED
@@ -0,0 +1,71 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{sigil}
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Steve Hoeksema"]
12
+ s.date = %q{2010-12-08}
13
+ s.description = %q{Signs and verifies a set of parameters}
14
+ s.email = %q{steve@seven.net.nz}
15
+ s.files = [
16
+ ".document",
17
+ "Gemfile",
18
+ "Gemfile.lock",
19
+ "Rakefile",
20
+ "VERSION",
21
+ "lib/sigil.rb",
22
+ "lib/sigil/base.rb",
23
+ "sigil.gemspec",
24
+ "spec/sigil/base_spec.rb",
25
+ "spec/spec_helper.rb"
26
+ ]
27
+ s.homepage = %q{http://github.com/steveh/sigil}
28
+ s.licenses = ["MIT"]
29
+ s.require_paths = ["lib"]
30
+ s.rubygems_version = %q{1.3.7}
31
+ s.summary = %q{Signs and verifies a set of parameters}
32
+ s.test_files = [
33
+ "spec/sigil/base_spec.rb",
34
+ "spec/spec_helper.rb"
35
+ ]
36
+
37
+ if s.respond_to? :specification_version then
38
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
39
+ s.specification_version = 3
40
+
41
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
42
+ s.add_runtime_dependency(%q<activesupport>, [">= 3.0.0"])
43
+ s.add_runtime_dependency(%q<i18n>, [">= 0"])
44
+ s.add_development_dependency(%q<rspec>, ["~> 2.1.0"])
45
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
46
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.1"])
47
+ s.add_development_dependency(%q<rcov>, [">= 0"])
48
+ s.add_development_dependency(%q<rspec>, [">= 2.1.0"])
49
+ s.add_development_dependency(%q<activesupport>, [">= 3.0.0"])
50
+ else
51
+ s.add_dependency(%q<activesupport>, [">= 3.0.0"])
52
+ s.add_dependency(%q<i18n>, [">= 0"])
53
+ s.add_dependency(%q<rspec>, ["~> 2.1.0"])
54
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
55
+ s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
56
+ s.add_dependency(%q<rcov>, [">= 0"])
57
+ s.add_dependency(%q<rspec>, [">= 2.1.0"])
58
+ s.add_dependency(%q<activesupport>, [">= 3.0.0"])
59
+ end
60
+ else
61
+ s.add_dependency(%q<activesupport>, [">= 3.0.0"])
62
+ s.add_dependency(%q<i18n>, [">= 0"])
63
+ s.add_dependency(%q<rspec>, ["~> 2.1.0"])
64
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
65
+ s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
66
+ s.add_dependency(%q<rcov>, [">= 0"])
67
+ s.add_dependency(%q<rspec>, [">= 2.1.0"])
68
+ s.add_dependency(%q<activesupport>, [">= 3.0.0"])
69
+ end
70
+ end
71
+
@@ -0,0 +1,81 @@
1
+ require "spec_helper"
2
+
3
+ describe Sigil::Base do
4
+
5
+ before :each do
6
+ @params = { :cutlery => "knife", "fruit" => :apple }
7
+ @key = "deadbeef"
8
+ @sigil = Sigil::Base.new(@params, @key)
9
+ @signature = "e8a8f0ba2d3be6ec3965e0d7b6f9daf690972f3a"
10
+ end
11
+
12
+ describe "initialization" do
13
+
14
+ it "should require params to be set and of type Hash" do
15
+ lambda { Sigil::Base.new(HashWithIndifferentAccess.new, "deadbeef") }.should_not raise_error
16
+ lambda { Sigil::Base.new("banana", "deadbeef") }.should raise_error
17
+ end
18
+
19
+ it "should convert the key to a string" do
20
+ sigil = Sigil::Base.new({}, 123)
21
+ sigil.send(:key).should == "123"
22
+ end
23
+
24
+ it "should symbolify params" do
25
+ @sigil.params.should == { :cutlery => "knife", :fruit => :apple }
26
+ end
27
+
28
+ end
29
+
30
+ describe "signature generation" do
31
+
32
+ it "should convert the params to a query string" do
33
+ @sigil.to_query.should == "cutlery=knife&fruit=apple"
34
+ end
35
+
36
+ it "should sign the query string with the provided key" do
37
+ @sigil.signature.should == @signature
38
+ end
39
+
40
+ it "should return the same signature when called more than once" do
41
+ @sigil.signature.should == @signature
42
+ @sigil.signature.should == @signature
43
+ end
44
+
45
+ end
46
+
47
+ describe "signing" do
48
+
49
+ it "should return the key as as HMAC object" do
50
+ Sigil::Base.sign("lorem ipsum", @key).should be_a_kind_of(OpenSSL::HMAC)
51
+ end
52
+
53
+ it "should sign a string with a key" do
54
+ Sigil::Base.sign("lorem ipsum", @key).to_s.should == "372b097876a405c2c5ceef96ca6eecea623ff649"
55
+ end
56
+
57
+ end
58
+
59
+ describe "input verification convenience methods" do
60
+
61
+ it "should verify against a provided signature" do
62
+ @sigil.verify(@signature).should == true
63
+ end
64
+
65
+ it "should verify against a wrong signature" do
66
+ @sigil.verify("abc").should == false
67
+ end
68
+
69
+ it "should raise an error if no params present" do
70
+ sigil = Sigil::Base.new({}, @key)
71
+ lambda { sigil.verify("abc") }.should raise_error(Sigil::Error, /Params not set/)
72
+ end
73
+
74
+ it "should raise an error if no signature specified" do
75
+ sigil = Sigil::Base.new(@params, @key)
76
+ lambda { sigil.verify("") }.should raise_error(Sigil::Error, /Signature not set/)
77
+ end
78
+
79
+ end
80
+
81
+ end
@@ -0,0 +1,11 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'sigil'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+ end
metadata ADDED
@@ -0,0 +1,190 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sigil
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Steve Hoeksema
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-12-08 00:00:00 +13:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activesupport
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 3
29
+ - 0
30
+ - 0
31
+ version: 3.0.0
32
+ type: :runtime
33
+ prerelease: false
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: i18n
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 0
44
+ version: "0"
45
+ type: :runtime
46
+ prerelease: false
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: rspec
50
+ requirement: &id003 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 2
57
+ - 1
58
+ - 0
59
+ version: 2.1.0
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: bundler
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ~>
69
+ - !ruby/object:Gem::Version
70
+ segments:
71
+ - 1
72
+ - 0
73
+ - 0
74
+ version: 1.0.0
75
+ type: :development
76
+ prerelease: false
77
+ version_requirements: *id004
78
+ - !ruby/object:Gem::Dependency
79
+ name: jeweler
80
+ requirement: &id005 !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ segments:
86
+ - 1
87
+ - 5
88
+ - 1
89
+ version: 1.5.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: *id005
93
+ - !ruby/object:Gem::Dependency
94
+ name: rcov
95
+ requirement: &id006 !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ segments:
101
+ - 0
102
+ version: "0"
103
+ type: :development
104
+ prerelease: false
105
+ version_requirements: *id006
106
+ - !ruby/object:Gem::Dependency
107
+ name: rspec
108
+ requirement: &id007 !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ segments:
114
+ - 2
115
+ - 1
116
+ - 0
117
+ version: 2.1.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: *id007
121
+ - !ruby/object:Gem::Dependency
122
+ name: activesupport
123
+ requirement: &id008 !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ segments:
129
+ - 3
130
+ - 0
131
+ - 0
132
+ version: 3.0.0
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: *id008
136
+ description: Signs and verifies a set of parameters
137
+ email: steve@seven.net.nz
138
+ executables: []
139
+
140
+ extensions: []
141
+
142
+ extra_rdoc_files: []
143
+
144
+ files:
145
+ - .document
146
+ - Gemfile
147
+ - Gemfile.lock
148
+ - Rakefile
149
+ - VERSION
150
+ - lib/sigil.rb
151
+ - lib/sigil/base.rb
152
+ - sigil.gemspec
153
+ - spec/sigil/base_spec.rb
154
+ - spec/spec_helper.rb
155
+ has_rdoc: true
156
+ homepage: http://github.com/steveh/sigil
157
+ licenses:
158
+ - MIT
159
+ post_install_message:
160
+ rdoc_options: []
161
+
162
+ require_paths:
163
+ - lib
164
+ required_ruby_version: !ruby/object:Gem::Requirement
165
+ none: false
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ hash: -1382831699325218252
170
+ segments:
171
+ - 0
172
+ version: "0"
173
+ required_rubygems_version: !ruby/object:Gem::Requirement
174
+ none: false
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ segments:
179
+ - 0
180
+ version: "0"
181
+ requirements: []
182
+
183
+ rubyforge_project:
184
+ rubygems_version: 1.3.7
185
+ signing_key:
186
+ specification_version: 3
187
+ summary: Signs and verifies a set of parameters
188
+ test_files:
189
+ - spec/sigil/base_spec.rb
190
+ - spec/spec_helper.rb