gris-middleware-json_web_token_decoder 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 59ad23037d6881c9aa162369342e9624ef1e3783
4
+ data.tar.gz: 72e40c5ff01b282948241f3be31caf885bd49c54
5
+ SHA512:
6
+ metadata.gz: 3b08ebe0410503f25c03769415274f2596c8d6dc83b2278683c5f32c48aa342657e75799057b11676407e636da9d6bac3e8c6f46241c1b886c85a9bff936d286
7
+ data.tar.gz: 5ed4efbecd1f567f50b9ebac464fa2dacb4f615b5bd5a3a23bb301cc764c4c2ac7f650ce62e9f6b59d3c08cf2525a30ab5e55ce33645222887d4796f996d30be
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --profile
3
+ --format documentation
@@ -0,0 +1 @@
1
+ inherit_from: .rubocop_todo.yml
@@ -0,0 +1,13 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2015-08-20 12:02:49 -0400 using RuboCop version 0.33.0.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 2
10
+ Style/Documentation:
11
+ Exclude:
12
+ - 'lib/gris/middleware/json_web_token_decoder.rb'
13
+ - 'lib/gris/middleware/json_web_token_decoder/version.rb'
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2015 Dylan Fareed
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,62 @@
1
+ # Gris::Middleware::JsonWebTokenDecoder
2
+
3
+ The cleverly named [Gris::Middleware::JsonWebTokenDecoder](https://github.com/dylanfareed/gris-middleware-json_web_token_decoder) is a simple middleware for decoding [JWT](http://jwt.io) token headers in your [Gris](https://github.com/artsy/gris) app's Grape endpoints.
4
+
5
+ It uses the Ruby [jwt](https://github.com/jwt/ruby-jwt) gem to do the heavy lifting.
6
+
7
+ Gris::Middleware::JsonWebTokenDecoder is alpha software and only presently supports HS256.
8
+
9
+ ---
10
+
11
+ ### Installation
12
+
13
+ Gris::Middleware::JsonWebTokenDecoder is [available as a gem on rubygems](https://rubygems.org/gems/gris-middleware-json_web_token_decoder), to install it run:
14
+
15
+ ```
16
+ gem install gris-middleware-json_web_token_decoder
17
+ ```
18
+
19
+ Otherwise, if your project uses [Bundler](http://bundler.io/), add gris-middleware-decoder to your Gemfile:
20
+
21
+ ```
22
+ gem 'gris-middleware-json_web_token_decoder'
23
+ ```
24
+
25
+ And run:
26
+
27
+ ```
28
+ $ bundle install
29
+ ```
30
+
31
+ ---
32
+
33
+ ### Usage
34
+
35
+ Set a value for `hmac_secret` in your Gris config/secrets.yml file.
36
+
37
+ ```
38
+ default: &default
39
+ service_name: smithson
40
+ hmac_secret: <%= ENV['HMAC_SECRET'] || 'https://www.youtube.com/watch?v=F5bAa6gFvLs' %>
41
+ base_url: <%= ENV['BASE_URL'] || 'http://localhost:9292' %>
42
+
43
+ development:
44
+ <<: *default
45
+
46
+ test:
47
+ <<: *default
48
+
49
+ production:
50
+ <<: *default
51
+ ```
52
+
53
+ Add the middleware to your endpoint:
54
+
55
+ ```ruby
56
+ class ApplicationEndpoint < Grape::API
57
+ use Gris::Middleware::JsonWebTokenDecoder
58
+ ```
59
+
60
+ For requests that include HTTP_AUTHORIZATION headers in the format of "Bearer: ENCODED_TOKEN", Gris::Middleware::JsonWebTokenDecoder will return a `GRIS_JWT_PAYLOAD` along with the `@env`.
61
+
62
+ You can choose how to make use of this in your applications.
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'rubocop/rake_task'
6
+
7
+ RSpec::Core::RakeTask.new
8
+
9
+ desc 'Run RuboCop'
10
+ RuboCop::RakeTask.new(:rubocop) do |task|
11
+ task.fail_on_error = true
12
+ task.options = %w(-D --auto-correct)
13
+ end
14
+
15
+ task default: [:rubocop, :spec]
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gris/middleware/json_web_token_decoder/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'gris-middleware-json_web_token_decoder'
8
+ spec.version = Gris::Middleware::JsonWebTokenDecoder::VERSION
9
+ spec.authors = ['Dylan Fareed']
10
+ spec.email = ['email@dylanfareed.com']
11
+ spec.summary = 'Simple middleware for decoding JWT tokens in Gris apps.'
12
+ spec.description = 'Simple middleware for decoding JWT tokens in Gris apps.'
13
+ spec.homepage = 'https://github.com/dylanfareed/gris-middleware-json_web_token_decoder'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'gris'
22
+ spec.add_dependency 'jwt'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.7'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rubocop'
27
+ spec.add_development_dependency 'rspec'
28
+ spec.add_development_dependency 'byebug'
29
+ end
@@ -0,0 +1,25 @@
1
+ require 'gris'
2
+ require 'gris/middleware/json_web_token_decoder/version'
3
+
4
+ module Gris
5
+ class Middleware
6
+ class JsonWebTokenDecoder
7
+ def initialize(app)
8
+ @app = app
9
+ end
10
+
11
+ def call(env)
12
+ if env['HTTP_AUTHORIZATION']
13
+ token = parse_header env['HTTP_AUTHORIZATION']
14
+ decoded_token = JWT.decode token, Gris.secrets.hmac_secret
15
+ env['GRIS_JWT_PAYLOAD'] = decoded_token
16
+ end
17
+ @app.call env
18
+ end
19
+
20
+ def parse_header(header)
21
+ header.split(' ').last
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,7 @@
1
+ module Gris
2
+ class Middleware
3
+ class JsonWebTokenDecoder
4
+ VERSION = '0.0.1'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe Gris::Middleware::JsonWebTokenDecoder do
4
+ let(:hmac_secret) { 'https://www.youtube.com/watch?v=F5bAa6gFvLs' }
5
+ subject { described_class.new(app) }
6
+
7
+ let(:token) do
8
+ JWT.encode payload_data, hmac_secret, 'HS256'
9
+ end
10
+
11
+ let(:payload_data) do
12
+ {
13
+ 'carl' => 'So its a code breaker?',
14
+ 'marty' => 'No its the code breaker.'
15
+ }
16
+ end
17
+
18
+ let(:app) { ->(env) { [200, env, env] } }
19
+
20
+ context 'with HTTP_AUTHORIZATION header' do
21
+ before do
22
+ Gris.secrets.hmac_secret = hmac_secret
23
+ end
24
+
25
+ it "decodes token and sets env['GRIS_JWT_PAYLOAD']" do
26
+ status, _, env = subject.call('HTTP_AUTHORIZATION' => "Bearer: #{token}")
27
+ expect(env['GRIS_JWT_PAYLOAD'].first).to eql payload_data
28
+ expect(status).to eq(200)
29
+ end
30
+ end
31
+
32
+ context 'without HTTP_AUTHORIZATION header' do
33
+ it "does not set env['GRIS_JWT_PAYLOAD']" do
34
+ status, _, env = subject.call({})
35
+ expect(env['GRIS_JWT_PAYLOAD']).to be_nil
36
+ expect(status).to eq(200)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ require 'byebug'
2
+ require 'gris/middleware/json_web_token_decoder'
3
+ require 'jwt'
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gris-middleware-json_web_token_decoder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dylan Fareed
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: gris
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: jwt
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
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
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: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Simple middleware for decoding JWT tokens in Gris apps.
112
+ email:
113
+ - email@dylanfareed.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".rubocop.yml"
121
+ - ".rubocop_todo.yml"
122
+ - Gemfile
123
+ - MIT-LICENSE
124
+ - README.md
125
+ - Rakefile
126
+ - gris-middleware-json_web_token_decoder.gemspec
127
+ - lib/gris/middleware/json_web_token_decoder.rb
128
+ - lib/gris/middleware/json_web_token_decoder/version.rb
129
+ - spec/lib/gris/middleware/json_web_token_decoder_spec.rb
130
+ - spec/spec_helper.rb
131
+ homepage: https://github.com/dylanfareed/gris-middleware-json_web_token_decoder
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.4.8
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: Simple middleware for decoding JWT tokens in Gris apps.
155
+ test_files:
156
+ - spec/lib/gris/middleware/json_web_token_decoder_spec.rb
157
+ - spec/spec_helper.rb