verge 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile CHANGED
@@ -15,11 +15,16 @@ begin
15
15
  gem.add_dependency "bcrypt-ruby", ">= 2.0.5"
16
16
  gem.add_dependency "activesupport", ">= 2.3.4"
17
17
  gem.add_development_dependency "rspec", ">= 1.2.9"
18
- gem.add_development_dependency "factory_girl", ">= 1.2.3"
18
+ gem.add_development_dependency "factory_girl"
19
19
  gem.add_development_dependency "rack-test", ">= 0.5.0"
20
20
  gem.add_development_dependency "do_sqlite3", ">= 0.9.0"
21
+
22
+ gem.executables = ['verge']
21
23
  end
22
24
  Jeweler::GemcutterTasks.new
25
+ # Jeweler::RubyforgeTasks.new do |rubyforge|
26
+ # rubyforge.doc_task = "yardoc"
27
+ # end
23
28
  rescue LoadError
24
29
  puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
25
30
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.4
1
+ 0.0.5
data/bin/verge ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # Verge server for the command line
3
+
4
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
5
+ require 'verge'
6
+
7
+ Verge::Server::Exec.new(ARGV)
data/lib/verge.rb CHANGED
@@ -3,10 +3,15 @@ module Verge
3
3
 
4
4
  module Server
5
5
  autoload :Base, "verge/server/base"
6
+
7
+ autoload :Config, "verge/server/config"
8
+ autoload :Exec, "verge/server/exec"
6
9
 
7
10
  autoload :User, "verge/server/models"
8
11
  autoload :SignedToken, "verge/server/models"
9
12
  autoload :Token, "verge/server/models"
10
13
  autoload :Site, "verge/server/models"
11
14
  end
12
- end
15
+
16
+ autoload :Client, "verge/client"
17
+ end
@@ -0,0 +1,25 @@
1
+ module Verge
2
+ module Client
3
+ class << self
4
+ attr_accessor :server_url, :site_token
5
+ end
6
+
7
+ module AssetHelper
8
+ # Return the javascript tag that include the token if the user's authenticated
9
+ def verge_javascript_tag
10
+ "<script type=\"text/javascript\" src=\"#{Verge::Client.server_url}/token.js\"></script>"
11
+ end
12
+ end
13
+
14
+ module PathHelper
15
+ # Return the path to the authentication action on the server
16
+ def verge_auth_url
17
+ "#{Verge::Client.server_url}/login"
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ # If using rails add the verge_javascript_tag to action view
24
+ ::ActionView::Base.send(:include, Verge::Client::AssetHelper) if defined? ::ActionView::Base
25
+ ::ActionView::Base.send(:include, Verge::Client::PathHelper) if defined? ::ActionView::Base
@@ -15,14 +15,23 @@ module Verge
15
15
  # and password. Returns a token that should be sent back to the site
16
16
  # along with the login passed here to be verified by the site
17
17
  # as allowed to login.
18
- get '/auth' do
18
+ post '/login' do
19
19
  extract_site
20
20
 
21
21
  user = User.authenticate(params[:login], params[:password])
22
22
  halt 401, "Bad user." if user.nil?
23
23
 
24
24
  set_cookie_for_user(user)
25
- user.token.value
25
+
26
+ result = {:token => user.token.value}
27
+ target = params[:redirect]
28
+
29
+ if target.nil? || target.blank?
30
+ # TODO: Add formatters for XML, JSON and Standard params
31
+ result.to_params
32
+ else
33
+ redirect(params[:redirect] + (target.index("?").nil? ? "?" : "&") + result.to_params)
34
+ end
26
35
  end
27
36
 
28
37
  get '/token.js' do
@@ -58,17 +67,17 @@ module Verge
58
67
 
59
68
  private
60
69
 
