authentication 0.0.1 → 0.0.2
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 +7 -0
- data/.travis.yml +6 -1
- data/Gemfile.lock +19 -11
- data/README.md +22 -7
- data/lib/authentication.rb +20 -35
- data/lib/authentication/authenticator.rb +56 -0
- data/lib/authentication/version.rb +1 -1
- data/spec/lib/authentication_spec.rb +9 -9
- metadata +14 -20
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2a79cf272b8049c3c5c4079363518bea601ef966
|
4
|
+
data.tar.gz: ebcdb430bc467c2586e1ac3d9adc2787c3a25996
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1504438b306f6a02d00b1aab3122fa95833b0a74bfe03b6187ef70974ed37850e220d47106cb4ed77ea06e1ae3077ee2c55c596aa811aa65fcf5365a348958de
|
7
|
+
data.tar.gz: 2597a069244ef5ffde178d66d2005ce1f037447f3e690747e61448b9e4ea90d65b6b1a1abf84497cfdb63f63b9c3174bd7c0bfcbd5cfe6ff0b378e632c0753c5
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,21 +1,26 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
authentication (0.0.
|
4
|
+
authentication (0.0.2)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
|
-
diff-lcs (1.
|
10
|
-
rake (
|
11
|
-
rspec (
|
12
|
-
rspec-core (~>
|
13
|
-
rspec-expectations (~>
|
14
|
-
rspec-mocks (~>
|
15
|
-
rspec-core (
|
16
|
-
|
17
|
-
|
18
|
-
|
9
|
+
diff-lcs (1.2.5)
|
10
|
+
rake (10.4.2)
|
11
|
+
rspec (3.4.0)
|
12
|
+
rspec-core (~> 3.4.0)
|
13
|
+
rspec-expectations (~> 3.4.0)
|
14
|
+
rspec-mocks (~> 3.4.0)
|
15
|
+
rspec-core (3.4.1)
|
16
|
+
rspec-support (~> 3.4.0)
|
17
|
+
rspec-expectations (3.4.0)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.4.0)
|
20
|
+
rspec-mocks (3.4.0)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.4.0)
|
23
|
+
rspec-support (3.4.0)
|
19
24
|
|
20
25
|
PLATFORMS
|
21
26
|
ruby
|
@@ -24,3 +29,6 @@ DEPENDENCIES
|
|
24
29
|
authentication!
|
25
30
|
rake
|
26
31
|
rspec
|
32
|
+
|
33
|
+
BUNDLED WITH
|
34
|
+
1.10.6
|
data/README.md
CHANGED
@@ -2,19 +2,34 @@
|
|
2
2
|
|
3
3
|
## Minimalist authentication library for Ruby
|
4
4
|
|
5
|
-
##
|
5
|
+
## Install
|
6
6
|
|
7
|
-
|
7
|
+
```
|
8
|
+
$ gem install authentication
|
9
|
+
```
|
8
10
|
|
9
|
-
|
11
|
+
or add ```gem 'authentication'``` to your Gemfile.
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
+
## How to use in a Rails app
|
14
|
+
|
15
|
+
0. Include `Authentication` to your controller.
|
16
|
+
1. Define `#find_current_user` in your controller.
|
17
|
+
2. That's all. You can use the method below in the controller:
|
18
|
+
- `#login!` and `#logout!` to log in/out.
|
19
|
+
- `#current_user` and `#current_user_id` to get current user or its id.
|
20
|
+
- `#logged_in?` to ask logged in or not.
|
21
|
+
|
22
|
+
[Example](https://github.com/fujimura/authentication_rails_example) and [spec](https://github.com/fujimura/authentication/blob/master/spec/lib/authentication_spec.rb) might be also helpful.
|
23
|
+
|
24
|
+
## Advanced usage
|
25
|
+
|
26
|
+
Under the hood, methods above(`current_user` etc.) are delegated to the instance of `Authentication::Authenticator`, which is built for "current_user". So, it's possible to create custom authenticator for a client which isn't a current user.
|
13
27
|
|
14
28
|
## Side-effects
|
15
29
|
|
16
|
-
* ```session[:current_user_id]```
|
17
|
-
*
|
30
|
+
* ```session[:current_user_id]``` of the class includes `Authentication` will be changed.
|
31
|
+
* The class includes `Authentication` will include `Forwardable`.
|
32
|
+
* The class includes `Authentication` will have a method called `current_user_authenticator`.
|
18
33
|
|
19
34
|
## Goal
|
20
35
|
|
data/lib/authentication.rb
CHANGED
@@ -1,45 +1,30 @@
|
|
1
1
|
require "authentication/version"
|
2
|
+
require "authentication/authenticator"
|
2
3
|
|
4
|
+
# Top-level namespace of this library, also works as a convenient module to supply authentication-related methods for Rails controller etc.
|
5
|
+
#
|
6
|
+
# By including this, the class will have methods like "login!" or "current_user!". See method list for the documentation of Authenticator.
|
7
|
+
#
|
8
|
+
# To find current_user, this module uses "find_current_user" method defined in included module to find current user, and "current_user_id" as the session key.
|
3
9
|
module Authentication
|
4
|
-
|
10
|
+
# Raised when authentication was failed.
|
5
11
|
class Unauthenticated < StandardError; end
|
6
12
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
@current_user = user
|
13
|
-
session[:current_user_id] = user.id
|
14
|
-
end
|
15
|
-
|
16
|
-
# Return current_user.
|
17
|
-
# If it does not exist, returns nil.
|
18
|
-
#
|
19
|
-
# @return [Object] The object stored as user or nil
|
20
|
-
def current_user
|
21
|
-
@current_user ||= find_current_user
|
22
|
-
end
|
23
|
-
|
24
|
-
# Return id of given current_user.
|
25
|
-
# If it does not exist, returns nil.
|
26
|
-
#
|
27
|
-
# @return [Object] The id of object stored as user or nil
|
28
|
-
def current_user_id
|
29
|
-
session[:current_user_id]
|
30
|
-
end
|
31
|
-
|
32
|
-
# Return current_user exists or not.
|
33
|
-
#
|
34
|
-
# @return [Boolean]
|
35
|
-
def logged_in?
|
36
|
-
not current_user.nil?
|
13
|
+
def self.included(base)
|
14
|
+
base.class_eval do
|
15
|
+
extend Forwardable
|
16
|
+
def_delegators :current_user_authenticator, :login!, :current_user, :current_user_id, :logged_in?, :logout!
|
17
|
+
end
|
37
18
|
end
|
38
19
|
|
39
|
-
#
|
20
|
+
# Return authenticator for current_user.
|
40
21
|
#
|
41
|
-
|
42
|
-
|
43
|
-
@
|
22
|
+
# @return [Authentication::Authenticator]
|
23
|
+
def current_user_authenticator
|
24
|
+
@__authenticator ||= Authentication::Authenticator.new(
|
25
|
+
session: session,
|
26
|
+
session_key: :current_user_id,
|
27
|
+
finder: -> { find_current_user }
|
28
|
+
)
|
44
29
|
end
|
45
30
|
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Authentication
|
2
|
+
# A object to authenticate client.
|
3
|
+
# @since 0.0.2
|
4
|
+
class Authenticator
|
5
|
+
# @param [Hash] options the options to create a authenticator with.
|
6
|
+
# @option options [Hash] :session Hash-like session object.
|
7
|
+
# @option options [Symbol] :session_key Key of session_id.
|
8
|
+
# @option options [Proc] :finder A proc which returns client to authenticate.
|
9
|
+
def initialize(options)
|
10
|
+
@session = options.fetch :session
|
11
|
+
@session_key = options.fetch :session_key
|
12
|
+
@finder = options.fetch :finder
|
13
|
+
end
|
14
|
+
|
15
|
+
# Set current_client.
|
16
|
+
#
|
17
|
+
# @raise [Unauthenticated] Raised when given client doesn't exist.
|
18
|
+
def login!(client)
|
19
|
+
raise Unauthenticated unless client
|
20
|
+
@current_client = client
|
21
|
+
@session[@session_key] = client.id
|
22
|
+
end
|
23
|
+
|
24
|
+
# Return current_client.
|
25
|
+
# If it does not exist, returns nil.
|
26
|
+
#
|
27
|
+
# @return [Object] The object stored as client or nil
|
28
|
+
def current_client
|
29
|
+
@current_client ||= @finder.call
|
30
|
+
end
|
31
|
+
alias current_user current_client
|
32
|
+
|
33
|
+
# Return id of given current_client.
|
34
|
+
# If it does not exist, returns nil.
|
35
|
+
#
|
36
|
+
# @return [Object] The id of object stored as client or nil
|
37
|
+
def current_client_id
|
38
|
+
@session[@session_key]
|
39
|
+
end
|
40
|
+
alias current_user_id current_client_id
|
41
|
+
|
42
|
+
# Return current_client exists or not.
|
43
|
+
#
|
44
|
+
# @return [Boolean]
|
45
|
+
def logged_in?
|
46
|
+
not current_client.nil?
|
47
|
+
end
|
48
|
+
|
49
|
+
# Delete current_client from database and session.
|
50
|
+
#
|
51
|
+
def logout!
|
52
|
+
return unless current_client
|
53
|
+
@current_client = @session[@session_key] = nil
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -8,7 +8,7 @@ class Controller
|
|
8
8
|
self.session = {}
|
9
9
|
end
|
10
10
|
|
11
|
-
def find_current_user
|
11
|
+
def find_current_user
|
12
12
|
if session[:current_user_id]
|
13
13
|
true
|
14
14
|
else
|
@@ -44,13 +44,13 @@ describe Authentication do
|
|
44
44
|
context 'when logged in' do
|
45
45
|
it "should return user" do
|
46
46
|
controller.login! user
|
47
|
-
controller.current_user.
|
47
|
+
expect(controller.current_user).to eq user
|
48
48
|
end
|
49
49
|
end
|
50
50
|
context 'when not logged in' do
|
51
51
|
it "should call #find_current_user and return its result" do
|
52
|
-
controller.
|
53
|
-
controller.current_user.
|
52
|
+
expect(controller).to receive(:find_current_user).and_return "something"
|
53
|
+
expect(controller.current_user). to eq "something"
|
54
54
|
end
|
55
55
|
end
|
56
56
|
end
|
@@ -59,12 +59,12 @@ describe Authentication do
|
|
59
59
|
context 'when logged in' do
|
60
60
|
it "should return user_id" do
|
61
61
|
controller.login! user
|
62
|
-
controller.current_user_id.
|
62
|
+
expect(controller.current_user_id).to eq user.id
|
63
63
|
end
|
64
64
|
end
|
65
65
|
context 'when not logged in' do
|
66
66
|
it "should return nil" do
|
67
|
-
controller.current_user_id.
|
67
|
+
expect(controller.current_user_id).to eq nil
|
68
68
|
end
|
69
69
|
end
|
70
70
|
end
|
@@ -73,7 +73,7 @@ describe Authentication do
|
|
73
73
|
context 'when logged in' do
|
74
74
|
it "should return true" do
|
75
75
|
controller.login! user
|
76
|
-
controller.
|
76
|
+
expect(controller).to be_logged_in
|
77
77
|
end
|
78
78
|
end
|
79
79
|
end
|
@@ -83,8 +83,8 @@ describe Authentication do
|
|
83
83
|
it "should set current_user to nil" do
|
84
84
|
controller.login! user
|
85
85
|
controller.logout!
|
86
|
-
controller.current_user.
|
87
|
-
controller.session[:current_user_id].
|
86
|
+
expect(controller.current_user).to eq nil
|
87
|
+
expect(controller.session[:current_user_id]).to eq nil
|
88
88
|
end
|
89
89
|
end
|
90
90
|
end
|
metadata
CHANGED
@@ -1,46 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: authentication
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.2
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Fujimura Daisuke
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2015-11-24 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rake
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rspec
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - ">="
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '0'
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - ">="
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '0'
|
46
41
|
description: Minimalist authentication library for Ruby
|
@@ -50,8 +45,8 @@ executables: []
|
|
50
45
|
extensions: []
|
51
46
|
extra_rdoc_files: []
|
52
47
|
files:
|
53
|
-
- .gitignore
|
54
|
-
- .travis.yml
|
48
|
+
- ".gitignore"
|
49
|
+
- ".travis.yml"
|
55
50
|
- Gemfile
|
56
51
|
- Gemfile.lock
|
57
52
|
- LICENSE.txt
|
@@ -59,34 +54,33 @@ files:
|
|
59
54
|
- Rakefile
|
60
55
|
- authentication.gemspec
|
61
56
|
- lib/authentication.rb
|
57
|
+
- lib/authentication/authenticator.rb
|
62
58
|
- lib/authentication/version.rb
|
63
59
|
- spec/lib/authentication_spec.rb
|
64
60
|
- spec/spec_helper.rb
|
65
61
|
homepage: https://github.com/fujimura/authentication
|
66
62
|
licenses: []
|
63
|
+
metadata: {}
|
67
64
|
post_install_message:
|
68
65
|
rdoc_options: []
|
69
66
|
require_paths:
|
70
67
|
- lib
|
71
68
|
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
-
none: false
|
73
69
|
requirements:
|
74
|
-
- -
|
70
|
+
- - ">="
|
75
71
|
- !ruby/object:Gem::Version
|
76
72
|
version: '0'
|
77
73
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
-
none: false
|
79
74
|
requirements:
|
80
|
-
- -
|
75
|
+
- - ">="
|
81
76
|
- !ruby/object:Gem::Version
|
82
77
|
version: '0'
|
83
78
|
requirements: []
|
84
79
|
rubyforge_project: authentication
|
85
|
-
rubygems_version:
|
80
|
+
rubygems_version: 2.4.5.1
|
86
81
|
signing_key:
|
87
|
-
specification_version:
|
82
|
+
specification_version: 4
|
88
83
|
summary: Minimalist authentication library for Ruby
|
89
84
|
test_files:
|
90
85
|
- spec/lib/authentication_spec.rb
|
91
86
|
- spec/spec_helper.rb
|
92
|
-
has_rdoc:
|