authstrategies 0.0.6 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MGY1ZDIxMDhhYWMxZmJiYmZiOWU5NzM4Y2EyZmY3NDA0NjBmYzRlYg==
4
+ NmY5MzNhOTc3ZDVlMGNiODg4OTkxZmY4ZjU2MTE1MmFjOTdmNGI2OQ==
5
5
  data.tar.gz: !binary |-
6
- MGUwMDE5ZDc0YTQ5ZGViNjZiNTczNmQxMWJlMTJlNTNlM2UwOWMxZQ==
6
+ ZjE4YmVhYjhhNDVhMmU2N2ZhOTBiZmRlNmU5M2VlYWRhM2E4OGE0Mg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NTk0MWUxOGU1MzRmMDk5YzhiYzAxMWQ3NWMwOWUxNzNhN2E4NzkxY2YxZWQ5
10
- NjEyNGVkNDFiMWYxNTQzNDc5NDczYjUxYmEwZGQwOGY1YTllNzExMGEwNTc2
11
- OTUwYzQ1NGZiYWNhYTU3ZTNhYzA3Y2Q1ZTMwZjVlYjE3YTY1ZTM=
9
+ OWMwZDJlZTQ5NTRlNzUyNTI4NWQ3ZjY0ODQ3MGQzYjAyNTU0ZmM5NWY3Y2Zi
10
+ NWFmMWNlMTI0N2IwZGNmYjNiOTRjMWFkOWMxNDI4NGNhYzg2ODZjNDIyYTA2
11
+ Y2EzNjVkNTQxZjlhMTAzOGNmZjk5ZGNmNTVmMzE5OTI3ZGQwNjQ=
12
12
  data.tar.gz: !binary |-
13
- YzI2NjViNzJlMzhkMzQ0NTljMGM1NDM5MjAyNDdiNDE2Mjk0NmQ3OWFjNmM0
14
- ZGJlNTJjYjY0MmNlYWViODk0ZTQzZDFhM2ZlZmU2YjRmYmY2NmQwNDU4YmFl
15
- ZGUyNWRmMDc2Y2FmMjUyMjdlNDYzZDRmM2MyZWI0ZThlNmNhOTk=
13
+ NzM4MDJiNzQzMWFhNmQ3MGY1YWZmMTBiZjAyZjI2ZjdhNWM3NGQwMzhiMjkw
14
+ MGJiNjliMDc1YjliYTZlOGI0NzEzN2FmOWJmNjQ3ZDQxOTU5ZWM5ZDNlMTRj
15
+ MTczMDEyZjQxZjczNmE5ODMxNDQ0NDYyZTc1ZTEwNjFjNWI1MTI=
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Authstrategies
2
2
 
3
- TODO: Write a gem description
3
+ Warden implementation for Sinatra
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,8 +18,56 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ Authstrategies uses sinatra-activerecord as orm. There is currently no rake task to generate a migration for the user model, but you can use the following: (courtesy of device)
22
22
 
23
+ def up
24
+ create_table :users do |t|
25
+ t.string :email, :null => false, :defautl => ""
26
+ t.string :encrypted_password, :null => false, :default => ""
27
+
28
+ t.string remember_token
29
+ t.boolean :remember_me
30
+
31
+ t.timestamps
32
+ end
33
+
34
+ add_index :users, :email, :unique => true
35
+ add_index :remember_token, :unique => true
36
+ end
37
+
38
+
39
+ After that your application should be configurad similarly to the following:
40
+
41
+ require 'authstrategies'
42
+
43
+ class YourApp < Sinatra::Application
44
+ use Rack::Session::Cookie, {
45
+ :secret => 'such secret many secure wow',
46
+ :expire_after => 3600
47
+ }
48
+ use Rack::Flash
49
+ use Authstrategies::Middleware
50
+ end
51
+
52
+ The expire after for Rack::Session::Cookie is optional, but I set it, because
53
+ some modern browsers will not delete session cookies after the user closes his browser like you would normally expect. This may pose a security thread if your users log in from a public computer.
54
+
55
+ If you want to use the helpers provided with authstrategies put:
56
+
57
+ require 'authstrategies/helpers'
58
+
59
+ helpers Authstrategies::Helpers
60
+
61
+ in your code.
62
+
63
+ To authenticate a user call authenticate!
64
+ To check if a user is authenticated call authenticated?
65
+ To get the currently logged in user call current_user
66
+ To logout the user class logout.
67
+
68
+ login_path returns the login path as a string
69
+ logout_path returns the logout path as a string
70
+ signup_path returns the signup path as a string
23
71
  ## Contributing
24
72
 
25
73
  1. Fork it
