faraday-jwt 0.0.0 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +46 -3
- data/Rakefile +14 -2
- data/faraday-jwt.gemspec +2 -1
- data/lib/faraday/jwt/request.rb +48 -0
- data/lib/faraday/jwt/response.rb +46 -0
- data/lib/faraday/jwt/version.rb +2 -2
- data/lib/faraday/jwt.rb +5 -3
- metadata +18 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 10706b798dca52baafed6fa07367d18101f394c6514f5efa4ba61e0445258ddc
|
4
|
+
data.tar.gz: 4af304d1ef1455d323a26df2213b634254e59abe9606bb04fcf99525dd33d649
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c6a0c14376301d8a3d6f07f591b4aa57b230a871041b318e8ae9be803e0e5b3dae6b9ce4a8297af057173fabecbe65e819651dc18f62307edbe4258b32c9f145
|
7
|
+
data.tar.gz: 5049412c9a8fe61c1a047aacd76c68891d8cadd1c1e84aed95fbf930465d1e9408ccb46d5e3b258645bcd68896e288f7c2e8afece2fdc50fbdb25e90938d784c
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Faraday::
|
1
|
+
# Faraday::JWT
|
2
2
|
|
3
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/faraday/jwt`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
4
|
|
@@ -16,7 +16,50 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
16
16
|
|
17
17
|
## Usage
|
18
18
|
|
19
|
-
|
19
|
+
### Request
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
conn = Faraday.new(...) do |f|
|
23
|
+
f.request :jwt
|
24
|
+
...
|
25
|
+
end
|
26
|
+
|
27
|
+
conn.post('/', { foo: :bar })
|
28
|
+
# POST with
|
29
|
+
# Content-Type: application/jwt
|
30
|
+
# Body: eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJmb28iOiJiYXIifQ.
|
31
|
+
```
|
32
|
+
|
33
|
+
or optionally signing
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
conn = Faraday.new(...) do |f|
|
37
|
+
f.request :jwt, signing_key: private_key
|
38
|
+
...
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
### Response
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
conn = Faraday.new(...) do |f|
|
46
|
+
f.response :jwt
|
47
|
+
...
|
48
|
+
end
|
49
|
+
|
50
|
+
res = conn.get('jwt')
|
51
|
+
res.body
|
52
|
+
# => JSON::JWT instance
|
53
|
+
```
|
54
|
+
|
55
|
+
or optionally verifying
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
conn = Faraday.new(...) do |f|
|
59
|
+
f.response :jwt, verification_key: public_key
|
60
|
+
...
|
61
|
+
end
|
62
|
+
```
|
20
63
|
|
21
64
|
## Development
|
22
65
|
|
@@ -34,4 +77,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
34
77
|
|
35
78
|
## Code of Conduct
|
36
79
|
|
37
|
-
Everyone interacting in the Faraday::
|
80
|
+
Everyone interacting in the Faraday::JWT project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/faraday-jwt/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
CHANGED
@@ -1,8 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rspec/core/rake_task'
|
5
5
|
|
6
6
|
RSpec::Core::RakeTask.new(:spec)
|
7
7
|
|
8
|
+
namespace :coverage do
|
9
|
+
desc "Open coverage report"
|
10
|
+
task :report do
|
11
|
+
require 'simplecov'
|
12
|
+
`open "#{File.join SimpleCov.coverage_path, 'index.html'}"`
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
task :spec do
|
17
|
+
Rake::Task[:'coverage:report'].invoke unless ENV['GITHUB_ACTIONS']
|
18
|
+
end
|
19
|
+
|
8
20
|
task default: :spec
|
data/faraday-jwt.gemspec
CHANGED
@@ -4,7 +4,7 @@ require_relative 'lib/faraday/jwt/version'
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = 'faraday-jwt'
|
7
|
-
spec.version = Faraday::
|
7
|
+
spec.version = Faraday::JWT::VERSION
|
8
8
|
spec.authors = ['nov']
|
9
9
|
spec.email = ['nov@matake.jp']
|
10
10
|
|
@@ -30,6 +30,7 @@ Gem::Specification.new do |spec|
|
|
30
30
|
spec.require_paths = ['lib']
|
31
31
|
|
32
32
|
spec.add_dependency 'faraday', '~> 2.0'
|
33
|
+
spec.add_dependency 'json-jwt', '~> 1.16'
|
33
34
|
spec.add_development_dependency 'rake'
|
34
35
|
spec.add_development_dependency 'rspec'
|
35
36
|
spec.add_development_dependency 'simplecov'
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Faraday
|
4
|
+
module JWT
|
5
|
+
class Request < Middleware
|
6
|
+
def initialize(app = nil, signing_key: nil, signing_alg: :autodetect, content_type: 'application/jwt')
|
7
|
+
super app
|
8
|
+
@signing_key = signing_key
|
9
|
+
@signing_alg = signing_alg
|
10
|
+
@content_types = Array(content_type)
|
11
|
+
end
|
12
|
+
|
13
|
+
def on_request(env)
|
14
|
+
encode_body!(env) if encodable_content?(env)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def encode_body!(env)
|
20
|
+
jwt = JSON::JWT.new env[:body]
|
21
|
+
env[:body] = if @signing_key.present?
|
22
|
+
jwt.sign @signing_key, @signing_alg
|
23
|
+
else
|
24
|
+
jwt
|
25
|
+
end.to_s
|
26
|
+
end
|
27
|
+
|
28
|
+
def encodable_content?(env)
|
29
|
+
!env[:body].respond_to?(:to_str) &&
|
30
|
+
encodable_content_type?(env[:request_headers])
|
31
|
+
end
|
32
|
+
|
33
|
+
def encodable_content_type?(headers)
|
34
|
+
@content_types.any? do |content_type|
|
35
|
+
content_type_of(headers) == content_type
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def content_type_of(headers)
|
40
|
+
type = headers[CONTENT_TYPE].to_s
|
41
|
+
type = type.split(';', 2).first if type.index(';')
|
42
|
+
type
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
Faraday::Request.register_middleware(jwt: Faraday::JWT::Request)
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Faraday
|
4
|
+
module JWT
|
5
|
+
class Response < Middleware
|
6
|
+
def initialize(app = nil, verification_key: :skip_verification, signing_algs: nil, content_type: 'application/jwt')
|
7
|
+
super app
|
8
|
+
@verification_key = verification_key
|
9
|
+
@signing_algs = signing_algs
|
10
|
+
@content_types = Array(content_type)
|
11
|
+
end
|
12
|
+
|
13
|
+
def on_complete(env)
|
14
|
+
decode_body!(env) if decodable_content?(env)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def decode_body!(env)
|
20
|
+
env[:raw_body] = env[:body]
|
21
|
+
env[:body] = JSON::JWT.decode(env[:body], @verification_key, @signing_algs)
|
22
|
+
rescue => e
|
23
|
+
raise Faraday::ParsingError.new(e, env[:response])
|
24
|
+
end
|
25
|
+
|
26
|
+
def decodable_content?(env)
|
27
|
+
env[:body].respond_to?(:to_str) && !env[:body].strip.empty? &&
|
28
|
+
decodable_content_type?(env[:response_headers])
|
29
|
+
end
|
30
|
+
|
31
|
+
def decodable_content_type?(headers)
|
32
|
+
@content_types.any? do |content_type|
|
33
|
+
content_type_of(headers) == content_type
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def content_type_of(headers)
|
38
|
+
type = headers[CONTENT_TYPE].to_s
|
39
|
+
type = type.split(';', 2).first if type.index(';')
|
40
|
+
type
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
Faraday::Response.register_middleware(jwt: Faraday::JWT::Response)
|
data/lib/faraday/jwt/version.rb
CHANGED
data/lib/faraday/jwt.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'faraday'
|
4
|
+
require 'json/jwt'
|
3
5
|
require_relative 'jwt/version'
|
6
|
+
require_relative 'jwt/request'
|
7
|
+
require_relative 'jwt/response'
|
4
8
|
|
5
9
|
module Faraday
|
6
|
-
module
|
7
|
-
class Error < StandardError; end
|
8
|
-
# Your code goes here...
|
10
|
+
module JWT
|
9
11
|
end
|
10
12
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday-jwt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- nov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-10-
|
11
|
+
date: 2022-10-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: json-jwt
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.16'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.16'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rake
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -81,6 +95,8 @@ files:
|
|
81
95
|
- Rakefile
|
82
96
|
- faraday-jwt.gemspec
|
83
97
|
- lib/faraday/jwt.rb
|
98
|
+
- lib/faraday/jwt/request.rb
|
99
|
+
- lib/faraday/jwt/response.rb
|
84
100
|
- lib/faraday/jwt/version.rb
|
85
101
|
homepage: https://github.com/nov/faraday-jwt
|
86
102
|
licenses:
|