61
- def extract_site # nodoc #
62
- site = Site.find_by_uri(request.referer)
63
- (site.nil? && halt(401, "Not a valid site.")) || site
64
- end
70
+ def extract_site # nodoc #
71
+ site = Site.find_by_uri(params[:site] || Site::GENERIC_HOST)
72
+ (site.nil? && halt(401, "Not a valid site.")) || site
73
+ end
65
74
 
66
- def set_cookie_for_user(user) # nodoc #
67
- response.set_cookie("token", {
68
- :value => user.token.value,
69
- :path => '/'
70
- })
71
- end
75
+ def set_cookie_for_user(user) # nodoc #
76
+ response.set_cookie("token", {
77
+ :value => user.token.value,
78
+ :path => '/'
79
+ })
80
+ end
72
81
  end
73
82
  end
74
83
  end
@@ -0,0 +1,50 @@
1
+ module Verge
2
+ module Server
3
+ module Config
4
+ class << self
5
+ attr_reader :database_path, :site_signatures
6
+
7
+ # Make sure the DB path has a type, defaults to sqlite3 if not
8
+ # specified.
9
+ def database_path=(val)
10
+ @database_path = val.index("://").nil? ? "sqlite3://#{val}" : val
11
+ load_signatures
12
+ end
13
+
14
+ # Set the generic site's token, if only one token is to be shared
15
+ # across all sites.
16
+ def generic_signature=(val)
17
+ @site_signatures[Verge::Server::Site::GENERIC_HOST] = val
18
+ end
19
+
20
+ def site_signatures=(val)
21
+ #::Verge::Server::Site.all.destroy!
22
+
23
+ val.instance_eval do
24
+ def []=(key, val)
25
+ super
26
+ Verge::Server::Config.load_signatures
27
+ end
28
+ end
29
+
30
+ @site_signatures = val
31
+ load_signatures
32
+ end
33
+
34
+ # Loads the signatures from the config into sites
35
+ def load_signatures
36
+ return if @site_signatures.nil?
37
+ @site_signatures.each do |host, signature|
38
+ site = Site.first(:host => host) || Site.new(:host => host)
39
+ site.signature = signature
40
+ site.save
41
+ end
42
+ end
43
+
44
+ end
45
+
46
+ self.database_path = "sqlite3://#{Dir.pwd}/verge.sqlite3"
47
+ self.site_signatures = {}
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'optparse'
3
+ require 'yaml'
4
+
5
+ module Verge
6
+ module Server
7
+ class Exec
8
+ def initialize(argv)
9
+ options = {}
10
+ signature = nil
11
+ database = nil
12
+ signature_file = nil
13
+
14
+ OptionParser.new do |opts|
15
+ opts.banner { "Usage: verge [options]" }
16
+ # Sinatra params
17
+ opts.on('-x') { options[:lock] = true }
18
+ opts.on('-s server') { |val| options[:server] = val }
19
+ opts.on('-e env') { |val| options[:environment] = val.to_sym }
20
+ opts.on('-p port') { |val| options[:port] = val.to_i }
21
+
22
+ # Verge params
23
+ opts.on('-g generic-signature') { |val| signature = val }
24
+ opts.on('-d database') { |val| database = val }
25
+ opts.on('-S signature-file') { |val| signature_file = val }
26
+
27
+ opts.on_tail('-h', '--help', "Show this message") { puts opts ; exit }
28
+ end
29
+
30
+ Verge::Server::Config.database_path = database unless database.nil?
31
+ Verge::Server::Config.generic_signature = signature unless signature.nil?
32
+
33
+ begin
34
+ YAML.load_file(signature_file).each do |key, value|
35
+ Verge::Server::Config.site_signatures[key] = value
36
+ end unless signature_file.nil?
37
+ rescue
38
+ puts "Can't read signature file!"
39
+ end
40
+
41
+ if Verge::Server::Site.count == 0
42
+ token = Verge::Crypto.token
43
+ Verge::Server::Config.generic_signature = token
44
+ puts "Verge created the GENERIC_HOST token:\n#{token}\n"
45
+ end
46
+
47
+ Verge::Server::Base.run! options
48
+ end
49
+ end
50
+ end
51
+ end
@@ -6,7 +6,7 @@ require 'dm-timestamps'
6
6
 
