simple-auth 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,28 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ spec/spec.opts
23
+ .bundle
24
+ spec/support/test.configuration.rb
25
+
26
+ .rvmrc
27
+ .rspec
28
+ specs.watchr.rb
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source :gemcutter
2
+
3
+ gem 'rest-client', :require => 'rest_client'
4
+ gem 'yajl-ruby', :require => 'yajl'
5
+
6
+ group :test do
7
+ gem 'rspec', '2.0.0.beta.19'
8
+ gem 'fakeweb'
9
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Umang Chouhan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,22 @@
1
+ simple-auth
2
+ ===========
3
+
4
+ Description goes here.
5
+
6
+ Note on Patches/Pull Requests
7
+ =============================
8
+
9
+ * Fork the project.
10
+ * Make your feature addition or bug fix.
11
+ * Add tests for it. This is important so I don't break it in a
12
+ future version unintentionally.
13
+ * Commit, do not mess with rakefile, version, or history.
14
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
15
+ * Send me a pull request. Bonus points for topic branches.
16
+
17
+ Copyright
18
+ =========
19
+
20
+ Copyright (c) 2010 OptimisCorp.
21
+
22
+ See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "simple-auth"
8
+ gem.summary = %Q{Simple shared authentication API}
9
+ gem.description = %Q{Provides a simple API to authenticate with a rails application}
10
+ gem.email = "uchouhan@optimiscorp.com"
11
+ gem.homepage = "http://github.com/uchouhan/simple-auth"
12
+ gem.authors = ["Umang Chouhan"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_development_dependency "yard", ">= 0"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ task :default => :spec
37
+
38
+ begin
39
+ require 'yard'
40
+ YARD::Rake::YardocTask.new
41
+ rescue LoadError
42
+ task :yardoc do
43
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
44
+ end
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.5
@@ -0,0 +1,11 @@
1
+ SimpleAuth.configure do
2
+ host do
3
+ development 'test.domain.local'
4
+ test 'test.domain.local'
5
+ production 'test.domain.local'
6
+ end
7
+
8
+ paths do
9
+ login 'login'
10
+ end
11
+ end
@@ -0,0 +1,2 @@
1
+ require 'simple-auth/configuration_helper'
2
+ require 'simple-auth/api'
@@ -0,0 +1,37 @@
1
+ module SimpleAuth
2
+ module Api
3
+ def self.api_host=(host)
4
+ @api_host = host
5
+ end
6
+
7
+ def self.api_host
8
+ @api_host
9
+ end
10
+
11
+ def api
12
+ @api ||= RestClient::Resource.new(SimpleAuth::Api.api_host)
13
+ end
14
+
15
+ def login
16
+ user_session = api['user_session.json'].get(:cookies => cookies) do |response, request, &block|
17
+ if response.code == 200
18
+ response.return!(request, &block)
19
+ set_user_session(response)
20
+ else
21
+ # response.follow_redirection(request, &block)
22
+ redirect_to response.headers[:location]
23
+ end
24
+ end
25
+ end
26
+
27
+ def current_session
28
+ session[:current_session]
29
+ end
30
+
31
+ private
32
+
33
+ def set_user_session(user_session)
34
+ session[:current_session] = Yajl::Parser.new.parse(user_session)['session']
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,42 @@
1
+ module SimpleAuth
2
+ class HostConfiguration
3
+ def self.test value
4
+ method_missing('test', value)
5
+ end
6
+
7
+ def self.method_missing(name, value)
8
+ if name.to_s == RAILS_ENV
9
+ absolute_path = 'http://' + value
10
+ path_getter = lambda { absolute_path }
11
+
12
+ SimpleAuth::Api.api_host = absolute_path
13
+ ActionController::Base.send(:define_method, 'api_host', &path_getter)
14
+ ActionView::Base.send(:define_method, 'api_host', &path_getter)
15
+ end
16
+ end
17
+ end
18
+
19
+ class PathConfiguration
20
+ def self.method_missing(name, value)
21
+ ActionController::Base.send(:define_method, "#{name}_path", lambda { value })
22
+ ActionController::Base.send(:define_method, "#{name}_url", lambda { api_host + '/' + value })
23
+ ActionView::Base.send(:define_method, "#{name}_path", lambda { value })
24
+ ActionView::Base.send(:define_method, "#{name}_url", lambda { api_host + '/' + value })
25
+ end
26
+ end
27
+
28
+ def configure(&block)
29
+ instance_eval(&block)
30
+ end
31
+ module_function :configure
32
+
33
+ def host(&block)
34
+ HostConfiguration.instance_eval(&block)
35
+ end
36
+ module_function :host
37
+
38
+ def paths(&block)
39
+ PathConfiguration.instance_eval(&block)
40
+ end
41
+ module_function :paths
42
+ end
@@ -0,0 +1,69 @@
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{simple-auth}
8
+ s.version = "0.2.5"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Umang Chouhan"]
12
+ s.date = %q{2010-07-29}
13
+ s.description = %q{Provides a simple API to authenticate with a rails application}
14
+ s.email = %q{uchouhan@optimiscorp.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "Gemfile",
23
+ "LICENSE",
24
+ "README.md",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "configuration.rb.example",
28
+ "lib/simple-auth.rb",
29
+ "lib/simple-auth/api.rb",
30
+ "lib/simple-auth/configuration_helper.rb",
31
+ "simple-auth.gemspec",
32
+ "spec/lib/simple-auth/api_spec.rb",
33
+ "spec/lib/simple-auth/configuration_helper_spec.rb",
34
+ "spec/spec_helper.rb",
35
+ "spec/support/configuration.rb",
36
+ "spec/support/mock_requests.rb",
37
+ "spec/support/stubs.rb"
38
+ ]
39
+ s.homepage = %q{http://github.com/uchouhan/simple-auth}
40
+ s.rdoc_options = ["--charset=UTF-8"]
41
+ s.require_paths = ["lib"]
42
+ s.rubygems_version = %q{1.3.7}
43
+ s.summary = %q{Simple shared authentication API}
44
+ s.test_files = [
45
+ "spec/lib/simple-auth/api_spec.rb",
46
+ "spec/lib/simple-auth/configuration_helper_spec.rb",
47
+ "spec/spec_helper.rb",
48
+ "spec/support/configuration.rb",
49
+ "spec/support/mock_requests.rb",
50
+ "spec/support/stubs.rb"
51
+ ]
52
+
53
+ if s.respond_to? :specification_version then
54
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
55
+ s.specification_version = 3
56
+
57
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
58
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
59
+ s.add_development_dependency(%q<yard>, [">= 0"])
60
+ else
61
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
62
+ s.add_dependency(%q<yard>, [">= 0"])
63
+ end
64
+ else
65
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
66
+ s.add_dependency(%q<yard>, [">= 0"])
67
+ end
68
+ end
69
+
@@ -0,0 +1,60 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe SimpleAuth do
4
+ subject { ApplicationController.new }
5
+
6
+ describe '#api' do
7
+ it 'should return the REST client' do
8
+ subject.api.should be_instance_of RestClient::Resource
9
+ end
10
+ end
11
+
12
+ describe '#login' do
13
+ before do
14
+ mock_valid_login
15
+
16
+ subject.stub!(:cookies => {})
17
+ subject.stub!(:redirect_to)
18
+ end
19
+
20
+ it 'should add the login method to the application controller' do
21
+ subject.should respond_to :login
22
+ end
23
+
24
+ it 'should set the current session' do
25
+ subject.stub!(:session => {})
26
+ subject.login
27
+
28
+ subject.session[:current_session].should == "test"
29
+ end
30
+ end
31
+
32
+ describe '#login' do
33
+ context 'The user is not logged into the authentication provider' do
34
+ before do
35
+ mock_invalid_login
36
+
37
+ subject.stub!(:cookies => {})
38
+ end
39
+
40
+ it 'should redirect the user to the login page' do
41
+ subject.should_receive :redirect_to
42
+ subject.login
43
+ end
44
+ end
45
+
46
+ context 'The user is logged into the authentication provider' do
47
+ before do
48
+ mock_valid_login
49
+
50
+ subject.stub!(:cookies => {})
51
+ subject.stub!(:session => {})
52
+ end
53
+
54
+ it 'should not redirect the user to the login page' do
55
+ subject.should_not_receive :redirect_to
56
+ subject.login
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec/spec_helper'
2
+
3
+ class ApplicationController < ActionController::Base; end
4
+
5
+ describe ApplicationController do
6
+ context '#api' do
7
+ it 'should initialize the api path from the settings' do
8
+ subject.api_host.should == 'http://test.domain.local'
9
+ end
10
+ end
11
+
12
+ context '#path' do
13
+ it 'should initialize the login path from the settings' do
14
+ subject.login_path.should == 'login'
15
+ end
16
+ end
17
+
18
+ context '#url' do
19
+ it 'should initialize the login path from the settings' do
20
+ subject.login_url.should == 'http://test.domain.local/login'
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup
4
+ Bundler.require(:default, :test)
5
+
6
+ RAILS_ENV = 'test'
7
+
8
+ require 'spec/support/stubs'
9
+ require 'lib/simple-auth'
10
+ require 'spec/support/configuration'
11
+ require 'spec/support/mock_requests'
12
+
13
+ FakeWeb.allow_net_connect = false
14
+
15
+ RSpec.configure do |config|
16
+ config.before(:each) do
17
+ FakeWeb.clean_registry
18
+ end
19
+ end
@@ -0,0 +1,13 @@
1
+ SimpleAuth.configure do
2
+ host do
3
+ development 'test.domain.local'
4
+ test 'test.domain.local'
5
+ production 'test.domain.local'
6
+ end
7
+
8
+ paths do
9
+ login 'login'
10
+ end
11
+ end
12
+
13
+ ApplicationController.send(:include, SimpleAuth::Api)
@@ -0,0 +1,18 @@
1
+ def mock_valid_login
2
+ FakeWeb.register_uri(:get,
3
+ 'http://test.domain.local/user_session.json',
4
+ :body => '{ "user" : "test", "session" : "test" }',
5
+ :status => [ '200', 'OK' ],
6
+ :content_type => 'application/json'
7
+ )
8
+ end
9
+
10
+ def mock_invalid_login
11
+ FakeWeb.register_uri(
12
+ :get,
13
+ 'http://test.domain.local/user_session.json',
14
+ :body => 'http://test.domain.local/login',
15
+ :status => [ '302', 'Found' ],
16
+ :content_type => 'text/html'
17
+ )
18
+ end
@@ -0,0 +1,12 @@
1
+ module ActionController
2
+ class Base
3
+ end
4
+ end
5
+
6
+ module ActionView
7
+ class Base
8
+ end
9
+ end
10
+
11
+ class ApplicationController < ActionController::Base
12
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple-auth
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 5
10
+ version: 0.2.5
11
+ platform: ruby
12
+ authors:
13
+ - Umang Chouhan
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-07-29 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: yard
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :development
50
+ version_requirements: *id002
51
+ description: Provides a simple API to authenticate with a rails application
52
+ email: uchouhan@optimiscorp.com
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ extra_rdoc_files:
58
+ - LICENSE
59
+ - README.md
60
+ files:
61
+ - .document
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE
65
+ - README.md
66
+ - Rakefile
67
+ - VERSION
68
+ - configuration.rb.example
69
+ - lib/simple-auth.rb
70
+ - lib/simple-auth/api.rb
71
+ - lib/simple-auth/configuration_helper.rb
72
+ - simple-auth.gemspec
73
+ - spec/lib/simple-auth/api_spec.rb
74
+ - spec/lib/simple-auth/configuration_helper_spec.rb
75
+ - spec/spec_helper.rb
76
+ - spec/support/configuration.rb
77
+ - spec/support/mock_requests.rb
78
+ - spec/support/stubs.rb
79
+ has_rdoc: true
80
+ homepage: http://github.com/uchouhan/simple-auth
81
+ licenses: []
82
+
83
+ post_install_message:
84
+ rdoc_options:
85
+ - --charset=UTF-8
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 3
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ hash: 3
103
+ segments:
104
+ - 0
105
+ version: "0"
106
+ requirements: []
107
+
108
+ rubyforge_project:
109
+ rubygems_version: 1.3.7
110
+ signing_key:
111
+ specification_version: 3
112
+ summary: Simple shared authentication API
113
+ test_files:
114
+ - spec/lib/simple-auth/api_spec.rb
115
+ - spec/lib/simple-auth/configuration_helper_spec.rb
116
+ - spec/spec_helper.rb
117
+ - spec/support/configuration.rb
118
+ - spec/support/mock_requests.rb
119
+ - spec/support/stubs.rb