faraday-oauth 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3b9a49fa8f01cff86e6b4097fab5b43afb2bb3d56709d5c0576d1531992537f3
4
+ data.tar.gz: 94b53c2e46f65d21c9c57c584bed32f33ac86652c17ed19d7e6905cf0e2410fc
5
+ SHA512:
6
+ metadata.gz: 4a1838d153d2553f2a51f6c93ee7130f890c43bdca22fe61ffe4baddced79f00d3a14b23d21d94109a88d2341a91bee29a209c737c7ba149ea9c68a4ea8d6bc7
7
+ data.tar.gz: 01f999083739df25dd2da66710fbfba77ed936195c1e9e1cb9a684288b3378ffc0a878a94aae0cb4687afdc91313a4ba648ab079339f90e2ef034fa26efbe3fd
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## Unreleased
4
+
5
+ * Initial release.
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Instructure Inc.
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.
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # Faraday OAuth
2
+
3
+ [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/instructure/faraday-oauth/ci)](https://github.com/instructure/faraday-oauth/actions?query=branch%3Amain)
4
+ [![Gem](https://img.shields.io/gem/v/faraday-oauth.svg?style=flat-square)](https://rubygems.org/gems/faraday-oauth)
5
+ [![License](https://img.shields.io/github/license/instructure/faraday-oauth.svg?style=flat-square)](LICENSE.md)
6
+
7
+ A Faraday middleware that adds an access token to each request.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'faraday-oauth'
15
+ ```
16
+
17
+ ## Usage and configuration
18
+
19
+ You have to use the middleware in the Faraday instance that you want to.
20
+
21
+ ```ruby
22
+
23
+ client = Faraday.new do |builder|
24
+ builder.use :oauth, {:consumer_key => consumer_key, :consumer_secret => consumer_secret, :body_hash => body_hash}
25
+
26
+ builder.adapter Faraday.default_adapter
27
+ end
28
+ ```
29
+
30
+ ## License
31
+
32
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "forwardable"
4
+ require "simple_oauth"
5
+
6
+ module Faraday
7
+ module OAuth
8
+ # Uses the simple_oauth library to sign requests according the
9
+ # OAuth protocol.
10
+ #
11
+ # The options for this middleware are forwarded to SimpleOAuth::Header:
12
+ # :consumer_key, :consumer_secret, :token, :token_secret. All these
13
+ # parameters are optional.
14
+ #
15
+ # The signature is added to the "Authorization" HTTP request header. If the
16
+ # value for this header already exists, it is not overriden.
17
+ #
18
+ # If no Content-Type header is specified, this middleware assumes that
19
+ # request body parameters should be included while signing the request.
20
+ # Otherwise, it only includes them if the Content-Type is
21
+ # "application/x-www-form-urlencoded", as per OAuth 1.0.
22
+ #
23
+ # For better performance while signing requests, this middleware should be
24
+ # positioned before UrlEncoded middleware on the stack, but after any other
25
+ # body-encoding middleware (such as EncodeJson).
26
+ class Middleware < Faraday::Middleware
27
+ AUTH_HEADER = "Authorization"
28
+ CONTENT_TYPE = "Content-Type"
29
+ TYPE_URLENCODED = "application/x-www-form-urlencoded"
30
+
31
+ extend Forwardable
32
+ def_delegator :"Faraday::Utils", :parse_nested_query
33
+
34
+ # @param env [Faraday::Env] the environment of the request being processed
35
+ def on_request(env)
36
+ env.request_headers[AUTH_HEADER] ||= oauth_header(env).to_s if sign_request?(env)
37
+ end
38
+
39
+ def oauth_header(env)
40
+ SimpleOAuth::Header.new(env.method,
41
+ env.url.to_s,
42
+ signature_params(body_params(env)),
43
+ oauth_options(env))
44
+ end
45
+
46
+ def sign_request?(env)
47
+ !!env.request.fetch(:oauth, true)
48
+ end
49
+
50
+ def oauth_options(env)
51
+ if (extra = env.request[:oauth]) && extra.is_a?(Hash) && !extra.empty?
52
+ options.merge extra
53
+ else
54
+ options
55
+ end
56
+ end
57
+
58
+ def body_params(env)
59
+ if include_body_params?(env)
60
+ if env.request_body.respond_to?(:to_str)
61
+ parse_nested_query(env.request_body)
62
+ else
63
+ env.request_body
64
+ end
65
+ end || {}
66
+ end
67
+
68
+ def include_body_params?(env)
69
+ # see RFC 5849, section 3.4.1.3.1 for details
70
+ !(type = env.request_headers[CONTENT_TYPE]) || (type == TYPE_URLENCODED)
71
+ end
72
+
73
+ def signature_params(params)
74
+ if params.empty?
75
+ params
76
+ else
77
+ params.reject { |_k, v| v.respond_to?(:content_type) }
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faraday
4
+ module OAuth
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "oauth/version"
4
+
5
+ module Faraday
6
+ module OAuth
7
+ autoload :Middleware, "faraday/oauth/middleware"
8
+
9
+ Faraday::Request.register_middleware(oauth: -> { Faraday::OAuth::Middleware })
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday/oauth"
metadata ADDED
@@ -0,0 +1,194 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faraday-oauth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Cody Cutrer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-08-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '3'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '1.10'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '3'
33
+ - !ruby/object:Gem::Dependency
34
+ name: simple_oauth
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.3'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '0.3'
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '2.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '2.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '13.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '13.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '3.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rubocop-inst
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '1.0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '1.0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: rubocop-rake
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '0.6'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '0.6'
117
+ - !ruby/object:Gem::Dependency
118
+ name: rubocop-rspec
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: '2.0'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: '2.0'
131
+ - !ruby/object:Gem::Dependency
132
+ name: simplecov
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - "~>"
136
+ - !ruby/object:Gem::Version
137
+ version: '0.21'
138
+ type: :development
139
+ prerelease: false
140
+ version_requirements: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - "~>"
143
+ - !ruby/object:Gem::Version
144
+ version: '0.21'
145
+ description: |
146
+ Uses the simple_oauth library to sign requests according the
147
+ OAuth protocol.
148
+ email:
149
+ - cody@instructure.com
150
+ executables: []
151
+ extensions: []
152
+ extra_rdoc_files: []
153
+ files:
154
+ - CHANGELOG.md
155
+ - LICENSE.md
156
+ - README.md
157
+ - lib/faraday-oauth.rb
158
+ - lib/faraday/oauth.rb
159
+ - lib/faraday/oauth/middleware.rb
160
+ - lib/faraday/oauth/version.rb
161
+ homepage: https://github.com/instructure/faraday-oauth
162
+ licenses:
163
+ - MIT
164
+ metadata:
165
+ bug_tracker_uri: https://github.com/instructure/faraday-oauth/issues
166
+ changelog_uri: https://github.com/instructure/faraday-oauth/blob/v0.1.0/CHANGELOG.md
167
+ documentation_uri: http://www.rubydoc.info/gems/faraday-oauth/0.1.0
168
+ homepage_uri: https://github.com/instructure/faraday-oauth
169
+ rubygems_mfa_required: 'true'
170
+ source_code_uri: https://github.com/instructure/faraday-oauth
171
+ wiki_uri: https://github.com/instructure/faraday-oauth/wiki
172
+ post_install_message:
173
+ rdoc_options: []
174
+ require_paths:
175
+ - lib
176
+ required_ruby_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '2.6'
181
+ - - "<"
182
+ - !ruby/object:Gem::Version
183
+ version: '4'
184
+ required_rubygems_version: !ruby/object:Gem::Requirement
185
+ requirements:
186
+ - - ">="
187
+ - !ruby/object:Gem::Version
188
+ version: '0'
189
+ requirements: []
190
+ rubygems_version: 3.3.26
191
+ signing_key:
192
+ specification_version: 4
193
+ summary: A Faraday middleware that adds an access token to each request.
194
+ test_files: []