7
7
  require 'activesupport'
8
8
 
9
- DataMapper::setup(:default, ENV['DATABASE_URL'] || "sqlite3:///#{Dir.pwd}/database.sqlite3")
9
+ DataMapper::setup(:default, ENV['DATABASE_URL'] || Verge::Server::Config.database_path)
10
10
 
11
11
  module Verge
12
12
  module Server
@@ -45,8 +45,8 @@ module Verge
45
45
 
46
46
  # Attempts to find a user based on the credentials passed.
47
47
  def self.authenticate(login, password)
48
- u = User.first(:login => login)
49
- (u.nil? || u.password != password) && nil || u
48
+ user = User.first(:login => login)
49
+ (user.nil? || user.password != password) && nil || user
50
50
  end
51
51
 
52
52
  # Removes expired users
@@ -101,7 +101,15 @@ module Verge
101
101
  end
102
102
  end
103
103
 
104
+ # Datastructure representing a site that will connect and authenticate
105
+ # against verge. If you've setup verge with just one generic site key
106
+ # none of your sites will need to specify who they are and should
107
+ # all share that common key.
104
108
  class Site
109
+ # Set a value that will be used to look up keys that are not associated
110
+ # with a particular site
111
+ GENERIC_HOST = "generic.hostname"
112
+
105
113
  include DataMapper::Resource
106
114
 
107
115
  property :id, Serial, :key => true
@@ -0,0 +1,19 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Verge::Client do
4
+ before :all do
5
+ Verge::Client.server_url = "http://login.example.com"
6
+ end
7
+
8
+ it "returns the correct script tag" do
9
+ extend Verge::Client::AssetHelper
10
+
11
+ verge_javascript_tag.should == '<script type="text/javascript" src="http://login.example.com/token.js"></script>'
12
+ end
13
+
14
+ it "should return the authentication path to the verge server" do
15
+ extend Verge::Client::PathHelper
16
+
17
+ verge_auth_url.should == "http://login.example.com/login"
18
+ end
19
+ end
data/spec/factories.rb CHANGED
@@ -10,6 +10,10 @@ Factory.define(:site, :class => Verge::Server::Site) do |s|
10
10
  s.host { Factory.next(:host) }
11
11
  end
12
12
 
13
+ Factory.define(:generic_site, :class => Verge::Server::Site) do |site|
14
+ site.host Verge::Server::Site::GENERIC_HOST
15
+ end
16
+
13
17
  Factory.define(:signed_token, :class => Verge::Server::SignedToken) do |s|
14
18
  s.token_id 1
15
19
  s.site_id 1
@@ -0,0 +1,30 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Verge::Server::Config do
4
+ before :all do
5
+ Verge::Server::Site.all.destroy
6
+ Verge::Server::Token.all.destroy
7
+
8
+ @token = Verge::Crypto::token
9
+ end
10
+
11
+ it "has a default database path" do
12
+ Verge::Server::Config.database_path.should_not be_nil
13
+ end
14
+
15
+ it "sets sqlite3 as the db type if not specified" do
16
+ Verge::Server::Config.database_path = "login.db"
17
+ Verge::Server::Config.database_path.should == "sqlite3://login.db"
18
+ end
19
+
20
+ it "sets the generic site's token" do
21
+ Verge::Server::Config.generic_signature = @token
22
+ Verge::Server::Config.site_signatures[Verge::Server::Site::GENERIC_HOST].should == @token
23
+ end
24
+
25
+ it "has the GENERIC_HOST with the specified token" do
26
+ Verge::Server::Config.generic_signature = @token
27
+ Verge::Server::Site.count.should == 1
28
+ end
29
+
30
+ end
@@ -0,0 +1,9 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Verge::Server::Config do
4
+ before :each do
5
+ end
6
+
7
+ it "should setup the database path for the server" do
8
+ end
9
+ end
@@ -1,8 +1,8 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
- module VergeSpecHelper
4
- def valid_auth_request_for_user(user)
5
- {:login => user.login, :password => "0rbital"}
3
+ module ServerSpecHelper
4
+ def valid_auth_request(user, redirect = nil, site = nil)
5
+ {:login => user.login, :password => "0rbital", :redirect => redirect, :site => site}
6
6
  end
