s2s-auth 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 30476f1d68de002751a1e5002c3f75f9fe4997e2
4
+ data.tar.gz: cae731a5d7b0c9db173f5d2d72e4df6c1d6d8ae7
5
+ SHA512:
6
+ metadata.gz: c045d152483d4f1769250940ee61e8f2a98caaee76a0b8017091005287e6b65cab415472df79f11d84571e5dff066233a9a92676d740729c035e92b6b93e772e
7
+ data.tar.gz: 33bc93af12c8405feb4f37c328421a4ff22469c1a24dbe8638aeb50554424eb569b0d51cc21a4aba6878590d822ee5904bcc18b62f002f56c09d0a41befb3b40
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in s2s-auth.gemspec
4
+ gemspec
@@ -0,0 +1,42 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ s2s-auth (0.0.1)
5
+ activesupport (>= 3.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (4.1.0.beta1)
11
+ i18n (~> 0.6, >= 0.6.9)
12
+ json (~> 1.7, >= 1.7.7)
13
+ minitest (~> 5.1)
14
+ thread_safe (~> 0.1)
15
+ tzinfo (~> 1.1)
16
+ atomic (1.1.14)
17
+ diff-lcs (1.2.5)
18
+ i18n (0.6.9)
19
+ json (1.7.7)
20
+ minitest (5.2.2)
21
+ rake (0.9.6)
22
+ rspec (2.14.1)
23
+ rspec-core (~> 2.14.0)
24
+ rspec-expectations (~> 2.14.0)
25
+ rspec-mocks (~> 2.14.0)
26
+ rspec-core (2.14.7)
27
+ rspec-expectations (2.14.5)
28
+ diff-lcs (>= 1.1.3, < 2.0)
29
+ rspec-mocks (2.14.5)
30
+ thread_safe (0.1.3)
31
+ atomic
32
+ tzinfo (1.1.0)
33
+ thread_safe (~> 0.1)
34
+
35
+ PLATFORMS
36
+ ruby
37
+
38
+ DEPENDENCIES
39
+ bundler (~> 1.5)
40
+ rake
41
+ rspec
42
+ s2s-auth!
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Matt Aimonetti
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Matt Aimonetti
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.
@@ -0,0 +1,50 @@
1
+ # S2S::Auth
2
+
3
+ This gem creates a S2S authentication header to make S2S API requests.
4
+ S2S header format: Bearer <token>
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 's2s-auth'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install s2s-auth
19
+
20
+ ## Usage
21
+
22
+ You need to setup the gem by setting four values:
23
+
24
+ * app name
25
+ * secret
26
+ * encryption salt
27
+ * signature salt
28
+
29
+ ```ruby
30
+ require "s2s/auth"
31
+
32
+ # setup
33
+ S2S::Auth.setup({secret: "this is my secret",
34
+ app: "test",
35
+ salt: "f7b5763636f4c1f3ff4bd444eacccca2",
36
+ sign_salt: "95d87b990cc104124017ad70550edcfd22b8e89465338254e0b608592a9aac29"
37
+ })
38
+
39
+ # generate http authorization header
40
+ S2S::Auth.header
41
+ # => {:Authorization=>"Bearer bUcweFFKcUpCcWxPQTczcjZTMm1yYkxBL3RDUFk1L2xKVVY4VjU2R1EwbkExTE00eUI0RkxmNzFwbWM4WS8waS0tQ1hiUWVMZ0FwVVZCOGVqQVc5cFJGQT09--afa1f7353e789cc8fc1a332b0c355fb07a7efb03"}
42
+
43
+ # generate just the token
44
+ S2S::Auth.generate_token
45
+ # => "TWRsNE16ZzR3dG9qVjcwKzlEc1B4R0h4UGwyTHcyVTlRZ0szV0EybE1jV3R6VjF1WHpPSnNDcjRaRVdIeGFlYS0tT3FqQktYbmU2cVpvVzdTZzM3ditMdz09--09144f0202ef708622ac8d778cc062f5d62c22a3"
46
+
47
+ # parse the token
48
+ S2S::Auth.parse_token(S2S::Auth.generate_token)
49
+ # => {"app"=>"test", "ts"=>"2014-02-11T06:33:39Z"}
50
+ ```
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,93 @@
1
+ require "s2s/auth/version"
2
+ require "time"
3
+ require "active_support/key_generator"
4
+ require "active_support/message_verifier"
5
+ require "active_support/message_encryptor"
6
+ require "active_support/key_generator"
7
+ require "json"
8
+
9
+ module S2S
10
+ module Auth
11
+ module_function
12
+
13
+ @secret = nil
14
+ @salt = nil
15
+ @sign_salt = nil
16
+ @app_name = nil
17
+ @encryptor = nil
18
+
19
+ # Setups the S2S::Auth module.
20
+ #
21
+ # @arg [Hash] opts
22
+ # @raise [ArgumentError] if the passed option doesn't contain a
23
+ # secret.
24
+ # @return [Bool] true
25
+ def setup(opts={})
26
+ clear
27
+ @secret = opts[:secret] || opts["secret"]
28
+ @app_name = opts[:app] || opts["app"]
29
+ @salt = opts[:salt] || opts["salt"]
30
+ @sign_salt = opts[:sign_salt] || opts["sign_salt"]
31
+ if [@secret, @app_name, @salt, @sign_salt].any?{|v| v.nil? || v.empty?}
32
+ raise ArgumentError.new("This module needs to be setup following keys: secret, app, salt, sign_salt")
33
+ end
34
+ @iterations = opts[:iterations] || opts["iterations"] || 1000
35
+ @serializer = opts[:serializer] || opts["serializer"] || JSON
36
+ keygen = ActiveSupport::CachingKeyGenerator.new(ActiveSupport::KeyGenerator.new(@secret, iterations: @iteration))
37
+ secret = keygen.generate_key(@salt)
38
+ sign_secret = keygen.generate_key(@sign_salt)
39
+ @encryptor = ActiveSupport::MessageEncryptor.new(secret, sign_secret, { serializer: @serializer } )
40
+ return true
41
+ end
42
+
43
+ # Clears the settings that were set during setup.
44
+ def clear
45
+ @secret = nil
46
+ @salt = nil
47
+ @sign_salt = nil
48
+ @app_name = nil
49
+ @iterations = nil
50
+ @encryptor = nil
51
+ @serializer = nil
52
+ end
53
+
54
+ # Returns the module's settings.
55
+ def settings
56
+ {
57
+ secret: @secret,
58
+ salt: @salt,
59
+ sign_salt: @sign_salt,
60
+ app_name: @app_name,
61
+ serializer: @serializer,
62
+ iterations: @iterations,
63
+ encryptor: @encryptor
64
+ }
65
+ end
66
+
67
+ # Returns a hash representing the auth header needed to be
68
+ # sent with the S2S request.
69
+ # Make sure to call #setup first.
70
+ #
71
+ # @return
72
+ def header
73
+ {Authorization: "Bearer #{generate_token}"}
74
+ end
75
+
76
+ # Generate an encypted and signed token.
77
+ # Tokens are time sensitive and usually expire in a few seconds.
78
+ def generate_token
79
+ if @app_name.nil? || @encryptor.nil?
80
+ raise ArgumentError.new("Can't generate a S2S header before setting up the class")
81
+ end
82
+ @encryptor.encrypt_and_sign({app: @app_name, ts: Time.now.utc.iso8601})
83
+ end
84
+
85
+ # Checks that a token is valid and return a hash with its content.
86
+ # Note that no logic is done to verify that the token is recent.
87
+ # @raise [ActiveSupport::MessageVerifier::InvalidSignature]
88
+ def parse_token(token)
89
+ @encryptor.decrypt_and_verify(token)
90
+ end
91
+
92
+ end
93
+ end
@@ -0,0 +1,5 @@
1
+ module S2S
2
+ module Auth
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 's2s/auth/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "s2s-auth"
8
+ spec.version = S2S::Auth::VERSION
9
+ spec.authors = ["Matt Aimonetti"]
10
+ spec.email = ["mattaimonetti@gmail.com"]
11
+ spec.summary = %q{S2S authentication lib based on ActiveSupport's crypto.}
12
+ spec.description = %q{Generates/parses encrypted and signed tokens.}
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
+
25
+ spec.add_dependency "activesupport", ">= 3.0"
26
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe S2S::Auth do
4
+ context "setup" do
5
+
6
+ it "complains if you don't pass a required keys" do
7
+ expect{ S2S::Auth.setup({})}.to raise_exception
8
+ end
9
+
10
+ it "sets values" do
11
+ S2S::Auth.setup({secret: "this is my secret",
12
+ app: "test",
13
+ salt: "f7b5763636f4c1f3ff4bd444eacccca2",
14
+ sign_salt: "95d87b990cc104124017ad70550edcfd22b8e89465338254e0b608592a9aac29"
15
+ })
16
+
17
+ expect(S2S::Auth.settings[:app_name]).to eql("test")
18
+ expect(S2S::Auth.settings[:salt]).to eql("f7b5763636f4c1f3ff4bd444eacccca2")
19
+ expect(S2S::Auth.settings[:sign_salt]).to eql("95d87b990cc104124017ad70550edcfd22b8e89465338254e0b608592a9aac29")
20
+ end
21
+
22
+ it "sets an encryptor" do
23
+ expect(S2S::Auth.settings[:encryptor]).to_not be_nil
24
+ end
25
+
26
+ end
27
+
28
+ context "generate an auth header" do
29
+ before(:all) do
30
+ S2S::Auth.setup({secret: "this is my secret",
31
+ app: "another_test",
32
+ salt: "f7b5763636f4c1f3ff4bd444eacccca2",
33
+ sign_salt: "95d87b990cc104124017ad70550edcfd22b8e89465338254e0b608592a9aac29"
34
+ })
35
+ end
36
+
37
+ it "can generate a header" do
38
+ header = S2S::Auth.header
39
+ expect(header.has_key?(:Authorization)).to be_true
40
+ expect(header[:Authorization]).to match(/Bearer\s.{162}/)
41
+ end
42
+
43
+ it "can generate a token that can be converted back" do
44
+ token = S2S::Auth.generate_token
45
+ expect(token.length).to eql(194)
46
+ crypt = S2S::Auth.settings[:encryptor]
47
+ data = crypt.decrypt_and_verify(token)
48
+ expect(data["app"]).to eql("another_test")
49
+ # check that the time matches by getting the amount of seconds
50
+ # since epoch.
51
+ expect(Time.parse(data["ts"]).to_i).to eql(Time.now.to_i)
52
+ end
53
+
54
+ it "can parse a valid token" do
55
+ token = S2S::Auth.generate_token
56
+ data = S2S::Auth.parse_token(token)
57
+ expect(data["app"]).to eql("another_test")
58
+ # check that the time matches by getting the amount of seconds
59
+ # since epoch.
60
+ expect(Time.parse(data["ts"]).to_i).to eql(Time.now.to_i)
61
+ end
62
+
63
+ it "fails to parse a bad token" do
64
+ token = "abFwNFBXSjhtRGZtZFJURzlvaG4zeTM1eTRMVVcvUmFjUFR4bWI0VjlSQUJuSWpGZWpFRjlHUnNxSWJWeENGNi0tKzgyMXRjeTU2TGJHL1pkSGlaUjBxZz09--454c07f2ae8dc744094128a6e68a02bc07dee003"
65
+ expect{ S2S::Auth.parse_token(token) }.to raise_exception(ActiveSupport::MessageVerifier::InvalidSignature)
66
+ end
67
+
68
+ end
69
+ end
@@ -0,0 +1,2 @@
1
+ require_relative "../lib/s2s/auth"
2
+ require 'rspec/autorun'
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: s2s-auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matt Aimonetti
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-11 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: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Generates/parses encrypted and signed tokens.
70
+ email:
71
+ - mattaimonetti@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - Gemfile.lock
79
+ - LICENSE
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/s2s/auth.rb
84
+ - lib/s2s/auth/version.rb
85
+ - s2s-auth.gemspec
86
+ - spec/auth_spec.rb
87
+ - spec/spec_helper.rb
88
+ homepage: ''
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.2.2
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: S2S authentication lib based on ActiveSupport's crypto.
112
+ test_files:
113
+ - spec/auth_spec.rb
114
+ - spec/spec_helper.rb