@@ -8,9 +8,9 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Authstrategies::VERSION
9
9
  spec.authors = ["Dobromir Ivanov"]
10
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.}
11
+ spec.description = %q{AuthStrategies is a Warden implementation for sinatra.}
12
12
  spec.summary = %q{Warden implementation for Sinatra}
13
- spec.homepage = ""
13
+ spec.homepage = "https://github.com/d0ivanov/authstrategies"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
@@ -23,7 +23,10 @@ Gem::Specification.new do |spec|
23
23
 
24
24
  spec.add_runtime_dependency "sinatra"
25
25
  spec.add_runtime_dependency "sinatra-activerecord"
26
- spec.add_runtime_dependency "activerecord"
26
+ spec.add_runtime_dependency "protected_attributes"
27
27
  spec.add_runtime_dependency "warden"
28
28
  spec.add_runtime_dependency "bcrypt-ruby"
29
+ spec.add_runtime_dependency "rack"
30
+ spec.add_runtime_dependency "rack-flash3", '1.0.5'
31
+
29
32
  end
@@ -1,7 +1,7 @@
1
1
  module Authstrategies
2
2
  module Helpers
3
- def authenticate!
4
- env['warden'].authenticate!
3
+ def authenticate! strategy= :password
4
+ env['warden'].authenticate! strategy
5
5
  end
6
6
 
7
7
  def authenticated?
@@ -15,5 +15,17 @@ module Authstrategies
15
15
  def logout
16
16
  env['warden'].logout
17
17
  end
18
+
19
+ def login_path
20
+ '/login'
21
+ end
22
+
23
+ def logout_path
24
+ '/logout'
25
+ end
26
+
27
+ def signup_path
28
+ '/signup'
29
+ end
18
30
  end
19
31
  end
@@ -0,0 +1,64 @@
1
+ module Authstrategies
2
+ class Middleware < Sinatra::Base
3
+ register Base
4
+ register RememberMe
5
+
6
+ get '/login/?' do
7
+ redirect '/' if authenticated?
8
+ erb :login
9
+ end
10
+
11
+ post '/login' do
12
+ redirect '/' if authenticated?
13
+ authenticate!
14
+ if authenticated?
15
+ if params["remember_me"] == "on"
16
+ current_user.remember_me!
17
+ response.set_cookie("authstrategies",
18
+ :value => current_user.remember_token,
19
+ :expires => Time.now + 7 * 24 * 3600
20
+ )
21
+ end
22
+ flash[:notice] = "Logged in successfully!"
23
+ redirect '/'
24
+ else
25
+ flash[:error] = env["warden"].message
26
+ redirect '/login'
27
+ end
28
+ end
29
+
30
+ get '/logout/?' do
31
+ if authenticated?
32
+ current_user.forget_me!
33
+ response.delete_cookie("authstrategies")
34
+ logout
35
+ flash[:notice] = "Successfully logged out!"
36
+ redirect '/'
37
+ end
38
+ redirect '/'
39
+ end
40
+
41
+ post '/unauthenticated' do
42
+ flash[:error] = env["warden"].message
43
+ redirect '/login'
44
+ end
45
+
46
+ get '/signup/?' do
47
+ redirect '/' if authenticated?
48
+ erb :signup
49
+ end
50
+
51
+ post '/signup' do
52
+ redirect '/' if authenticated?
53
+ user = User.new(params)
54
+ if user.valid?
55
+ user.save
56
+ flash[:notice] = "Successfully signed up!"
57
+ redirect '/'
58
+ else
59
+ flash[:error] = user.errors.messages
60
+ redirect '/signup'
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,49 @@
1
+ require 'bcrypt'
2
+ require 'active_record'
3
+ require 'protected_attributes'
4
+
5
+ class User < ActiveRecord::Base
6
+ include BCrypt
7
+
8
+ validates :email, :password, presence: true
9
+ validates :email, uniqueness: true
10
+
11
+ validates :password, confirmation: true
12
+ validates :password, length: { in: 8..20,
13
+ too_long: "%{count} is the maximum allowed!",
14
+ too_short: "must be at least %{count}" }
15
+
16
+ attr_accessible :email, :password, :remember_me, :remember_me_token
17
+
18
+ def password
19
+ @password ||= Password.new(encrypted_password)
20
+ end
21
+
22
+ def password= password
23
+ @password = Password.create(password)
24
+ self.encrypted_password = @password
25
+ end
26
+
27
+ def authenticate request
28
+ if self.password == request["password"]
29
+ true
30
+ else
31
+ false
32
+ end
33
+ end
34
+
35
+ def remember_me!
36
+ self.update_attribute('remember_me', true)
37
+ self.update_attribute('remember_token', new_token)
38
+ end
39
+
40
+ def forget_me!
41
+ self.update_attribute('remember_me', false)
42
+ self.update_attribute('remember_token', nil)
43
+ end
44
+
45
+ private
46
+ def new_token
47
+ Password.create(Time.new.to_s)
48
+ end
49
+ end
@@ -0,0 +1,16 @@
1
+ module Authstrategies
2
+ class PasswordStrategy < Warden::Strategies::Base
3
+ def valid?
4
+ !!(request["email"] && request["password"])
5
+ end
6
+
7
+ def authenticate!
8
+ user = User.find_by_email(request["email"])
9
+ if user && user.authenticate(request)
10
+ success!(user)
11
+ else
12
+ fail("Invalid username or password!")
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ module Authstrategies
2
+ class RememberMeStrategy < Warden::Strategies::Base
3
+ def valid?
4
+ !!(request.cookies["authstrategies"])
5
+ end
6
+
7
+ def authenticate!
8
+ user = User.find_by_remember_token(request.cookies["authstrategies"])
9
+ if user && user.remember_me
10
+ success!(user)
11
+ end
12
+ fail!('')
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ module Authstrategies
2
+ class Warden::SessionSerializer
3
+ def serialize(user)
4
+ user.id
5
+ end
6
+
7
+ def deserialize id
8
+ User.find(id)
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module Authstrategies
2
- VERSION = "0.0.6"
2
+ VERSION = "0.1.0"
3
3
  end