7
7
 
8
8
  def new_user_credentials_for_site(site)
@@ -12,13 +12,13 @@ module VergeSpecHelper
12
12
  end
13
13
 
14
14
  describe Verge::Server do
15
- include VergeSpecHelper
15
+ include ServerSpecHelper
16
16
 
17
17
  before :each do
18
- @site = Factory(:site)
19
- header("Referer", @site.host)
18
+ Verge::Server::Site.all.destroy!
19
+ @site = Factory(:generic_site)
20
20
  end
21
-
21
+
22
22
  describe "GET to /token.js" do
23
23
  it "echos cookie back in javascript" do
24
24
  login = "astro"
@@ -31,7 +31,7 @@ describe Verge::Server do
31
31
  last_response.body.should =~ /#{login}/
32
32
  last_response.body.should =~ /#{token}/
33
33
  end
34
-
34
+
35
35
  it "echos nothing if no cookies are sent" do
36
36
  get '/token.js'
37
37
  last_response.body.should == ""
@@ -58,25 +58,39 @@ describe Verge::Server do
58
58
  end
59
59
 
60
60
 
61
- describe 'GET to /auth' do
61
+ describe 'POST to /login' do
62
62
  before :each do
63
63
  @user = Factory(:user)
64
64
  end
65
65
 
66
66
  it 'fails with empty request' do
67
- get '/auth'
67
+ post '/login'
68
68
  last_response.status.should == 401
69
69
  end
70
70
 
71
71
  it 'returns a code when valid' do
72
- get '/auth', valid_auth_request_for_user(@user)
73
- last_response.body.should == @user.token.value
72
+ post '/login', valid_auth_request(@user)
73
+ last_response.body.should == "token=#{@user.token.value}"
74
74
  end
75
75
 
76
76
  it 'sets a cookie on success' do
77
- get '/auth', valid_auth_request_for_user(@user)
77
+ post '/login', valid_auth_request(@user)
78
78
  last_response.headers["Set-Cookie"].should == "token=#{@user.token.value}; path=/"
79
79
  end
80
+
81
+ it 'redirects to the desired target with the token' do
82
+ post '/login', valid_auth_request(@user, "http://example.com")
83
+
84
+ last_response.status.should == 302
85
+ last_response.headers["Location"].should == "http://example.com?token=#{@user.token.value}"
86
+ end
87
+
88
+ it 'redirects to the desired target with the token and detects other url params' do
89
+ post '/login', valid_auth_request(@user, "http://example.com?snowball=cat")
90
+
91
+ last_response.status.should == 302
92
+ last_response.headers["Location"].should == "http://example.com?snowball=cat&token=#{@user.token.value}"
93
+ end
80
94
  end
81
95
 
82
96
  describe "POST to /create" do
@@ -85,8 +99,7 @@ describe Verge::Server do
85
99
  end
86
100
 
87
101
  it "fails if site not found" do
88
- header("Referer", "BAD://SITE")
89
- post '/create'
102
+ post '/create', {:site => "bad.site"}
90
103
 
91
104
  last_response.status.should == 401
92
105
  end
@@ -102,13 +115,10 @@ describe Verge::Server do
102
115
  before :each do
103
116
  @user = Factory(:user)
104
117
  @signed_token = @user.token.signed_tokens.first(:site_id => @site.id)
