fernet-rack 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: a4441c05c680dd602dffd66e4c9fcaff3a07bfb2
4
+ data.tar.gz: aa631be49fd57313984520b4c839a1f4bf7f3a98
5
+ SHA512:
6
+ metadata.gz: 380ec72be8d743a6e8c8a7d09bf606c5a1318f9f2b55dd187986282dd2df80fac25f866ecb8da6a9e09bfed844f28c9535cffec7dd97a1ddb73c7e23a161a3fc
7
+ data.tar.gz: ba5833842c0d17acf19a7cf25c1a824b489f701da107021a3544622a5e3952f77f66f0904810afa015bfdf5f20ab9c4b3fa6814597564e0592c75a082a746e03
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.gem
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Timothée Peignier
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/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["Timothée Peignier"]
5
+ gem.email = ["timothee.peignier@tryphon.org"]
6
+ gem.description = %q{Fernet rack authentication middleware}
7
+ gem.summary = %q{Easily authenticate }
8
+ gem.homepage = "http://rubygems.org/gems/fernet-rack"
9
+ gem.license = 'MIT'
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "fernet-rack"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = '0.1'
17
+
18
+ gem.add_runtime_dependency "fernet", '~> 2.0'
19
+ gem.add_development_dependency "minitest", '~> 5.4'
20
+ end
@@ -0,0 +1,21 @@
1
+ require 'rack'
2
+ require 'fernet'
3
+
4
+
5
+ module Rack
6
+ module Auth
7
+ class Fernet < Rack::Auth::Basic
8
+ def initialize(app, secret, realm=nil)
9
+ @app = app
10
+ @realm = realm
11
+ @secret = secret
12
+ end
13
+
14
+ private
15
+ def valid?(auth)
16
+ verifier = ::Fernet.verifier(@secret, auth.credentials.last)
17
+ verifier.valid?
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,53 @@
1
+ require 'minitest/autorun'
2
+ require 'rack/fernet'
3
+ require 'rack/lint'
4
+ require 'rack/mock'
5
+
6
+
7
+ class FernetTest < Minitest::Test
8
+ def setup
9
+ unprotected_app = Rack::Lint.new(lambda do |env|
10
+ [ 200, {'Content-Type' => 'text/plain'}, ["Hello"] ]
11
+ end)
12
+ @realm = "Lillet"
13
+ @secret = "SqD5Mz/qFnXPLVTvkQKRDyVpli3Q6/habc7i89IrBRA="
14
+ @app = Rack::Auth::Fernet.new(unprotected_app, @secret, @realm)
15
+ @request = Rack::MockRequest.new(@app)
16
+ end
17
+
18
+ def test_no_credentials
19
+ request do |response|
20
+ assert_basic_auth_challenge(response)
21
+ end
22
+ end
23
+
24
+ def test_wrong_credentials
25
+ request_with_auth('token') do |response|
26
+ assert_basic_auth_challenge(response)
27
+ end
28
+ end
29
+
30
+ def test_correct_credentials
31
+ token = Fernet.generate(@secret, 'Podensac')
32
+ request_with_auth(token) do |response|
33
+ assert_equal(response.status, 200)
34
+ assert_equal(response.body, "Hello")
35
+ end
36
+ end
37
+
38
+ private
39
+ def request(headers={})
40
+ yield @request.get('/', headers)
41
+ end
42
+
43
+ def request_with_auth(token, &block)
44
+ request('HTTP_AUTHORIZATION' => 'Basic ' + [":#{token}"].pack("m*"), &block)
45
+ end
46
+
47
+ def assert_basic_auth_challenge(response)
48
+ assert_equal(response.status, 401)
49
+ assert_includes(response, 'WWW-Authenticate')
50
+ assert(response.headers['WWW-Authenticate'] =~ /Basic realm="#{Regexp.escape(@realm)}"/)
51
+ assert_empty(response.body)
52
+ end
53
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fernet-rack
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Timothée Peignier
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fernet
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.4'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.4'
41
+ description: Fernet rack authentication middleware
42
+ email:
43
+ - timothee.peignier@tryphon.org
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - LICENSE
50
+ - Rakefile
51
+ - fernet-rack.gemspec
52
+ - lib/rack/fernet.rb
53
+ - test/test_fernet.rb
54
+ homepage: http://rubygems.org/gems/fernet-rack
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.2.2
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Easily authenticate
78
+ test_files:
79
+ - test/test_fernet.rb
80
+ has_rdoc: