garage-strategy-cor 0.1.0.beta1

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: 103f41e912e68eb9c0098ec059c8a50334324ef5
4
+ data.tar.gz: 8d73e0a40d09b28c2aaebe07c4a1bf8b0a53910d
5
+ SHA512:
6
+ metadata.gz: 42cda21a88692014b37dfe567179cd09c8ccb7c9254cdd327c2437850898c81c26b1cec3a94d4ce89b68f737a7f6441d4f770ee1802acc2904c92196ace42dfb
7
+ data.tar.gz: 8bfee8b8d43abb4b1e198fe3bbab3ca0634d1d064ee87384f26790f9a22799b05081fc7c9b87d74698df08e04ca9ed3c48404d21257224997ac41884ddc94ecd
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in garage-strategy-cor.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 shinya-watanabe
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all 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,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ # Garage::Strategy::Cor
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/garage/strategy/cor`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'garage-strategy-cor'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install garage-strategy-cor
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/garage-strategy-cor.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "garage/strategy/cor"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'garage/strategy/cor/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "garage-strategy-cor"
8
+ spec.version = Garage::Strategy::Cor::VERSION
9
+ spec.authors = ["wata"]
10
+ spec.email = ["wata-gh"]
11
+
12
+ spec.summary = %q{Garage strategy Chain of Responsibility.}
13
+ spec.description = %q{Garage strategy Chain of Responsibility pattern.}
14
+ spec.homepage = "https://github.com/wata-gh/garage-strategy-cor"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "the_garage"
25
+ spec.add_development_dependency "bundler", "~> 1.14"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ end
@@ -0,0 +1,15 @@
1
+ module Garage
2
+ module Strategy
3
+ class ChainOfResponsibility
4
+ class << self
5
+ def configure
6
+ yield(configuration)
7
+ end
8
+
9
+ def configuration
10
+ @configuration ||= Cor::Configuration.new
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,47 @@
1
+ require 'garage/strategy/cor/version'
2
+ require 'garage/strategy/chain_of_responsibility'
3
+ require 'garage/strategy/cor/base'
4
+ require 'garage/strategy/cor/basic_auth'
5
+ require 'garage/strategy/cor/configuration'
6
+ require 'garage/strategy/cor/params'
7
+
8
+ module Garage
9
+ module Strategy
10
+ module Cor
11
+ extend ActiveSupport::Concern
12
+
13
+ included do
14
+ before_action :verify_auth, if: -> (_) { verify_permission? }
15
+ end
16
+
17
+ def verify_permission?
18
+ true
19
+ end
20
+
21
+ def access_token
22
+ return @access_token if defined?(@access_token)
23
+
24
+ strategy = build_strategy
25
+ loop do
26
+ @access_token = strategy.access_token
27
+ return @access_token if @access_token
28
+
29
+ strategy = strategy.successor
30
+ break unless strategy
31
+ end
32
+ end
33
+
34
+ def build_strategy
35
+ return @strategies.first if defined?(@strategies)
36
+
37
+ strategy_classes = Garage::Strategy::ChainOfResponsibility.configuration.strategies
38
+ @strategies = [].tap do |strategies|
39
+ strategy_classes.reverse.inject(nil) do |successor, strategy|
40
+ strategy.new(self, successor).tap { |strategy| strategies.unshift(strategy) }
41
+ end
42
+ end
43
+ @strategies.first
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,29 @@
1
+ module Garage
2
+ module Strategy
3
+ module Cor
4
+ class Base
5
+ attr_reader :controller, :successor
6
+
7
+ def initialize(controller, successor)
8
+ @controller = controller
9
+ @successor = successor
10
+ end
11
+
12
+ private
13
+
14
+ def request
15
+ @controller.request
16
+ end
17
+
18
+ def params
19
+ @controller.params
20
+ end
21
+
22
+ def from_header(name)
23
+ canonical = name.to_s.dasherize.split('-').map(&:capitalize).join('-')
24
+ request.headers[canonical]
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,54 @@
1
+ module Garage
2
+ module Strategy
3
+ module Cor
4
+ class BasicAuth < Base
5
+ def access_token
6
+ if defined?(@access_token)
7
+ @access_token
8
+ elsif target?
9
+ token = Garage::Strategy::AccessToken.new(attributes.merge(token: requested_token, token_type: :basic_auth))
10
+ @access_token = token.accessible? ? token : nil
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def target?
17
+ value = request.authorization
18
+ value.present? && value.starts_with?('Basic ')
19
+ end
20
+
21
+ def attribute_names
22
+ %i(application_id expired_at resource_owner_id scope)
23
+ end
24
+
25
+ def attributes
26
+ Hash[attribute_names.map {|name| [name, from_header(name)] }]
27
+ end
28
+
29
+ def requested_token
30
+ value = request.authorization
31
+ value.gsub(/^Basic\s(.*)/) { $1 }
32
+ end
33
+
34
+ def self.configure
35
+ yield(config)
36
+ end
37
+
38
+ def self.config
39
+ @config ||= Configuration.new
40
+ end
41
+
42
+ class Configuration
43
+ def initialize
44
+ @users = {}
45
+ end
46
+
47
+ def register(user, password)
48
+ @users[user] = password
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,17 @@
1
+ module Garage
2
+ module Strategy
3
+ module Cor
4
+ class Configuration
5
+ attr_reader :strategies
6
+
7
+ def initialize
8
+ @strategies = []
9
+ end
10
+
11
+ def add_strategy(strategy)
12
+ @strategies << strategy
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,70 @@
1
+ module Garage
2
+ module Strategy
3
+ module Cor
4
+ class Params < Base
5
+ def access_token
6
+ if defined?(@access_token)
7
+ @access_token
8
+ elsif target? && valid?
9
+ token = Garage::Strategy::AccessToken.new(attributes.merge(token: params[:identifier], token_type: "params_#{self.class.config.parameter_name}".to_sym))
10
+ @access_token = token.accessible? ? token : nil
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def target?
17
+ params.key?(parameter_name)
18
+ end
19
+
20
+ def parameter_name
21
+ self.class.config.parameter_name
22
+ end
23
+
24
+ def valid?
25
+ validator = self.class.config.validator
26
+ return true unless validator
27
+
28
+ validator.valid_params?(self)
29
+ end
30
+
31
+ def attribute_names
32
+ %i(application_id expired_at resource_owner_id scope)
33
+ end
34
+
35
+ def attributes
36
+ Hash[attribute_names.map {|name| [name, from_header(name)] }]
37
+ end
38
+
39
+ def self.configure
40
+ yield(config)
41
+ end
42
+
43
+ def self.config
44
+ @config ||= Configuration.new
45
+ end
46
+
47
+ class Configuration
48
+ def parameter_name=(parameter_name)
49
+ @parameter_name = parameter_name
50
+ end
51
+
52
+ def parameter_name
53
+ unless defined?(@parameter_name)
54
+ raise "Garage::Strategy::Cor::Params::Configuraion.paramter_name must be configured."
55
+ end
56
+ @parameter_name
57
+ end
58
+
59
+ def validator=(validator)
60
+ @validator = validator
61
+ end
62
+
63
+ def validator
64
+ @validator
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,7 @@
1
+ module Garage
2
+ module Strategy
3
+ module Cor
4
+ VERSION = "0.1.0.beta1"
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: garage-strategy-cor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.beta1
5
+ platform: ruby
6
+ authors:
7
+ - wata
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-12-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: the_garage
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Garage strategy Chain of Responsibility pattern.
56
+ email:
57
+ - wata-gh
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - bin/console
68
+ - bin/setup
69
+ - garage-strategy-cor.gemspec
70
+ - lib/garage/strategy/chain_of_responsibility.rb
71
+ - lib/garage/strategy/cor.rb
72
+ - lib/garage/strategy/cor/base.rb
73
+ - lib/garage/strategy/cor/basic_auth.rb
74
+ - lib/garage/strategy/cor/configuration.rb
75
+ - lib/garage/strategy/cor/params.rb
76
+ - lib/garage/strategy/cor/version.rb
77
+ homepage: https://github.com/wata-gh/garage-strategy-cor
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">"
93
+ - !ruby/object:Gem::Version
94
+ version: 1.3.1
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.6.13
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Garage strategy Chain of Responsibility.
101
+ test_files: []