oauth-bwergemn 1.0.1
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 +7 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/.rubocop.yml +1149 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +100 -0
- data/LICENSE.txt +21 -0
- data/README.md +114 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/generators/oauth_bwergemn/install_generator.rb +11 -0
- data/lib/generators/templates/initializer.rb +5 -0
- data/lib/oauth-bwergemn.rb +28 -0
- data/lib/oauth_bwergemn/auth_methods/auth_methods.rb +50 -0
- data/lib/oauth_bwergemn/auth_strategies/hub.rb +45 -0
- data/lib/oauth_bwergemn/base_strategy.rb +7 -0
- data/lib/oauth_bwergemn/configuration.rb +29 -0
- data/lib/oauth_bwergemn/errors/invalid_scope.rb +11 -0
- data/lib/oauth_bwergemn/errors/invalid_token.rb +11 -0
- data/lib/oauth_bwergemn/extension.rb +28 -0
- data/lib/oauth_bwergemn/helpers.rb +6 -0
- data/lib/oauth_bwergemn/oauth2.rb +125 -0
- data/lib/oauth_bwergemn/version.rb +6 -0
- data/oauth-bwergemn.gemspec +42 -0
- metadata +155 -0
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OauthBwergemn
|
4
|
+
module AuthStrategies
|
5
|
+
class Hub < OauthBwergemn::BaseStrategy
|
6
|
+
def optional_endpoint?
|
7
|
+
has_authorizations? && !!optional_oauth2
|
8
|
+
end
|
9
|
+
|
10
|
+
def endpoint_protected?
|
11
|
+
has_authorizations? && !!authorization_type_oauth2
|
12
|
+
end
|
13
|
+
|
14
|
+
def has_auth_scopes?
|
15
|
+
endpoint_protected? && !authorization_type_oauth2.empty?
|
16
|
+
end
|
17
|
+
|
18
|
+
def auth_scopes
|
19
|
+
if optional_endpoint?
|
20
|
+
optional_oauth2.map { |s| s.is_a?(String) || s.is_a?(Symbol) ? s.to_sym : s }
|
21
|
+
else
|
22
|
+
authorization_type_oauth2.map { |s| s.is_a?(String) || s.is_a?(Symbol) ? s.to_sym : s }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def has_authorizations?
|
29
|
+
!!endpoint_authorizations
|
30
|
+
end
|
31
|
+
|
32
|
+
def endpoint_authorizations
|
33
|
+
api_context.options[:route_options][:auth][:scopes]
|
34
|
+
end
|
35
|
+
|
36
|
+
def authorization_type_oauth2
|
37
|
+
endpoint_authorizations
|
38
|
+
end
|
39
|
+
|
40
|
+
def optional_oauth2
|
41
|
+
endpoint_authorizations
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OauthBwergemn
|
4
|
+
module Configuration
|
5
|
+
def setup
|
6
|
+
yield self
|
7
|
+
end
|
8
|
+
|
9
|
+
def define_setting(name, default = nil)
|
10
|
+
class_variable_set("@@#{name}", default)
|
11
|
+
|
12
|
+
define_class_method "#{name}=" do |value|
|
13
|
+
class_variable_set("@@#{name}", value)
|
14
|
+
end
|
15
|
+
|
16
|
+
define_class_method name do
|
17
|
+
class_variable_get("@@#{name}")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def define_class_method(name, &block)
|
24
|
+
(class << self; self; end).instance_eval do
|
25
|
+
define_method name, &block
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module OauthBwergemn
|
4
|
+
module Extension
|
5
|
+
def oauth2(*scopes)
|
6
|
+
description = if respond_to?(:route_setting) # >= grape-0.10.0
|
7
|
+
route_setting(:description) || route_setting(:description, {})
|
8
|
+
else
|
9
|
+
@last_description ||= {}
|
10
|
+
end
|
11
|
+
|
12
|
+
description[:auth] = { scopes: scopes }
|
13
|
+
description[:authorizations] = { oauth2: scopes.map { |x| { scope: x } } }
|
14
|
+
end
|
15
|
+
|
16
|
+
def optional_oauth2(*scopes)
|
17
|
+
description = if respond_to?(:route_setting) # >= grape-0.10.0
|
18
|
+
route_setting(:description) || route_setting(:description, {})
|
19
|
+
else
|
20
|
+
@last_description ||= {}
|
21
|
+
end
|
22
|
+
|
23
|
+
description[:authorizations] = { optional_oauth2: scopes.map { |x| { scope: x } } }
|
24
|
+
end
|
25
|
+
|
26
|
+
Grape::API.extend self
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rack/auth/abstract/request'
|
4
|
+
|
5
|
+
module OauthBwergemn
|
6
|
+
class Oauth2 < Grape::Middleware::Base
|
7
|
+
attr_reader :auth_strategy
|
8
|
+
|
9
|
+
def context
|
10
|
+
env['api.endpoint']
|
11
|
+
end
|
12
|
+
|
13
|
+
def the_request=(env)
|
14
|
+
@_the_request = ActionDispatch::Request.new(env)
|
15
|
+
end
|
16
|
+
|
17
|
+
def request
|
18
|
+
@_the_request
|
19
|
+
end
|
20
|
+
|
21
|
+
def token
|
22
|
+
token = if request.headers['Authorization'].present?
|
23
|
+
if request.headers['Authorization'].include?('bearer')
|
24
|
+
request.headers['Authorization'].try('split', 'bearer').try(:last).try(:strip)
|
25
|
+
elsif request.headers['Authorization'].include?('Bearer')
|
26
|
+
request.headers['Authorization'].try('split', 'Bearer').try(:last).try(:strip)
|
27
|
+
else
|
28
|
+
request.headers['Authorization']
|
29
|
+
end
|
30
|
+
else
|
31
|
+
request.parameters['access_token']
|
32
|
+
end
|
33
|
+
token
|
34
|
+
end
|
35
|
+
|
36
|
+
############
|
37
|
+
# Authorization control.
|
38
|
+
############
|
39
|
+
|
40
|
+
def endpoint_protected?
|
41
|
+
auth_strategy.endpoint_protected?
|
42
|
+
end
|
43
|
+
|
44
|
+
def optional_endpoint?
|
45
|
+
auth_strategy.optional_endpoint?
|
46
|
+
end
|
47
|
+
|
48
|
+
def args
|
49
|
+
results = {}
|
50
|
+
auth_strategy.auth_scopes.map { |s| (results = results.merge(s)) if s.is_a?(Hash) }
|
51
|
+
results
|
52
|
+
end
|
53
|
+
|
54
|
+
def scopes
|
55
|
+
results = []
|
56
|
+
auth_strategy.auth_scopes.map { |s| (results << s) unless s.is_a?(Hash) }
|
57
|
+
results
|
58
|
+
end
|
59
|
+
|
60
|
+
def authorize!
|
61
|
+
access = Doorkeeper::AccessToken.find_by(token: token)
|
62
|
+
unless access.present?
|
63
|
+
raise OauthBwergemn::Errors::InvalidToken
|
64
|
+
end
|
65
|
+
resource = begin
|
66
|
+
# rubocop:disable Security/Eval
|
67
|
+
eval(OauthBwergemn.resources[args[:as].to_sym]).find_by(id: access.resource_owner_id)
|
68
|
+
# rubocop:enable Security/Eval
|
69
|
+
rescue
|
70
|
+
nil
|
71
|
+
end
|
72
|
+
{
|
73
|
+
resource_owner: resource,
|
74
|
+
resource_credential: {
|
75
|
+
access_token: access.token,
|
76
|
+
scopes: scopes,
|
77
|
+
token_type: 'bearer',
|
78
|
+
expires_in: access.expires_in,
|
79
|
+
refresh_token: access.refresh_token,
|
80
|
+
created_at: access.created_at.to_i
|
81
|
+
}
|
82
|
+
}
|
83
|
+
end
|
84
|
+
|
85
|
+
############
|
86
|
+
# Grape middleware methods
|
87
|
+
############
|
88
|
+
|
89
|
+
def before
|
90
|
+
set_auth_strategy(OauthBwergemn.auth_strategy)
|
91
|
+
auth_strategy.api_context = context
|
92
|
+
context.extend(OauthBwergemn::AuthMethods)
|
93
|
+
|
94
|
+
context.protected_endpoint = endpoint_protected?
|
95
|
+
context.optional_endpoint = optional_endpoint?
|
96
|
+
|
97
|
+
return unless context.protected_endpoint? || context.optional_endpoint?
|
98
|
+
|
99
|
+
self.the_request = env
|
100
|
+
|
101
|
+
if token.present? && (context.protected_endpoint? || context.optional_endpoint?)
|
102
|
+
response = authorize!
|
103
|
+
context.resource_token = token
|
104
|
+
context.resource_owner = begin
|
105
|
+
response[:resource_owner]
|
106
|
+
rescue
|
107
|
+
nil
|
108
|
+
end
|
109
|
+
context.resource_credentials = begin
|
110
|
+
response[:resource_credentials]
|
111
|
+
rescue
|
112
|
+
nil
|
113
|
+
end
|
114
|
+
elsif token.nil? && context.protected_endpoint?
|
115
|
+
raise OauthBwergemn::Errors::InvalidToken
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
private
|
120
|
+
|
121
|
+
def set_auth_strategy(strategy)
|
122
|
+
@auth_strategy = OauthBwergemn::AuthStrategies.const_get(strategy.to_s.capitalize.to_s).new
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'oauth_bwergemn/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'oauth-bwergemn'
|
9
|
+
spec.version = OauthBwergemn::VERSION
|
10
|
+
spec.authors = ['Alam Ybs']
|
11
|
+
spec.email = ['namakukingkong@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = 'Oauth Bwergemn is a Grape middleware for your API authenticator'
|
14
|
+
spec.description = 'Oauth Bwergemn is a Grape middleware to connect your API resources with your API authenticator.'
|
15
|
+
spec.homepage = 'https://github.com/namakukingkong/oauth-bwergemn'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
19
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
20
|
+
if spec.respond_to?(:metadata)
|
21
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org/'
|
22
|
+
else
|
23
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
24
|
+
'public gem pushes.'
|
25
|
+
end
|
26
|
+
|
27
|
+
# Specify which files should be added to the gem when it is released.
|
28
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
29
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
30
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
31
|
+
end
|
32
|
+
spec.bindir = 'exe'
|
33
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
34
|
+
spec.require_paths = ['lib']
|
35
|
+
|
36
|
+
spec.add_development_dependency 'bundler', '~> 2.1.0'
|
37
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
38
|
+
spec.add_development_dependency 'rspec', '~> 3.7.0'
|
39
|
+
spec.add_development_dependency 'rubocop', '~> 0.79.0'
|
40
|
+
spec.add_development_dependency 'rubocop-performance', '~> 1.5.2'
|
41
|
+
spec.add_dependency 'grape', '~> 1.1.0'
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: oauth-bwergemn
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alam Ybs
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-01-10 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: 2.1.0
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.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: 3.7.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.7.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.79.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.79.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop-performance
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 1.5.2
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.5.2
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: grape
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 1.1.0
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.1.0
|
97
|
+
description: Oauth Bwergemn is a Grape middleware to connect your API resources with
|
98
|
+
your API authenticator.
|
99
|
+
email:
|
100
|
+
- namakukingkong@gmail.com
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".gitignore"
|
106
|
+
- ".rspec"
|
107
|
+
- ".rubocop.yml"
|
108
|
+
- ".travis.yml"
|
109
|
+
- CODE_OF_CONDUCT.md
|
110
|
+
- Gemfile
|
111
|
+
- Gemfile.lock
|
112
|
+
- LICENSE.txt
|
113
|
+
- README.md
|
114
|
+
- Rakefile
|
115
|
+
- bin/console
|
116
|
+
- bin/setup
|
117
|
+
- lib/generators/oauth_bwergemn/install_generator.rb
|
118
|
+
- lib/generators/templates/initializer.rb
|
119
|
+
- lib/oauth-bwergemn.rb
|
120
|
+
- lib/oauth_bwergemn/auth_methods/auth_methods.rb
|
121
|
+
- lib/oauth_bwergemn/auth_strategies/hub.rb
|
122
|
+
- lib/oauth_bwergemn/base_strategy.rb
|
123
|
+
- lib/oauth_bwergemn/configuration.rb
|
124
|
+
- lib/oauth_bwergemn/errors/invalid_scope.rb
|
125
|
+
- lib/oauth_bwergemn/errors/invalid_token.rb
|
126
|
+
- lib/oauth_bwergemn/extension.rb
|
127
|
+
- lib/oauth_bwergemn/helpers.rb
|
128
|
+
- lib/oauth_bwergemn/oauth2.rb
|
129
|
+
- lib/oauth_bwergemn/version.rb
|
130
|
+
- oauth-bwergemn.gemspec
|
131
|
+
homepage: https://github.com/namakukingkong/oauth-bwergemn
|
132
|
+
licenses:
|
133
|
+
- MIT
|
134
|
+
metadata:
|
135
|
+
allowed_push_host: https://rubygems.org/
|
136
|
+
post_install_message:
|
137
|
+
rdoc_options: []
|
138
|
+
require_paths:
|
139
|
+
- lib
|
140
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - ">="
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
145
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
146
|
+
requirements:
|
147
|
+
- - ">="
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: '0'
|
150
|
+
requirements: []
|
151
|
+
rubygems_version: 3.1.1
|
152
|
+
signing_key:
|
153
|
+
specification_version: 4
|
154
|
+
summary: Oauth Bwergemn is a Grape middleware for your API authenticator
|
155
|
+
test_files: []
|