minimalist_authentication 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +24 -8
- data/lib/minimalist_authentication.rb +5 -6
- data/lib/minimalist_authentication/configuration.rb +38 -0
- data/lib/{minimalist/authorization.rb → minimalist_authentication/controller.rb} +8 -4
- data/lib/{minimalist → minimalist_authentication}/sessions.rb +4 -4
- data/lib/minimalist_authentication/test_helper.rb +22 -0
- data/lib/{minimalist/authentication.rb → minimalist_authentication/user.rb} +2 -4
- data/lib/minimalist_authentication/version.rb +1 -1
- metadata +6 -5
- data/lib/minimalist/test_helper.rb +0 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bda0fef1ab5c360af94e908ec11ba51059788808
|
4
|
+
data.tar.gz: f691883871f612e2ed2f709736f3fb3eebf68d8b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3fb4e425254e581abdf5e2fc9a7e461a4c5208513bcc122447acdf2db79e8dfd3db9b1595d58758b2c274ed667711863533e298ca469676c51091802273eb7bc
|
7
|
+
data.tar.gz: b782a7c2a6070fa446636a040ac60b969014a17500e22d68fdca2332426163759dedb58843107b358096ff5896a3e2047c32c5ceaca120fed4e4cb3771a61c5a
|
data/README.md
CHANGED
@@ -14,7 +14,7 @@ And then execute:
|
|
14
14
|
$ bundle
|
15
15
|
```
|
16
16
|
|
17
|
-
Create a user model
|
17
|
+
Create a user model with **email** for an identifier:
|
18
18
|
```bash
|
19
19
|
bin/rails generate model user active:boolean email:string crypted_password:string salt:string last_logged_in_at:datetime
|
20
20
|
```
|
@@ -26,31 +26,47 @@ bin/rails generate model user active:boolean username:string crypted_password:st
|
|
26
26
|
|
27
27
|
|
28
28
|
## Example
|
29
|
-
Include
|
29
|
+
Include MinimalistAuthentication::User in your user model (app/models/user.rb)
|
30
30
|
```ruby
|
31
31
|
class User < ApplicationRecord
|
32
|
-
include
|
32
|
+
include MinimalistAuthentication::User
|
33
33
|
end
|
34
34
|
```
|
35
35
|
|
36
|
-
Include
|
36
|
+
Include MinimalistAuthentication::Controller in your ApplicationController (app/controllers/application.rb)
|
37
37
|
```ruby
|
38
38
|
class ApplicationController < ActionController::Base
|
39
|
-
include
|
39
|
+
include MinimalistAuthentication::Controller
|
40
40
|
end
|
41
41
|
```
|
42
42
|
|
43
|
-
Include
|
43
|
+
Include MinimalistAuthentication::Sessions in your SessionsController (app/controllers/sessions_controller.rb)
|
44
44
|
```ruby
|
45
45
|
class SessionsController < ApplicationController
|
46
|
-
include
|
46
|
+
include MinimalistAuthentication::Sessions
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
Add session to your routes file (config/routes.rb)
|
51
|
+
```ruby
|
52
|
+
Rails.application.routes.draw do
|
53
|
+
resource :session, only: %i(new create destroy)
|
47
54
|
end
|
48
55
|
```
|
49
56
|
|
50
57
|
Include Minimalist::TestHelper in your test helper (test/test_helper.rb)
|
51
58
|
```ruby
|
52
59
|
class ActiveSupport::TestCase
|
53
|
-
include
|
60
|
+
include MinimalistAuthentication::TestHelper
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
## Example
|
65
|
+
Customize the configuration with an initializer. Create a **minimalist_authentication.rb** file in /Users/baldwina/git/brightways/config/initializers.
|
66
|
+
```ruby
|
67
|
+
MinimalistAuthentication.configure do |configuration|
|
68
|
+
configuration.user_model_name = 'CustomModelName' # default is '::User'
|
69
|
+
configuration.session_key = :custom_session_key # default is ':user_id'
|
54
70
|
end
|
55
71
|
```
|
56
72
|
|
@@ -1,8 +1,7 @@
|
|
1
1
|
require 'minimalist_authentication/engine'
|
2
|
+
require 'minimalist_authentication/configuration'
|
3
|
+
require 'minimalist_authentication/user'
|
2
4
|
require 'minimalist_authentication/null_password'
|
3
|
-
|
4
|
-
|
5
|
-
require '
|
6
|
-
require 'minimalist/authorization'
|
7
|
-
require 'minimalist/sessions'
|
8
|
-
require 'minimalist/test_helper'
|
5
|
+
require 'minimalist_authentication/controller'
|
6
|
+
require 'minimalist_authentication/sessions'
|
7
|
+
require 'minimalist_authentication/test_helper'
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module MinimalistAuthentication
|
2
|
+
# store the configuration object
|
3
|
+
def self.configuration
|
4
|
+
@configuration ||= Configuration.new
|
5
|
+
end
|
6
|
+
|
7
|
+
# yield the configuration object for modification
|
8
|
+
def self.configure
|
9
|
+
yield configuration
|
10
|
+
end
|
11
|
+
|
12
|
+
# reset the configuration object
|
13
|
+
def self.reset_configuration!
|
14
|
+
@configuration = nil
|
15
|
+
end
|
16
|
+
|
17
|
+
class Configuration
|
18
|
+
# The session_key used to store the current_user id.
|
19
|
+
# Defaults to :user_id
|
20
|
+
attr_accessor :session_key
|
21
|
+
|
22
|
+
# The application user class name
|
23
|
+
# Defaults to '::User'
|
24
|
+
attr_accessor :user_model_name
|
25
|
+
|
26
|
+
def initialize
|
27
|
+
self.user_model_name = '::User'
|
28
|
+
self.session_key = :user_id
|
29
|
+
end
|
30
|
+
|
31
|
+
# Returns the user_model class
|
32
|
+
# Calling constantize on a string makes this work correctly with
|
33
|
+
# the spring application preloader gem.
|
34
|
+
def user_model
|
35
|
+
@user_model ||= user_model_name.constantize
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -1,5 +1,5 @@
|
|
1
|
-
module
|
2
|
-
module
|
1
|
+
module MinimalistAuthentication
|
2
|
+
module Controller
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
included do
|
@@ -13,11 +13,15 @@ module Minimalist
|
|
13
13
|
private
|
14
14
|
|
15
15
|
def current_user
|
16
|
-
@current_user ||= (get_user_from_session ||
|
16
|
+
@current_user ||= (get_user_from_session || MinimalistAuthentication.configuration.user_model.guest)
|
17
17
|
end
|
18
18
|
|
19
19
|
def get_user_from_session
|
20
|
-
|
20
|
+
MinimalistAuthentication.configuration.user_model.find_by_id(session_user_id) if session_user_id
|
21
|
+
end
|
22
|
+
|
23
|
+
def session_user_id
|
24
|
+
session[MinimalistAuthentication.configuration.session_key]
|
21
25
|
end
|
22
26
|
|
23
27
|
def authorization_required
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module
|
1
|
+
module MinimalistAuthentication
|
2
2
|
module Sessions
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
@@ -8,14 +8,14 @@ module Minimalist
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def new
|
11
|
-
@user =
|
11
|
+
@user = MinimalistAuthentication.configuration.user_model.new
|
12
12
|
end
|
13
13
|
|
14
14
|
def create
|
15
15
|
if authenticated_user
|
16
16
|
scrub_session!
|
17
17
|
authenticated_user.logged_in
|
18
|
-
session[
|
18
|
+
session[MinimalistAuthentication.configuration.session_key] = authenticated_user.id
|
19
19
|
after_authentication_success
|
20
20
|
return
|
21
21
|
else
|
@@ -32,7 +32,7 @@ module Minimalist
|
|
32
32
|
private
|
33
33
|
|
34
34
|
def authenticated_user
|
35
|
-
@authenticated_user ||=
|
35
|
+
@authenticated_user ||= MinimalistAuthentication.configuration.user_model.authenticate(user_params)
|
36
36
|
end
|
37
37
|
|
38
38
|
def user_params
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module MinimalistAuthentication
|
2
|
+
module TestHelper
|
3
|
+
def login_as(user_fixture_name, password = 'password')
|
4
|
+
post session_path, params: { user: { email: users(user_fixture_name).email, password: password } }
|
5
|
+
end
|
6
|
+
|
7
|
+
|
8
|
+
def current_user
|
9
|
+
@current_user ||= load_user_from_session
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def load_user_from_session
|
15
|
+
MinimalistAuthentication.configuration.user_model.find(session_user_id) if session_user_id
|
16
|
+
end
|
17
|
+
|
18
|
+
def session_user_id
|
19
|
+
@request.session[MinimalistAuthentication.configuration.session_key]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'bcrypt'
|
2
2
|
|
3
|
-
module
|
4
|
-
module
|
3
|
+
module MinimalistAuthentication
|
4
|
+
module User
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
7
|
GUEST_USER_EMAIL = 'guest'
|
@@ -88,8 +88,6 @@ module Minimalist
|
|
88
88
|
|
89
89
|
def encrypt_password
|
90
90
|
return if password.blank?
|
91
|
-
# self.salt = self.class.make_token
|
92
|
-
# self.crypted_password = encrypt(password)
|
93
91
|
password_hash = self.class.password_hash(password)
|
94
92
|
self.salt = password_hash.salt
|
95
93
|
self.crypted_password = password_hash.checksum
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: minimalist_authentication
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aaron Baldwin
|
@@ -74,13 +74,14 @@ files:
|
|
74
74
|
- app/views/sessions/_form.html.erb
|
75
75
|
- app/views/sessions/new.html.erb
|
76
76
|
- config/routes.rb
|
77
|
-
- lib/minimalist/authentication.rb
|
78
|
-
- lib/minimalist/authorization.rb
|
79
|
-
- lib/minimalist/sessions.rb
|
80
|
-
- lib/minimalist/test_helper.rb
|
81
77
|
- lib/minimalist_authentication.rb
|
78
|
+
- lib/minimalist_authentication/configuration.rb
|
79
|
+
- lib/minimalist_authentication/controller.rb
|
82
80
|
- lib/minimalist_authentication/engine.rb
|
83
81
|
- lib/minimalist_authentication/null_password.rb
|
82
|
+
- lib/minimalist_authentication/sessions.rb
|
83
|
+
- lib/minimalist_authentication/test_helper.rb
|
84
|
+
- lib/minimalist_authentication/user.rb
|
84
85
|
- lib/minimalist_authentication/version.rb
|
85
86
|
- lib/tasks/minimalist_authentication_tasks.rake
|
86
87
|
homepage: https://github.com/wwidea/minimalist_authentication
|
@@ -1,12 +0,0 @@
|
|
1
|
-
module Minimalist
|
2
|
-
module TestHelper
|
3
|
-
def login_as(user_fixture_name, password = 'password')
|
4
|
-
post session_path, params: { user: { email: users(user_fixture_name).email, password: password } }
|
5
|
-
end
|
6
|
-
|
7
|
-
|
8
|
-
def current_user
|
9
|
-
@current_user ||= (@request.session[:user_id] ? User.find(@request.session[:user_id]) : nil)
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|