File without changes
File without changes
@@ -3,95 +3,34 @@ require "warden"
3
3
  require "rack-flash"
4
4
  require "sinatra/base"
5
5
  require "active_record"
6
- require "authstrategies/helpers"
7
- require "authstrategies/user"
6
+ require "bcrypt"
7
+ require "authstrategies/session_serializer.rb"
8
+ require "authstrategies/helpers.rb"
9
+ require "authstrategies/password.rb"
10
+ require "authstrategies/remember_me.rb"
11
+ require "authstrategies/models/user.rb"
8
12
 
9
13
  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
14
+ module Base
15
+ def self.registered(app)
16
+ app.helpers Helpers
20
17
 
21
- class PasswordStrategy < Warden::Strategies::Base
22
- def valid?
23
- !!(request["email"] && request["password"])
18
+ app.use Warden::Manager do |manager|
19
+ manager.failure_app = app
20
+ manager.default_strategies :password, :remember_me
24
21
  end
25
22
 
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, 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
23
+ Warden::Manager.before_failure do |env,opts|
24
+ env['REQUEST_METHOD'] = 'POST'
91
25
  end
26
+ Warden::Strategies.add(:password, PasswordStrategy)
92
27
  end
28
+ end
93
29
 
94
- class Middleware < Sinatra::Base
95
- register Base
30
+ module RememberMe
31
+ def self.registered(app)
32
+ Warden::Strategies.add(:remember_me, RememberMeStrategy)
96
33
  end
34
+ end
97
35
  end
36
+ require "authstrategies/middleware.rb"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: authstrategies
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dobromir Ivanov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-22 00:00:00.000000000 Z
11
+ date: 2013-12-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: activerecord
70
+ name: protected_attributes
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ! '>='
@@ -108,8 +108,35 @@ dependencies:
108
108
  - - ! '>='
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
- description: AuthStrategies is a Warden implementation for sinatra. For now it only
112
- implements a password strategy.
111
+ - !ruby/object:Gem::Dependency
112
+ name: rack
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rack-flash3
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '='
130
+ - !ruby/object:Gem::Version
131
+ version: 1.0.5
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '='
137
+ - !ruby/object:Gem::Version
138
+ version: 1.0.5
139
+ description: AuthStrategies is a Warden implementation for sinatra.
113
140
  email:
114
141
  - dobromir0ivanov@gmail.com
115
142
  executables: []
@@ -124,10 +151,15 @@ files:
124
151
  - authstrategies.gemspec
125
152
  - lib/authstrategies.rb
126
153
  - lib/authstrategies/helpers.rb
154
+ - lib/authstrategies/middleware.rb
155
+ - lib/authstrategies/models/user.rb
156
+ - lib/authstrategies/password.rb
157
+ - lib/authstrategies/remember_me.rb
158
+ - lib/authstrategies/session_serializer.rb
127
159
  - lib/authstrategies/version.rb
128
- - lib/views/login.erb
129
- - lib/views/signup.erb
130
- homepage: ''
160
+ - lib/authstrategies/views/login.erb
161
+ - lib/authstrategies/views/signup.erb
162
+ homepage: https://github.com/d0ivanov/authstrategies
131
163
  licenses:
132
164
  - MIT
133
165
  metadata: {}