bona_fide 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f74be48b01fbc32e5c08c193c7963c6275ddcf95
4
+ data.tar.gz: 565c0042dec150887e41694211d37d1f1b3f726e
5
+ SHA512:
6
+ metadata.gz: 5a2fd4d5ee93481e31961b2f0a9c0a293e6d8149a30d6d612a78aedb2b4aaddba7e32e5c70d9df2ea10e5700a6315010d12e6ee170d57099aabfd8e734f441a8
7
+ data.tar.gz: 750d866dd36607929d5f5445598168cb06c9d6506bb01d7eedec8a57868529f542b4e0674bc38de425bb216c8dc835bd89dbf8e69e5242472f61a6d35f0672e7
@@ -0,0 +1,16 @@
1
+ *.rbc
2
+ *.sassc
3
+ .sass-cache
4
+ capybara-*.html
5
+ .rspec
6
+ /.bundle
7
+ /vendor/bundle
8
+ /log/*
9
+ /tmp/*
10
+ /db/*.sqlite3
11
+ /public/system/*
12
+ /coverage/
13
+ /spec/tmp/*
14
+ **.orig
15
+ rerun.txt
16
+ pickle-email-*.html
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rails', '>= 3.0'
6
+ gem 'rake', '~> 10'
7
+ gem 'rspec', '~> 2.14'
@@ -0,0 +1,103 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bona_fide (0.0.1.beta)
5
+ rack (>= 1.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionmailer (3.2.14)
11
+ actionpack (= 3.2.14)
12
+ mail (~> 2.5.4)
13
+ actionpack (3.2.14)
14
+ activemodel (= 3.2.14)
15
+ activesupport (= 3.2.14)
16
+ builder (~> 3.0.0)
17
+ erubis (~> 2.7.0)
18
+ journey (~> 1.0.4)
19
+ rack (~> 1.4.5)
20
+ rack-cache (~> 1.2)
21
+ rack-test (~> 0.6.1)
22
+ sprockets (~> 2.2.1)
23
+ activemodel (3.2.14)
24
+ activesupport (= 3.2.14)
25
+ builder (~> 3.0.0)
26
+ activerecord (3.2.14)
27
+ activemodel (= 3.2.14)
28
+ activesupport (= 3.2.14)
29
+ arel (~> 3.0.2)
30
+ tzinfo (~> 0.3.29)
31
+ activeresource (3.2.14)
32
+ activemodel (= 3.2.14)
33
+ activesupport (= 3.2.14)
34
+ activesupport (3.2.14)
35
+ i18n (~> 0.6, >= 0.6.4)
36
+ multi_json (~> 1.0)
37
+ arel (3.0.2)
38
+ builder (3.0.4)
39
+ diff-lcs (1.2.4)
40
+ erubis (2.7.0)
41
+ hike (1.2.3)
42
+ i18n (0.6.4)
43
+ journey (1.0.4)
44
+ json (1.8.0)
45
+ mail (2.5.4)
46
+ mime-types (~> 1.16)
47
+ treetop (~> 1.4.8)
48
+ mime-types (1.23)
49
+ multi_json (1.7.7)
50
+ polyglot (0.3.3)
51
+ rack (1.4.5)
52
+ rack-cache (1.2)
53
+ rack (>= 0.4)
54
+ rack-ssl (1.3.3)
55
+ rack
56
+ rack-test (0.6.2)
57
+ rack (>= 1.0)
58
+ rails (3.2.14)
59
+ actionmailer (= 3.2.14)
60
+ actionpack (= 3.2.14)
61
+ activerecord (= 3.2.14)
62
+ activeresource (= 3.2.14)
63
+ activesupport (= 3.2.14)
64
+ bundler (~> 1.0)
65
+ railties (= 3.2.14)
66
+ railties (3.2.14)
67
+ actionpack (= 3.2.14)
68
+ activesupport (= 3.2.14)
69
+ rack-ssl (~> 1.3.2)
70
+ rake (>= 0.8.7)
71
+ rdoc (~> 3.4)
72
+ thor (>= 0.14.6, < 2.0)
73
+ rake (10.1.0)
74
+ rdoc (3.12.2)
75
+ json (~> 1.4)
76
+ rspec (2.14.1)
77
+ rspec-core (~> 2.14.0)
78
+ rspec-expectations (~> 2.14.0)
79
+ rspec-mocks (~> 2.14.0)
80
+ rspec-core (2.14.4)
81
+ rspec-expectations (2.14.0)
82
+ diff-lcs (>= 1.1.3, < 2.0)
83
+ rspec-mocks (2.14.1)
84
+ sprockets (2.2.2)
85
+ hike (~> 1.2)
86
+ multi_json (~> 1.0)
87
+ rack (~> 1.0)
88
+ tilt (~> 1.1, != 1.3.0)
89
+ thor (0.18.1)
90
+ tilt (1.4.1)
91
+ treetop (1.4.14)
92
+ polyglot
93
+ polyglot (>= 0.3.1)
94
+ tzinfo (0.3.37)
95
+
96
+ PLATFORMS
97
+ ruby
98
+
99
+ DEPENDENCIES
100
+ bona_fide!
101
+ rails (>= 3.0)
102
+ rake (~> 10)
103
+ rspec (~> 2.14)
@@ -0,0 +1,57 @@
1
+ # BonaFide
2
+
3
+ [![Build Status](https://travis-ci.org/Tyr0/bona_fide.png?branch=master)](https://travis-ci.org/Tyr0/bona_fide)
4
+
5
+ Low level Rack authentication enforcement.
6
+
7
+ ## Features
8
+
9
+ BonaFide was built to bring advanced middleware functionality to all Rails apps. By providing the key functions to setup user state, limit access, and redirect others, BonaFide is a simple solution to a complex problem.
10
+
11
+ Essentially, BonaFide injects it's middleware just before Rails and it's routing are called, allowing it to read the current request's cookies, and call your designated model with the requested user state cookie. By setting user state before routing is called, BonaFide allows for more inteligent routing, as well as reduce request initialization code.
12
+
13
+ ## Installation
14
+
15
+ Install BonaFide gem
16
+
17
+ ```ruby
18
+ gem install bona_fide
19
+ ```
20
+
21
+ Configure BonaFide settings
22
+
23
+ Create initializer in `config/initializers/bona_fide.rb`:
24
+
25
+ ```ruby
26
+ BonaFide.configure do |config|
27
+ config.base_class = User
28
+ config.setter = :authenticate
29
+ config.cookie_name = "user_cookie"
30
+ end
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ BonaFide provides a helper for routing via `BonaFide::Constraint` which allows for calling methods on the configured base class. For example:
36
+
37
+ ```ruby
38
+ ExampleApp::Applicatoin.routes.draw do
39
+
40
+ # routes where User.admin? is true
41
+ constraints(BonaFide::Constraint.new(:admin?)) do
42
+ root to: "index#admin"
43
+ end
44
+
45
+ # routes where User.user? is true
46
+ constraints(BonaFide::Constraint.new(:user?)) do
47
+ root to: "index#user"
48
+ end
49
+
50
+ # fallback route
51
+ root to: "index#visitor"
52
+ end
53
+ ```
54
+
55
+ ## License
56
+
57
+ BonaFide is released under the [MIT License](http://www.opensource.org/licenses/MIT)
@@ -0,0 +1,12 @@
1
+ require 'rake'
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc "Default"
5
+ task :default => [:all]
6
+
7
+ desc 'Test the engine under all supported Rails versions'
8
+ task :all do |t|
9
+ exec 'rake spec'
10
+ end
11
+
12
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,22 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ require 'bona_fide/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ # human readable information
7
+ s.name = "bona_fide"
8
+ s.version = BonaFide.version
9
+ s.authors = ["Tyler Calderone"]
10
+ s.email = "bona_fide@tylercalderone.com"
11
+ s.homepage = "http://github.com/tyr0/bona_fide"
12
+ s.summary = "Low level Rack authentication enforcement"
13
+ # file requirements
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- spec/*`.split("\n")
16
+ s.extra_rdoc_files = %w(README.md)
17
+ s.rdoc_options = ["--charset=UTF-8"]
18
+ s.require_paths = ["lib"]
19
+ # gem requirements
20
+ s.required_ruby_version = Gem::Requirement.new('>= 1.9.2')
21
+ s.add_dependency("rack", ">= 1.0")
22
+ end
@@ -0,0 +1,25 @@
1
+ require 'bona_fide/configuration'
2
+ require 'bona_fide/constraint'
3
+ require 'bona_fide/version'
4
+ if defined?(Rails)
5
+ require 'bona_fide/engine'
6
+ end
7
+
8
+ module BonaFide
9
+ class << self
10
+ attr_accessor :config
11
+ end
12
+
13
+ # Configure BonaFide settings via a block
14
+ #
15
+ # BonaFide.configure do |config|
16
+ # config.base_class = User
17
+ # config.setter = :authenticate
18
+ # config.cookie_name = "user_cookie"
19
+ # end
20
+ #
21
+ def self.configure
22
+ @config ||= BonaFide::Configuration.new
23
+ yield config
24
+ end
25
+ end
@@ -0,0 +1,12 @@
1
+ module BonaFide
2
+ class Configuration
3
+ attr_accessor \
4
+ :base_class,
5
+ :cookie_name,
6
+ :setter
7
+
8
+ def call_setter(cookie) #:nodoc:
9
+ @base_class.send(@setter, cookie)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ module BonaFide
2
+ class Constraint
3
+ # Sets up a constraint for use with <tt>ActionDispatch::Routing</tt>. Pass
4
+ # in a method, +getter+, to be called on the specified +base_class+ for the
5
+ # current <tt>BonaFide</tt> session.
6
+ def initialize(getter)
7
+ @getter = getter
8
+ end
9
+
10
+ # Called from <tt>ActionDispatch::Routing</tt>, calls the requested +getter+ method
11
+ # on the <tt>BonaFide::Configuration</tt> +base_class+. The response should be
12
+ # either +true+ or +false+.
13
+ def matches?(request)
14
+ BonaFide.config.base_class.send(@getter)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ # This file ensures BonaFide is loaded as middleware on application initialization
2
+ # See: http://api.rubyonrails.org/classes/Rails/Engine.html
3
+ require 'bona_fide/session'
4
+
5
+ module BonaFide
6
+ # This is a subclass of <tt>Rails::Engine</tt> so that it gets
7
+ # called on startup of <tt>Rails</tt> applications, although
8
+ # the basic setup for <tt>BonaFide::Configuration</tt> is
9
+ # still required.
10
+ class Engine < Rails::Engine
11
+ initializer "bona_fide.initializer" do |app|
12
+ app.middleware.use BonaFide::Session
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,35 @@
1
+ module BonaFide
2
+ class Session
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ # Called on every web request,
8
+ def call(env)
9
+ @env = env
10
+ # reset cookies on each request
11
+ @cookies = nil
12
+
13
+ if cookie = get_cookie
14
+ BonaFide.config.call_setter(cookie)
15
+ end
16
+
17
+ @app.call(env)
18
+ end
19
+
20
+ private
21
+
22
+ def cookies
23
+ @cookies ||= @env["action_dispatch.cookies"] || Rack::Request.new(@env).cookies
24
+ end
25
+
26
+ def get_cookie
27
+ return if cookies.nil?
28
+
29
+ cookie_name = BonaFide.config.cookie_name
30
+ return if cookie_name.nil?
31
+
32
+ cookies[cookie_name]
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,10 @@
1
+ module BonaFide
2
+ def self.version
3
+ Gem::Version.new("0.1.0")
4
+ end
5
+
6
+ module VERSION
7
+ MAJOR, MINOR, TINY, PRE = BonaFide.version.segments
8
+ STRING = BonaFide.version.to_s
9
+ end
10
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe "BonaFide::Configuration" do
4
+ before(:all) { require 'bona_fide' }
5
+
6
+ let (:config) { BonaFide::Configuration.new }
7
+
8
+ it "should allow read/write of :base_class" do
9
+ config.should respond_to(:base_class)
10
+ config.should respond_to(:base_class=)
11
+ end
12
+
13
+ it "should allow read/write of :cookie_name" do
14
+ config.should respond_to(:cookie_name)
15
+ config.should respond_to(:cookie_name=)
16
+ end
17
+
18
+ it "should allow read/write of :setter" do
19
+ config.should respond_to(:setter)
20
+ config.should respond_to(:setter=)
21
+ end
22
+
23
+ it "should reset on initialize" do
24
+ config.base_class.should eq(nil)
25
+ config.cookie_name.should eq(nil)
26
+ config.setter.should eq(nil)
27
+ end
28
+
29
+ describe "#call_base" do
30
+ it "should respond to :call_setter" do
31
+ config.should respond_to(:call_setter)
32
+ end
33
+
34
+ it "should call :setter on :base_class" do
35
+ config.base_class = BonaFideHelper::User
36
+ config.setter = :authenticate
37
+
38
+ config.send(:call_setter, BonaFideHelper::User::TEST_TOKEN).should eq(true)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe "BonaFide::Constraint" do
4
+ before(:all) { require 'bona_fide' }
5
+
6
+ before(:all) {
7
+ intialize_default_configuration
8
+ }
9
+
10
+ it "should initialize with getter" do
11
+ expect { BonaFide::Constraint.new(:getter) }.not_to raise_error
12
+ end
13
+
14
+ it "should return true for true" do
15
+ constraint = BonaFide::Constraint.new(:true_method)
16
+ constraint.matches?(nil).should be(true)
17
+ end
18
+
19
+ it "should return false for false" do
20
+ constraint = BonaFide::Constraint.new(:false_method)
21
+ constraint.matches?(nil).should be(false)
22
+ end
23
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe "BonaFide::Engine" do
4
+ it "should be valid" do
5
+ expect { BonaFide::Engine }.not_to raise_error
6
+ end
7
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe "BonaFide::Session" do
4
+ before(:all) { BonaFideHelper::Application.initialize! }
5
+
6
+ it "should be valid" do
7
+ expect { BonaFide::Session.new(nil) }.not_to raise_error
8
+ end
9
+
10
+ it "should intialize fine" do
11
+ session = BonaFide::Session.new(BonaFideHelper::Application)
12
+ session.call(Rack::MockRequest.env_for("/"))
13
+ end
14
+
15
+ it "should read requested cookie" do
16
+ session = BonaFide::Session.new(BonaFideHelper::Application)
17
+
18
+ # setup mock request
19
+ env = Rack::MockRequest.env_for("/")
20
+ # add cookie to request
21
+ env["action_dispatch.cookies"] = {
22
+ BonaFideHelper::User::COOKIE_NAME => "session_spec_test"
23
+ }
24
+
25
+ # make sure it raises the proper error (better way to check?)
26
+ expect { session.call(env) }.to raise_error(StandardError, "RAISE_FOR_SUCCESS")
27
+ end
28
+ end
@@ -0,0 +1,30 @@
1
+ module BonaFideHelper
2
+ class User
3
+ COOKIE_NAME = "user_cookie"
4
+ TEST_TOKEN = "test_token"
5
+
6
+ def self.authenticate(token)
7
+ case token
8
+ when BonaFideHelper::User::TEST_TOKEN.to_s
9
+ return true
10
+ when "session_spec_test"
11
+ raise StandardError, "RAISE_FOR_SUCCESS"
12
+ end
13
+
14
+ false
15
+ end
16
+
17
+ def self.true_method
18
+ true
19
+ end
20
+
21
+ def self.false_method
22
+ false
23
+ end
24
+ end
25
+
26
+ class Application < Rails::Application
27
+ config.active_support.deprecation = :stderr
28
+ config.threadsafe!
29
+ end
30
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper.rb'
2
+
3
+ describe "BonaFide" do
4
+ it "should load without errors" do
5
+ expect { require 'bona_fide' }.not_to raise_error
6
+ end
7
+
8
+ it "should be valid" do
9
+ BonaFide.should be_a(Module)
10
+ end
11
+
12
+ it "should be configurable" do
13
+ intialize_default_configuration
14
+
15
+ BonaFide.config.base_class.should eq(BonaFideHelper::User)
16
+ BonaFide.config.setter.should eq(:authenticate)
17
+ BonaFide.config.cookie_name.should eq(BonaFideHelper::User::COOKIE_NAME)
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ require 'rspec/expectations'
2
+ require 'rails'
3
+ require 'bona_fide_helper'
4
+
5
+ RSpec.configure do |config|
6
+ config.treat_symbols_as_metadata_keys_with_true_values = true
7
+ config.include RSpec::Matchers
8
+ config.include BonaFideHelper
9
+ end
10
+
11
+ def intialize_default_configuration
12
+ BonaFide.configure do |config|
13
+ config.base_class = BonaFideHelper::User
14
+ config.setter = :authenticate
15
+ config.cookie_name = BonaFideHelper::User::COOKIE_NAME
16
+ end
17
+ end
18
+
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bona_fide
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tyler Calderone
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2013-07-28 00:00:00 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ prerelease: false
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: "1.0"
22
+ type: :runtime
23
+ version_requirements: *id001
24
+ description:
25
+ email: bona_fide@tylercalderone.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README.md
32
+ files:
33
+ - .gitignore
34
+ - .travis.yml
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - README.md
38
+ - Rakefile
39
+ - bona_fide.gemspec
40
+ - lib/bona_fide.rb
41
+ - lib/bona_fide/configuration.rb
42
+ - lib/bona_fide/constraint.rb
43
+ - lib/bona_fide/engine.rb
44
+ - lib/bona_fide/session.rb
45
+ - lib/bona_fide/version.rb
46
+ - spec/bona_fide/configuraton_spec.rb
47
+ - spec/bona_fide/constraints_spec.rb
48
+ - spec/bona_fide/engine_spec.rb
49
+ - spec/bona_fide/session_spec.rb
50
+ - spec/bona_fide_helper.rb
51
+ - spec/bona_fide_spec.rb
52
+ - spec/spec_helper.rb
53
+ homepage: http://github.com/tyr0/bona_fide
54
+ licenses: []
55
+
56
+ metadata: {}
57
+
58
+ post_install_message:
59
+ rdoc_options:
60
+ - --charset=UTF-8
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 1.9.2
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ requirements: []
74
+
75
+ rubyforge_project:
76
+ rubygems_version: 2.0.6
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Low level Rack authentication enforcement
80
+ test_files:
81
+ - spec/bona_fide/configuraton_spec.rb
82
+ - spec/bona_fide/constraints_spec.rb
83
+ - spec/bona_fide/engine_spec.rb
84
+ - spec/bona_fide/session_spec.rb
85
+ - spec/bona_fide_helper.rb
86
+ - spec/bona_fide_spec.rb
87
+ - spec/spec_helper.rb