105
-
106
- header("Referer", @site.host)
107
118
  end
108
119
 
109
120
  it "fails if no regisered site is found" do
110
- header("Referer", "BAD://SITE")
111
- get "/verify/anything"
121
+ get "/verify/anything", {:site => "bad.site"}
112
122
 
113
123
  last_response.status.should == 401
114
124
  end
data/verge.gemspec CHANGED
@@ -5,13 +5,15 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{verge}
8
- s.version = "0.0.4"
8
+ s.version = "0.0.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Adam Elliot"]
12
- s.date = %q{2009-10-20}
12
+ s.date = %q{2009-11-02}
13
+ s.default_executable = %q{verge}
13
14
  s.description = %q{Simple system that grants trusted sites tokens if users have successfully authenticated. So they are free to interact with each other securely.}
14
15
  s.email = %q{adam@wartube.com}
16
+ s.executables = ["verge"]
15
17
  s.extra_rdoc_files = [
16
18
  "LICENSE",
17
19
  "README.rdoc"
@@ -23,18 +25,25 @@ Gem::Specification.new do |s|
23
25
  "README.rdoc",
24
26
  "Rakefile",
25
27
  "VERSION",
28
+ "bin/verge",
26
29
  "lib/verge.rb",
30
+ "lib/verge/client.rb",
27
31
  "lib/verge/crypto.rb",
28
32
  "lib/verge/server/base.rb",
33
+ "lib/verge/server/config.rb",
34
+ "lib/verge/server/exec.rb",
29
35
  "lib/verge/server/models.rb",
30
36
  "lib/verge/server/views/token.js.erb",
37
+ "spec/client_spec.rb",
31
38
  "spec/crypto_spec.rb",
32
39
  "spec/factories.rb",
33
40
  "spec/models_spec.rb",
34
41
  "spec/rcov.opts",
42
+ "spec/server_config_spec.rb",
43
+ "spec/server_exec_spec.rb",
44
+ "spec/server_spec.rb",
35
45
  "spec/spec.opts",
36
46
  "spec/spec_helper.rb",
37
- "spec/verge_spec.rb",
38
47
  "verge.gemspec"
39
48
  ]
