Permalink
Browse files

renaming and shuffle a few things

  • Loading branch information...
1 parent a0b6b7e commit c621e778fb70e3869ea7ebee4dc1803170777027 @atmos committed Nov 10, 2009
View
@@ -54,7 +54,6 @@ Spec::Rake::SpecTask.new do |t|
t.rcov_opts << '--text-summary'
t.rcov_opts << '--sort' << 'coverage' << '--sort-reverse'
t.rcov_opts << '--exclude' << '.gem/,spec,examples'
- #t.rcov_opts << '--only-uncovered'
end
require 'cucumber/rake/task'
View
@@ -1,4 +1,4 @@
-class MySweetSSOServer < Hancock::App
+class MySweetSSOServer < Hancock::SSO::App
get '/' do
redirect '/sso/login' unless session_user
erb "<h3>Hello <%= session_user.full_name %></h3><!-- <%= session.inspect %>"
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -15,22 +15,12 @@
lib_dir = File.expand_path(File.join(File.dirname(__FILE__), 'hancock'))
+%w(sessions sso).each do |lib|
+ require File.join(lib_dir, 'sso', 'helpers', lib)
+ require File.join(lib_dir, 'sso', lib)
+end
require File.join(lib_dir, 'api')
-require File.join(lib_dir, 'sso', 'sessions')
-require File.join(lib_dir, 'sso', 'openid_server')
+require File.join(lib_dir, 'sso')
require File.join(lib_dir, 'models', 'model')
require File.join(lib_dir, 'models', 'user')
require File.join(lib_dir, 'models', 'consumer')
-
-module Hancock
- class ConfigurationError < StandardError; end
-
- class App < Sinatra::Base
- disable :show_exceptions
-
- set :sreg_params, [:id, :email, :first_name, :last_name, :internal, :admin]
-
- register Sinatra::Hancock::Sessions
- register Sinatra::Hancock::OpenIDServer
- end
-end
@@ -3,7 +3,7 @@ class Consumer < Model
include DataMapper::Resource
property :id, Serial
- property :url, String, :nullable => false, :unique => true, :unique_index => true, :length => 1024
+ property :url, String, :nullable => false, :unique => true, :unique_index => true, :length => 2048
property :label, String, :nullable => true, :default => nil
property :internal, Boolean, :nullable => true, :defalut => false
@@ -15,6 +15,14 @@ def self.attributes_for_create
%w(url label internal)
end
+ def self.params_for_create(params)
+ params.reject { |key, value| !attributes_for_create.include?(key) }
+ end
+
+ def self.create_from_params(params)
+ create(params_for_create(params))
+ end
+
def attributes_for_update
self.class.attributes_for_create
end
@@ -34,13 +42,5 @@ def self.internal
c.label
end
end
-
- def self.params_for_create(params)
- params.reject { |key, value| !attributes_for_create.include?(key) }
- end
-
- def self.create_from_params(params)
- create(params_for_create(params))
- end
end
end
View
@@ -0,0 +1,18 @@
+module Hancock
+ module SSO
+ def self.app
+ @app ||= Rack::Builder.app do
+ run ::Hancock::SSO::App
+ end
+ end
+ class ConfigurationError < StandardError; end
+
+ class App < Sinatra::Base
+ disable :show_exceptions
+
+ set :sreg_params, [:id, :email, :first_name, :last_name, :internal, :admin]
+ register ::Hancock::Sessions
+ register ::Hancock::OpenIDServer
+ end
+ end
+end
@@ -0,0 +1,37 @@
+module Hancock
+ module Sessions
+ module Helpers
+ def login_as(user)
+ if user.nil?
+ session.delete(:hancock_server_user_id)
+ else
+ session[:hancock_server_user_id] = user.id
+ end
+ end
+
+ def session_user
+ session[:hancock_server_user_id].nil? ?
+ nil : ::Hancock::User.get(session[:hancock_server_user_id])
+ end
+
+ def session_return_to
+ session[:hancock_server_return_to]
+ end
+
+ def session_cleanup
+ session.reject! { |key,value| key != :hancock_server_user_id }
+ end
+
+ def ensure_authenticated
+ if trust_root = session_return_to
+ forbidden! unless ::Hancock::Consumer.allowed?(trust_root)
+ end
+ throw(:halt, [401, haml(:unauthenticated)]) unless session_user
+ end
+
+ def forbidden!
+ throw :halt, [403, 'Forbidden']
+ end
+ end
+ end
+end
@@ -0,0 +1,41 @@
+module Hancock
+ module OpenIDServer
+ module Helpers
+ def server
+ if @server.nil?
+ store = OpenID::Store::Filesystem.new(File.join(Dir.tmpdir, 'openid-store'))
+ @server = OpenID::Server::Server.new(store, absolute_url('/sso'))
+ end
+ return @server
+ end
+
+ def url_for_user
+ absolute_url("/sso/users/#{session_user.id}")
+ end
+
+ def render_response(oidresp)
+ if oidresp.needs_signing
+ signed_response = server.signatory.sign(oidresp)
+ end
+ web_response = server.encode_response(oidresp)
+
+ case web_response.code
+ when 302
+ redirect web_response.headers['location']
+ else
+ web_response.body
+ end
+ end
+
+ def absolute_url(suffix = nil)
+ port_part = case request.scheme
+ when "http"
+ request.port == 80 ? "" : ":#{request.port}"
+ when "https"
+ request.port == 443 ? "" : ":#{request.port}"
+ end
+ "#{request.scheme}://#{request.host}#{port_part}#{suffix}"
+ end
+ end
+ end
+end
@@ -1,81 +0,0 @@
-# TODO MY EYES!
-module Sinatra
- module Hancock
- module OpenIDServer
- module Helpers
- def server
- if @server.nil?
- store = OpenID::Store::Filesystem.new(File.join(Dir.tmpdir, 'openid-store'))
- @server = OpenID::Server::Server.new(store, absolute_url('/sso'))
- end
- return @server
- end
-
- def url_for_user
- absolute_url("/sso/users/#{session_user.id}")
- end
-
- def render_response(oidresp)
- if oidresp.needs_signing
- signed_response = server.signatory.sign(oidresp)
- end
- web_response = server.encode_response(oidresp)
-
- case web_response.code
- when 302
- redirect web_response.headers['location']
- else
- web_response.body
- end
- end
-
- def absolute_url(suffix = nil)
- port_part = case request.scheme
- when "http"
- request.port == 80 ? "" : ":#{request.port}"
- when "https"
- request.port == 443 ? "" : ":#{request.port}"
- end
- "#{request.scheme}://#{request.host}#{port_part}#{suffix}"
- end
- end
-
- def self.registered(app)
- app.disable :show_exceptions
- app.send(:include, Sinatra::Hancock::OpenIDServer::Helpers)
-
- [:get, :post].each do |meth|
- app.send(meth, '/sso') do
- begin
- oidreq = server.decode_request(params)
- rescue OpenID::Server::ProtocolError => e
- oidreq = session[:hancock_server_last_oidreq]
- end
- throw(:halt, [400, 'Bad Request']) unless oidreq
-
- oidresp = nil
- if oidreq.kind_of?(OpenID::Server::CheckIDRequest)
- session[:hancock_server_last_oidreq] = oidreq
- session[:hancock_server_return_to] = oidreq.return_to
-
- ensure_authenticated
- forbidden! unless ::Hancock::Consumer.allowed?(oidreq.trust_root)
-
- oidreq.identity = oidreq.claimed_id = url_for_user
- oidresp = oidreq.answer(true, nil, oidreq.identity)
- sreg_data = {
- 'last_name' => session_user.last_name,
- 'first_name' => session_user.first_name,
- 'email' => session_user.email
- }
- oidresp.add_extension(OpenID::SReg::Response.new(sreg_data))
- else #associate
- oidresp = server.handle_request(oidreq)
- end
- render_response(oidresp)
- end
- end
- end
- end
- end
-end
@@ -1,65 +1,29 @@
-module Sinatra
- module Hancock
- module Sessions
- def self.sessions_template(file)
- template = File.expand_path(File.dirname(__FILE__)+'/views/')
- File.read("#{template}/#{file}.haml")
- end
-
- module Helpers
- def login_as(user)
- if user.nil?
- session.delete(:hancock_server_user_id)
- else
- session[:hancock_server_user_id] = user.id
- end
- end
-
- def session_user
- session[:hancock_server_user_id].nil? ?
- nil : ::Hancock::User.get(session[:hancock_server_user_id])
- end
-
- def session_return_to
- session[:hancock_server_return_to]
- end
-
- def session_cleanup
- session.reject! { |key,value| key != :hancock_server_user_id }
- end
+module Hancock
+ module Sessions
+ def self.sessions_template(file)
+ template = File.expand_path(File.dirname(__FILE__)+'/views/')
+ File.read("#{template}/#{file}.haml")
+ end
- def ensure_authenticated
- if trust_root = session_return_to
- forbidden! unless ::Hancock::Consumer.allowed?(trust_root)
- end
- throw(:halt, [401, haml(:unauthenticated)]) unless session_user
- end
+ def self.registered(app)
+ app.helpers Helpers
+ app.template(:unauthenticated) { sessions_template('unauthenticated') }
- def forbidden!
- throw :halt, [403, 'Forbidden']
- end
+ app.get '/sso/login' do
+ ensure_authenticated
+ redirect '/'
end
- def self.registered(app)
- app.send(:include, Sinatra::Hancock::Sessions::Helpers)
- app.template(:unauthenticated) { sessions_template('unauthenticated') }
-
- app.get '/sso/login' do
- ensure_authenticated
- redirect '/'
- end
-
- app.post '/sso/login' do
- @user = ::Hancock::User.authenticate(params['email'], params['password'])
- login_as(@user)
- ensure_authenticated
- redirect session_return_to || '/'
- end
+ app.post '/sso/login' do
+ @user = ::Hancock::User.authenticate(params['email'], params['password'])
+ login_as(@user)
+ ensure_authenticated
+ redirect session_return_to || '/'
+ end
- app.get '/sso/logout' do
- session.clear
- redirect '/'
- end
+ app.get '/sso/logout' do
+ session.clear
+ redirect '/'
end
end
end
View
@@ -0,0 +1,40 @@
+module Hancock
+ module OpenIDServer
+ def self.registered(app)
+ app.disable :show_exceptions
+ app.helpers Helpers
+
+ [:get, :post].each do |meth|
+ app.send(meth, '/sso') do
+ begin
+ oidreq = server.decode_request(params)
+ rescue OpenID::Server::ProtocolError => e
+ oidreq = session[:hancock_server_last_oidreq]
+ end
+ throw(:halt, [400, 'Bad Request']) unless oidreq
+
+ oidresp = nil
+ if oidreq.kind_of?(OpenID::Server::CheckIDRequest)
+ session[:hancock_server_last_oidreq] = oidreq
+ session[:hancock_server_return_to] = oidreq.return_to
+
+ ensure_authenticated
+ forbidden! unless ::Hancock::Consumer.allowed?(oidreq.trust_root)
+
+ oidreq.identity = oidreq.claimed_id = url_for_user
+ oidresp = oidreq.answer(true, nil, oidreq.identity)
+ sreg_data = {
+ 'last_name' => session_user.last_name,
+ 'first_name' => session_user.first_name,
+ 'email' => session_user.email
+ }
+ oidresp.add_extension(OpenID::SReg::Response.new(sreg_data))
+ else #associate
+ oidresp = server.handle_request(oidreq)
+ end
+ render_response(oidresp)
+ end
+ end
+ end
+ end
+end
File renamed without changes.
File renamed without changes.
Oops, something went wrong.

0 comments on commit c621e77

Please sign in to comment.