authstrategies 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 +15 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/authstrategies.gemspec +29 -0
- data/lib/authstrategies.rb +97 -0
- data/lib/authstrategies/helpers.rb +19 -0
- data/lib/authstrategies/models/User.rb +55 -0
- data/lib/authstrategies/version.rb +3 -0
- data/lib/authstrategies/views/login.erb +8 -0
- data/lib/authstrategies/views/signup.erb +8 -0
- metadata +155 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZTI1NjViYTUxZjViNTU1ZjZlN2UzZjllNjc1ZWRkNGNiZDliMDZjNA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
OGEwMWEwNWM1NzI1MTM2NzkwYjdkNWVjMTNkYWMzZjgyNmU0M2JlMQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MTI1ZjI4ZDY0MjJhNzEwMDNjNDQ1YTA5NGY0ODg5MTcxMWIzNTJiMTU5NGI4
|
10
|
+
ZjU3MTEwNzBjZGU1ZDk2NjE4ODBjYmUyZTlhYmYxYjM5Njk0MmM5YmRiMDFh
|
11
|
+
ZTBmZjVmNzQyODNjY2RlNTBmY2Q5NWE5NzRhYjAxODhkZGFjNjA=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NWRjYzY0NmY3MzUxMTY0NGNiYWM3MWVhMzg4ODBlMTVmNjRmNzlkMWU5Zjg2
|
14
|
+
NTdkYjQ4NjkzMGFhN2YyMjJiYzY3ZGQ5YTg2YjdmNmRmMjFiNzdlZmI3ZTk2
|
15
|
+
M2M1YTcxNDZhYzA0MGNjMWJmMmNlMzBkODAxYjA1Mzg0YmI5MDY=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Dobromir Ivanov
|
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,29 @@
|
|
1
|
+
# Authstrategies
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'authstrategies'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install authstrategies
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'authstrategies/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "authstrategies"
|
8
|
+
spec.version = Authstrategies::VERSION
|
9
|
+
spec.authors = ["Dobromir Ivanov"]
|
10
|
+
spec.email = ["dobromir0ivanov@gmail.com"]
|
11
|
+
spec.description = %q{AuthStrategies is a Warden implementation for sinatra. For now it only implements a password strategy.}
|
12
|
+
spec.summary = %q{Warden implementation for Sinatra}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
|
24
|
+
spec.add_runtime_dependency "sinatra"
|
25
|
+
spec.add_runtime_dependency "sinatra-activerecord"
|
26
|
+
spec.add_runtime_dependency "activerecord"
|
27
|
+
spec.add_runtime_dependency "warden"
|
28
|
+
spec.add_runtime_dependency "bcrypt-ruby"
|
29
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require "authstrategies/version"
|
2
|
+
require "warden"
|
3
|
+
require "rack-flash"
|
4
|
+
require "sinatra/base"
|
5
|
+
require "active_record"
|
6
|
+
require "authstrategies/models/User"
|
7
|
+
require "authstrategies/helpers"
|
8
|
+
|
9
|
+
module Authstrategies
|
10
|
+
private
|
11
|
+
class Warden::SessionSerializer
|
12
|
+
def serialize(user)
|
13
|
+
user.id
|
14
|
+
end
|
15
|
+
|
16
|
+
def deserialize(id)
|
17
|
+
User.find(id)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class PasswordStrategy < Warden::Strategies::Base
|
22
|
+
def valid?
|
23
|
+
!!(request["email"] && request["password"])
|
24
|
+
end
|
25
|
+
|
26
|
+
def authenticate!
|
27
|
+
user = User.find_by_email(request["email"])
|
28
|
+
if user && user.authenticate(request)
|
29
|
+
success!(user)
|
30
|
+
else
|
31
|
+
fail!("Invalid username or password")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
module Base
|
37
|
+
def self.registered(app)
|
38
|
+
app.helpers Helpers
|
39
|
+
app.use Warden::Manager do |manager|
|
40
|
+
manager.failure_app = app
|
41
|
+
manager.default_strategies :password
|
42
|
+
end
|
43
|
+
|
44
|
+
Warden::Strategies.add(:password, Strategies::PasswordStrategy)
|
45
|
+
|
46
|
+
app.get '/login/?' do
|
47
|
+
redirect '/' if authenticated?
|
48
|
+
erb :login
|
49
|
+
end
|
50
|
+
|
51
|
+
app.post '/login' do
|
52
|
+
redirect '/' if authenticated?
|
53
|
+
authenticate!
|
54
|
+
if authenticated?
|
55
|
+
flash[:notice] = "Logged in successfully!"
|
56
|
+
redirect '/'
|
57
|
+
else
|
58
|
+
flash[:error] = env["warden"].message
|
59
|
+
redirect '/login'
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
app.get '/logout/?' do
|
64
|
+
logout
|
65
|
+
flash[:notice] = "Successfully logged out!"
|
66
|
+
redirect '/'
|
67
|
+
end
|
68
|
+
|
69
|
+
app.post '/unauthenticated' do
|
70
|
+
flash[:error] = "Invalid username or password!"
|
71
|
+
redirect '/login'
|
72
|
+
end
|
73
|
+
|
74
|
+
app.get '/signup/?' do
|
75
|
+
redirect '/' if authenticated?
|
76
|
+
erb :signup
|
77
|
+
end
|
78
|
+
|
79
|
+
app.post '/signup' do
|
80
|
+
redirect '/' if authenticated?
|
81
|
+
user = User.new(params)
|
82
|
+
if user.valid?
|
83
|
+
user.save
|
84
|
+
flash[:notice] = "Successfully signed up!"
|
85
|
+
redirect '/'
|
86
|
+
else
|
87
|
+
flash[:error] = user.errors.messages
|
88
|
+
redirect '/login'
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
class Middleware < Sinatra::Base
|
95
|
+
register Base
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Authstrategies
|
2
|
+
module Helpers
|
3
|
+
def authenticate!
|
4
|
+
env['warden'].authenticate!
|
5
|
+
end
|
6
|
+
|
7
|
+
def authenticated?
|
8
|
+
env['warden'].authenticated?
|
9
|
+
end
|
10
|
+
|
11
|
+
def current_user
|
12
|
+
env['warden'].user
|
13
|
+
end
|
14
|
+
|
15
|
+
def logout
|
16
|
+
env['warden'].logout
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'bcrypt'
|
2
|
+
require 'activerecord'
|
3
|
+
|
4
|
+
class User < ActiveRecord::Base
|
5
|
+
include BCrypt
|
6
|
+
|
7
|
+
validates :email, :password, presence: true
|
8
|
+
validates :email, uniqueness: true
|
9
|
+
|
10
|
+
validates :password, confirmation: true
|
11
|
+
validates :password, length: { in: 8..20,
|
12
|
+
too_long: "%{count} is the maximum allowed!",
|
13
|
+
too_short: "must be at least %{count}" }
|
14
|
+
|
15
|
+
before_save :encrypt_password
|
16
|
+
#before_save :set_confirm_token
|
17
|
+
|
18
|
+
def password
|
19
|
+
@password ||= Password.new(encrypted_password)
|
20
|
+
end
|
21
|
+
|
22
|
+
def password= password
|
23
|
+
@password = password
|
24
|
+
end
|
25
|
+
|
26
|
+
def authenticate request
|
27
|
+
if self.password == request["password"]
|
28
|
+
true
|
29
|
+
else
|
30
|
+
false
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def remember_me!
|
35
|
+
self.update_attributes(:remember_token => new_token)
|
36
|
+
end
|
37
|
+
|
38
|
+
def forget_me!
|
39
|
+
self.update_attributes(:remember_token => nil)
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
def encrypt_password
|
44
|
+
self.encrypted_password = Password.create(@password)
|
45
|
+
end
|
46
|
+
|
47
|
+
def set_confirm_token
|
48
|
+
self.confirm_token = new_token
|
49
|
+
end
|
50
|
+
|
51
|
+
def new_token
|
52
|
+
Password.create(Time.new.to_s)
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
<%= flash[:error] %>
|
2
|
+
<%= flash[:notice] %>
|
3
|
+
<form action="login" method="post">
|
4
|
+
<p>Username:<input type="text" name="email" /></p>
|
5
|
+
<p>Password:<input type="password" name="password" /></p>
|
6
|
+
<p>Remember Me: <input type="radio" name="remember_me" /></p>
|
7
|
+
<p><input type="submit" name="Submit" /></p>
|
8
|
+
</form>
|
@@ -0,0 +1,8 @@
|
|
1
|
+
<%= flash[:error] %>
|
2
|
+
<%= flash[:notice] %>
|
3
|
+
<form action="signup" method="post">
|
4
|
+
<p>E-mail:<input type="text" name="email" /></p>
|
5
|
+
<p>Password:<input type="password" name="password" /></p>
|
6
|
+
<p>Confirm password:<input type="password" name="password_confirmation" /></p>
|
7
|
+
<input type="submit" value="Register!" />
|
8
|
+
</form>
|
metadata
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: authstrategies
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dobromir Ivanov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: sinatra
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sinatra-activerecord
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activerecord
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: warden
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ! '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: bcrypt-ruby
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ! '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ! '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: AuthStrategies is a Warden implementation for sinatra. For now it only
|
112
|
+
implements a password strategy.
|
113
|
+
email:
|
114
|
+
- dobromir0ivanov@gmail.com
|
115
|
+
executables: []
|
116
|
+
extensions: []
|
117
|
+
extra_rdoc_files: []
|
118
|
+
files:
|
119
|
+
- .gitignore
|
120
|
+
- Gemfile
|
121
|
+
- LICENSE.txt
|
122
|
+
- README.md
|
123
|
+
- Rakefile
|
124
|
+
- authstrategies.gemspec
|
125
|
+
- lib/authstrategies.rb
|
126
|
+
- lib/authstrategies/helpers.rb
|
127
|
+
- lib/authstrategies/models/User.rb
|
128
|
+
- lib/authstrategies/version.rb
|
129
|
+
- lib/authstrategies/views/login.erb
|
130
|
+
- lib/authstrategies/views/signup.erb
|
131
|
+
homepage: ''
|
132
|
+
licenses:
|
133
|
+
- MIT
|
134
|
+
metadata: {}
|
135
|
+
post_install_message:
|
136
|
+
rdoc_options: []
|
137
|
+
require_paths:
|
138
|
+
- lib
|
139
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
140
|
+
requirements:
|
141
|
+
- - ! '>='
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: '0'
|
144
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
145
|
+
requirements:
|
146
|
+
- - ! '>='
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
version: '0'
|
149
|
+
requirements: []
|
150
|
+
rubyforge_project:
|
151
|
+
rubygems_version: 2.1.11
|
152
|
+
signing_key:
|
153
|
+
specification_version: 4
|
154
|
+
summary: Warden implementation for Sinatra
|
155
|
+
test_files: []
|