oauth2c 0.1.0

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.
@@ -0,0 +1,24 @@
1
+ # Copyright 2017 Doximity, Inc. <support@doximity.com>
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module OAuth2c
16
+ module Grants
17
+ autoload :Assertion, "oauth2c/grants/assertion"
18
+ autoload :AuthorizationCode, "oauth2c/grants/authorization_code"
19
+ autoload :ClientCredentials, "oauth2c/grants/client_credentials"
20
+ autoload :Implicit, "oauth2c/grants/implicit"
21
+ autoload :RefreshToken, "oauth2c/grants/refresh_token"
22
+ autoload :ResourceOwnerCredentials, "oauth2c/grants/resource_owner_credentials"
23
+ end
24
+ end
@@ -0,0 +1,33 @@
1
+ module OAuth2c
2
+ module Refinements
3
+ refine String do
4
+ def camelize
5
+ gsub(/(?:\A|_)([a-z])/) { $1.upcase }
6
+ end
7
+ end
8
+
9
+ refine Hash do
10
+ def slice(*keys)
11
+ keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
12
+ keys.each_with_object(self.class.new) { |k, hash| hash[k] = self[k] if has_key?(k) }
13
+ end
14
+
15
+ def symbolize_keys
16
+ transform_keys{ |key| key.to_sym rescue key }
17
+ end
18
+
19
+ def stringify_keys
20
+ transform_keys(&:to_s)
21
+ end
22
+
23
+ def transform_keys
24
+ return enum_for(:transform_keys) { size } unless block_given?
25
+ result = {}
26
+ each_key do |key|
27
+ result[yield(key)] = self[key]
28
+ end
29
+ result
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,85 @@
1
+ # Copyright 2017 Doximity, Inc. <support@doximity.com>
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module OAuth2c
16
+ module ThreeLegged
17
+ using Refinements
18
+
19
+ InvalidStateError = Class.new(StandardError)
20
+
21
+ class Base
22
+ attr_reader :scope
23
+
24
+ def initialize(agent, state:, scope: [])
25
+ @agent = agent
26
+ @state = state
27
+ update_scope(scope)
28
+ end
29
+
30
+ def update_scope(scope)
31
+ @scope = scope.dup.freeze
32
+ end
33
+
34
+ def authz_url
35
+ @agent.authz_url(state: @state, scope: @scope, **authz_params)
36
+ end
37
+
38
+ def token(callback_url)
39
+ query_params, fragment_params = parse_callback_url(callback_url)
40
+
41
+ if query_params[:error]
42
+ raise Error.new(query_params[:error], query_params[:error_description])
43
+ end
44
+
45
+ if query_params[:state] != @state
46
+ raise InvalidStateError, "callback url state mismatch"
47
+ end
48
+
49
+ if block_given?
50
+ yield(query_params, fragment_params)
51
+ else
52
+ ok, response = @agent.token(include_redirect_uri: true, **token_params(query_params))
53
+ handle_token_response(ok, response)
54
+ end
55
+ end
56
+
57
+ protected
58
+
59
+ def authz_params
60
+ raise NotImplementedError
61
+ end
62
+
63
+ def token_params
64
+ raise NotImplementedError
65
+ end
66
+
67
+ def parse_callback_url(callback_url)
68
+ uri = URI.parse(callback_url)
69
+
70
+ query_params = Hash[URI.decode_www_form(uri.query.to_s)].symbolize_keys
71
+ fragment_params = Hash[URI.decode_www_form(uri.fragment.to_s)].symbolize_keys
72
+
73
+ [query_params, fragment_params]
74
+ end
75
+
76
+ def handle_token_response(ok, response)
77
+ if ok
78
+ AccessToken.new(**response.symbolize_keys)
79
+ else
80
+ raise Error.new(response["error"], response["error_description"])
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,51 @@
1
+ # Copyright 2017 Doximity, Inc. <support@doximity.com>
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module OAuth2c
16
+ module TwoLegged
17
+ class Base
18
+ using Refinements
19
+
20
+ attr_reader :scope
21
+
22
+ def initialize(agent, scope: [])
23
+ @agent = agent
24
+ update_scope(scope)
25
+ end
26
+
27
+ def update_scope(scope)
28
+ @scope = scope.dup.freeze
29
+ end
30
+
31
+ def token
32
+ ok, response = @agent.token({ **token_params, scope: @scope })
33
+ handle_token_response(ok, response)
34
+ end
35
+
36
+ protected
37
+
38
+ def token_params
39
+ raise NotImplementedError
40
+ end
41
+
42
+ def handle_token_response(ok, response)
43
+ if ok
44
+ AccessToken.new(**response.symbolize_keys)
45
+ else
46
+ raise Error.new(response["error"], response["error_description"])
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,17 @@
1
+ # Copyright 2017 Doximity, Inc. <support@doximity.com>
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module OAuth2c
16
+ VERSION = "0.1.0"
17
+ end
data/lib/oauth2c.rb ADDED
@@ -0,0 +1,26 @@
1
+ # Copyright 2017 Doximity, Inc. <support@doximity.com>
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module OAuth2c
16
+ autoload :AccessToken, "oauth2c/access_token"
17
+ autoload :Agent, "oauth2c/agent"
18
+ autoload :Cache, "oauth2c/cache"
19
+ autoload :Client, "oauth2c/client"
20
+ autoload :Error, "oauth2c/error"
21
+ autoload :Grants, "oauth2c/grants"
22
+ autoload :Refinements, "oauth2c/refinements"
23
+ autoload :ThreeLegged, "oauth2c/three_legged"
24
+ autoload :TwoLegged, "oauth2c/two_legged"
25
+ autoload :VERSION, "oauth2c/version"
26
+ end
data/oauth2c.gemspec ADDED
@@ -0,0 +1,58 @@
1
+ # coding: utf-8
2
+
3
+ # Copyright 2017 Doximity, Inc. <support@doximity.com>
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ lib = File.expand_path('../lib', __FILE__)
18
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
19
+ require 'oauth2c/version'
20
+
21
+ Gem::Specification.new do |spec|
22
+ spec.name = "oauth2c"
23
+ spec.version = OAuth2c::VERSION
24
+ spec.authors = ["Rodrigo Kochenburger"]
25
+ spec.email = ["divoxx@gmail.com"]
26
+
27
+ spec.summary = %q{OAuth2c is a extensible OAuth2 client implementation}
28
+ spec.description = %q{OAuth2c is a extensible OAuth2 client implementation}
29
+ spec.homepage = "https://github.com/doximity/oauth2c"
30
+
31
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
32
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
33
+ # if spec.respond_to?(:metadata)
34
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
35
+ # else
36
+ # raise "RubyGems 2.0 or newer is required to protect against " \
37
+ # "public gem pushes."
38
+ # end
39
+
40
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
41
+ f.match(%r{^(test|spec|features)/})
42
+ end
43
+
44
+ spec.bindir = "exe"
45
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
46
+ spec.require_paths = ["lib"]
47
+
48
+ spec.add_runtime_dependency "http", "~> 2.0"
49
+ spec.add_runtime_dependency "jwt", "~> 1.5"
50
+
51
+ spec.add_development_dependency "bundler", "~> 1.14"
52
+ spec.add_development_dependency "pry"
53
+ spec.add_development_dependency "byebug"
54
+ spec.add_development_dependency "rake", "~> 10.0"
55
+ spec.add_development_dependency "redis", "~> 3.0"
56
+ spec.add_development_dependency "rspec", "~> 3.0"
57
+ spec.add_development_dependency "webmock", "~> 2.0"
58
+ end
metadata ADDED
@@ -0,0 +1,204 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oauth2c
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rodrigo Kochenburger
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: jwt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
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: byebug
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: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: redis
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: webmock
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '2.0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '2.0'
139
+ description: OAuth2c is a extensible OAuth2 client implementation
140
+ email:
141
+ - divoxx@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rspec"
148
+ - ".travis.yml"
149
+ - CODE_OF_CONDUCT.md
150
+ - Gemfile
151
+ - LICENSE.txt
152
+ - README.md
153
+ - Rakefile
154
+ - bin/console
155
+ - bin/rspec
156
+ - bin/setup
157
+ - lib/oauth2c.rb
158
+ - lib/oauth2c/access_token.rb
159
+ - lib/oauth2c/agent.rb
160
+ - lib/oauth2c/cache.rb
161
+ - lib/oauth2c/cache/backends.rb
162
+ - lib/oauth2c/cache/backends/in_memory_lru.rb
163
+ - lib/oauth2c/cache/backends/null.rb
164
+ - lib/oauth2c/cache/backends/redis.rb
165
+ - lib/oauth2c/cache/manager.rb
166
+ - lib/oauth2c/cache/store.rb
167
+ - lib/oauth2c/client.rb
168
+ - lib/oauth2c/error.rb
169
+ - lib/oauth2c/grants.rb
170
+ - lib/oauth2c/grants/assertion.rb
171
+ - lib/oauth2c/grants/authorization_code.rb
172
+ - lib/oauth2c/grants/client_credentials.rb
173
+ - lib/oauth2c/grants/implicit.rb
174
+ - lib/oauth2c/grants/refresh_token.rb
175
+ - lib/oauth2c/grants/resource_owner_credentials.rb
176
+ - lib/oauth2c/refinements.rb
177
+ - lib/oauth2c/three_legged.rb
178
+ - lib/oauth2c/two_legged.rb
179
+ - lib/oauth2c/version.rb
180
+ - oauth2c.gemspec
181
+ homepage: https://github.com/doximity/oauth2c
182
+ licenses: []
183
+ metadata: {}
184
+ post_install_message:
185
+ rdoc_options: []
186
+ require_paths:
187
+ - lib
188
+ required_ruby_version: !ruby/object:Gem::Requirement
189
+ requirements:
190
+ - - ">="
191
+ - !ruby/object:Gem::Version
192
+ version: '0'
193
+ required_rubygems_version: !ruby/object:Gem::Requirement
194
+ requirements:
195
+ - - ">="
196
+ - !ruby/object:Gem::Version
197
+ version: '0'
198
+ requirements: []
199
+ rubyforge_project:
200
+ rubygems_version: 2.6.8
201
+ signing_key:
202
+ specification_version: 4
203
+ summary: OAuth2c is a extensible OAuth2 client implementation
204
+ test_files: []