rack-indicium 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e2c76d792fe805b3a669ecb042fa8842707e9ca7
4
+ data.tar.gz: 1012a5a357b0c0d8bb8b37b3df7760a75caddc99
5
+ SHA512:
6
+ metadata.gz: d9b41b6559bf5e2466f233fed93bfaaafa72ded1b8e7f54587ffc6dbdda69f8c422509013241f0d3b56cfd50d43becfbf530706917b256b3d10a702f6c8fa52c
7
+ data.tar.gz: d2716be198f797d4c30e3e8adfc0a666511669da1a2d8be5ccb964dfea2a3ab4a198e4f59a383f535592ce555c3dd58027af9f056e1b6c07626b4d7e0ca08a81
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --format documentation
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.2
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-indicium.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Twingly AB
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,71 @@
1
+ # Rack::Indicium
2
+
3
+ [![Build Status](https://magnum.travis-ci.com/twingly/rack-indicium.svg?token=ADz8fWxRD3uP4KZPPZQS&branch=master)](https://magnum.travis-ci.com/twingly/rack-indicium)
4
+
5
+ If a [JSON Web Token (JWT)](http://jwt.io/) is sent in the header, it will be decoded and available in the `jwt.payload` and `jwt.header` rack `env` variables.
6
+
7
+ Optional integration with [Sentry Raven] for jwt-context to exceptions.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'rack-indicium'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install rack-indicium
24
+
25
+ ## Usage
26
+
27
+ ```Ruby
28
+ require "rack/indicium"
29
+ require "rack/indicium/sentry" # Optional to add jwt context to Sentry
30
+ use Rack::Indicium, ENV.fetch("JWT_SECRET")
31
+ use Rack::Indicium::Sentry # Add after use Raven::Rack
32
+ run App
33
+ ```
34
+
35
+ Once the middleware is included you get access to `jwt.header` and `jwt.payload` in the `env` object.
36
+
37
+ ```Ruby
38
+ # It will only be set if there's a valid JWT that is verified with the jwt secret
39
+ payload = env.fetch("jwt.payload") { nil }
40
+ ```
41
+
42
+ This could then be used for authorization
43
+
44
+ ```Ruby
45
+ # Only allow requests from our clients
46
+ def authorized?
47
+ payload = env.fetch("jwt.payload") { {} }
48
+ payload["aud"] == ENV.fetch("CLIENT_ID")
49
+ end
50
+ ```
51
+
52
+ If you need custom options to decode JWT, override the decoder:
53
+
54
+ ```Ruby
55
+ require "rack/indicium"
56
+
57
+ unsafe_decoder = lambda { |jwt, secret| JWT.decode(jwt, secret, true, verify_expiration: false) }
58
+
59
+ use Rack::Indicium, ENV.fetch("JWT_SECRET"), unsafe_decoder
60
+ run App
61
+ ```
62
+
63
+ ## Contributing
64
+
65
+ 1. Fork it ( https://github.com/twingly/rack-indicium/fork )
66
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
67
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
68
+ 4. Push to the branch (`git push origin my-new-feature`)
69
+ 5. Create a new Pull Request
70
+
71
+ [Sentry Raven]: https://github.com/getsentry/raven-ruby
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ rescue LoadError
7
+ end
8
+ task(default: :spec)
@@ -0,0 +1,44 @@
1
+ require "rack/indicium/version"
2
+ require "jwt"
3
+
4
+ module Rack
5
+ class Indicium
6
+
7
+ HTTP_AUTHORIZATION = "HTTP_AUTHORIZATION".freeze
8
+ BEARER_REGEXP = /\ABearer /i
9
+
10
+ def initialize(app, secret, decoder = nil)
11
+ @app = app
12
+
13
+ @secret = secret
14
+ @decoder = decoder || lambda { |jwt, secret| JWT.decode(jwt, secret) }
15
+ end
16
+
17
+ def call(env)
18
+ look_for_authorization_header(env)
19
+
20
+ @app.call(env)
21
+ end
22
+
23
+ def look_for_authorization_header(env)
24
+ authorization_header = env[HTTP_AUTHORIZATION]
25
+ return unless authorization_header
26
+
27
+ _, jwt = authorization_header.split(BEARER_REGEXP)
28
+ return unless jwt
29
+
30
+ jwt_payload, jwt_header = decode(jwt)
31
+
32
+ return unless jwt_payload
33
+ return unless jwt_header
34
+
35
+ env["jwt.payload"] = jwt_payload
36
+ env["jwt.header"] = jwt_header
37
+ end
38
+
39
+ def decode(jwt)
40
+ @decoder.call(jwt, @secret)
41
+ rescue
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,38 @@
1
+ require "rack/indicium/version"
2
+
3
+ module Rack
4
+ class Indicium
5
+ class Sentry
6
+ def initialize(app)
7
+ @app = app
8
+
9
+ if defined?(Raven)
10
+ @sentry_client = Raven
11
+ else
12
+ warn "%s: Raven not definied, can't send JWT headers to Sentry." % self.class.to_s
13
+ end
14
+ end
15
+
16
+ def call(env)
17
+ check_for_jwt(env) if enabled?
18
+ @app.call(env)
19
+ end
20
+
21
+ def check_for_jwt(env)
22
+ context = {
23
+ "jwt.header" => env["jwt.header"],
24
+ "jwt.payload" => env["jwt.payload"],
25
+ }
26
+ client.extra_context(context)
27
+ end
28
+
29
+ def client
30
+ @sentry_client
31
+ end
32
+
33
+ def enabled?
34
+ !!@sentry_client
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class Indicium
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/indicium/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-indicium"
8
+ spec.version = Rack::Indicium::VERSION
9
+ spec.authors = ["Twingly AB"]
10
+ spec.email = ["support@twingly.com"]
11
+
12
+ spec.summary = %q{Rack JWT helpers}
13
+ spec.description = "Rack middleware to decode JWT and append it to the rack environment"
14
+ spec.homepage = "https://github.com/twingly/rack-indicium"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "jwt", "~> 1.4"
22
+ spec.add_dependency "rack", "~> 1.6"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.8"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.2"
27
+ spec.add_development_dependency "rack-test", "~> 0.6"
28
+ spec.add_development_dependency "sinatra", "~> 1.4"
29
+ spec.add_development_dependency "grape", "~> 0.11"
30
+ spec.add_development_dependency "json", "~> 1.7"
31
+ end
metadata ADDED
@@ -0,0 +1,182 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-indicium
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Twingly AB
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-31 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: '1.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
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.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.2'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rack-test
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.6'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.6'
97
+ - !ruby/object:Gem::Dependency
98
+ name: sinatra
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.4'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.4'
111
+ - !ruby/object:Gem::Dependency
112
+ name: grape
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.11'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.11'
125
+ - !ruby/object:Gem::Dependency
126
+ name: json
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1.7'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '1.7'
139
+ description: Rack middleware to decode JWT and append it to the rack environment
140
+ email:
141
+ - support@twingly.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rspec"
148
+ - ".ruby-version"
149
+ - ".travis.yml"
150
+ - Gemfile
151
+ - LICENSE.txt
152
+ - README.md
153
+ - Rakefile
154
+ - lib/rack/indicium.rb
155
+ - lib/rack/indicium/sentry.rb
156
+ - lib/rack/indicium/version.rb
157
+ - rack-indicium.gemspec
158
+ homepage: https://github.com/twingly/rack-indicium
159
+ licenses:
160
+ - MIT
161
+ metadata: {}
162
+ post_install_message:
163
+ rdoc_options: []
164
+ require_paths:
165
+ - lib
166
+ required_ruby_version: !ruby/object:Gem::Requirement
167
+ requirements:
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ version: '0'
171
+ required_rubygems_version: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ version: '0'
176
+ requirements: []
177
+ rubyforge_project:
178
+ rubygems_version: 2.4.5
179
+ signing_key:
180
+ specification_version: 4
181
+ summary: Rack JWT helpers
182
+ test_files: []