password_required 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0d345fbf7213d55b231133b6228475d4bfa7c88e
4
+ data.tar.gz: ed2e5b82afef68715535586a238c7a5d72fa664a
5
+ SHA512:
6
+ metadata.gz: 9f69827443b45de22bc5ac19c01eeac6e856d5847e629719356567d13e37a4fefc44b2806d327cc3089deb8f8ee6d403db65b990ff0322bfa582ac464fe65631
7
+ data.tar.gz: 06e99222096126065ed1391589dbcc933563a3e4203b794a656912884451264168337577be8878c4fa8dbcb68a13ea99084690989efc99c8d0dcc62ac9b79f9b
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 Benjamin Falk
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.
data/README.md ADDED
@@ -0,0 +1,75 @@
1
+ ## PasswordRequired
2
+
3
+ [![GitHub version](https://badge.fury.io/gh/benfalk%2Fpassword_required.svg)](http://badge.fury.io/gh/benfalk%2Fpassword_required)
4
+ [![Build Status](https://travis-ci.org/benfalk/password_required.svg?branch=master)](https://travis-ci.org/benfalk/password_required)
5
+ [![Code Climate](https://codeclimate.com/github/benfalk/password_required/badges/gpa.svg)](https://codeclimate.com/github/benfalk/password_required)
6
+ [![Coverage Status](https://img.shields.io/coveralls/benfalk/password_required.svg)](https://coveralls.io/r/benfalk/password_required)
7
+ [![Dependency Status](https://gemnasium.com/benfalk/password_required.svg)](https://gemnasium.com/benfalk/password_required)
8
+ [![Inline docs](http://inch-ci.org/github/benfalk/password_required.svg?branch=master)](http://inch-ci.org/github/benfalk/password_required)
9
+
10
+ ### About
11
+
12
+ Used to password protect sensitive actions. This was inspired by the need to
13
+ follow the same pattern Github uses when adding a new key to your account. The
14
+ goal of PasswordRequired is to make this pattern easy and flexible without
15
+ requiring additional rails libraries.
16
+
17
+ ### Usage Example
18
+
19
+ ```ruby
20
+ # In your gemfile
21
+ gem 'password_required'
22
+ ```
23
+
24
+ ```ruby
25
+ # In your controller
26
+ class WidgetsController < ApplicationController
27
+ include PasswordRequired::ControllerConcern
28
+
29
+ password_required for: [:create, :update, :destroy],
30
+ with: ->(password) { password == 'roflcopters' },
31
+ if: :request_ip_untrusted?
32
+
33
+ # ...
34
+ end
35
+ ```
36
+
37
+ ### password_required options
38
+
39
+ * `for:` (Required) An array of methods you want to protect
40
+
41
+ * `with:` (Optional) lambda that receives the password given OR a symbol of a
42
+ method to call. If either returns a truthy result the action will be allowed.
43
+ You may optionally define a method `password_correct?` that will be used for
44
+ all password protected actions.
45
+
46
+ * `if:` (Optional) lambda or method name that determines if a request needs to
47
+ be password protected. Always true by default. Useful if there are some times
48
+ you do not need to prompt for a password. You optionally define a method
49
+ `password_required?` on the controller that will be called for all password
50
+ protected actions.
51
+
52
+ ### Current Limitations and Issues
53
+
54
+ * Only POST type actions are supported DELETE, POST, PUT
55
+ * Only designed and tested with rails 4.1
56
+
57
+ ### FAQ
58
+
59
+ Q: "What if I don't like the idea of magical callbacks?"
60
+
61
+ A: No problem, you'll need to define the following methods in your controller
62
+
63
+ * `password_correct?` (hint) `password_given` is the password from the request
64
+ * `password_required?` (optional) always true by default
65
+
66
+ In the controller actions you want to password protect: `guard_with_password!`
67
+
68
+ ```ruby
69
+ def destroy
70
+ guard_with_password!
71
+ # ...
72
+ end
73
+ ```
74
+
75
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'PasswordRequired'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path('../spec/dummy/Rakefile', __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
22
+ task default: ['app:db:migrate', 'app:spec', 'app:cucumber', 'app:rubocop']
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module PasswordRequired
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module PasswordRequired
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>PasswordRequired</title>
5
+ <%= stylesheet_link_tag "password_required/application", media: "all" %>
6
+ <%= javascript_include_tag "password_required/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,11 @@
1
+ <%= form_for @password_request, url: @password_request.target_url do |f| %>
2
+ <%= @password_request.hidden_form_inputs %>
3
+ <p>You're password is required to continue.</p>
4
+ <div>
5
+ <%= f.label :password %>
6
+ <%= f.password_field :password %>
7
+ </div>
8
+ <div>
9
+ <%= f.submit 'continue' %>
10
+ </div>
11
+ <% end %>
@@ -0,0 +1,8 @@
1
+ <%
2
+ rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
3
+ rerun_opts = rerun.to_s.strip.empty? ? "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'} features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
4
+ std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~@wip"
5
+ %>
6
+ default: <%= std_opts %> features
7
+ wip: --tags @wip:3 --wip features
8
+ rerun: <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ PasswordRequired::Engine.routes.draw do
2
+ end
@@ -0,0 +1,35 @@
1
+ module PasswordRequired
2
+ module ControllerConcern
3
+ class ArgumentsComposer
4
+ VALID_KEYS = [:for, :with]
5
+
6
+ def initialize(opts = {})
7
+ @options = opts.slice(*VALID_KEYS)
8
+ @options[:for] = Array(@options[:for])
9
+ end
10
+
11
+ def call(klass)
12
+ @options.each_pair do |key, value|
13
+ send("handle_#{key}_key", klass, value, @options[:for])
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def handle_with_key(klass, value, actions)
20
+ actions.each do |action|
21
+ klass.password_check_methods[action] =
22
+ if value.is_a? Proc
23
+ value
24
+ elsif value.is_a? Symbol
25
+ ->(_) { send(value) }
26
+ end
27
+ end
28
+ end
29
+
30
+ def handle_for_key(klass, value, _)
31
+ klass.send :before_action, :guard_with_password!, only: value
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,66 @@
1
+ require 'password_required/password_request'
2
+ require 'password_required/controller_concern/arguments_composer'
3
+
4
+ module PasswordRequired
5
+ module ControllerConcern
6
+ extend ActiveSupport::Concern
7
+
8
+ class PasswordMissing < Exception; end
9
+ class PasswordWrong < Exception; end
10
+ class UserCanceled < Exception; end
11
+
12
+ included do
13
+ def self.password_required(opts = {})
14
+ fail ArgumentError, ':for key is required' unless opts[:for].present?
15
+ ArgumentsComposer.new(opts).call(self)
16
+ end
17
+
18
+ rescue_from PasswordMissing, PasswordWrong, with: :present_password_request
19
+
20
+ cattr_accessor :password_check_methods, instance_writer: false do
21
+ ActiveSupport::HashWithIndifferentAccess.new(->(_) { false })
22
+ end
23
+
24
+ cattr_accessor :password_guard_conditions, instance_writer: false do
25
+ ActiveSupport::HashWithIndifferentAccess.new(->() { true })
26
+ end
27
+ end
28
+
29
+ def password_supplied?
30
+ password_given.present?
31
+ end
32
+
33
+ def password_correct?
34
+ instance_exec(password_given, &password_check_method)
35
+ end
36
+
37
+ def password_required?
38
+ instance_exec(&password_guard_condition)
39
+ end
40
+
41
+ def password_guard_condition
42
+ password_guard_conditions[action_name]
43
+ end
44
+
45
+ def password_check_method
46
+ password_check_methods[action_name]
47
+ end
48
+
49
+ def password_given
50
+ params[:password_request][:password]
51
+ rescue
52
+ ''
53
+ end
54
+
55
+ def guard_with_password!
56
+ return unless password_required?
57
+ fail PasswordMissing unless password_supplied?
58
+ fail PasswordWrong unless password_correct?
59
+ end
60
+
61
+ def present_password_request
62
+ @password_request ||= PasswordRequest.new(request)
63
+ render 'password_request/new'
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,5 @@
1
+ module PasswordRequired
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace PasswordRequired
4
+ end
5
+ end
@@ -0,0 +1,40 @@
1
+ module PasswordRequired
2
+ class PasswordRequest
3
+ class HiddenInputs
4
+ include ActionView::Helpers::FormTagHelper
5
+
6
+ attr_reader :namespaces
7
+
8
+ def initialize(params, namespaces: [])
9
+ @params = params
10
+ @namespaces = namespaces
11
+ end
12
+
13
+ def to_s
14
+ @params.reduce('') do |input_html, key_value_pair|
15
+ input_html + input_from(*key_value_pair)
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def input_from(key, value)
22
+ if value.is_a? Hash
23
+ self.class.new(value, namespaces: namespaces | [key]).to_s
24
+ else
25
+ devise_input namespaced(key), value
26
+ end
27
+ end
28
+
29
+ def devise_input(key, value)
30
+ key += '[]' if value.is_a? Array
31
+ Array(value).map { |i| hidden_field_tag key, i }.join('')
32
+ end
33
+
34
+ def namespaced(key)
35
+ first, *rest = *(namespaces | [key])
36
+ "#{first}#{rest.reduce('') { |a, e| a + "[#{e}]" }}"
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,30 @@
1
+ require 'password_required/password_request/hidden_inputs'
2
+
3
+ module PasswordRequired
4
+ class PasswordRequest
5
+ include ActiveModel::Model
6
+
7
+ IGNORED_PARAMETER_KEYS = [:action, :controller]
8
+
9
+ delegate :env, :parameters, to: :@request
10
+
11
+ attr_accessor :password
12
+
13
+ def initialize(request)
14
+ @request = request
15
+ super({})
16
+ end
17
+
18
+ def target_url
19
+ env['ORIGINAL_FULLPATH']
20
+ end
21
+
22
+ def additional_params
23
+ parameters.except(*IGNORED_PARAMETER_KEYS)
24
+ end
25
+
26
+ def hidden_form_inputs
27
+ HiddenInputs.new(additional_params).to_s.html_safe
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module PasswordRequired
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,5 @@
1
+ require 'password_required/engine'
2
+ require 'password_required/controller_concern'
3
+
4
+ module PasswordRequired
5
+ end
@@ -0,0 +1,65 @@
1
+ # IMPORTANT: This file is generated by cucumber-rails - edit at your own peril.
2
+ # It is recommended to regenerate this file in the future when you upgrade to a
3
+ # newer version of cucumber-rails. Consider adding your own code to a new file
4
+ # instead of editing this one. Cucumber will automatically load all features/**/*.rb
5
+ # files.
6
+
7
+
8
+ unless ARGV.any? {|a| a =~ /^gems/} # Don't load anything when running the gems:* tasks
9
+
10
+ vendored_cucumber_bin = Dir["#{Rails.root}/vendor/{gems,plugins}/cucumber*/bin/cucumber"].first
11
+ $LOAD_PATH.unshift(File.dirname(vendored_cucumber_bin) + '/../lib') unless vendored_cucumber_bin.nil?
12
+
13
+ begin
14
+ require 'cucumber/rake/task'
15
+
16
+ namespace :cucumber do
17
+ Cucumber::Rake::Task.new({:ok => 'test:prepare'}, 'Run features that should pass') do |t|
18
+ t.binary = vendored_cucumber_bin # If nil, the gem's binary is used.
19
+ t.fork = true # You may get faster startup if you set this to false
20
+ t.profile = 'default'
21
+ end
22
+
23
+ Cucumber::Rake::Task.new({:wip => 'test:prepare'}, 'Run features that are being worked on') do |t|
24
+ t.binary = vendored_cucumber_bin
25
+ t.fork = true # You may get faster startup if you set this to false
26
+ t.profile = 'wip'
27
+ end
28
+
29
+ Cucumber::Rake::Task.new({:rerun => 'test:prepare'}, 'Record failing features and run only them if any exist') do |t|
30
+ t.binary = vendored_cucumber_bin
31
+ t.fork = true # You may get faster startup if you set this to false
32
+ t.profile = 'rerun'
33
+ end
34
+
35
+ desc 'Run all features'
36
+ task :all => [:ok, :wip]
37
+
38
+ task :statsetup do
39
+ require 'rails/code_statistics'
40
+ ::STATS_DIRECTORIES << %w(Cucumber\ features features) if File.exist?('features')
41
+ ::CodeStatistics::TEST_TYPES << "Cucumber features" if File.exist?('features')
42
+ end
43
+ end
44
+ desc 'Alias for cucumber:ok'
45
+ task :cucumber => 'cucumber:ok'
46
+
47
+ task :default => :cucumber
48
+
49
+ task :features => :cucumber do
50
+ STDERR.puts "*** The 'features' task is deprecated. See rake -T cucumber ***"
51
+ end
52
+
53
+ # In case we don't have the generic Rails test:prepare hook, append a no-op task that we can depend upon.
54
+ task 'test:prepare' do
55
+ end
56
+
57
+ task :stats => 'cucumber:statsetup'
58
+ rescue LoadError
59
+ desc 'cucumber rake task not available (cucumber not installed)'
60
+ task :cucumber do
61
+ abort 'Cucumber rake task is not available. Be sure to install cucumber as a gem or plugin'
62
+ end
63
+ end
64
+
65
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :password_required do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,4 @@
1
+ begin
2
+ require 'rubocop/rake_task'
3
+ RuboCop::RakeTask.new
4
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: password_required
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Benjamin Falk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Requires password for rails controller actions
42
+ email:
43
+ - benjamin.falk@yahoo.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - app/assets/javascripts/password_required/application.js
52
+ - app/assets/stylesheets/password_required/application.css
53
+ - app/controllers/password_required/application_controller.rb
54
+ - app/helpers/password_required/application_helper.rb
55
+ - app/views/layouts/password_required/application.html.erb
56
+ - app/views/password_request/new.html.erb
57
+ - config/cucumber.yml
58
+ - config/routes.rb
59
+ - lib/password_required.rb
60
+ - lib/password_required/controller_concern.rb
61
+ - lib/password_required/controller_concern/arguments_composer.rb
62
+ - lib/password_required/engine.rb
63
+ - lib/password_required/password_request.rb
64
+ - lib/password_required/password_request/hidden_inputs.rb
65
+ - lib/password_required/version.rb
66
+ - lib/tasks/cucumber.rake
67
+ - lib/tasks/password_required_tasks.rake
68
+ - lib/tasks/rubocop.rake
69
+ homepage: https://github.com/benfalk/password_required
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.2.2
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Password Protection
93
+ test_files: []