sudo_rails 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
+ SHA256:
3
+ metadata.gz: 638291947f7e86c97efc387c94903a011517acb4782ce38e82203086ebd2435c
4
+ data.tar.gz: 48ccb8a2114553330cab7e3fbe31e6ff36edb8a9ff1e8a709b549354cf9e6501
5
+ SHA512:
6
+ metadata.gz: a8ea7742c4449d1389c7f1c392f274ea371d14d6890dfcb3bb010c766e4d9d625a011314a41f1b1e04017c3ecb8f635582cef2e7907406f47a84533d5b27dc65
7
+ data.tar.gz: 6c76d8fdc1c1ddda5376bd4cc466719ef386e1fdd349ed8e1a1f5d5aae94115374c67b5c3d276bf520cede3fc5b14cd4b3a3fd763413b7d818d3815d478ffe5e
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2019 Marc Anguera Insa @markets
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,66 @@
1
+ # Sudo Rails
2
+
3
+ [![Gem](https://img.shields.io/gem/v/sudo_rails.svg?style=flat-square)](https://rubygems.org/gems/sudo_rails)
4
+ [![Build Status](https://travis-ci.org/markets/sudo_rails.svg)](https://travis-ci.org/markets/sudo_rails)
5
+
6
+ > Sudo mode for your Rails controllers
7
+
8
+ Protect :lock: any Rails action with a customizable password confirmation strategy.
9
+
10
+ ```ruby
11
+ class SecretController < ApplicationController
12
+ sudo
13
+ end
14
+ ```
15
+
16
+ *Inspired by Unix `sudo` command and [GitHub Sudo mode](https://help.github.com/en/articles/sudo-mode).*
17
+
18
+ ## Installation
19
+
20
+ Add this line to your Gemfile and then execute `bundle install`:
21
+
22
+ ```ruby
23
+ gem 'sudo_rails'
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ From now on, you have the `sudo` method available in your controllers, you can protect the whole controller or only some actions:
29
+
30
+ ```ruby
31
+ class SettingsController < ApplicationController
32
+ sudo only: :sensible_settings
33
+ end
34
+ ```
35
+
36
+ ### Configuration
37
+
38
+ You can use the `setup` method to customize different things:
39
+
40
+ ```ruby
41
+ # config/initializers/sudo_rails.rb
42
+ SudoRails.setup do |config|
43
+ config.enabled = true
44
+ config.sudo_session_time = 20.minutes # default is 1 hour
45
+ config.layout = 'admin'
46
+ config.custom_logo = 'logos/medium_dark.png'
47
+ config.primary_color = '#1A7191'
48
+ config.reset_pass_link = '/users/password/new'
49
+ config.confirm_with = -> (context, password) {
50
+ user = context.current_user
51
+ user.valid_password?(password)
52
+ }
53
+ end
54
+ ```
55
+
56
+ ## Development
57
+
58
+ Any kind of feedback, bug report, idea or enhancement are really appreciated.
59
+
60
+ To contribute, just fork the repo, hack on it and send a pull request. Don't forget to add tests for behaviour changes and run the test suite:
61
+
62
+ > bundle exec rspec
63
+
64
+ ## License
65
+
66
+ Copyright (c) Marc Anguera. SudoRails is released under the [MIT](LICENSE) License.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,47 @@
1
+ body {
2
+ text-align: center;
3
+ font-size: 22px;
4
+ font-family: Helvetica, Arial, sans-serif;
5
+ background-color: #f9f9f9;
6
+ transform: translateY(20%);
7
+ }
8
+
9
+ .sudo-header {
10
+ margin: 40px auto;
11
+
12
+ img {
13
+ max-width: 280px;
14
+ }
15
+ }
16
+
17
+ .sudo-form {
18
+ background-color: #fff;
19
+ border-radius: 5px;
20
+ border: 1px solid #d8dee2;
21
+ font-size: 14px;
22
+ padding: 20px;
23
+ margin: 10px auto;
24
+ width: 340px;
25
+
26
+ input {
27
+ display: block;
28
+ width: 100%;
29
+ font-size: 16px;
30
+ line-height: 30px;
31
+ padding: 4px;
32
+ border-radius: 5px;
33
+ border: 1px solid #d8dee2;
34
+ }
35
+
36
+ input[type="submit"] {
37
+ margin: 20px auto;
38
+ width: 60%;
39
+ padding: 4px;
40
+ background-color: #d8dee2;
41
+ border-radius: 25px;
42
+ }
43
+ }
44
+
45
+ .sudo-tip {
46
+ font-size: 14px;
47
+ }
@@ -0,0 +1,25 @@
1
+ module SudoRails
2
+ class ApplicationController < ActionController::Base
3
+ before_action :sudo_enabled?
4
+
5
+ def confirm
6
+ if request.post? && confirm_sudo?
7
+ session[:sudo_rails_session] = Time.zone.now
8
+ redirect_to params[:target_path]
9
+ else
10
+ render 'sudo_rails/confirm_form', layout: SudoRails.get_layout
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def sudo_enabled?
17
+ SudoRails.enabled || head(404, message: "SudoRails disabled")
18
+ end
19
+
20
+ def confirm_sudo?
21
+ block = SudoRails.confirm_with
22
+ block.call(self, params[:password])
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <%= csrf_meta_tags %>
5
+ <%= stylesheet_link_tag "sudo_rails/application", media: "all" %>
6
+ <%= render 'sudo_rails/inject_custom_styles' if SudoRails.primary_color %>
7
+ </head>
8
+ <body>
9
+ <%= yield %>
10
+ </body>
11
+ </html>
@@ -0,0 +1,8 @@
1
+ <style type="text/css">
2
+ .sudo-form input[type="submit"] {
3
+ background-color: <%= SudoRails.primary_color %>;
4
+ }
5
+ .sudo-form a {
6
+ color: <%= SudoRails.primary_color %>;
7
+ }
8
+ </style>
@@ -0,0 +1,24 @@
1
+ <header class='sudo-header'>
2
+ <% if SudoRails.custom_logo %>
3
+ <%= image_tag SudoRails.custom_logo %>
4
+ <% end %>
5
+
6
+ <h3>Confirm password to continue</h3>
7
+ </header>
8
+
9
+ <div class='sudo-form'>
10
+ <%= form_tag '/sudo_rails/confirm' do |f| %>
11
+ <%= hidden_field_tag :target_path, params[:target_path] || request.url %>
12
+ <%= password_field_tag :password, nil, required: true, placeholder: 'Password' %>
13
+ <%= submit_tag 'Confirm password' %>
14
+ <% end %>
15
+
16
+ <% if SudoRails.reset_pass_link %>
17
+ <%= link_to 'Forgot your password?', SudoRails.reset_pass_link, target: '_blank' %>
18
+ <% end %>
19
+ </div>
20
+
21
+ <div class='sudo-tip'>
22
+ You are entering <b>sudo mode</b>.<br>
23
+ We won’t ask for your password again for <i><%= time_ago_in_words(SudoRails.sudo_session_time.ago) %></i>.
24
+ </div>
@@ -0,0 +1,20 @@
1
+ module SudoRails
2
+ module ControllerExt
3
+ def sudo(options = {})
4
+ before_action(options) do
5
+ next unless SudoRails.enabled
6
+ next if SudoRails::ControllerExt.valid_sudo_session?(session[:sudo_rails_session])
7
+
8
+ render 'sudo_rails/confirm_form', layout: SudoRails.get_layout
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def self.valid_sudo_session?(started_at)
15
+ return false unless started_at
16
+
17
+ Time.parse(started_at) + SudoRails.sudo_session_time > Time.zone.now
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ module SudoRails
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace SudoRails
4
+
5
+ initializer "sudo_rails.controller_ext" do
6
+ ActiveSupport.on_load(:action_controller) do
7
+ extend SudoRails::ControllerExt
8
+ end
9
+ end
10
+
11
+ initializer 'sudo_rails.routes' do |app|
12
+ app.routes.append do
13
+ match '/sudo_rails/confirm' => 'sudo_rails/application#confirm', via: [:get, :post]
14
+ end
15
+ end
16
+
17
+ config.assets.precompile << %w(
18
+ sudo_rails/application.css
19
+ )
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module SudoRails
2
+ VERSION = "0.1.0"
3
+ end
data/lib/sudo_rails.rb ADDED
@@ -0,0 +1,34 @@
1
+ require "sudo_rails/version"
2
+ require "sudo_rails/controller_ext"
3
+ require "sudo_rails/engine"
4
+
5
+ module SudoRails
6
+ class << self
7
+ attr_accessor :enabled,
8
+ :layout,
9
+ :custom_logo,
10
+ :primary_color,
11
+ :confirm_with,
12
+ :sudo_session_time,
13
+ :reset_pass_link
14
+
15
+ def setup
16
+ yield(self) if block_given?
17
+ end
18
+
19
+ def get_layout
20
+ layout || 'sudo_rails/application'
21
+ end
22
+ end
23
+
24
+ self.enabled = true
25
+ self.sudo_session_time = 1.hour
26
+
27
+ if defined?(Devise)
28
+ self.confirm_with = -> (context, password) {
29
+ user = context.current_user
30
+ user.valid_password?(password)
31
+ }
32
+ self.reset_pass_link = "/users/password/new"
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sudo_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - markets
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-05-01 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.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sass-rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Protect any Rails action with password confirmation.
56
+ email:
57
+ - srmarc.ai@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - app/assets/stylesheets/sudo_rails/application.scss
66
+ - app/controllers/sudo_rails/application_controller.rb
67
+ - app/views/layouts/sudo_rails/application.html.erb
68
+ - app/views/sudo_rails/_inject_custom_styles.html.erb
69
+ - app/views/sudo_rails/confirm_form.html.erb
70
+ - lib/sudo_rails.rb
71
+ - lib/sudo_rails/controller_ext.rb
72
+ - lib/sudo_rails/engine.rb
73
+ - lib/sudo_rails/version.rb
74
+ homepage: https://github.com/markets/sudo_rails
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.7.6
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Sudo mode for Rails
98
+ test_files: []