ichverstehe-chowder 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/chowder/helpers/sinatra.rb +28 -0
- data/lib/chowder.rb +95 -0
- metadata +54 -0
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
|
3
|
+
module Sinatra
|
4
|
+
module Chowder
|
5
|
+
def current_user
|
6
|
+
session[:current_user]
|
7
|
+
end
|
8
|
+
|
9
|
+
def authorized?
|
10
|
+
current_user
|
11
|
+
end
|
12
|
+
|
13
|
+
def login
|
14
|
+
session[:redirect_to] = request.path_info
|
15
|
+
redirect '/login'
|
16
|
+
end
|
17
|
+
|
18
|
+
def logout
|
19
|
+
session[:current_user] = nil
|
20
|
+
end
|
21
|
+
|
22
|
+
def require_user
|
23
|
+
login unless authorized?
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
helpers Chowder
|
28
|
+
end
|
data/lib/chowder.rb
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require 'ostruct'
|
3
|
+
require 'openid'
|
4
|
+
require 'openid/store/filesystem'
|
5
|
+
|
6
|
+
module Chowder
|
7
|
+
class Base < Sinatra::Base
|
8
|
+
LOGIN_VIEW = <<-HTML
|
9
|
+
<form action="/login" method="POST">
|
10
|
+
Login: <input type="text" name="login" /><br />
|
11
|
+
Password: <input type="password" name="password" /><br />
|
12
|
+
<input type="submit" value="Login" />
|
13
|
+
</form>
|
14
|
+
OpenID:
|
15
|
+
<form action="/openid/initiate" method="POST">
|
16
|
+
URL: <input type="text" name="openid_identifier" /><br />
|
17
|
+
<input type="submit" value="Login" />
|
18
|
+
</form>
|
19
|
+
HTML
|
20
|
+
|
21
|
+
# Override this until in Sinatra supports it. See
|
22
|
+
# http://sinatra.lighthouseapp.com/projects/9779/tickets/160
|
23
|
+
def initialize(app=nil, *args, &block)
|
24
|
+
@app = app
|
25
|
+
@middleware = OpenStruct.new(:args => args, :block => block)
|
26
|
+
end
|
27
|
+
|
28
|
+
def authorize(user)
|
29
|
+
session[:current_user] = user
|
30
|
+
end
|
31
|
+
|
32
|
+
def return_or_redirect_to(path)
|
33
|
+
redirect(session[:return_to] || path)
|
34
|
+
end
|
35
|
+
|
36
|
+
def find_login_template
|
37
|
+
views_dir = self.options.views || "./views"
|
38
|
+
template = Dir[File.join(views_dir, 'login.*')].first
|
39
|
+
end
|
40
|
+
|
41
|
+
get '/login' do
|
42
|
+
if template = find_login_template
|
43
|
+
engine = File.extname(template)[1..-1]
|
44
|
+
send(engine, :login)
|
45
|
+
else
|
46
|
+
LOGIN_VIEW
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
get '/logout' do
|
51
|
+
session[:current_user] = nil
|
52
|
+
redirect '/'
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class Basic < Base
|
57
|
+
post '/login' do
|
58
|
+
login, password = params[:login], params[:password]
|
59
|
+
if authorize @middleware.block.call(login, password)
|
60
|
+
return_or_redirect_to '/'
|
61
|
+
else
|
62
|
+
redirect '/login'
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
class OpenID < Base
|
68
|
+
def host
|
69
|
+
host = env['HTTP_HOST'] || "#{env['SERVER_NAME']}:#{env['SERVER_PORT']}"
|
70
|
+
"http://#{host}"
|
71
|
+
end
|
72
|
+
|
73
|
+
def setup_consumer
|
74
|
+
store = ::OpenID::Store::Filesystem.new('.openid')
|
75
|
+
osession = session[:openid] ||= {}
|
76
|
+
@consumer = ::OpenID::Consumer.new(osession, store)
|
77
|
+
end
|
78
|
+
|
79
|
+
post '/openid/initiate' do
|
80
|
+
setup_consumer
|
81
|
+
url = @consumer.begin(params['openid_identifier']).redirect_url(host, host + '/openid/authenticate')
|
82
|
+
redirect url
|
83
|
+
end
|
84
|
+
|
85
|
+
get '/openid/authenticate' do
|
86
|
+
setup_consumer
|
87
|
+
res = @consumer.complete(request.params, host + '/openid/authenticate')
|
88
|
+
user = @middleware.block.call(res.identity_url)
|
89
|
+
if res.is_a?(::OpenID::Consumer::SuccessResponse) && authorize(user)
|
90
|
+
return_or_redirect_to '/'
|
91
|
+
end
|
92
|
+
redirect '/login'
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ichverstehe-chowder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.1"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Harry Vangberg
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-02-26 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: harry@vangberg.name
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/chowder.rb
|
26
|
+
- lib/chowder/helpers/sinatra.rb
|
27
|
+
has_rdoc: false
|
28
|
+
homepage: http://github.com/ichverstehe/chowder
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: "0"
|
39
|
+
version:
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
requirements: []
|
47
|
+
|
48
|
+
rubyforge_project:
|
49
|
+
rubygems_version: 1.2.0
|
50
|
+
signing_key:
|
51
|
+
specification_version: 2
|
52
|
+
summary: rack middleware providing session based authentication
|
53
|
+
test_files: []
|
54
|
+
|