api_key_tools 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 02d4ff655e6924e64423e1a56853387f19274093
4
+ data.tar.gz: 274e201db90b330ecd26139ac50bcf1bd7e5ca1a
5
+ SHA512:
6
+ metadata.gz: 4db8d5b10d3e3192c0ba5a202ed3b2e9e9a7175b6516f76dfe1821686390ff40f141447aed6d48466f0dfbdc6db049e9d8e0f1e61ea1ad2b8c1e5fd36ca60d39
7
+ data.tar.gz: b69e7436ae84c8781b1922a18b8e2eb690de1302bbf496fabf1f33bcc292e591fecc5de6848b1dd5a0b0fd8e67a06bd65e866c54dfaa2f7dc443c0de9213c401
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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in api_key_tools.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,11 @@
1
+ guard 'rspec', cmd: 'bundle exec rspec' do
2
+ # watch /lib/ files
3
+ watch(%r{^lib/(.+).rb$}) do |m|
4
+ "spec/#{m[1]}_spec.rb"
5
+ end
6
+
7
+ # watch /spec/ files
8
+ watch(%r{^spec/(.+).rb$}) do |m|
9
+ "spec/#{m[1]}.rb"
10
+ end
11
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Samuel O. Obukwelu
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,38 @@
1
+ # ApiKeyTools
2
+
3
+ A small set of classes that I use to encode and validate data sent between servers
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'api_key_tools'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install api_key_tools
18
+
19
+ ## Usage
20
+
21
+ To encode:
22
+
23
+ key_encoder = ApiKeyTools::Encode.new(user_id, secret, time_as_an_integer)
24
+ open_secret = key_encoder.to_s # this is what you want exposed not the secret
25
+
26
+ To validate:
27
+
28
+ validator = ApiKeyTools::Validator.new(key_encoder, open_secret)
29
+ validator.valid?
30
+
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it ( http://github.com/fcgmedia/api_key_tools/fork )
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'api_key_tools/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "api_key_tools"
8
+ spec.version = ApiKeyTools::VERSION
9
+ spec.authors = ["Samuel O. Obukwelu"]
10
+ spec.email = ["sam@fcgmedia.com"]
11
+ spec.summary = %q{A small set of tools to encode and validate data sent to api }
12
+ spec.description = %q{A tool to protect api data}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "guard"
25
+ spec.add_development_dependency "guard-rspec"
26
+ spec.add_development_dependency "pry"
27
+ spec.add_development_dependency "timecop"
28
+ end
@@ -0,0 +1,3 @@
1
+ module ApiKeyTools
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,41 @@
1
+ require "api_key_tools/version"
2
+ require 'digest/sha1'
3
+
4
+ module ApiKeyTools
5
+ DELIMITER = "-"
6
+
7
+ class Encoder
8
+ attr_reader :time
9
+ def initialize(id, user_key, time)
10
+ @id = id
11
+ @user_key = user_key
12
+ @time = time
13
+ end
14
+
15
+ def to_s
16
+ Digest::SHA1.hexdigest([@id, @user_key, @time].join(DELIMITER))
17
+ end
18
+
19
+ def as_json
20
+ { id: @id, key: to_s, time: @time}
21
+ end
22
+ end
23
+
24
+ class Validator
25
+ TIME_THRESHOLD = 60 * 5 # 5 minutes or 300 seconds
26
+ def initialize(encoder, value)
27
+ @encoder = encoder
28
+ @value = value
29
+ end
30
+
31
+ def valid?
32
+ within_time_limit? && @encoder.to_s == @value
33
+ end
34
+
35
+ private
36
+ def within_time_limit?
37
+ time_now_as_int = Time.now.to_i
38
+ @encoder.time >= time_now_as_int && @encoder.time <= time_now_as_int + TIME_THRESHOLD
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe ApiKeyTools::Encoder do
4
+ describe "#encode" do
5
+ let(:id){ 1 }
6
+ let(:key){ "0a185854152be354388a105a727164d3" }
7
+ let(:time){ Time.now.to_i }
8
+ subject{ described_class.new(id, key, time) }
9
+ it "should create a md5 hash" do
10
+ expect(subject.to_s).to eq("51b61e2e0569a8f8929de0cbc25714f4abadcad2")
11
+ end
12
+
13
+ it "should create a valid as_json value" do
14
+ expect(subject.as_json).to eq({ id: 1, key: "51b61e2e0569a8f8929de0cbc25714f4abadcad2", time: time })
15
+ end
16
+ end
17
+ end
18
+
19
+ describe ApiKeyTools::Validator do
20
+ describe "#valid?" do
21
+ let(:id){ 1 }
22
+ let(:key){ "0a185854152be354388a105a727164d3" }
23
+ let(:time){ Time.now.to_i }
24
+ let(:encoder){ ApiKeyTools::Encoder.new(id, key, time) }
25
+ subject{ ApiKeyTools::Validator.new(encoder, value) }
26
+ context "valid value" do
27
+ let(:value){ "51b61e2e0569a8f8929de0cbc25714f4abadcad2" }
28
+ it "should be true" do
29
+ expect(subject.valid?).to eq(true)
30
+ end
31
+ end
32
+
33
+ context "valid value but pass the time limit" do
34
+ let(:value){ "51b61e2e0569a8f8929de0cbc25714f4abadcad2" }
35
+ it "should be false" do
36
+ subject
37
+ Timecop.travel(Time.now + 10_000)
38
+ expect(Time.at(time)).to_not eq(Time.now)
39
+ expect(subject.valid?).to eq(false)
40
+ end
41
+ end
42
+
43
+ context "invalid value" do
44
+ let(:value){ "BAD_VALUE" }
45
+ it "should be false" do
46
+ expect(subject.valid?).to eq(false)
47
+ end
48
+ end
49
+ end
50
+ end
51
+
@@ -0,0 +1,8 @@
1
+ require 'pry'
2
+ require 'api_key_tools'
3
+ Dir[
4
+ './spec/support/*.rb'
5
+ ].each do |f|
6
+ require f
7
+ end
8
+
@@ -0,0 +1,11 @@
1
+ require 'timecop'
2
+ RSpec.configure do |config|
3
+ config.before(:each) do
4
+ Timecop.freeze(Time.local(2010, 1, 1))
5
+ end
6
+
7
+ config.after(:each) do
8
+ Timecop.return
9
+ end
10
+ end
11
+
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: api_key_tools
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Samuel O. Obukwelu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: timecop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: A tool to protect api data
112
+ email:
113
+ - sam@fcgmedia.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - .rspec
120
+ - Gemfile
121
+ - Guardfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - api_key_tools.gemspec
126
+ - lib/api_key_tools.rb
127
+ - lib/api_key_tools/version.rb
128
+ - spec/api_key_tools_spec.rb
129
+ - spec/spec_helper.rb
130
+ - spec/support/timecop.rb
131
+ homepage: ''
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - '>='
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.2.2
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: A small set of tools to encode and validate data sent to api
155
+ test_files:
156
+ - spec/api_key_tools_spec.rb
157
+ - spec/spec_helper.rb
158
+ - spec/support/timecop.rb
159
+ has_rdoc: