authentication 0.0.1
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.
- data/.gitignore +18 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +26 -0
- data/LICENSE.txt +22 -0
- data/README.md +21 -0
- data/Rakefile +7 -0
- data/authentication.gemspec +24 -0
- data/lib/authentication.rb +45 -0
- data/lib/authentication/version.rb +3 -0
- data/spec/lib/authentication_spec.rb +91 -0
- data/spec/spec_helper.rb +8 -0
- metadata +92 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
authentication (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.1.3)
|
10
|
+
rake (0.9.2.2)
|
11
|
+
rspec (2.11.0)
|
12
|
+
rspec-core (~> 2.11.0)
|
13
|
+
rspec-expectations (~> 2.11.0)
|
14
|
+
rspec-mocks (~> 2.11.0)
|
15
|
+
rspec-core (2.11.1)
|
16
|
+
rspec-expectations (2.11.3)
|
17
|
+
diff-lcs (~> 1.1.3)
|
18
|
+
rspec-mocks (2.11.3)
|
19
|
+
|
20
|
+
PLATFORMS
|
21
|
+
ruby
|
22
|
+
|
23
|
+
DEPENDENCIES
|
24
|
+
authentication!
|
25
|
+
rake
|
26
|
+
rspec
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Fujimura Daisuke
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# Authentication [<img src="https://secure.travis-ci.org/fujimura/authentication.png"/>](http://travis-ci.org/fujimura/authentication) [](https://codeclimate.com/github/fujimura/authentication)
|
2
|
+
|
3
|
+
## Minimalist authentication library for Ruby
|
4
|
+
|
5
|
+
## How to use
|
6
|
+
|
7
|
+
See an [example](https://github.com/fujimura/authentication-rails-example) or [spec](https://github.com/fujimura/authentication/blob/master/spec/authentication_spec.rb)
|
8
|
+
|
9
|
+
## Requirements
|
10
|
+
|
11
|
+
* Included class should have a method called `find_current_user` which returns the user object.
|
12
|
+
* Included class should have a method called `session` method which returns Hash-like object.
|
13
|
+
|
14
|
+
## Side-effects
|
15
|
+
|
16
|
+
* ```session[:current_user_id]``` in included module will be changed.
|
17
|
+
* ```@current_user``` in included module will be changed.
|
18
|
+
|
19
|
+
## Goal
|
20
|
+
|
21
|
+
The goal of this library is to provide standard authorization mechanism for web application with minimal dependency by short and concise code which can be understand easily.
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'authentication/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "authentication"
|
8
|
+
gem.version = Authentication::VERSION
|
9
|
+
gem.authors = ["Fujimura Daisuke"]
|
10
|
+
gem.email = ["me@fujimuradaisuke.com"]
|
11
|
+
gem.description = %q{Minimalist authentication library for Ruby}
|
12
|
+
gem.summary = %q{Minimalist authentication library for Ruby}
|
13
|
+
gem.homepage = "https://github.com/fujimura/authentication"
|
14
|
+
|
15
|
+
gem.rubyforge_project = "authentication"
|
16
|
+
|
17
|
+
gem.files = `git ls-files`.split($/)
|
18
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
19
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
20
|
+
gem.require_paths = ["lib"]
|
21
|
+
|
22
|
+
gem.add_development_dependency "rake"
|
23
|
+
gem.add_development_dependency "rspec"
|
24
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "authentication/version"
|
2
|
+
|
3
|
+
module Authentication
|
4
|
+
|
5
|
+
class Unauthenticated < StandardError; end
|
6
|
+
|
7
|
+
# Log in with given object.
|
8
|
+
#
|
9
|
+
# @param [Object] user The object you want to store as `current_user`.
|
10
|
+
def login!(user)
|
11
|
+
raise Unauthenticated unless user
|
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?
|
37
|
+
end
|
38
|
+
|
39
|
+
# Delete current_user from database and session.
|
40
|
+
#
|
41
|
+
def logout!
|
42
|
+
return unless current_user
|
43
|
+
@current_user = session[:current_user_id] = nil
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Controller
|
4
|
+
include Authentication
|
5
|
+
attr_accessor :session
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
self.session = {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def find_current_user success = true
|
12
|
+
if session[:current_user_id]
|
13
|
+
true
|
14
|
+
else
|
15
|
+
nil
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class User
|
21
|
+
def initialize(attributes)
|
22
|
+
@attributes = attributes
|
23
|
+
end
|
24
|
+
def id
|
25
|
+
@attributes[:id]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe Authentication do
|
30
|
+
let(:controller) { Controller.new }
|
31
|
+
let(:user) { User.new(id: 300) }
|
32
|
+
|
33
|
+
describe '#login!' do
|
34
|
+
context 'with nil' do
|
35
|
+
it "should raise Unauthenticated" do
|
36
|
+
expect do
|
37
|
+
controller.login! nil
|
38
|
+
end.to raise_error Authentication::Unauthenticated
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe '#current_user' do
|
44
|
+
context 'when logged in' do
|
45
|
+
it "should return user" do
|
46
|
+
controller.login! user
|
47
|
+
controller.current_user.should == user
|
48
|
+
end
|
49
|
+
end
|
50
|
+
context 'when not logged in' do
|
51
|
+
it "should call #find_current_user and return its result" do
|
52
|
+
controller.should_receive(:find_current_user).and_return "something"
|
53
|
+
controller.current_user.should == "something"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe '#current_user_id' do
|
59
|
+
context 'when logged in' do
|
60
|
+
it "should return user_id" do
|
61
|
+
controller.login! user
|
62
|
+
controller.current_user_id.should == user.id
|
63
|
+
end
|
64
|
+
end
|
65
|
+
context 'when not logged in' do
|
66
|
+
it "should return nil" do
|
67
|
+
controller.current_user_id.should == nil
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe '#logged_in?' do
|
73
|
+
context 'when logged in' do
|
74
|
+
it "should return true" do
|
75
|
+
controller.login! user
|
76
|
+
controller.logged_in?.should == true
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe '#logout!' do
|
82
|
+
context 'when logged in' do
|
83
|
+
it "should set current_user to nil" do
|
84
|
+
controller.login! user
|
85
|
+
controller.logout!
|
86
|
+
controller.current_user.should == nil
|
87
|
+
controller.session[:current_user_id].should == nil
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: authentication
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Fujimura Daisuke
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Minimalist authentication library for Ruby
|
47
|
+
email:
|
48
|
+
- me@fujimuradaisuke.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- .travis.yml
|
55
|
+
- Gemfile
|
56
|
+
- Gemfile.lock
|
57
|
+
- LICENSE.txt
|
58
|
+
- README.md
|
59
|
+
- Rakefile
|
60
|
+
- authentication.gemspec
|
61
|
+
- lib/authentication.rb
|
62
|
+
- lib/authentication/version.rb
|
63
|
+
- spec/lib/authentication_spec.rb
|
64
|
+
- spec/spec_helper.rb
|
65
|
+
homepage: https://github.com/fujimura/authentication
|
66
|
+
licenses: []
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project: authentication
|
85
|
+
rubygems_version: 1.8.23
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: Minimalist authentication library for Ruby
|
89
|
+
test_files:
|
90
|
+
- spec/lib/authentication_spec.rb
|
91
|
+
- spec/spec_helper.rb
|
92
|
+
has_rdoc:
|