rack-jwt 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e20bf24155aaeda649c643bde660ed334b5e081d
4
+ data.tar.gz: 125bff003cd0fc9f3d2ec2feb7fd7d1fa01438bc
5
+ SHA512:
6
+ metadata.gz: 8ebece61f89d84ae0b61e82a3631a556a09597bbc2e6265eb9beacc6ef958859ceb2f7ab6bcdabf407ee36e97160396ce7db80da7791d611860329560f5c77b7
7
+ data.tar.gz: 85107621925d5e21c2a8d7d275b429622155a596a49cad78520dcb93f80ba7104ac6e84f4a627095fbb685f74eee28680fc242f7ed91c883c76faa937a69d7b5
data/.gitignore ADDED
@@ -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 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-jwt.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Mr. Eigenbart
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # Rack::Jwt
2
+
3
+ This gem provides JSON Web Token (JWT) based authentication.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rack-jwt'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rack-jwt
20
+
21
+ ## Usage
22
+
23
+ ### sinatra
24
+
25
+ ```
26
+ use Rack::JWT::Auth secret: 'you_secret_token_goes_here', exclude: ['/api/docs']
27
+ ```
28
+
29
+ ### Rails
30
+
31
+ ```
32
+ Rails.application.config.middleware.use, Rack::JWT::Auth, secret: Rails.application.secrets.secret_key_base, exclude: ['/api/docs']
33
+ ```
34
+
35
+ ## Generating tokens
36
+ You can generate JSON Wen Tokens for your users using the `Token#encode` method
37
+
38
+ ```
39
+ Rack::JWT::Token.encode(payload, secret)
40
+ ```
41
+
42
+ ## Contributing
43
+
44
+ 1. Fork it ( https://github.com/[my-github-username]/rack-jwt/fork )
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
47
+ 4. Push to the branch (`git push origin my-new-feature`)
48
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,67 @@
1
+ require 'jwt'
2
+
3
+ module Rack
4
+ module JWT
5
+ class Auth
6
+ def initialize(app, opts = {})
7
+ @app = app
8
+ @secret = opts.fetch(:secret)
9
+ @exclude = opts.fetch(:exclude, [])
10
+ end
11
+
12
+ def call(env)
13
+ if @exclude.include? env["PATH_INFO"]
14
+ @app.call(env)
15
+ elsif env["HTTP_AUTHORIZATION"]
16
+ begin
17
+ if env["HTTP_AUTHORIZATION"].split(" ").first != 'Bearer'
18
+ invalid_auth_header
19
+ else
20
+ token = env["HTTP_AUTHORIZATION"].split(" ")[-1]
21
+ decoded_token = Token.decode(token, @secret)
22
+ env["jwt.header"] = decoded_token.last
23
+ env["jwt.payload"] = decoded_token.first
24
+ @app.call(env)
25
+ end
26
+ rescue
27
+ unauthorized
28
+ end
29
+ else
30
+ no_auth_header
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def unauthorized
37
+ body = { error: "Invalid JWT token" }.to_json
38
+ headers = {
39
+ 'Content-Type' => 'application/json',
40
+ 'Content-Length' => body.bytesize.to_s
41
+ }
42
+
43
+ return [401, headers, [body]]
44
+ end
45
+
46
+ def no_auth_header
47
+ body = { error: "Missing Authorization header" }.to_json
48
+ headers = {
49
+ 'Content-Type' => 'application/json',
50
+ 'Content-Length' => body.bytesize.to_s
51
+ }
52
+
53
+ return [401, headers, [body]]
54
+ end
55
+
56
+ def invalid_auth_header
57
+ body = { error: "Invalid Authorization header format" }.to_json
58
+ headers = {
59
+ 'Content-Type' => 'application/json',
60
+ 'Content-Length' => body.bytesize.to_s
61
+ }
62
+
63
+ return [401, headers, [body]]
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,16 @@
1
+ module Rack
2
+ module JWT
3
+ class Token
4
+ def self.encode(payload, secret)
5
+ ::JWT.encode(payload, secret)
6
+ end
7
+
8
+ def self.decode(token, secret)
9
+ ::JWT.decode(token, secret)
10
+ rescue
11
+ # It will raise an error if it is not a valid token due to any reason
12
+ nil
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ module Jwt
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
data/lib/rack/jwt.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "rack/jwt/version"
2
+
3
+ module Rack
4
+ module JWT
5
+ autoload :Auth, 'rack/jwt/auth'
6
+ autoload :Token, 'rack/jwt/token'
7
+ end
8
+ end
data/rack-jwt.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/jwt/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-jwt"
8
+ spec.version = Rack::Jwt::VERSION
9
+ spec.authors = ["Mr. Eigenbart"]
10
+ spec.email = ["eigenbart@gmail.com"]
11
+ spec.summary = %q{Rack middleware that provides authentication based on JSON Web Tokens.}
12
+ spec.description = %q{Rack middleware that provides authentication based on JSON Web Tokens.}
13
+ spec.homepage = ""
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_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency 'rack-test', '~> 0.6.3'
24
+ spec.add_development_dependency 'rspec', '~> 3.2.0'
25
+
26
+ spec.add_runtime_dependency 'rack', '>= 1.6.0'
27
+ spec.add_runtime_dependency 'jwt', '~> 1.2.1'
28
+ end
data/spec/auth_spec.rb ADDED
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::JWT::Auth do
4
+ include Rack::Test::Methods
5
+
6
+ let(:issuer) { Rack::JWT::Token }
7
+ let(:secret) { 'foo' }
8
+
9
+ let(:app) do
10
+ main_app = lambda { |env| [200, env, ['Hello']] }
11
+ Rack::JWT::Auth.new(main_app, {secret: secret})
12
+ end
13
+
14
+ it 'raises an exception if no secret if provided' do
15
+ expect{ Rack::JWT::Auth.new(main_app, {}) }.to raise_error
16
+ end
17
+
18
+ it 'returns 200 ok if the request is authenticated' do
19
+ token = issuer.encode({ iss: 1 }, secret)
20
+ get('/', {}, {'HTTP_AUTHORIZATION' => 'Bearer #{token}'})
21
+
22
+ expect(last_response.status).to eq 200
23
+ expect(last_response.body).to eq 'Hello'
24
+
25
+ payload = last_response.header['jwt.payload']
26
+
27
+ expect(payload['iss']).to eql(1)
28
+ end
29
+
30
+ it 'returns 401 if the authorization header is missing' do
31
+ get('/')
32
+
33
+ jsonResponse = JSON.parse(last_response.body)
34
+
35
+ expect(last_response.status).to eql(401)
36
+ expect(jsonResponse['error']).to eql('Missing Authorization header')
37
+ end
38
+
39
+ it 'returns 401 if the authorization header signature is invalid' do
40
+ token = issuer.encode({ iss: 1 }, 'invalid secret')
41
+ get('/', {}, {'HTTP_AUTHORIZATION' => 'Bearer #{token}'})
42
+
43
+ jsonResponse = JSON.parse(last_response.body)
44
+
45
+ expect(last_response.status).to eql(401)
46
+ expect(jsonResponse['error']).to eql('Invalid JWT token')
47
+ end
48
+
49
+ it 'returns 401 if the header format is not Authorization: Bearer [token]' do
50
+ token = issuer.encode({ iss: 1 }, secret)
51
+ get('/', {}, {'HTTP_AUTHORIZATION' => '#{token}'})
52
+
53
+ jsonResponse = JSON.parse(last_response.body)
54
+
55
+ expect(last_response.status).to eql(401)
56
+ expect(jsonResponse['error']).to eql('Invalid Authorization header format')
57
+ end
58
+
59
+ it 'returns 401 if authorization scheme is not Bearer' do
60
+ token = issuer.encode({ iss: 1 }, secret)
61
+ get('/', {}, {'HTTP_AUTHORIZATION' => 'WrongScheme #{token}'})
62
+
63
+ jsonResponse = JSON.parse(last_response.body)
64
+
65
+ expect(last_response.status).to eql(401)
66
+ expect(jsonResponse['error']).to eql('Invalid Authorization header format')
67
+ end
68
+ end
@@ -0,0 +1,2 @@
1
+ require "rack/test"
2
+ require "rack/jwt"
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-jwt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mr. Eigenbart
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack-test
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.6.3
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.6.3
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.2.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.2.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 1.6.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 1.6.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: jwt
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.2.1
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.2.1
97
+ description: Rack middleware that provides authentication based on JSON Web Tokens.
98
+ email:
99
+ - eigenbart@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - lib/rack/jwt.rb
111
+ - lib/rack/jwt/auth.rb
112
+ - lib/rack/jwt/token.rb
113
+ - lib/rack/jwt/version.rb
114
+ - rack-jwt.gemspec
115
+ - spec/auth_spec.rb
116
+ - spec/spec_helper.rb
117
+ homepage: ''
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.4.5
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Rack middleware that provides authentication based on JSON Web Tokens.
141
+ test_files:
142
+ - spec/auth_spec.rb
143
+ - spec/spec_helper.rb