sinatra-dm-auth 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/LICENSE +19 -0
- data/README.md +12 -0
- data/Rakefile +18 -0
- data/VERSION +1 -0
- data/lib/sinatra/datamapperauth.rb +89 -0
- data/sinatra-dm-auth.gemspec +44 -0
- metadata +62 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
nbproject
|
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2009 Dominic Werner
|
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.md
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# sinatra-dm-auth - a simple extension to add user authorization with DataMapper
|
2
|
+
|
3
|
+
This extension adds the ability to authorize users in your application
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
The following gems are needed
|
8
|
+
$ rake
|
9
|
+
$ sinatra
|
10
|
+
$ datamapper (+ db driver e.g. do_sqlite3)
|
11
|
+
|
12
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gemspec|
|
7
|
+
gemspec.name = "sinatra-dm-auth"
|
8
|
+
gemspec.summary = "User authorization extension for Sinatra"
|
9
|
+
gemspec.description = "sinatra-dm-auth is an extension for Sinatra to add simple user authorization"
|
10
|
+
gemspec.email = "aka.vince@gmail.com"
|
11
|
+
gemspec.homepage = "http://github.com/daddz/sinatra-dm-auth"
|
12
|
+
gemspec.authors = ["Dominic Werner"]
|
13
|
+
end
|
14
|
+
Jeweler::GemcutterTasks.new
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
17
|
+
end
|
18
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require 'dm-core'
|
3
|
+
require 'dm-validations'
|
4
|
+
require 'date'
|
5
|
+
require 'digest/sha1'
|
6
|
+
|
7
|
+
module Sinatra
|
8
|
+
module DataMapperAuth
|
9
|
+
class User
|
10
|
+
include DataMapper::Resource
|
11
|
+
|
12
|
+
property :id, Serial
|
13
|
+
property :login, String, :key => true, :length => (3..40), :nullable => false
|
14
|
+
property :hashed_password, String
|
15
|
+
property :email, String, :format => :email_address
|
16
|
+
property :salt, String
|
17
|
+
property :created_at, DateTime, :default => DateTime.now
|
18
|
+
|
19
|
+
validates_present :login, :email
|
20
|
+
|
21
|
+
def password=(pass)
|
22
|
+
@password = pass
|
23
|
+
self.salt = User.random_string(10) unless self.salt
|
24
|
+
self.hashed_password = User.encrypt(@password, self.salt)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.encrypt(pass, salt)
|
28
|
+
Digest::SHA1.hexdigest(pass + salt)
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.authenticate(login, pass)
|
32
|
+
u = User.first(:login => login)
|
33
|
+
return nil if u.nil?
|
34
|
+
return u if User.encrypt(pass, u.salt) == u.hashed_password
|
35
|
+
nil
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.random_string(len)
|
39
|
+
chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
|
40
|
+
str = ""
|
41
|
+
1.upto(len) { |i| str << chars[rand(chars.size-1)] }
|
42
|
+
return str
|
43
|
+
end
|
44
|
+
end
|
45
|
+
module Helpers
|
46
|
+
def authorized?
|
47
|
+
return true if session[:user]
|
48
|
+
end
|
49
|
+
def authorize!
|
50
|
+
redirect '/login' unless authorized?
|
51
|
+
end
|
52
|
+
def logout!
|
53
|
+
session[:user] = false
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.registered(app)
|
58
|
+
app.helpers DataMapperAuth::Helpers
|
59
|
+
|
60
|
+
app.get '/login' do
|
61
|
+
'<form action="/login" method="post">
|
62
|
+
<p>Login</p><input type="text" size="30" name="login"/>
|
63
|
+
<p>Password</p><input type="password" size="30" name="password"/>
|
64
|
+
<br/>
|
65
|
+
<input type="submit" value="Submit" name="submit"/>
|
66
|
+
</form>'
|
67
|
+
end
|
68
|
+
|
69
|
+
app.post '/login' do
|
70
|
+
if session[:user] = User.authenticate(params[:login], params[:password])
|
71
|
+
flash[:notice] = "Login succesful"
|
72
|
+
redirect '/'
|
73
|
+
else
|
74
|
+
flash[:notice] = "Login failed - Try again"
|
75
|
+
redirect '/login'
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
app.get '/logout' do
|
80
|
+
logout!
|
81
|
+
flash[:notice] = "Logged out"
|
82
|
+
redirect '/'
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
register DataMapperAuth
|
89
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{sinatra-dm-auth}
|
8
|
+
s.version = "0.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Dominic Werner"]
|
12
|
+
s.date = %q{2009-10-22}
|
13
|
+
s.description = %q{sinatra-dm-auth is an extension for Sinatra to add simple user authorization}
|
14
|
+
s.email = %q{aka.vince@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".gitignore",
|
21
|
+
"LICENSE",
|
22
|
+
"README.md",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION",
|
25
|
+
"lib/sinatra/datamapperauth.rb",
|
26
|
+
"sinatra-dm-auth.gemspec"
|
27
|
+
]
|
28
|
+
s.homepage = %q{http://github.com/daddz/sinatra-dm-auth}
|
29
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
30
|
+
s.require_paths = ["lib"]
|
31
|
+
s.rubygems_version = %q{1.3.5}
|
32
|
+
s.summary = %q{User authorization extension for Sinatra}
|
33
|
+
|
34
|
+
if s.respond_to? :specification_version then
|
35
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
36
|
+
s.specification_version = 3
|
37
|
+
|
38
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
39
|
+
else
|
40
|
+
end
|
41
|
+
else
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-dm-auth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dominic Werner
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-22 00:00:00 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: sinatra-dm-auth is an extension for Sinatra to add simple user authorization
|
17
|
+
email: aka.vince@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
files:
|
26
|
+
- .gitignore
|
27
|
+
- LICENSE
|
28
|
+
- README.md
|
29
|
+
- Rakefile
|
30
|
+
- VERSION
|
31
|
+
- lib/sinatra/datamapperauth.rb
|
32
|
+
- sinatra-dm-auth.gemspec
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://github.com/daddz/sinatra-dm-auth
|
35
|
+
licenses: []
|
36
|
+
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options:
|
39
|
+
- --charset=UTF-8
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
requirements: []
|
55
|
+
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 1.3.5
|
58
|
+
signing_key:
|
59
|
+
specification_version: 3
|
60
|
+
summary: User authorization extension for Sinatra
|
61
|
+
test_files: []
|
62
|
+
|