faraday-oauth1 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +7 -0
- data/LICENSE.md +21 -0
- data/README.md +73 -0
- data/lib/faraday/oauth1/request.rb +136 -0
- data/lib/faraday/oauth1/version.rb +7 -0
- data/lib/faraday/oauth1.rb +15 -0
- metadata +207 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3702006a3f91e531f37e456f5c9d873d20b8f9b1fd1cd6a708f3aaec603058a7
|
4
|
+
data.tar.gz: b88fc3ab462f82dea5b06c6e02cca1195ce67c148e989d26d376c08df93b7679
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: abcdb0e1a65cf6872ec1e206b5625b4d82b833f0d2e43e881f07fcce59ccc1260292177e2bb9f36279827c3141dee8af1e08a1351b53aeba44dbc7f6b5205193
|
7
|
+
data.tar.gz: a0605dfb01f4b1c4e1dabd9a011632980d8cbe2ff7b2ccd4a99936d959c4eea820b2e65bedaffae332faa0ee4415a4413554c6c64ab129424269e1ff9539704d
|
data/CHANGELOG.md
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Benjamin Fleischer
|
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,73 @@
|
|
1
|
+
# Faraday OAuth1
|
2
|
+
|
3
|
+
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/gemhome/faraday-oauth1/ci)](https://github.com/gemhome/faraday-oauth1/actions?query=branch%3Amain)
|
4
|
+
[![Gem](https://img.shields.io/gem/v/faraday-oauth1.svg?style=flat-square)](https://rubygems.org/gems/faraday-oauth1)
|
5
|
+
[![License](https://img.shields.io/github/license/gemhome/faraday-oauth1.svg?style=flat-square)](LICENSE.md)
|
6
|
+
|
7
|
+
Faraday OAuth1 Middleware.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'faraday-oauth1'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
```shell
|
20
|
+
bundle install
|
21
|
+
```
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
```shell
|
26
|
+
gem install faraday-oauth1
|
27
|
+
```
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
require 'faraday'
|
33
|
+
require 'faraday/oauth1'
|
34
|
+
|
35
|
+
base_uri = "https://example.com"
|
36
|
+
path = "/webservices/has-oauth1"
|
37
|
+
headers = {}
|
38
|
+
headers["Content-Type"] ||= "application/x-www-form-urlencoded"
|
39
|
+
# see https://github.com/laserlemon/simple_oauth for credentials options
|
40
|
+
credentials = {
|
41
|
+
consumer_key: consumer_key,
|
42
|
+
consumer_secret: consumer_secret,
|
43
|
+
access_token: nil,
|
44
|
+
access_token_secret: nil
|
45
|
+
}.compact
|
46
|
+
auth_type = "param"
|
47
|
+
conn = Faraday.new(url: base_uri) do |builder|
|
48
|
+
builder.request :oauth1, auth_type, **credentials
|
49
|
+
builder.use Faraday::Response::RaiseError
|
50
|
+
builder.adapter Faraday.default_adapter
|
51
|
+
builder.headers.update(headers) if headers
|
52
|
+
end
|
53
|
+
response = conn.get(path)
|
54
|
+
```
|
55
|
+
|
56
|
+
## Development
|
57
|
+
|
58
|
+
After checking out the repo, run `bin/setup` to install dependencies.
|
59
|
+
|
60
|
+
Then, run `bin/test` to run the tests.
|
61
|
+
|
62
|
+
To install this gem onto your local machine, run `rake build`.
|
63
|
+
|
64
|
+
To release a new version, make a commit with a message such as "Bumped to 0.0.2" and then run `rake release`.
|
65
|
+
See how it works [here](https://bundler.io/guides/creating_gem.html#releasing-the-gem).
|
66
|
+
|
67
|
+
## Contributing
|
68
|
+
|
69
|
+
Bug reports and pull requests are welcome on [GitHub](https://github.com/gemhome/faraday-oauth1).
|
70
|
+
|
71
|
+
## License
|
72
|
+
|
73
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -0,0 +1,136 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copied from https://github.com/lostisland/faraday_middleware/blob/v1.2.0/lib/faraday_middleware/request/oauth.rb
|
4
|
+
|
5
|
+
require 'forwardable'
|
6
|
+
|
7
|
+
module Faraday
|
8
|
+
module OAuth1
|
9
|
+
# Public: Uses the simple_oauth library to sign requests according the
|
10
|
+
# OAuth 1 protocol.
|
11
|
+
#
|
12
|
+
# The options for this middleware are forwarded to SimpleOAuth::Header:
|
13
|
+
# :consumer_key, :consumer_secret, :token, :token_secret. All these
|
14
|
+
# parameters are optional.
|
15
|
+
#
|
16
|
+
# The signature is added to the "Authorization" HTTP request header. If the
|
17
|
+
# value for this header already exists, it is not overridden.
|
18
|
+
#
|
19
|
+
# If no Content-Type header is specified, this middleware assumes that
|
20
|
+
# request body parameters should be included while signing the request.
|
21
|
+
# Otherwise, it only includes them if the Content-Type is
|
22
|
+
# "application/x-www-form-urlencoded", as per OAuth 1.0.
|
23
|
+
#
|
24
|
+
# For better performance while signing requests, this middleware should be
|
25
|
+
# positioned before UrlEncoded middleware on the stack, but after any other
|
26
|
+
# body-encoding middleware (such as EncodeJson).
|
27
|
+
class Request < Middleware
|
28
|
+
require 'simple_oauth'
|
29
|
+
|
30
|
+
AUTH_HEADER = 'Authorization'
|
31
|
+
CONTENT_TYPE = 'Content-Type'
|
32
|
+
TYPE_URLENCODED = 'application/x-www-form-urlencoded'
|
33
|
+
DEFAULT_AUTH_METHOD = 'param'
|
34
|
+
AUTH_METHODS = [
|
35
|
+
DEFAULT_AUTH_METHOD,
|
36
|
+
'header'
|
37
|
+
].tap(&:freeze)
|
38
|
+
|
39
|
+
extend Forwardable
|
40
|
+
def_delegators :'Faraday::Utils', :parse_nested_query, :parse_query, :build_query
|
41
|
+
|
42
|
+
attr_reader :auth_methods
|
43
|
+
|
44
|
+
def initialize(app = nil, auth_methods = nil, options = {})
|
45
|
+
super(app)
|
46
|
+
@auth_methods = Array(auth_methods || DEFAULT_AUTH_METHOD)
|
47
|
+
validate_auth_methods!
|
48
|
+
@options = options
|
49
|
+
end
|
50
|
+
|
51
|
+
def on_request(env)
|
52
|
+
if sign_request?(env)
|
53
|
+
header = oauth_header(env)
|
54
|
+
auth_methods.each do |auth_method|
|
55
|
+
case auth_method
|
56
|
+
when 'param', :param
|
57
|
+
params = {
|
58
|
+
'oauth_consumer_key' => header.signed_attributes.fetch(:oauth_consumer_key),
|
59
|
+
'oauth_signature_method' => header.signed_attributes.fetch(:oauth_signature_method),
|
60
|
+
'oauth_timestamp' => header.signed_attributes.fetch(:oauth_timestamp),
|
61
|
+
'oauth_nonce' => header.signed_attributes.fetch(:oauth_nonce),
|
62
|
+
'oauth_version' => header.signed_attributes.fetch(:oauth_version),
|
63
|
+
'oauth_signature' => header.signed_attributes.fetch(:oauth_signature)
|
64
|
+
}
|
65
|
+
.merge(query_params(env[:url]))
|
66
|
+
env[:url].query = build_query(params)
|
67
|
+
when 'header', :header
|
68
|
+
env[:request_headers][AUTH_HEADER] ||= header.to_s
|
69
|
+
else
|
70
|
+
raise ArgumentError, "invalid auth method: #{auth_method} not in #{AUTH_METHODS.join(', ')}"
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def validate_auth_methods!
|
77
|
+
invalid_auth_methods = auth_methods.map(&:to_s) - AUTH_METHODS
|
78
|
+
return if invalid_auth_methods.empty?
|
79
|
+
|
80
|
+
raise ArgumentError,
|
81
|
+
"invalid auth method(s): #{invalid_auth_methods.join(', ')} not in #{AUTH_METHODS.join(', ')}"
|
82
|
+
end
|
83
|
+
|
84
|
+
def oauth_header(env)
|
85
|
+
SimpleOAuth::Header.new(env[:method],
|
86
|
+
env[:url].to_s,
|
87
|
+
signature_params(body_params(env)),
|
88
|
+
oauth_options(env))
|
89
|
+
end
|
90
|
+
|
91
|
+
def sign_request?(env)
|
92
|
+
!!env[:request].fetch(:oauth, true)
|
93
|
+
end
|
94
|
+
alias process_request? sign_request?
|
95
|
+
|
96
|
+
def oauth_options(env)
|
97
|
+
if (extra = env[:request][:oauth]) && extra.is_a?(Hash) && !extra.empty?
|
98
|
+
@options.merge extra
|
99
|
+
else
|
100
|
+
@options
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def body_params(env)
|
105
|
+
if include_body_params?(env)
|
106
|
+
if env[:body].respond_to?(:to_str)
|
107
|
+
parse_nested_query env[:body]
|
108
|
+
else
|
109
|
+
env[:body]
|
110
|
+
end
|
111
|
+
end || {}
|
112
|
+
end
|
113
|
+
|
114
|
+
def include_body_params?(env)
|
115
|
+
# see RFC 5849, section 3.4.1.3.1 for details
|
116
|
+
!(type = env[:request_headers][CONTENT_TYPE]) || (type == TYPE_URLENCODED)
|
117
|
+
end
|
118
|
+
|
119
|
+
def signature_params(params)
|
120
|
+
if params.empty?
|
121
|
+
params
|
122
|
+
else
|
123
|
+
params.reject { |_k, v| v.respond_to?(:content_type) }
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def query_params(url)
|
128
|
+
if url.query.nil? || url.query.empty?
|
129
|
+
{}
|
130
|
+
else
|
131
|
+
parse_query url.query
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'oauth1/request'
|
4
|
+
require_relative 'oauth1/version'
|
5
|
+
|
6
|
+
module Faraday
|
7
|
+
# The Faraday::OAuth1 middleware main module
|
8
|
+
module OAuth1
|
9
|
+
# Load middleware with
|
10
|
+
# conn.use Faraday::OAuth1::Request
|
11
|
+
# or
|
12
|
+
# conn.request :oauth1
|
13
|
+
Faraday::Request.register_middleware(oauth1: Faraday::OAuth1::Request)
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,207 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: faraday-oauth1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Benjamin Fleischer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-10-12 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'
|
40
|
+
type: :runtime
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
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: simplecov
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - "~>"
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: 0.21.0
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - "~>"
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: 0.21.0
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: rubocop
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - "~>"
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 1.31.0
|
110
|
+
type: :development
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - "~>"
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: 1.31.0
|
117
|
+
- !ruby/object:Gem::Dependency
|
118
|
+
name: rubocop-packaging
|
119
|
+
requirement: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - "~>"
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: 0.5.0
|
124
|
+
type: :development
|
125
|
+
prerelease: false
|
126
|
+
version_requirements: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - "~>"
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: 0.5.0
|
131
|
+
- !ruby/object:Gem::Dependency
|
132
|
+
name: rubocop-performance
|
133
|
+
requirement: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - "~>"
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '1.0'
|
138
|
+
type: :development
|
139
|
+
prerelease: false
|
140
|
+
version_requirements: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - "~>"
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '1.0'
|
145
|
+
- !ruby/object:Gem::Dependency
|
146
|
+
name: rubocop-rspec
|
147
|
+
requirement: !ruby/object:Gem::Requirement
|
148
|
+
requirements:
|
149
|
+
- - "~>"
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
version: '2.0'
|
152
|
+
type: :development
|
153
|
+
prerelease: false
|
154
|
+
version_requirements: !ruby/object:Gem::Requirement
|
155
|
+
requirements:
|
156
|
+
- - "~>"
|
157
|
+
- !ruby/object:Gem::Version
|
158
|
+
version: '2.0'
|
159
|
+
description: 'Faraday OAuth1 Middleware.
|
160
|
+
|
161
|
+
'
|
162
|
+
email:
|
163
|
+
- github@benjaminfleischer.com
|
164
|
+
executables: []
|
165
|
+
extensions: []
|
166
|
+
extra_rdoc_files: []
|
167
|
+
files:
|
168
|
+
- CHANGELOG.md
|
169
|
+
- LICENSE.md
|
170
|
+
- README.md
|
171
|
+
- lib/faraday/oauth1.rb
|
172
|
+
- lib/faraday/oauth1/request.rb
|
173
|
+
- lib/faraday/oauth1/version.rb
|
174
|
+
homepage: https://github.com/gemhome/faraday-oauth1
|
175
|
+
licenses:
|
176
|
+
- MIT
|
177
|
+
metadata:
|
178
|
+
bug_tracker_uri: https://github.com/gemhome/faraday-oauth1/issues
|
179
|
+
changelog_uri: https://github.com/gemhome/faraday-oauth1/blob/v0.1.1/CHANGELOG.md
|
180
|
+
documentation_uri: http://www.rubydoc.info/gems/faraday-oauth1/0.1.1
|
181
|
+
homepage_uri: https://github.com/gemhome/faraday-oauth1
|
182
|
+
rubygems_mfa_required: 'true'
|
183
|
+
source_code_uri: https://github.com/gemhome/faraday-oauth1
|
184
|
+
wiki_uri: https://github.com/gemhome/faraday-oauth1/wiki
|
185
|
+
post_install_message:
|
186
|
+
rdoc_options: []
|
187
|
+
require_paths:
|
188
|
+
- lib
|
189
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
190
|
+
requirements:
|
191
|
+
- - ">="
|
192
|
+
- !ruby/object:Gem::Version
|
193
|
+
version: '2.6'
|
194
|
+
- - "<"
|
195
|
+
- !ruby/object:Gem::Version
|
196
|
+
version: '4'
|
197
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
198
|
+
requirements:
|
199
|
+
- - ">="
|
200
|
+
- !ruby/object:Gem::Version
|
201
|
+
version: '0'
|
202
|
+
requirements: []
|
203
|
+
rubygems_version: 3.3.7
|
204
|
+
signing_key:
|
205
|
+
specification_version: 4
|
206
|
+
summary: Faraday OAuth1 Middleware
|
207
|
+
test_files: []
|