passwd 0.1.3 → 0.4.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 +5 -5
- data/.github/workflows/ci.yml +28 -0
- data/.gitignore +11 -17
- data/.rubocop.yml +168 -0
- data/.ruby-version +1 -0
- data/Gemfile +3 -6
- data/LICENSE +21 -0
- data/README.md +89 -138
- data/Rakefile +5 -6
- data/bin/console +7 -0
- data/bin/setup +8 -0
- data/lib/generators/passwd/install/USAGE +5 -0
- data/lib/generators/passwd/install/install_generator.rb +10 -0
- data/lib/generators/passwd/install/templates/passwd.rb +21 -0
- data/lib/passwd.rb +33 -6
- data/lib/passwd/config.rb +27 -0
- data/lib/passwd/errors.rb +4 -0
- data/lib/passwd/rails/action_controller_ext.rb +78 -0
- data/lib/passwd/rails/active_record_ext.rb +35 -0
- data/lib/passwd/railtie.rb +18 -0
- data/lib/passwd/version.rb +2 -4
- data/passwd.gemspec +20 -11
- metadata +83 -53
- data/.coveralls.yml +0 -1
- data/.travis.yml +0 -10
- data/LICENSE.txt +0 -22
- data/lib/passwd/active_record.rb +0 -58
- data/lib/passwd/base.rb +0 -72
- data/lib/passwd/configuration/abstract_config.rb +0 -36
- data/lib/passwd/configuration/config.rb +0 -23
- data/lib/passwd/configuration/policy.rb +0 -46
- data/lib/passwd/configuration/tmp_config.rb +0 -18
- data/lib/passwd/password.rb +0 -41
- data/samples/activerecord/user.rake +0 -28
- data/spec/passwd/active_record_spec.rb +0 -142
- data/spec/passwd/base_spec.rb +0 -224
- data/spec/passwd/configuration/config_spec.rb +0 -242
- data/spec/passwd/configuration/policy_spec.rb +0 -133
- data/spec/passwd/configuration/tmp_config_spec.rb +0 -257
- data/spec/passwd/password_spec.rb +0 -150
- data/spec/spec_helper.rb +0 -23
data/Rakefile
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
|
-
require
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
require "rubocop/rake_task"
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
t.rspec_opts = ["-c", "-fs"]
|
7
|
-
end
|
5
|
+
RSpec::Core::RakeTask.new(:spec)
|
6
|
+
RuboCop::RakeTask.new
|
8
7
|
|
9
|
-
task :default =>
|
8
|
+
task :default => %i(spec rubocop)
|
data/bin/console
ADDED
data/bin/setup
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
module Passwd::Generators
|
2
|
+
class InstallGenerator < Rails::Generators::Base
|
3
|
+
source_root File.expand_path("templates", __dir__)
|
4
|
+
|
5
|
+
desc "Create Passwd config file"
|
6
|
+
def create_config_file
|
7
|
+
copy_file "passwd.rb", "config/initializers/passwd.rb"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
Passwd.current.config.tap do |config|
|
2
|
+
# Number of hashed by stretching
|
3
|
+
# Minimum is 4, maximum is 31, default is 12.
|
4
|
+
# See also BCrypt::Engine
|
5
|
+
# config.stretching = 12
|
6
|
+
|
7
|
+
# Random generate password length
|
8
|
+
# config.length = 10
|
9
|
+
|
10
|
+
# Array of characters used for random password generation
|
11
|
+
# config.characters = [("a".."z"), ("A".."Z"), ("0".."9")].map(&:to_a).flatten
|
12
|
+
end
|
13
|
+
|
14
|
+
# Session key for authentication
|
15
|
+
# Rails.application.config.passwd.session_key = :user_id
|
16
|
+
|
17
|
+
# Authentication Model Class
|
18
|
+
# Rails.application.config.passwd.auth_class = :User
|
19
|
+
|
20
|
+
# Redirect path when not signin
|
21
|
+
# Rails.application.config.passwd.signin_path = :signin_path
|
data/lib/passwd.rb
CHANGED
@@ -1,8 +1,35 @@
|
|
1
|
-
|
1
|
+
require "bcrypt"
|
2
|
+
require "passwd/version"
|
3
|
+
require "passwd/errors"
|
4
|
+
require "passwd/config"
|
5
|
+
require "passwd/railtie" if defined?(Rails)
|
2
6
|
|
3
|
-
|
7
|
+
class Passwd
|
8
|
+
class << self
|
9
|
+
def current
|
10
|
+
@current ||= new
|
11
|
+
end
|
4
12
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
13
|
+
attr_writer :current
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize(conf = nil)
|
17
|
+
@config = conf
|
18
|
+
end
|
19
|
+
|
20
|
+
def password_hashing(plain)
|
21
|
+
BCrypt::Password.create(plain, cost: config.stretching.clamp(BCrypt::Engine::MIN_COST, BCrypt::Engine::MAX_COST))
|
22
|
+
end
|
23
|
+
|
24
|
+
def load_password(hashed_password)
|
25
|
+
BCrypt::Password.new(hashed_password)
|
26
|
+
end
|
27
|
+
|
28
|
+
def random(long = nil)
|
29
|
+
Array.new(long || config.length) { config.characters[rand(config.characters.size)] }.join
|
30
|
+
end
|
31
|
+
|
32
|
+
def config
|
33
|
+
@config ||= Config.new
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class Passwd
|
2
|
+
class Config
|
3
|
+
VALID_OPTIONS = [
|
4
|
+
:stretching,
|
5
|
+
:length,
|
6
|
+
:characters,
|
7
|
+
].freeze
|
8
|
+
|
9
|
+
attr_accessor(*VALID_OPTIONS)
|
10
|
+
|
11
|
+
def initialize(options = {})
|
12
|
+
reset
|
13
|
+
merge(options)
|
14
|
+
end
|
15
|
+
|
16
|
+
def merge(options)
|
17
|
+
options.each_key {|key| send("#{key}=", options[key]) }
|
18
|
+
self
|
19
|
+
end
|
20
|
+
|
21
|
+
def reset
|
22
|
+
@stretching = 12
|
23
|
+
@length = 10
|
24
|
+
@characters = [("a".."z"), ("A".."Z"), ("0".."9")].map(&:to_a).flatten
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module Passwd::Rails
|
2
|
+
module ActionControllerExt
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
helper_method :current_user, :signin?
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def current_user
|
12
|
+
return @current_user if instance_variable_defined?(:@current_user)
|
13
|
+
|
14
|
+
@current_user = _auth_class.find_by(id: session[_auth_key])
|
15
|
+
end
|
16
|
+
|
17
|
+
def signin?
|
18
|
+
current_user.present?
|
19
|
+
end
|
20
|
+
|
21
|
+
def signin(user)
|
22
|
+
if user.present?
|
23
|
+
@current_user = user
|
24
|
+
session[_auth_key] = user&.id
|
25
|
+
end
|
26
|
+
|
27
|
+
user.present?
|
28
|
+
end
|
29
|
+
|
30
|
+
def signout
|
31
|
+
session[_auth_key] = nil
|
32
|
+
@current_user = nil
|
33
|
+
end
|
34
|
+
|
35
|
+
def redirect_to_referer_or(path, options = {})
|
36
|
+
redirect_to session[:signin_referer].presence || path, **options
|
37
|
+
end
|
38
|
+
|
39
|
+
def require_signin
|
40
|
+
return if signin?
|
41
|
+
|
42
|
+
path = _signin_path
|
43
|
+
raise UnauthorizedAccess unless path
|
44
|
+
|
45
|
+
session[:signin_referer] = request.fullpath
|
46
|
+
redirect_to path
|
47
|
+
end
|
48
|
+
|
49
|
+
def passwd_auth_class
|
50
|
+
nil
|
51
|
+
end
|
52
|
+
|
53
|
+
def passwd_auth_key
|
54
|
+
nil
|
55
|
+
end
|
56
|
+
|
57
|
+
def passwd_signin_path
|
58
|
+
nil
|
59
|
+
end
|
60
|
+
|
61
|
+
def _auth_class
|
62
|
+
(Rails.application.config.passwd.auth_class || passwd_auth_class || :User).to_s.constantize
|
63
|
+
end
|
64
|
+
|
65
|
+
def _auth_key
|
66
|
+
Rails.application.config.passwd.session_key || passwd_auth_key || :user_id
|
67
|
+
end
|
68
|
+
|
69
|
+
def _signin_path
|
70
|
+
name = Rails.application.config.passwd.signin_path || passwd_signin_path || :signin_path
|
71
|
+
_url_helpers.respond_to?(name) ? _url_helpers.public_send(name) : nil
|
72
|
+
end
|
73
|
+
|
74
|
+
def _url_helpers
|
75
|
+
Rails.application.routes.url_helpers
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Passwd::Rails
|
2
|
+
module ActiveRecordExt
|
3
|
+
def with_authenticate(passwd: nil, user_id: :email, password: :password)
|
4
|
+
passwd ||= Passwd.current
|
5
|
+
define_singleton_auth_with_passwd(user_id)
|
6
|
+
define_instance_auth_with_passwd(passwd, password)
|
7
|
+
define_instance_set_password(passwd, password)
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def define_singleton_auth_with_passwd(user_id_col)
|
13
|
+
define_singleton_method :authenticate do |user_id, plain|
|
14
|
+
user = find_by(user_id_col => user_id)
|
15
|
+
return nil unless user
|
16
|
+
|
17
|
+
user.authenticate(plain) ? user : nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def define_instance_auth_with_passwd(passwd, password_col)
|
22
|
+
define_method :authenticate do |plain|
|
23
|
+
BCrypt::Password.new(send(password_col)) == plain
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def define_instance_set_password(passwd, password_col)
|
28
|
+
define_method :set_password do |plain = nil|
|
29
|
+
plain ||= passwd.random
|
30
|
+
send("#{password_col}=", passwd.password_hashing(plain))
|
31
|
+
plain
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class Passwd
|
2
|
+
class Railtie < ::Rails::Railtie
|
3
|
+
config.passwd = ActiveSupport::OrderedOptions.new
|
4
|
+
|
5
|
+
initializer "passwd" do
|
6
|
+
require "passwd/rails/action_controller_ext"
|
7
|
+
require "passwd/rails/active_record_ext"
|
8
|
+
|
9
|
+
ActiveSupport.on_load(:action_controller) do
|
10
|
+
::ActionController::Base.include ::Passwd::Rails::ActionControllerExt
|
11
|
+
end
|
12
|
+
|
13
|
+
ActiveSupport.on_load(:active_record) do
|
14
|
+
::ActiveRecord::Base.extend Passwd::Rails::ActiveRecordExt
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/passwd/version.rb
CHANGED
data/passwd.gemspec
CHANGED
@@ -1,23 +1,32 @@
|
|
1
|
-
lib = File.expand_path(
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
-
require
|
3
|
+
require "passwd/version"
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "passwd"
|
7
7
|
spec.version = Passwd::VERSION
|
8
8
|
spec.authors = ["i2bskn"]
|
9
|
-
spec.email = ["
|
10
|
-
|
11
|
-
spec.
|
9
|
+
spec.email = ["iiboshi@craftake.co.jp"]
|
10
|
+
|
11
|
+
spec.description = "Passwd is provide hashed password creation and authentication."
|
12
|
+
spec.summary = "Passwd is provide hashed password creation and authentication."
|
12
13
|
spec.homepage = "https://github.com/i2bskn/passwd"
|
13
14
|
spec.license = "MIT"
|
14
15
|
|
15
|
-
spec.files = `git ls-files`.split(
|
16
|
-
|
17
|
-
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f) }
|
18
22
|
spec.require_paths = ["lib"]
|
19
23
|
|
20
|
-
spec.
|
21
|
-
|
22
|
-
spec.
|
24
|
+
spec.required_ruby_version = ">= 2.7.0"
|
25
|
+
|
26
|
+
spec.add_dependency "bcrypt", "~> 3.1.0"
|
27
|
+
spec.add_development_dependency "bundler", ">= 2.1.0"
|
28
|
+
spec.add_development_dependency "pry", "~> 0.14.0"
|
29
|
+
spec.add_development_dependency "rake", "~> 13.0.0"
|
30
|
+
spec.add_development_dependency "rspec", "~> 3.10.0"
|
31
|
+
spec.add_development_dependency "rubocop", "1.11.0"
|
23
32
|
end
|
metadata
CHANGED
@@ -1,118 +1,148 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: passwd
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- i2bskn
|
8
|
-
autorequire:
|
9
|
-
bindir:
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-03-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bcrypt
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.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: 3.1.0
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
16
30
|
requirements:
|
17
|
-
- -
|
31
|
+
- - ">="
|
18
32
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
33
|
+
version: 2.1.0
|
20
34
|
type: :development
|
21
35
|
prerelease: false
|
22
36
|
version_requirements: !ruby/object:Gem::Requirement
|
23
37
|
requirements:
|
24
|
-
- -
|
38
|
+
- - ">="
|
25
39
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
40
|
+
version: 2.1.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.14.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.14.0
|
27
55
|
- !ruby/object:Gem::Dependency
|
28
56
|
name: rake
|
29
57
|
requirement: !ruby/object:Gem::Requirement
|
30
58
|
requirements:
|
31
|
-
- -
|
59
|
+
- - "~>"
|
32
60
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
61
|
+
version: 13.0.0
|
34
62
|
type: :development
|
35
63
|
prerelease: false
|
36
64
|
version_requirements: !ruby/object:Gem::Requirement
|
37
65
|
requirements:
|
38
|
-
- -
|
66
|
+
- - "~>"
|
39
67
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
68
|
+
version: 13.0.0
|
41
69
|
- !ruby/object:Gem::Dependency
|
42
70
|
name: rspec
|
43
71
|
requirement: !ruby/object:Gem::Requirement
|
44
72
|
requirements:
|
45
|
-
- -
|
73
|
+
- - "~>"
|
46
74
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
75
|
+
version: 3.10.0
|
48
76
|
type: :development
|
49
77
|
prerelease: false
|
50
78
|
version_requirements: !ruby/object:Gem::Requirement
|
51
79
|
requirements:
|
52
|
-
- -
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 3.10.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '='
|
53
88
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
55
|
-
|
89
|
+
version: 1.11.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.11.0
|
97
|
+
description: Passwd is provide hashed password creation and authentication.
|
56
98
|
email:
|
57
|
-
-
|
99
|
+
- iiboshi@craftake.co.jp
|
58
100
|
executables: []
|
59
101
|
extensions: []
|
60
102
|
extra_rdoc_files: []
|
61
103
|
files:
|
62
|
-
- .
|
63
|
-
- .gitignore
|
64
|
-
- .
|
104
|
+
- ".github/workflows/ci.yml"
|
105
|
+
- ".gitignore"
|
106
|
+
- ".rubocop.yml"
|
107
|
+
- ".ruby-version"
|
65
108
|
- Gemfile
|
66
|
-
- LICENSE
|
109
|
+
- LICENSE
|
67
110
|
- README.md
|
68
111
|
- Rakefile
|
112
|
+
- bin/console
|
113
|
+
- bin/setup
|
114
|
+
- lib/generators/passwd/install/USAGE
|
115
|
+
- lib/generators/passwd/install/install_generator.rb
|
116
|
+
- lib/generators/passwd/install/templates/passwd.rb
|
69
117
|
- lib/passwd.rb
|
70
|
-
- lib/passwd/
|
71
|
-
- lib/passwd/
|
72
|
-
- lib/passwd/
|
73
|
-
- lib/passwd/
|
74
|
-
- lib/passwd/
|
75
|
-
- lib/passwd/configuration/tmp_config.rb
|
76
|
-
- lib/passwd/password.rb
|
118
|
+
- lib/passwd/config.rb
|
119
|
+
- lib/passwd/errors.rb
|
120
|
+
- lib/passwd/rails/action_controller_ext.rb
|
121
|
+
- lib/passwd/rails/active_record_ext.rb
|
122
|
+
- lib/passwd/railtie.rb
|
77
123
|
- lib/passwd/version.rb
|
78
124
|
- passwd.gemspec
|
79
|
-
- samples/activerecord/user.rake
|
80
|
-
- spec/passwd/active_record_spec.rb
|
81
|
-
- spec/passwd/base_spec.rb
|
82
|
-
- spec/passwd/configuration/config_spec.rb
|
83
|
-
- spec/passwd/configuration/policy_spec.rb
|
84
|
-
- spec/passwd/configuration/tmp_config_spec.rb
|
85
|
-
- spec/passwd/password_spec.rb
|
86
|
-
- spec/spec_helper.rb
|
87
125
|
homepage: https://github.com/i2bskn/passwd
|
88
126
|
licenses:
|
89
127
|
- MIT
|
90
128
|
metadata: {}
|
91
|
-
post_install_message:
|
129
|
+
post_install_message:
|
92
130
|
rdoc_options: []
|
93
131
|
require_paths:
|
94
132
|
- lib
|
95
133
|
required_ruby_version: !ruby/object:Gem::Requirement
|
96
134
|
requirements:
|
97
|
-
- -
|
135
|
+
- - ">="
|
98
136
|
- !ruby/object:Gem::Version
|
99
|
-
version:
|
137
|
+
version: 2.7.0
|
100
138
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
139
|
requirements:
|
102
|
-
- -
|
140
|
+
- - ">="
|
103
141
|
- !ruby/object:Gem::Version
|
104
142
|
version: '0'
|
105
143
|
requirements: []
|
106
|
-
|
107
|
-
|
108
|
-
signing_key:
|
144
|
+
rubygems_version: 3.1.4
|
145
|
+
signing_key:
|
109
146
|
specification_version: 4
|
110
|
-
summary:
|
111
|
-
test_files:
|
112
|
-
- spec/passwd/active_record_spec.rb
|
113
|
-
- spec/passwd/base_spec.rb
|
114
|
-
- spec/passwd/configuration/config_spec.rb
|
115
|
-
- spec/passwd/configuration/policy_spec.rb
|
116
|
-
- spec/passwd/configuration/tmp_config_spec.rb
|
117
|
-
- spec/passwd/password_spec.rb
|
118
|
-
- spec/spec_helper.rb
|
147
|
+
summary: Passwd is provide hashed password creation and authentication.
|
148
|
+
test_files: []
|