40
49
  s.homepage = %q{http://github.com/adamelliot/verge}
@@ -43,11 +52,14 @@ Gem::Specification.new do |s|
43
52
  s.rubygems_version = %q{1.3.5}
44
53
  s.summary = %q{Lightweight centralized authentication system built on Sinatra}
45
54
  s.test_files = [
46
- "spec/crypto_spec.rb",
55
+ "spec/client_spec.rb",
56
+ "spec/crypto_spec.rb",
47
57
  "spec/factories.rb",
48
58
  "spec/models_spec.rb",
49
- "spec/spec_helper.rb",
50
- "spec/verge_spec.rb"
59
+ "spec/server_config_spec.rb",
60
+ "spec/server_exec_spec.rb",
61
+ "spec/server_spec.rb",
62
+ "spec/spec_helper.rb"
51
63
  ]
52
64
 
53
65
  if s.respond_to? :specification_version then
@@ -60,7 +72,7 @@ Gem::Specification.new do |s|
60
72
  s.add_runtime_dependency(%q<bcrypt-ruby>, [">= 2.0.5"])
61
73
  s.add_runtime_dependency(%q<activesupport>, [">= 2.3.4"])
62
74
  s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
63
- s.add_development_dependency(%q<factory_girl>, [">= 1.2.3"])
75
+ s.add_development_dependency(%q<factory_girl>, [">= 0"])
64
76
  s.add_development_dependency(%q<rack-test>, [">= 0.5.0"])
65
77
  s.add_development_dependency(%q<do_sqlite3>, [">= 0.9.0"])
66
78
  else
@@ -69,7 +81,7 @@ Gem::Specification.new do |s|
69
81
  s.add_dependency(%q<bcrypt-ruby>, [">= 2.0.5"])
70
82
  s.add_dependency(%q<activesupport>, [">= 2.3.4"])
71
83
  s.add_dependency(%q<rspec>, [">= 1.2.9"])
72
- s.add_dependency(%q<factory_girl>, [">= 1.2.3"])
84
+ s.add_dependency(%q<factory_girl>, [">= 0"])
73
85
  s.add_dependency(%q<rack-test>, [">= 0.5.0"])
74
86
  s.add_dependency(%q<do_sqlite3>, [">= 0.9.0"])
75
87
  end
@@ -79,7 +91,7 @@ Gem::Specification.new do |s|
79
91
  s.add_dependency(%q<bcrypt-ruby>, [">= 2.0.5"])
80
92
  s.add_dependency(%q<activesupport>, [">= 2.3.4"])
81
93
  s.add_dependency(%q<rspec>, [">= 1.2.9"])
82
- s.add_dependency(%q<factory_girl>, [">= 1.2.3"])
94
+ s.add_dependency(%q<factory_girl>, [">= 0"])
83
95
  s.add_dependency(%q<rack-test>, [">= 0.5.0"])
84
96
  s.add_dependency(%q<do_sqlite3>, [">= 0.9.0"])
85
97
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: verge
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Elliot
@@ -9,8 +9,8 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-20 00:00:00 -06:00
13
- default_executable:
12
+ date: 2009-11-02 00:00:00 -07:00
13
+ default_executable: verge
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: sinatra
@@ -70,7 +70,7 @@ dependencies:
70
70
  requirements:
71
71
  - - ">="
72
72
  - !ruby/object:Gem::Version
73
- version: 1.2.3
73
+ version: "0"
74
74
  version:
75
75
  - !ruby/object:Gem::Dependency
76
76
  name: rack-test
@@ -94,8 +94,8 @@ dependencies:
94
94
  version:
95
95
  description: Simple system that grants trusted sites tokens if users have successfully authenticated. So they are free to interact with each other securely.
96
96
  email: adam@wartube.com
97
- executables: []
98
-
97
+ executables:
98
+ - verge
99
99
  extensions: []
100
100
 
101
101
  extra_rdoc_files:
@@ -108,18 +108,25 @@ files:
108
108
  - README.rdoc
109
109
  - Rakefile
110
110
  - VERSION
111
+ - bin/verge
111
112
  - lib/verge.rb
113
+ - lib/verge/client.rb
112
114
  - lib/verge/crypto.rb
113
115
  - lib/verge/server/base.rb
116
+ - lib/verge/server/config.rb
117
+ - lib/verge/server/exec.rb
114
118
  - lib/verge/server/models.rb
115
119
  - lib/verge/server/views/token.js.erb
120
+ - spec/client_spec.rb
116
121
  - spec/crypto_spec.rb
117
122
  - spec/factories.rb
118
123
  - spec/models_spec.rb
119
124
  - spec/rcov.opts
125
+ - spec/server_config_spec.rb
126
+ - spec/server_exec_spec.rb
127
+ - spec/server_spec.rb
120
128
  - spec/spec.opts
121
129
  - spec/spec_helper.rb
122
- - spec/verge_spec.rb
123
130
  - verge.gemspec
124
131
  has_rdoc: true
125
132
  homepage: http://github.com/adamelliot/verge
@@ -150,8 +157,11 @@ signing_key:
150
157
  specification_version: 3
151
158
  summary: Lightweight centralized authentication system built on Sinatra
152
159
  test_files:
160
+ - spec/client_spec.rb
153
161
  - spec/crypto_spec.rb
154
162
  - spec/factories.rb
155
163
  - spec/models_spec.rb
164
+ - spec/server_config_spec.rb
165
+ - spec/server_exec_spec.rb
166
+ - spec/server_spec.rb
156
167
  - spec/spec_helper.rb
157
- - spec/verge_spec.rb