rack-jwt-auth 0.0.1

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: 622acddc8ea69970df80b8b5ce4aad3e48457a7d
4
+ data.tar.gz: f35222e0d2c48f0afdc4f90caef9f990e2a91762
5
+ SHA512:
6
+ metadata.gz: c193f94500ff74c1ad274da96acc82b67b65347d7addade4d25734f56a6a0dca21377ff2353e174abb017a1954e417fa7e692875ee24af196e8d574dc6a59542
7
+ data.tar.gz: a1e86e551429e6592e05a64c5c6acecd59773834d1a03059c18c1dc538915c359adda29b3cfa954e0b4975457a1c488f7bf1499cd912becfedf645e6e3d46499
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 João Almeida
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,29 @@
1
+ # Rack::Jwt::Auth
2
+
3
+ Rack jwt authentication middleware
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rack-jwt-auth'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rack-jwt-auth
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/rack-jwt-auth/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,23 @@
1
+ module Rack
2
+ module Jwt
3
+ module Auth
4
+
5
+ module AuthToken
6
+
7
+ def self.issue_token(payload, secret)
8
+ JWT.encode(payload, secret)
9
+ end
10
+
11
+ def self.valid?(token, secret)
12
+ begin
13
+ JWT.decode(token, secret)
14
+ rescue
15
+ false
16
+ end
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,77 @@
1
+ module Rack
2
+ module Jwt
3
+ module Auth
4
+
5
+ class Authenticate
6
+
7
+ def initialize(app, opts = {})
8
+ @app = app
9
+ @opts = opts
10
+
11
+ raise 'Secret must be provided' if opts[:secret].nil?
12
+
13
+ @secret = opts[:secret]
14
+ @unauthenticated_routes = compile_paths(opts[:except])
15
+ end
16
+
17
+ def call(env)
18
+ with_authorization(env) do |payload|
19
+ env['rack.jwt.session'] = payload
20
+ @app.call(env)
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def authenticated_route?(env)
27
+ !@unauthenticated_routes.find { |route| route =~ env['PATH_INFO']}
28
+ end
29
+
30
+ def with_authorization(env)
31
+ if authenticated_route?(env)
32
+ header = env['HTTP_AUTHORIZATION']
33
+
34
+ return [401, {}, ['Missing Authorization header']] if header.nil?
35
+
36
+ payload = AuthToken.valid?(header, @secret)
37
+
38
+ return [401, {}, ['Invalid Authorization']] unless payload
39
+ end
40
+
41
+ yield payload
42
+ end
43
+
44
+ def compile_paths(paths)
45
+ return [] if paths.nil?
46
+
47
+ paths.map do |path|
48
+ compile(path)
49
+ end
50
+ end
51
+
52
+ def compile(path)
53
+ if path.respond_to? :to_str
54
+ special_chars = %w{. + ( )}
55
+ pattern =
56
+ path.to_str.gsub(/((:\w+)|[\*#{special_chars.join}])/) do |match|
57
+ case match
58
+ when "*"
59
+ "(.*?)"
60
+ when *special_chars
61
+ Regexp.escape(match)
62
+ else
63
+ "([^/?&#]+)"
64
+ end
65
+ end
66
+ /^#{pattern}$/
67
+ elsif path.respond_to? :match
68
+ path
69
+ else
70
+ raise TypeError, path
71
+ end
72
+ end
73
+ end
74
+
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,7 @@
1
+ module Rack
2
+ module Jwt
3
+ module Auth
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ require "rack/jwt/auth/version"
2
+ require "rack/jwt/auth/auth_token"
3
+ require "rack/jwt/auth/authenticate"
@@ -0,0 +1,27 @@
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/auth/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-jwt-auth"
8
+ spec.version = Rack::Jwt::Auth::VERSION
9
+ spec.authors = ["João Almeida"]
10
+ spec.email = ["jg.almeida56@gmail.com"]
11
+ spec.summary = %q{Rack jwt auth middleware}
12
+ spec.description = %q{Rack jwt auth middleware}
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_dependency "jwt", "~> 1.0"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake", "~> 10.3"
25
+ spec.add_development_dependency "rspec", "~> 3.1"
26
+ spec.add_development_dependency "rack-test", "~> 0.6"
27
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::Jwt::Auth::AuthToken do
4
+
5
+ let(:secret) { 'supertestsecret' }
6
+ let(:data) { { user_id: 1, username: 'test' } }
7
+
8
+ describe '.issue_token' do
9
+
10
+ it 'issues a token' do
11
+ token = subject.issue_token(data, secret)
12
+
13
+ expect(token).to be
14
+ end
15
+
16
+ end
17
+
18
+ describe '.valid?' do
19
+
20
+ it 'checks if the provided token is valid' do
21
+ token = subject.issue_token(data, secret)
22
+ payload = subject.valid?(token, secret)
23
+
24
+ meta, data = payload
25
+
26
+ expect(payload).to be
27
+ expect(data['user_id']).to eql(data[:user_id])
28
+ expect(data['username']).to eql(data[:username])
29
+ end
30
+
31
+ it 'checks if the provided token is invalid when decoded with other secret' do
32
+ token = subject.issue_token(data, secret)
33
+ payload = subject.valid?(token, 'secret')
34
+
35
+ meta, data = payload
36
+
37
+ expect(payload).not_to be
38
+ end
39
+
40
+ end
41
+
42
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::Jwt::Auth::Authenticate do
4
+ include Rack::Test::Methods
5
+
6
+ let(:issuer) { Rack::Jwt::Auth::AuthToken }
7
+
8
+ let(:app) do
9
+ main_app = lambda { |env| [200, env, ['Hello']] }
10
+ Rack::Jwt::Auth::Authenticate.new(main_app, {except: ['/not_authenticated', '/not_authenticated/*'], secret: 'supertestsecret'})
11
+ end
12
+
13
+ it 'returns 200 ok if the request is authenticated' do
14
+ token = issuer.issue_token({user_id: 1, username: 'test'}, 'supertestsecret')
15
+ get('/', {}, {'HTTP_AUTHORIZATION' => token})
16
+
17
+ expect(last_response.status).to eql(200)
18
+ expect(last_response.body).to eql('Hello')
19
+
20
+ session = last_response.header['rack.jwt.session'][0]
21
+
22
+ expect(session['user_id']).to eql(1)
23
+ expect(session['username']).to eql('test')
24
+ end
25
+
26
+ it 'raises an exception if no secret if provided' do
27
+ token = issuer.issue_token({user_id: 1, username: 'test'}, 'supertestsecret')
28
+ get('/', {}, {'HTTP_AUTHORIZATION' => token})
29
+
30
+ expect(last_response.status).to eql(200)
31
+ expect(last_response.body).to eql('Hello')
32
+
33
+ session = last_response.header['rack.jwt.session'][0]
34
+
35
+ expect(session['user_id']).to eql(1)
36
+ expect(session['username']).to eql('test')
37
+ end
38
+
39
+ it 'returns 401 if the authorization header is missing' do
40
+ get('/')
41
+
42
+ expect(last_response.status).to eql(401)
43
+ expect(last_response.body).to eql('Missing Authorization header')
44
+ end
45
+
46
+ it 'returns 401 if the authorization header signature is invalid' do
47
+ token = issuer.issue_token({user_id: 1}, 'invalid_secret')
48
+ get('/', {}, {'HTTP_AUTHORIZATION' => token})
49
+
50
+ expect(last_response.status).to eql(401)
51
+ expect(last_response.body).to eql('Invalid Authorization')
52
+ end
53
+
54
+ it 'returns 200 ok if the request is for a route that is not authorized' do
55
+ get('/not_authenticated')
56
+
57
+ expect(last_response.status).to eql(200)
58
+ expect(last_response.body).to eql('Hello')
59
+
60
+ get('/not_authenticated/other')
61
+
62
+ expect(last_response.status).to eql(200)
63
+ expect(last_response.body).to eql('Hello')
64
+
65
+ get('/not_authenticated/other/test')
66
+
67
+ expect(last_response.status).to eql(200)
68
+ expect(last_response.body).to eql('Hello')
69
+ end
70
+
71
+ end
@@ -0,0 +1,4 @@
1
+ require 'jwt'
2
+ require 'rack/test'
3
+ require 'rack/jwt/auth/auth_token'
4
+ require 'rack/jwt/auth/authenticate'
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-jwt-auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - João Almeida
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-16 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.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.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.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack-test
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.6'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.6'
83
+ description: Rack jwt auth middleware
84
+ email:
85
+ - jg.almeida56@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - lib/rack/jwt/auth.rb
96
+ - lib/rack/jwt/auth/auth_token.rb
97
+ - lib/rack/jwt/auth/authenticate.rb
98
+ - lib/rack/jwt/auth/version.rb
99
+ - rack-jwt-auth.gemspec
100
+ - spec/auth_token_spec.rb
101
+ - spec/authenticate_spec.rb
102
+ - spec/spec_helper.rb
103
+ homepage: ''
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.4.2
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Rack jwt auth middleware
127
+ test_files:
128
+ - spec/auth_token_spec.rb
129
+ - spec/authenticate_spec.rb
130
+ - spec/spec_helper.rb