rack-jwt-decode 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.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.ruby-version +1 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +23 -0
- data/LICENSE.txt +21 -0
- data/README.md +49 -0
- data/Rakefile +2 -0
- data/config.ru +12 -0
- data/lib/rack-jwt-decode.rb +1 -0
- data/lib/rack_jwt_decode.rb +29 -0
- data/rack-jwt-decode.gemspec +27 -0
- metadata +85 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 98b92491ce03d394d60c1fb08e47be75ea8e27e049c9a7804c7204aecf0715e4
|
4
|
+
data.tar.gz: 96ab74597d9e3bda5dcb6e499a99301d6e7e96597781e43cd3c3db9f770eebb5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a7143dc5f0ddc1e70e6ab86f127a8db1505a58ba4499d223be25c6519266ce065927e71c0f52d6257d4d64f7e8367f4bd25d4698e55b69dc45edbed65653a882
|
7
|
+
data.tar.gz: 1ba6bb4f43006c3034bb4995138aeb6ca73767f15321633a9d7cf1eb030f963d5a78a8db1fa98a1a6f69fba0220917a0f9be9ee4400b3a8d0cb45afa6e84bbed
|
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.6.6
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rack-jwt-decode (0.1.0)
|
5
|
+
jwt
|
6
|
+
rack
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
jwt (2.2.2)
|
12
|
+
rack (2.2.3)
|
13
|
+
rake (12.3.3)
|
14
|
+
|
15
|
+
PLATFORMS
|
16
|
+
ruby
|
17
|
+
|
18
|
+
DEPENDENCIES
|
19
|
+
rack-jwt-decode!
|
20
|
+
rake (~> 12.0)
|
21
|
+
|
22
|
+
BUNDLED WITH
|
23
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Paul Barry
|
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,49 @@
|
|
1
|
+
# Rack JWT Decode
|
2
|
+
|
3
|
+
This is a Rack middleware that will look for a [JSON Web Token (JWT)](https://jwt.io) in the Authorization Bearer header and if it is present, decode it and add the values from the payload to the Rack env, with the Rack env key prefixed with `"jwt."`.
|
4
|
+
|
5
|
+
For example, say your application generated a JWT like this:
|
6
|
+
|
7
|
+
>> token = JWT.encode({ sub: 1234, iat: Time.now.utc.to_i }, ENV["APPLICATION_SECRET"])
|
8
|
+
=> "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEyMzQsImlhdCI6MTU5ODg5ODc3Mn0.MOaM22JkvCi2Bg-vUXAPuYXA9NtsApGNDplRdYMYerw"
|
9
|
+
|
10
|
+
Now say that clients pass that token back in an HTTP Header like this:
|
11
|
+
|
12
|
+
curl -v -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEyMzQsImlhdCI6MTU5ODg5ODc3Mn0.MOaM22JkvCi2Bg-vUXAPuYXA9NtsApGNDplRdYMYerw' 'http://localhost:9292'
|
13
|
+
|
14
|
+
This middleware would then result in the following additions to the Rack env:
|
15
|
+
|
16
|
+
env["jwt.sub"] # => 1234
|
17
|
+
env["jwt.iat"] # => 1598898472
|
18
|
+
|
19
|
+
## Installation
|
20
|
+
|
21
|
+
Add this line to your application's Gemfile:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
gem "rack-jwt-decode"
|
25
|
+
```
|
26
|
+
|
27
|
+
And then execute:
|
28
|
+
|
29
|
+
$ bundle install
|
30
|
+
|
31
|
+
Or install it yourself as:
|
32
|
+
|
33
|
+
$ gem install rack-jwt-decode
|
34
|
+
|
35
|
+
## Usage
|
36
|
+
|
37
|
+
Configure your Rack application to use this middleware in your Rackup file like this:
|
38
|
+
|
39
|
+
use RackJWTDecode, ENV["APPLICATION_SECRET"]
|
40
|
+
|
41
|
+
The second argument is the secret key that you used to encode the JWT token.
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
|
45
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/pjb3/rack-jwt-decode.
|
46
|
+
|
47
|
+
## License
|
48
|
+
|
49
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/config.ru
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require_relative "rack_jwt_decode"
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require "jwt"
|
2
|
+
|
3
|
+
class RackJWTDecode
|
4
|
+
HTTP_AUTHORIZATION = "HTTP_AUTHORIZATION".freeze
|
5
|
+
BEARER_REGEXP = /\ABearer /i.freeze
|
6
|
+
|
7
|
+
def initialize(app, application_secret)
|
8
|
+
@app = app
|
9
|
+
@application_secret = application_secret
|
10
|
+
end
|
11
|
+
|
12
|
+
def call(env)
|
13
|
+
add_jwt_payload_to_env(env)
|
14
|
+
@app.call(env)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def add_jwt_payload_to_env(env)
|
20
|
+
return unless @application_secret
|
21
|
+
return unless auth = env[HTTP_AUTHORIZATION]
|
22
|
+
return unless token = auth.split(BEARER_REGEXP)&.last
|
23
|
+
return unless payload = JWT.decode(token, @application_secret)&.first
|
24
|
+
|
25
|
+
payload&.each do |k, v|
|
26
|
+
env["jwt.#{k}"] = v
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
spec.name = "rack-jwt-decode"
|
3
|
+
spec.version = "0.1.0"
|
4
|
+
spec.authors = ["Paul Barry"]
|
5
|
+
spec.email = ["pauljbarry3@gmail.com"]
|
6
|
+
|
7
|
+
spec.summary = "Rack middleware to decode the JWT token and add the payload to the Rack env"
|
8
|
+
spec.description = "Rack middleware to decode the JWT token and add the payload to the Rack env"
|
9
|
+
spec.homepage = "https://github.com/pjb3/rack-jwt-decode"
|
10
|
+
spec.license = "MIT"
|
11
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
12
|
+
|
13
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
14
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_runtime_dependency "jwt"
|
26
|
+
spec.add_runtime_dependency "rack"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-jwt-decode
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Paul Barry
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-09-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: jwt
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rack
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Rack middleware to decode the JWT token and add the payload to the Rack
|
42
|
+
env
|
43
|
+
email:
|
44
|
+
- pauljbarry3@gmail.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- ".ruby-version"
|
51
|
+
- Gemfile
|
52
|
+
- Gemfile.lock
|
53
|
+
- LICENSE.txt
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- config.ru
|
57
|
+
- lib/rack-jwt-decode.rb
|
58
|
+
- lib/rack_jwt_decode.rb
|
59
|
+
- rack-jwt-decode.gemspec
|
60
|
+
homepage: https://github.com/pjb3/rack-jwt-decode
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata:
|
64
|
+
homepage_uri: https://github.com/pjb3/rack-jwt-decode
|
65
|
+
source_code_uri: https://github.com/pjb3/rack-jwt-decode
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 2.3.0
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubygems_version: 3.0.3
|
82
|
+
signing_key:
|
83
|
+
specification_version: 4
|
84
|
+
summary: Rack middleware to decode the JWT token and add the payload to the Rack env
|
85
|
+
test_files: []
|