escher-rack_middleware 0.1.3

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9f27b7d7a1f8f6e9d85d291fd15ba133a0e37063
4
+ data.tar.gz: 273d4bb3b297d602b3fbf66a09b66950f70ec325
5
+ SHA512:
6
+ metadata.gz: e6a2397b7cc05b2aa628c845362641413f4511fd51ab0f6bbed42ff9af6fce9d1947cf7c53d3366079b27b87853d9e2669b24f0dab813e95b5ff13b440421515
7
+ data.tar.gz: 95c7e46c73ff4d29bb616654ce3e05ad9746e758b52ebc69f83f53f9de91767bf733493d8313974f7af46f4cee243f4fe0c6a5bdbadd94c11b723a72abc3295a
data/.gitignore ADDED
@@ -0,0 +1,22 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Emarsys-Technologies Ltd.
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,52 @@
1
+ # Escher::RackMiddleware
2
+
3
+ Rack Middleware for ease of use escher authentication for your application
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'escher-rack_middleware'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install escher-rack_middleware
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+
23
+
24
+ require 'escher/rack_middleware'
25
+ Escher::RackMiddleware.config do |c|
26
+
27
+ # the default logger use the ruby core logger with STDOUT
28
+ c.logger= some_logger_instance
29
+
30
+ # for read more about escher auth object initialization please visit escherauth.io
31
+ c.add_escher_authenticator{ Escher::Auth.new( CredentialScope, AuthOptions ) }
32
+
33
+ # this will be triggered every time a request hit your appication
34
+ c.add_credential_updater{ Escher::Keypool.new.get_key_db }
35
+
36
+ # this help you exclude path(s) if you dont want require authorization for every endpoint
37
+ c.add_exclude_path(/^\/*monitoring\//)
38
+
39
+ end
40
+
41
+ use Escher::RackMiddleware
42
+ run YourAwesomeApplication
43
+
44
+ ```
45
+
46
+ ## Contributing
47
+
48
+ 1. Fork it ( https://github.com/[my-github-username]/escher-rack_middleware/fork )
49
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
50
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
51
+ 4. Push to the branch (`git push origin my-new-feature`)
52
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -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 'escher/rack_middleware/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+
8
+ spec.name = 'escher-rack_middleware'
9
+ spec.version = Escher::RackMiddleware::VERSION
10
+ spec.authors = ['Adam Luzsi']
11
+ spec.email = ['aluzsi@emarsys.com']
12
+ spec.summary = %q{Escher authorization for rack based http servers}
13
+ spec.description = %q{Escher authorization for rack based http servers with ease in a form of middleware}
14
+ spec.homepage = 'https://github.com/emartech/escher-rack_middleware-ruby'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '>= 1.6'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'spec'
25
+
26
+ spec.add_dependency 'escher', '>= 0.3.3'
27
+
28
+ end
@@ -0,0 +1,47 @@
1
+ require 'escher'
2
+ class Escher::RackMiddleware
3
+
4
+ require 'escher/rack_middleware/version'
5
+ require 'escher/rack_middleware/logging'
6
+ require 'escher/rack_middleware/credential'
7
+ require 'escher/rack_middleware/exclude_path'
8
+ require 'escher/rack_middleware/authenticator'
9
+
10
+ extend Logging
11
+ extend Credential
12
+ extend ExcludePath
13
+ extend Authenticator
14
+
15
+ def initialize(app)
16
+ @app = app
17
+ end
18
+
19
+ def call(request_env)
20
+
21
+ unless excluded_path?(request_env['REQUEST_URI'])
22
+ return unauthorized_response unless authorized?(request_env)
23
+ end
24
+
25
+ @app.call(request_env)
26
+
27
+ end
28
+
29
+ protected
30
+
31
+ def unauthorized_response
32
+ response = Rack::Response.new
33
+ response.write 'Unauthorized'
34
+ response.status = 401
35
+ response.finish
36
+ end
37
+
38
+ def env_dump_string(request_env)
39
+ require 'yaml' unless defined?(YAML)
40
+ YAML.dump(request_env)
41
+ end
42
+
43
+ def self.config(&block)
44
+ block.call(self)
45
+ end
46
+
47
+ end
@@ -0,0 +1,16 @@
1
+ module Escher::RackMiddleware::Authenticator
2
+
3
+ require 'escher/rack_middleware/authenticator/helper'
4
+ def self.extended(klass)
5
+ klass.__send__(:include,self::Helper)
6
+ end
7
+
8
+ def add_escher_authenticator(&escher_instance_initializer)
9
+ escher_authenticators.push(escher_instance_initializer)
10
+ end
11
+
12
+ def escher_authenticators
13
+ @escher_authenticators ||= []
14
+ end
15
+
16
+ end
@@ -0,0 +1,33 @@
1
+ module Escher::RackMiddleware::Authenticator::Helper
2
+
3
+ def escher_authenticators
4
+ self.class.escher_authenticators
5
+ end
6
+
7
+ def authorized?(request_env)
8
+ escher_authenticators.any? { |instance_init| authorized_with?(instance_init.call, request_env) }
9
+ end
10
+
11
+ def authorized_with?(escher_authenticator, request_env)
12
+
13
+ request_env['escher.request.api_key_id'] = escher_authenticator.authenticate(
14
+ Rack::Request.new(request_env),credentials
15
+ )
16
+
17
+ requester_succeed_log_msg = [
18
+ request_env['escher.request.api_key_id'],
19
+ request_env['REQUEST_URI']
20
+ ].join(' => ')
21
+
22
+ logger.debug("authentication succeeded!(#{requester_succeed_log_msg})")
23
+
24
+ true
25
+ rescue Escher::EscherError => ex
26
+
27
+ logger.debug("authentication failed!(#{ex.message})")
28
+
29
+ false
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,36 @@
1
+ module Escher::RackMiddleware::Credential
2
+
3
+ require 'escher/rack_middleware/credential/helper'
4
+ def self.extended(klass)
5
+ klass.__send__(:include,self::Helper)
6
+ end
7
+
8
+ def add_credential(key,value)
9
+ raw_credentials.merge!(key => value)
10
+ end
11
+
12
+ def add_credential_updater(&block)
13
+ raise(ArgumentError,'block was not given') unless block_given?
14
+ @credential_callback = block
15
+ end
16
+
17
+ def credentials
18
+ new_credentials = credential_callback.respond_to?(:call) && credential_callback.call
19
+ if new_credentials.is_a?(Hash)
20
+ raw_credentials.merge(new_credentials)
21
+ else
22
+ raw_credentials
23
+ end
24
+ end
25
+
26
+ protected
27
+
28
+ def raw_credentials
29
+ @credentials ||= {}
30
+ end
31
+
32
+ def credential_callback
33
+ @credential_callback ||= Proc.new{{}}
34
+ end
35
+
36
+ end
@@ -0,0 +1,7 @@
1
+ module Escher::RackMiddleware::Credential::Helper
2
+
3
+ def credentials
4
+ self.class.credentials
5
+ end
6
+
7
+ end
@@ -0,0 +1,18 @@
1
+ module Escher::RackMiddleware::ExcludePath
2
+
3
+ require 'escher/rack_middleware/exclude_paths/helper'
4
+ def self.extended(klass)
5
+ klass.__send__(:include,self::Helper)
6
+ end
7
+
8
+ def add_exclude_paths(*paths)
9
+ excluded_paths.push(*paths)
10
+ end
11
+
12
+ alias add_exclude_path add_exclude_paths
13
+
14
+ def excluded_paths
15
+ @excluded_paths ||= []
16
+ end
17
+
18
+ end
@@ -0,0 +1,17 @@
1
+ module Escher::RackMiddleware::ExcludePath::Helper
2
+
3
+ def excluded_paths
4
+ @excluded_paths ||= self.class.excluded_paths.dup
5
+ end
6
+
7
+ def excluded_path?(path)
8
+ excluded_paths.any? do |matcher|
9
+ if matcher.is_a?(Regexp)
10
+ !!(path =~ matcher)
11
+ else
12
+ path == matcher.to_s
13
+ end
14
+ end
15
+ end
16
+
17
+ end
@@ -0,0 +1,20 @@
1
+ module Escher::RackMiddleware::Logging
2
+
3
+ require 'escher/rack_middleware/logging/helper'
4
+
5
+ def self.extended(klass)
6
+ klass.__send__(:include,Helper)
7
+ end
8
+
9
+ def logger=(logger)
10
+ @logger=logger
11
+ end
12
+
13
+ def logger
14
+ @logger ||= -> {
15
+ require 'logger'
16
+ Logger.new(STDOUT)
17
+ }.call
18
+ end
19
+
20
+ end
@@ -0,0 +1,7 @@
1
+ module Escher::RackMiddleware::Logging::Helper
2
+
3
+ def logger
4
+ self.class.logger
5
+ end
6
+
7
+ end
@@ -0,0 +1,2 @@
1
+ require 'escher/rack_middleware'
2
+ Escher::RackMiddleware::VERSION = '0.1.3'
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: escher-rack_middleware
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Adam Luzsi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-09 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
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: spec
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: escher
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 0.3.3
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 0.3.3
69
+ description: Escher authorization for rack based http servers with ease in a form
70
+ of middleware
71
+ email:
72
+ - aluzsi@emarsys.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - escher-rack_middleware.gemspec
83
+ - lib/escher/rack_middleware.rb
84
+ - lib/escher/rack_middleware/authenticator.rb
85
+ - lib/escher/rack_middleware/authenticator/helper.rb
86
+ - lib/escher/rack_middleware/credential.rb
87
+ - lib/escher/rack_middleware/credential/helper.rb
88
+ - lib/escher/rack_middleware/exclude_path.rb
89
+ - lib/escher/rack_middleware/exclude_paths/helper.rb
90
+ - lib/escher/rack_middleware/logging.rb
91
+ - lib/escher/rack_middleware/logging/helper.rb
92
+ - lib/escher/rack_middleware/version.rb
93
+ homepage: https://github.com/emartech/escher-rack_middleware-ruby
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.2.2
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Escher authorization for rack based http servers
117
+ test_files: []