shield-contrib 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2009 Michel Martens, Damian Janowski and Cyril David
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.markdown ADDED
File without changes
data/Rakefile ADDED
File without changes
@@ -0,0 +1,8 @@
1
+ module Shield
2
+ module Contrib
3
+ VERSION = "0.0.1"
4
+ end
5
+
6
+ autoload :Ohm, "shield/contrib/ohm"
7
+ autoload :Sinatra, "shield/contrib/sinatra"
8
+ end
@@ -0,0 +1,8 @@
1
+ require "shield"
2
+ require "ohm"
3
+
4
+ module Shield
5
+ module Ohm
6
+ autoload :EmailPassword, "shield/contrib/ohm/email_password"
7
+ end
8
+ end
@@ -0,0 +1,50 @@
1
+ module Shield
2
+ module Ohm
3
+ module EmailPassword
4
+ def self.included(model)
5
+ model.extend Shield::Model
6
+ model.extend Fetch
7
+ model.attribute :email
8
+ model.index :email
9
+
10
+ model.attribute :crypted_password
11
+ end
12
+
13
+ EMAIL_REGEX = /^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i
14
+
15
+ module Fetch
16
+ def fetch(email)
17
+ find(:email => email).first
18
+ end
19
+ end
20
+
21
+ attr_reader :password
22
+ attr_accessor :password_confirmation
23
+
24
+ def validate
25
+ super
26
+
27
+ assert_present(:email) && assert_email(:email) && assert_unique(:email)
28
+
29
+ assert_present(:password) if new?
30
+
31
+ unless password.to_s.empty?
32
+ assert password == password_confirmation, [:password, :not_confirmed]
33
+ end
34
+ end
35
+
36
+ def password=(password)
37
+ unless password.to_s.empty?
38
+ self.crypted_password = Shield::Password.encrypt(password)
39
+ end
40
+
41
+ @password = password
42
+ end
43
+
44
+ private
45
+ def assert_email(att, error = [att, :not_email])
46
+ assert_format(att, EMAIL_REGEX, error)
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,8 @@
1
+ require "shield"
2
+ require "sinatra/base"
3
+
4
+ module Shield
5
+ module Sinatra
6
+ autoload :Default, "shield/contrib/sinatra/default"
7
+ end
8
+ end
@@ -0,0 +1,50 @@
1
+ module Shield
2
+ module Sinatra
3
+ module Default
4
+ def self.registered(m)
5
+ m.set :shield_redirect_after_login, "/"
6
+ m.set :shield_redirect_after_logout, "/"
7
+ m.set :shield_auth_failure, "Wrong Username and/or Password combination."
8
+
9
+ m.enable :sessions
10
+
11
+ m.helpers Shield::Helpers
12
+ m.helpers do
13
+ def current_user
14
+ authenticated(User)
15
+ end
16
+
17
+ def is_logged_in?
18
+ !! current_user
19
+ end
20
+
21
+ def ensure_authenticated(model = User)
22
+ super
23
+ end
24
+
25
+ def logout(model = User)
26
+ super
27
+ end
28
+ end
29
+
30
+ m.get "/login" do
31
+ haml :login
32
+ end
33
+
34
+ m.post "/login" do
35
+ if login(User, params[:username], params[:password])
36
+ redirect settings.shield_redirect_after_login
37
+ else
38
+ session[:error] = settings.shield_auth_failure
39
+ redirect "/login"
40
+ end
41
+ end
42
+
43
+ m.get "/logout" do
44
+ logout
45
+ redirect settings.shield_redirect_after_logout
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,32 @@
1
+ $:.unshift(File.expand_path("../lib", File.dirname(__FILE__)))
2
+
3
+ puts "!! REMOVE THIS IN LINE: #{__LINE__} of #{__FILE__}"
4
+ $:.unshift("/Users/cyx/Labs/shield/lib")
5
+
6
+ require "shield/contrib"
7
+ require "spawn"
8
+ require "ohm"
9
+ require "rack/test"
10
+ require "sinatra/base"
11
+ require "haml"
12
+
13
+ prepare { Ohm.flush }
14
+
15
+ def setup(&block)
16
+ @_setup = block if block_given?
17
+ @_setup
18
+ end
19
+
20
+ class Cutest::Scope
21
+ include Rack::Test::Methods
22
+
23
+ def assert_redirected_to(path)
24
+ assert 302 == last_response.status
25
+ assert path == last_response.headers["Location"]
26
+ end
27
+
28
+
29
+ def session
30
+ last_request.env["rack.session"]
31
+ end
32
+ end
@@ -0,0 +1,97 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+
3
+ class User < Ohm::Model
4
+ include Shield::Ohm::EmailPassword
5
+
6
+ extend Spawn
7
+ spawner do |u|
8
+ u.email = "quentin@test.com"
9
+ u.password = "password"
10
+ u.password_confirmation = "password"
11
+ end
12
+ end
13
+
14
+ test "adds email and password attributes and indices" do
15
+ assert User.attributes.include?(:email)
16
+ assert User.attributes.include?(:crypted_password)
17
+
18
+ assert User.indices.include?(:email)
19
+ end
20
+
21
+ # email validation
22
+ scope do
23
+ setup do
24
+ User.new
25
+ end
26
+
27
+ test "assert present email" do |u|
28
+ u.email = ""
29
+ assert ! u.valid?
30
+ assert u.errors.include?([:email, :not_present])
31
+ end
32
+
33
+ test "assert email format" do |u|
34
+ u.email = "q"
35
+ assert ! u.valid?
36
+ assert u.errors.include?([:email, :not_email])
37
+ end
38
+
39
+ test "assert unique email" do |u|
40
+ User.spawn(:email => "quentin@test.com")
41
+
42
+ u.email = "quentin@test.com"
43
+ assert ! u.valid?
44
+ assert u.errors.include?([:email, :not_unique])
45
+ end
46
+ end
47
+
48
+ # password validation on a new record
49
+ scope do
50
+ setup do
51
+ User.new
52
+ end
53
+
54
+ test "assert present password" do |u|
55
+ u.password = ""
56
+ assert ! u.valid?
57
+ assert u.errors.include?([:password, :not_present])
58
+ end
59
+
60
+ test "assert confirmed password" do |u|
61
+ u.password = "foobar"
62
+ assert ! u.valid?
63
+ assert u.errors.include?([:password, :not_confirmed])
64
+ end
65
+ end
66
+
67
+ # password validation on an existing record
68
+ scope do
69
+ setup do
70
+ User.spawn(:email => "quentin@test.com")
71
+ end
72
+
73
+ test "no assert present when existing" do |u|
74
+ u.password = ""
75
+ u.password_confirmation = ""
76
+
77
+ assert u.valid?
78
+ end
79
+
80
+ test "assert confirmed password when existing" do |u|
81
+ u.password = "foobar"
82
+ assert ! u.valid?
83
+ assert u.errors.include?([:password, :not_confirmed])
84
+ end
85
+
86
+ test "password is not persisted when set as an empty string" do |u|
87
+ u.password = ""
88
+ assert u.save
89
+
90
+ assert u == User.authenticate("quentin@test.com", "password")
91
+ end
92
+ end
93
+
94
+ test "implements fetch" do
95
+ quentin = User.spawn(:email => "quentin@test.com")
96
+ assert quentin == User.authenticate("quentin@test.com", "password")
97
+ end
data/test/ohm_test.rb ADDED
@@ -0,0 +1,12 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+
3
+ test "autoloads Ohm" do
4
+ ex = nil
5
+ begin
6
+ Shield::Ohm
7
+ rescue Exception => e
8
+ ex = e
9
+ end
10
+
11
+ assert nil == e
12
+ end
@@ -0,0 +1,53 @@
1
+ require File.expand_path("helper", File.dirname(__FILE__))
2
+
3
+ class App < Sinatra::Base
4
+ register Shield::Sinatra::Default
5
+
6
+ set :shield_redirect_after_login, "/dashboard"
7
+ set :shield_redirect_after_logout, "/thank-you"
8
+
9
+ set :views, File.join(File.dirname(__FILE__), "fixtures", "views")
10
+
11
+ get "/dashboard" do
12
+ ensure_authenticated
13
+
14
+ "Dashboard"
15
+ end
16
+
17
+ get "/thank-you" do
18
+ "Thank you."
19
+ end
20
+ end
21
+
22
+ class User < Struct.new(:id)
23
+ def self.authenticate(username, password)
24
+ new(1001) if ["quentin", "password"] == [username, password]
25
+ end
26
+ end
27
+ scope do
28
+ def app
29
+ App.new
30
+ end
31
+
32
+ test "GET /login" do
33
+ get "/login"
34
+ assert "<h1>Login</h1>\n" == last_response.body
35
+ end
36
+
37
+ test "POST /login" do
38
+ post "/login", :username => "quentin", :password => "password"
39
+ assert 1001 == session["User"]
40
+
41
+ assert_redirected_to "/dashboard"
42
+ follow_redirect!
43
+
44
+ assert "Dashboard" == last_response.body
45
+ end
46
+
47
+ test "GET /logout" do
48
+ post "/login", :username => "quentin", :password => "password"
49
+ get "/logout"
50
+
51
+ assert_redirected_to "/thank-you"
52
+ end
53
+ end
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shield-contrib
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Michel Martens
13
+ - Damian Janowski
14
+ - Cyril David
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-10-21 00:00:00 +08:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: shield
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: cutest
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :development
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: spawn
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ type: :development
60
+ version_requirements: *id003
61
+ - !ruby/object:Gem::Dependency
62
+ name: ohm
63
+ prerelease: false
64
+ requirement: &id004 !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ type: :development
73
+ version_requirements: *id004
74
+ - !ruby/object:Gem::Dependency
75
+ name: rack-test
76
+ prerelease: false
77
+ requirement: &id005 !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ type: :development
86
+ version_requirements: *id005
87
+ - !ruby/object:Gem::Dependency
88
+ name: sinatra
89
+ prerelease: false
90
+ requirement: &id006 !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ segments:
96
+ - 0
97
+ version: "0"
98
+ type: :development
99
+ version_requirements: *id006
100
+ - !ruby/object:Gem::Dependency
101
+ name: haml
102
+ prerelease: false
103
+ requirement: &id007 !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ segments:
109
+ - 0
110
+ version: "0"
111
+ type: :development
112
+ version_requirements: *id007
113
+ description: "\n Provides various specific implementations based on the Shield protocol.\n Examples include a drop-in Sinatra Extension, an Ohm specific model,\n and a Sequel specific model.\n "
114
+ email:
115
+ - michel@soveran.com
116
+ - djanowski@dimaion.com
117
+ - cyx@pipetodevnull.com
118
+ executables: []
119
+
120
+ extensions: []
121
+
122
+ extra_rdoc_files: []
123
+
124
+ files:
125
+ - lib/shield/contrib/ohm/email_password.rb
126
+ - lib/shield/contrib/ohm.rb
127
+ - lib/shield/contrib/sinatra/default.rb
128
+ - lib/shield/contrib/sinatra.rb
129
+ - lib/shield/contrib.rb
130
+ - README.markdown
131
+ - LICENSE
132
+ - Rakefile
133
+ - test/helper.rb
134
+ - test/ohm_email_password_test.rb
135
+ - test/ohm_test.rb
136
+ - test/sinatra_default_test.rb
137
+ has_rdoc: true
138
+ homepage: http://github.com/cyx/shield-contrib
139
+ licenses: []
140
+
141
+ post_install_message:
142
+ rdoc_options: []
143
+
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ none: false
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ segments:
152
+ - 0
153
+ version: "0"
154
+ required_rubygems_version: !ruby/object:Gem::Requirement
155
+ none: false
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ segments:
160
+ - 0
161
+ version: "0"
162
+ requirements: []
163
+
164
+ rubyforge_project: shield-contrib
165
+ rubygems_version: 1.3.7
166
+ signing_key:
167
+ specification_version: 3
168
+ summary: A collection of implementations based on the Shield protocol.
169
+ test_files: []
170
+