authn 1.0.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.
- data/.gitignore +40 -0
- data/.rvmrc +48 -0
- data/.travis.yml +1 -0
- data/CHANGELOG.md +8 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +152 -0
- data/Rakefile +31 -0
- data/authn.gemspec +27 -0
- data/lib/authn.rb +9 -0
- data/lib/authn/config.rb +19 -0
- data/lib/authn/model.rb +47 -0
- data/lib/authn/session.rb +72 -0
- data/lib/authn/version.rb +3 -0
- data/test/helper.rb +58 -0
- data/test/lib/authn/config_test.rb +17 -0
- data/test/lib/authn/model_test.rb +68 -0
- data/test/lib/authn/session_test.rb +98 -0
- data/test/lib/authn/version_test.rb +12 -0
- data/test/lib/authn_test.rb +16 -0
- metadata +158 -0
data/.gitignore
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# See http://help.github.com/ignore-files/ for more about ignoring files.
|
2
|
+
#
|
3
|
+
# If you find yourself ignoring temporary files generated by your text editor
|
4
|
+
# or operating system, you probably want to add a global ignore instead:
|
5
|
+
# git config --global core.excludesfile ~/.gitignore
|
6
|
+
|
7
|
+
# Ignore all of the generated gem stuff
|
8
|
+
/pkg
|
9
|
+
/*.gem
|
10
|
+
|
11
|
+
# Ignore bundler config
|
12
|
+
/.bundle
|
13
|
+
/Gemfile.lock
|
14
|
+
|
15
|
+
# Ignore all bundler caching
|
16
|
+
/vendor/cache
|
17
|
+
/vendor/ruby
|
18
|
+
|
19
|
+
# Ignore all tempfiles
|
20
|
+
/tmp
|
21
|
+
|
22
|
+
# Ignores that should be in the global gitignore
|
23
|
+
# /*.rbc
|
24
|
+
# /.config
|
25
|
+
# /.yardoc
|
26
|
+
# /InstalledFiles
|
27
|
+
# /_yardoc
|
28
|
+
# /coverage/
|
29
|
+
# /doc/
|
30
|
+
# /lib/bundler/man/
|
31
|
+
# /rdoc/
|
32
|
+
# /spec/reports/
|
33
|
+
# /test/tmp/
|
34
|
+
# /test/version_tmp/
|
35
|
+
|
36
|
+
# Ignore sub-gems
|
37
|
+
/lib/authn/activation
|
38
|
+
/lib/authn/recovery
|
39
|
+
/lib/authn/protection
|
40
|
+
/lib/authn/rails
|
data/.rvmrc
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
# This is an RVM Project .rvmrc file, used to automatically load the ruby
|
4
|
+
# development environment upon cd'ing into the directory
|
5
|
+
|
6
|
+
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
|
7
|
+
# Only full ruby name is supported here, for short names use:
|
8
|
+
# echo "rvm use 1.9.3" > .rvmrc
|
9
|
+
environment_id="ruby-1.9.3-p194@authn"
|
10
|
+
|
11
|
+
# Uncomment the following lines if you want to verify rvm version per project
|
12
|
+
# rvmrc_rvm_version="1.13.8 (master)" # 1.10.1 seams as a safe start
|
13
|
+
# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
|
14
|
+
# echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
|
15
|
+
# return 1
|
16
|
+
# }
|
17
|
+
|
18
|
+
# First we attempt to load the desired environment directly from the environment
|
19
|
+
# file. This is very fast and efficient compared to running through the entire
|
20
|
+
# CLI and selector. If you want feedback on which environment was used then
|
21
|
+
# insert the word 'use' after --create as this triggers verbose mode.
|
22
|
+
if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
|
23
|
+
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
|
24
|
+
then
|
25
|
+
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
|
26
|
+
[[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
|
27
|
+
\. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
|
28
|
+
else
|
29
|
+
# If the environment file has not yet been created, use the RVM CLI to select.
|
30
|
+
rvm --create "$environment_id" || {
|
31
|
+
echo "Failed to create RVM environment '${environment_id}'."
|
32
|
+
return 1
|
33
|
+
}
|
34
|
+
fi
|
35
|
+
|
36
|
+
# If you use bundler, this might be useful to you:
|
37
|
+
# if [[ -s Gemfile ]] && {
|
38
|
+
# ! builtin command -v bundle >/dev/null ||
|
39
|
+
# builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
|
40
|
+
# }
|
41
|
+
# then
|
42
|
+
# printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
|
43
|
+
# gem install bundler
|
44
|
+
# fi
|
45
|
+
# if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
|
46
|
+
# then
|
47
|
+
# bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
|
48
|
+
# fi
|
data/.travis.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
- 1.9.3
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Kurtis Rainbolt-Greene
|
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,152 @@
|
|
1
|
+
AuthN
|
2
|
+
-----
|
3
|
+
|
4
|
+
authn is a simplistic and barebones authentication library.
|
5
|
+
It only does a few things for you, such as:
|
6
|
+
|
7
|
+
* Give you a controller and helper method to access the "current user"
|
8
|
+
* Give you simple "sign in", "sign out", and "auto sign in" methods
|
9
|
+
* Works with any ORM that behaves likes ActiveModel
|
10
|
+
* Functions with any framework that has a session interface
|
11
|
+
|
12
|
+
In addition you can bring in other behavior sets like:
|
13
|
+
|
14
|
+
* authn-activation: Account activation
|
15
|
+
* authn-protection: Failed login protection
|
16
|
+
* authn-recovery: Password recovery
|
17
|
+
* rails-authn: Extend Rails with AuthN
|
18
|
+
|
19
|
+
Using AuthN
|
20
|
+
===========
|
21
|
+
|
22
|
+
To start using authn you simply need to install and hook up to your existing "user" model:
|
23
|
+
|
24
|
+
``` ruby
|
25
|
+
# create_table :accounts do |t|
|
26
|
+
# t.string :email, null: false, default: nil
|
27
|
+
# t.binary :password_digest, null: false, default: nil
|
28
|
+
# t.timestamps
|
29
|
+
# end
|
30
|
+
# add_index :accounts, :email, unique: true
|
31
|
+
#
|
32
|
+
|
33
|
+
class Account < ActiveRecord::Base
|
34
|
+
include AuthN::Model
|
35
|
+
|
36
|
+
has_authentication
|
37
|
+
has_secure_password
|
38
|
+
|
39
|
+
validates :email, uniqueness: true, presence: true, length: 5..255
|
40
|
+
validates :password, length: 10..1024
|
41
|
+
|
42
|
+
attr_accessible :email
|
43
|
+
attr_accessible :password, :password_confirmation
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
See?
|
48
|
+
No muss, no fuss.
|
49
|
+
Now what about all those addons?
|
50
|
+
We'll you can see their own pages, but here's a taste:
|
51
|
+
|
52
|
+
``` ruby
|
53
|
+
# create_table :accounts do |t|
|
54
|
+
# t.string :email, null: false, default: nil
|
55
|
+
# t.binary :password_digest, null: false, default: nil
|
56
|
+
#
|
57
|
+
# t.string :activation_token, default: nil
|
58
|
+
# t.boolean :activation_state, null: false, default: false
|
59
|
+
# t.datetime :activation_expires_at, default: nil
|
60
|
+
#
|
61
|
+
# t.string :password_recovery_token, default: nil
|
62
|
+
# t.datetime :password_recovery_expires_at, default: nil
|
63
|
+
#
|
64
|
+
# t.string :login_protection_token, default: nil
|
65
|
+
# t.datetime :login_protection_expires_at, default: nil
|
66
|
+
# t.integer :login_protection_attempts, default: 0
|
67
|
+
#
|
68
|
+
# t.timestamps
|
69
|
+
# end
|
70
|
+
# add_index :accounts, :email, unique: true
|
71
|
+
# add_index :accounts, :activation_token, unique: true
|
72
|
+
# add_index :accounts, :activation_state
|
73
|
+
# add_index :accounts, :password_recovery_token, unique: true
|
74
|
+
# add_index :accounts, :login_protection_token, unique: true
|
75
|
+
#
|
76
|
+
|
77
|
+
class Account < ActiveRecord::Base
|
78
|
+
include AuthN::Model
|
79
|
+
|
80
|
+
has_authentication
|
81
|
+
has_password_recovery mailer: PasswordRecoveryMailer
|
82
|
+
has_activation mailer: ActivationMailer, on_create: false
|
83
|
+
has_login_protection maximum: 3, redirect: { controller: :accounts, action: :maximum_login_failure }
|
84
|
+
has_secure_password
|
85
|
+
|
86
|
+
validates :email, uniqueness: true, presence: true, length: 5..255
|
87
|
+
validates :password, length: 10..1024
|
88
|
+
|
89
|
+
attr_accessible :email
|
90
|
+
attr_accessible :password, :password_confirmation
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
You'll notice that there are options after some of the addon singleton methods.
|
95
|
+
These are used to overwrite the global configuration.
|
96
|
+
authn assumes quite a few things, but never stops you from changing how it works.
|
97
|
+
As above you can change how each of your "user" models functions (for say admin recovery emails vs support recovery emails).
|
98
|
+
In addition you can either programatically write the "global" configuration or have a `authn.yml` file ready to be loaded.
|
99
|
+
|
100
|
+
Installing AuthN
|
101
|
+
================
|
102
|
+
|
103
|
+
Add this line to your application's Gemfile:
|
104
|
+
|
105
|
+
gem 'authn'
|
106
|
+
|
107
|
+
And then execute:
|
108
|
+
|
109
|
+
$ bundle
|
110
|
+
|
111
|
+
Or install it yourself as:
|
112
|
+
|
113
|
+
$ gem install authn
|
114
|
+
|
115
|
+
Looking for instructions on how to setup with Rails?
|
116
|
+
Find them over [here](https://github.com/krainboltgreene/rails-authn)
|
117
|
+
|
118
|
+
|
119
|
+
Contributing
|
120
|
+
============
|
121
|
+
|
122
|
+
1. Fork it
|
123
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
124
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
125
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
126
|
+
5. Create new Pull Request
|
127
|
+
|
128
|
+
License
|
129
|
+
=======
|
130
|
+
|
131
|
+
Copyright (c) 2012 Kurtis Rainbolt-Greene
|
132
|
+
|
133
|
+
MIT License
|
134
|
+
|
135
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
136
|
+
a copy of this software and associated documentation files (the
|
137
|
+
"Software"), to deal in the Software without restriction, including
|
138
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
139
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
140
|
+
permit persons to whom the Software is furnished to do so, subject to
|
141
|
+
the following conditions:
|
142
|
+
|
143
|
+
The above copyright notice and this permission notice shall be
|
144
|
+
included in all copies or substantial portions of the Software.
|
145
|
+
|
146
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
147
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
148
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
149
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
150
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
151
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
152
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
require 'rake/clean'
|
4
|
+
require 'rake/testtask'
|
5
|
+
require 'yard'
|
6
|
+
|
7
|
+
begin
|
8
|
+
Bundler.setup :default, :development
|
9
|
+
rescue Bundler::BundlerError => error
|
10
|
+
$stderr.puts error.message
|
11
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
12
|
+
exit error.status_code
|
13
|
+
end
|
14
|
+
|
15
|
+
Bundler::GemHelper.install_tasks
|
16
|
+
|
17
|
+
desc "Run all of the tests"
|
18
|
+
Rake::TestTask.new do |config|
|
19
|
+
config.libs << 'test'
|
20
|
+
config.pattern = 'test/**/*_test*'
|
21
|
+
config.verbose = true
|
22
|
+
config.warning = true
|
23
|
+
end
|
24
|
+
|
25
|
+
desc "Generate all of the docs"
|
26
|
+
YARD::Rake::YardocTask.new do |config|
|
27
|
+
config.files = Dir['lib/**/*.rb']
|
28
|
+
end
|
29
|
+
|
30
|
+
desc 'Default: run tests, and generate docs'
|
31
|
+
task :default => [ :test, :yard ]
|
data/authn.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'authn/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "authn"
|
8
|
+
gem.version = AuthN::VERSION
|
9
|
+
gem.authors = ["Kurtis Rainbolt-Greene"]
|
10
|
+
gem.email = ["kurtisrainboltgreene@gmail.com"]
|
11
|
+
gem.summary = %q{An incredibly bare bones authentication library}
|
12
|
+
gem.description = gem.summary
|
13
|
+
gem.homepage = "http://github.com/krainboltgreene/authn"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_runtime_dependency 'activemodel', '~> 3.0'
|
21
|
+
gem.add_runtime_dependency 'astruct', '~> 2.9'
|
22
|
+
gem.add_runtime_dependency 'bcrypt-ruby', '~> 3.0'
|
23
|
+
gem.add_development_dependency 'yard'
|
24
|
+
gem.add_development_dependency 'kramdown'
|
25
|
+
# gem.add_runtime_dependency 'gemname', '~> 1.0'
|
26
|
+
# gem.add_development_dependency 'gemname', '~> 1.0'
|
27
|
+
end
|
data/lib/authn.rb
ADDED
data/lib/authn/config.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
module AuthN
|
2
|
+
def self.config
|
3
|
+
@config ||= Config.new Config::DEFAULTS
|
4
|
+
end
|
5
|
+
|
6
|
+
class Config
|
7
|
+
include AltStruct::M
|
8
|
+
DEFAULTS = {
|
9
|
+
account_klass: :Account,
|
10
|
+
password_digest_method: :password_digest,
|
11
|
+
login_password_key: :password,
|
12
|
+
model_id_method: :id,
|
13
|
+
model_critera_method: :where,
|
14
|
+
session_key_function: ->(klass) {
|
15
|
+
:"session_#{klass}_id"
|
16
|
+
}
|
17
|
+
}
|
18
|
+
end
|
19
|
+
end
|
data/lib/authn/model.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
module AuthN
|
2
|
+
module Model
|
3
|
+
def self.included(object)
|
4
|
+
object.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def has_authentication(o = {})
|
9
|
+
options = AuthN.config.table.any? ? o.merge!(AuthN.config) : o
|
10
|
+
config.merge! options
|
11
|
+
end
|
12
|
+
|
13
|
+
def config=(options)
|
14
|
+
@@config = options
|
15
|
+
end
|
16
|
+
|
17
|
+
def config
|
18
|
+
@@config ||= AuthN::Config.new
|
19
|
+
end
|
20
|
+
|
21
|
+
def authenticate(identifiers = {})
|
22
|
+
# Extract the password from the identifiers
|
23
|
+
password = identifiers.delete AuthN.config.login_password_key
|
24
|
+
|
25
|
+
# Find the documents that match the criteria
|
26
|
+
criteria = send AuthN.config.model_critera_method, identifiers
|
27
|
+
|
28
|
+
# Get the first document that matches the criteria
|
29
|
+
instance = criteria.first
|
30
|
+
|
31
|
+
# Check to see if the instance exists and if a password was given
|
32
|
+
if instance && password
|
33
|
+
# Check to see if the instance can authenticate with password
|
34
|
+
if instance.authenticate password
|
35
|
+
instance
|
36
|
+
else
|
37
|
+
false
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Return instance if account was found and password matched
|
42
|
+
# Return false if account was found and password didn't match
|
43
|
+
# Return nil if account wasn't found OR password wasn't given
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module AuthN
|
2
|
+
module Session
|
3
|
+
def self.included(object)
|
4
|
+
object.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def login(identifiers, klass = AuthN.config.account_klass)
|
9
|
+
generate_session_and_instance_from find_instance_klass(klass).authenticate identifiers
|
10
|
+
end
|
11
|
+
|
12
|
+
def auto_login(instance)
|
13
|
+
instance_and_session instance
|
14
|
+
end
|
15
|
+
|
16
|
+
def logged_in?(instance = nil, klass = AuthN.config.account_klass)
|
17
|
+
klass = instance.class if instance
|
18
|
+
klass = find_instance_klass klass unless instance
|
19
|
+
check_session klass
|
20
|
+
end
|
21
|
+
|
22
|
+
def logout(instance = nil, klass = AuthN.config.account_klass)
|
23
|
+
klass = instance.class if instance
|
24
|
+
klass = find_instance_klass klass unless instance
|
25
|
+
destroy_session klass
|
26
|
+
end
|
27
|
+
|
28
|
+
def current_user(klass = AuthN.config.account_klass)
|
29
|
+
@current_user ||= get_session klass_as_name klass
|
30
|
+
end
|
31
|
+
|
32
|
+
alias_method :current_account, :current_user
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def find_instance_klass(klass)
|
37
|
+
const_get klass.capitalize
|
38
|
+
end
|
39
|
+
|
40
|
+
def klass_as_name(klass)
|
41
|
+
klass.name.downcase
|
42
|
+
end
|
43
|
+
|
44
|
+
def generate_session_and_instance_from(instance)
|
45
|
+
instance.tap { instance_and_session instance if instance }
|
46
|
+
end
|
47
|
+
|
48
|
+
def instance_and_session(instance)
|
49
|
+
instance.tap { |instance| create_session instance.class, instance }
|
50
|
+
end
|
51
|
+
|
52
|
+
def create_session(klass, instance)
|
53
|
+
key = AuthN.config.session_key_function.call klass_as_name klass
|
54
|
+
session[key] = instance.send AuthN.config.model_id_method
|
55
|
+
end
|
56
|
+
|
57
|
+
def destroy_session(klass)
|
58
|
+
key = AuthN.config.session_key_function.call klass_as_name klass
|
59
|
+
session.delete key
|
60
|
+
end
|
61
|
+
|
62
|
+
def check_session(klass)
|
63
|
+
get_session(klass).present?
|
64
|
+
end
|
65
|
+
|
66
|
+
def get_session(klass)
|
67
|
+
key = AuthN.config.session_key_function.call klass_as_name klass
|
68
|
+
session[key]
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require_relative '../lib/authn'
|
2
|
+
|
3
|
+
module ActiveModel::Model
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval do
|
6
|
+
extend ActiveModel::Naming
|
7
|
+
extend ActiveModel::Translation
|
8
|
+
include ActiveModel::Validations
|
9
|
+
include ActiveModel::Conversion
|
10
|
+
include ActiveModel::SecurePassword
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(params={})
|
15
|
+
params.each do |attr, value|
|
16
|
+
self.public_send("#{attr}=", value)
|
17
|
+
end if params
|
18
|
+
end
|
19
|
+
|
20
|
+
def persisted?
|
21
|
+
false
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Account
|
26
|
+
include ActiveModel::Model
|
27
|
+
include AuthN::Model
|
28
|
+
has_authentication
|
29
|
+
has_secure_password
|
30
|
+
attr_accessor :password_digest
|
31
|
+
|
32
|
+
def self.password(password=nil)
|
33
|
+
if password
|
34
|
+
@password = password
|
35
|
+
else
|
36
|
+
@password
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.where(hash)
|
41
|
+
account = if hash[:email] == "kurtis@example.com"
|
42
|
+
Account.new(password: password, password_confirmation: password)
|
43
|
+
end
|
44
|
+
[account]
|
45
|
+
end
|
46
|
+
|
47
|
+
def id
|
48
|
+
@id ||= rand(100000)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
class Controller
|
54
|
+
include AuthN::Session
|
55
|
+
def self.session
|
56
|
+
@session ||= {}
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require_relative '../../helper'
|
3
|
+
|
4
|
+
class TestAuthNConfig < MiniTest::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
|
7
|
+
end
|
8
|
+
|
9
|
+
# def self.config
|
10
|
+
# @config ||= Config.new
|
11
|
+
# end
|
12
|
+
def test_config_returns_config_object
|
13
|
+
actual = AuthN.config.class
|
14
|
+
expected = AuthN::Config
|
15
|
+
assert_equal expected, actual
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require_relative '../../helper'
|
3
|
+
|
4
|
+
class TestAuthNModel < MiniTest::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
|
7
|
+
end
|
8
|
+
|
9
|
+
# def has_authentication(o = {})
|
10
|
+
# options = AuthN::Config::DEFAULTS.dup
|
11
|
+
# options.merge! AuthN.config if AuthN.config.to_hash
|
12
|
+
# options.merge! o unless o.empty?
|
13
|
+
# config options
|
14
|
+
# end
|
15
|
+
def test_
|
16
|
+
actual =
|
17
|
+
expected =
|
18
|
+
assert_equal expected, actual
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_
|
22
|
+
actual =
|
23
|
+
expected =
|
24
|
+
assert_equal expected, actual
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_
|
28
|
+
actual =
|
29
|
+
expected =
|
30
|
+
assert_equal expected, actual
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_
|
34
|
+
actual =
|
35
|
+
expected =
|
36
|
+
assert_equal expected, actual
|
37
|
+
end
|
38
|
+
|
39
|
+
# def authenticate(identifiers = {})
|
40
|
+
# password = identifiers.delete :password
|
41
|
+
# criteria = where identifiers
|
42
|
+
# instance = criteria.first
|
43
|
+
# if instance && password
|
44
|
+
# if instance.authenticate password
|
45
|
+
# instance
|
46
|
+
# else
|
47
|
+
# false
|
48
|
+
# end
|
49
|
+
# end
|
50
|
+
# end
|
51
|
+
def test_
|
52
|
+
actual =
|
53
|
+
expected =
|
54
|
+
assert_equal expected, actual
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_
|
58
|
+
actual =
|
59
|
+
expected =
|
60
|
+
assert_equal expected, actual
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_
|
64
|
+
actual =
|
65
|
+
expected =
|
66
|
+
assert_equal expected, actual
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require_relative '../../helper'
|
3
|
+
|
4
|
+
class TestAuthNSession < MiniTest::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
AuthN.config.account_klass = :Account
|
7
|
+
Account.password "12341234"
|
8
|
+
Controller.instance_variable_set :@session, {}
|
9
|
+
end
|
10
|
+
|
11
|
+
# def login(identifiers, password, klass = AuthN.config.account_klass)
|
12
|
+
# generate_session_and_instance_from klass.authenticate identifiers, password
|
13
|
+
# end
|
14
|
+
def test_that_login_returns_account_given_good_credentials
|
15
|
+
instance = Controller.login email: "kurtis@example.com", password: "12341234"
|
16
|
+
actual = instance.class
|
17
|
+
expected = Account
|
18
|
+
assert_equal expected, actual
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_that_login_returns_false_if_bad_password
|
22
|
+
actual = Controller.login email: "kurtis@example.com", password: "12341233"
|
23
|
+
expected = false
|
24
|
+
assert_equal expected, actual
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_that_login_returns_nil_if_no_account_found
|
28
|
+
actual = Controller.login email: "kurti@example.com", password: "12341234"
|
29
|
+
expected = nil
|
30
|
+
assert_equal expected, actual
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_that_login_sets_session
|
34
|
+
Controller.login email: "kurtis@example.com", password: "12341234"
|
35
|
+
assert Controller.session[:session_account_id]
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_that_login_stores_session_account_id
|
39
|
+
instance = Controller.login email: "kurtis@example.com", password: "12341234"
|
40
|
+
actual = Controller.session[:session_account_id]
|
41
|
+
expected = instance.id
|
42
|
+
assert_equal expected, actual
|
43
|
+
end
|
44
|
+
|
45
|
+
# def auto_login(instance)
|
46
|
+
# instance_and_session instance
|
47
|
+
# end
|
48
|
+
def test_that_auto_login_returns_instance
|
49
|
+
instance = Controller.auto_login Account.new
|
50
|
+
actual = instance.class
|
51
|
+
expected = Account
|
52
|
+
assert_equal expected, actual
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_that_auto_login_sets_session
|
56
|
+
Controller.auto_login Account.new
|
57
|
+
assert Controller.session.has_key? :session_account_id
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_that_auto_login_stores_session_account_id
|
61
|
+
instance = Controller.auto_login Account.new
|
62
|
+
actual = Controller.session[:session_account_id]
|
63
|
+
expected = instance.id
|
64
|
+
assert_equal expected, actual
|
65
|
+
end
|
66
|
+
|
67
|
+
# def logged_in?(instance = nil, klass = AuthN.config.account_klass)
|
68
|
+
# klass = instance.class if instance
|
69
|
+
# klass = const_get(klass) unless instance
|
70
|
+
# check_session klass
|
71
|
+
# end
|
72
|
+
|
73
|
+
def test_that_logged_in_returns_true_if_logged_in
|
74
|
+
Controller.login email: "kurtis@example.com", password: "12341234"
|
75
|
+
actual = Controller.logged_in?
|
76
|
+
expected = true
|
77
|
+
assert_equal expected, actual
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_that_logged_in_returns_false_if_not_logged_in
|
81
|
+
Controller.session
|
82
|
+
actual = Controller.logged_in?
|
83
|
+
expected = false
|
84
|
+
assert_equal expected, actual
|
85
|
+
end
|
86
|
+
|
87
|
+
# def logout(instance = nil, klass = AuthN.config.account_klass)
|
88
|
+
# klass = instance.class if instance
|
89
|
+
# klass = const_get(klass) unless instance
|
90
|
+
# destroy_session klass
|
91
|
+
# end
|
92
|
+
|
93
|
+
def test_that_logout_empties_the_session
|
94
|
+
Controller.login email: "kurtis@example.com", password: "12341234"
|
95
|
+
Controller.logout
|
96
|
+
refute Controller.session.has_key? :session_account_id
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'helper'
|
3
|
+
|
4
|
+
class TestAuthN < MiniTest::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
# Setup logic
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_that_things_work
|
10
|
+
assert true
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_that_AuthN_is_defined
|
14
|
+
assert(defined?(:AuthN))
|
15
|
+
end
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: authn
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kurtis Rainbolt-Greene
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activemodel
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: astruct
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '2.9'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '2.9'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: bcrypt-ruby
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '3.0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: yard
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: kramdown
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: An incredibly bare bones authentication library
|
95
|
+
email:
|
96
|
+
- kurtisrainboltgreene@gmail.com
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files: []
|
100
|
+
files:
|
101
|
+
- .gitignore
|
102
|
+
- .rvmrc
|
103
|
+
- .travis.yml
|
104
|
+
- CHANGELOG.md
|
105
|
+
- Gemfile
|
106
|
+
- LICENSE.txt
|
107
|
+
- README.md
|
108
|
+
- Rakefile
|
109
|
+
- authn.gemspec
|
110
|
+
- lib/authn.rb
|
111
|
+
- lib/authn/config.rb
|
112
|
+
- lib/authn/model.rb
|
113
|
+
- lib/authn/session.rb
|
114
|
+
- lib/authn/version.rb
|
115
|
+
- test/helper.rb
|
116
|
+
- test/lib/authn/config_test.rb
|
117
|
+
- test/lib/authn/model_test.rb
|
118
|
+
- test/lib/authn/session_test.rb
|
119
|
+
- test/lib/authn/version_test.rb
|
120
|
+
- test/lib/authn_test.rb
|
121
|
+
homepage: http://github.com/krainboltgreene/authn
|
122
|
+
licenses: []
|
123
|
+
post_install_message:
|
124
|
+
rdoc_options: []
|
125
|
+
require_paths:
|
126
|
+
- lib
|
127
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
128
|
+
none: false
|
129
|
+
requirements:
|
130
|
+
- - ! '>='
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
segments:
|
134
|
+
- 0
|
135
|
+
hash: -4128468406629184695
|
136
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ! '>='
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
segments:
|
143
|
+
- 0
|
144
|
+
hash: -4128468406629184695
|
145
|
+
requirements: []
|
146
|
+
rubyforge_project:
|
147
|
+
rubygems_version: 1.8.24
|
148
|
+
signing_key:
|
149
|
+
specification_version: 3
|
150
|
+
summary: An incredibly bare bones authentication library
|
151
|
+
test_files:
|
152
|
+
- test/helper.rb
|
153
|
+
- test/lib/authn/config_test.rb
|
154
|
+
- test/lib/authn/model_test.rb
|
155
|
+
- test/lib/authn/session_test.rb
|
156
|
+
- test/lib/authn/version_test.rb
|
157
|
+
- test/lib/authn_test.rb
|
158
|
+
has_rdoc:
|