rack-auth-cheat 0.1.0

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/CHANGES ADDED
@@ -0,0 +1,2 @@
1
+ = 0.1.0 - 21-Dec-2009
2
+ * Initial release
@@ -0,0 +1,7 @@
1
+ CHANGES
2
+ MANIFEST
3
+ README
4
+ Rakefile
5
+ rack-auth-cheat.gemspec
6
+ lib/rack/auth/cheat.rb
7
+ test/test_rack_auth_cheat.rb
data/README ADDED
@@ -0,0 +1,38 @@
1
+ = Description
2
+ Rack middleware that authenticates requests with matching username and password
3
+
4
+ = Prerequisites
5
+ rack 1.0.0 or later
6
+
7
+ = Usage
8
+ use "Rack::Auth::Cheat", "user_field", "password_field"
9
+
10
+ = Default Fields
11
+ The default user field is "username".
12
+ The default password field is "password".
13
+
14
+ = Details
15
+ The rack-auth-cheat library provides a Rack middleware interface which authenticates
16
+ any request with a matching username and password. This is useful in development
17
+ environments, especially as a way to stub out an external authentication system. Be
18
+ sure to include this from an environment-specific file such as
19
+ config/environments/development.rb rather than from a global configuration file such
20
+ as config/environment.rb
21
+
22
+ The call method we've defined first checks to see if the AUTH_USER
23
+ environment variable is set. If it is, we assume that the user has
24
+ already been authenticated and move on.
25
+
26
+ If AUTH_USER is not set, and AUTH_FAIL is not set, we then check whether
27
+ the username and password match (the "Cheat" authentication method). If
28
+ they match, AUTH_USER is set to the username.
29
+
30
+ If they don't match then the request is passed on without modification.
31
+ AUTH_FAIL will not be set by this class.
32
+
33
+ It up to the application to check for the presence of AUTH_USER and/or
34
+ AUTH_FAIL and act as necessary.
35
+
36
+ = Authors
37
+ Daniel Berger
38
+ Charlie O'Keefe
@@ -0,0 +1,54 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rbconfig'
4
+
5
+ desc 'Install the rack-auth-cheat library (non-gem)'
6
+ task :install do
7
+ dir = File.join(CONFIG['sitelibdir'], 'rack', 'auth')
8
+ FileUtils.mkdir_p(dir) unless File.exists?(dir)
9
+ file = 'lib/rack/auth/cheat.rb'
10
+ FileUtils.cp_r(file, dir, :verbose => true)
11
+ end
12
+
13
+ desc 'Build the gem'
14
+ task :gem do
15
+ spec = eval(IO.read('rack-auth-cheat.gemspec'))
16
+ Gem::Builder.new(spec).build
17
+ end
18
+
19
+ desc 'Install the rack-auth-cheat library as a gem'
20
+ task :install_gem => [:gem] do
21
+ file = Dir["*.gem"].first
22
+ sh "gem install #{file}"
23
+ end
24
+
25
+ desc 'Export the git archive to a .zip, .gz and .bz2 file in your home directory'
26
+ task :export, :output_file do |t, args|
27
+ file = args[:output_file]
28
+
29
+ sh "git archive --prefix #{file}/ --output #{ENV['HOME']}/#{file}.tar master"
30
+
31
+ Dir.chdir(ENV['HOME']) do
32
+ sh "gzip -f #{ENV['HOME']}/#{file}.tar"
33
+ end
34
+
35
+ sh "git archive --prefix #{file}/ --output #{ENV['HOME']}/#{file}.tar master"
36
+
37
+ Dir.chdir(ENV['HOME']) do
38
+ sh "bzip2 -f #{ENV['HOME']}/#{file}.tar"
39
+ end
40
+
41
+ sh "git archive --prefix #{file}/ --output #{ENV['HOME']}/#{file}.zip --format zip master"
42
+
43
+ Dir.chdir(ENV['HOME']) do
44
+ sh "unzip #{file}.zip"
45
+ Dir.chdir(file) do
46
+ sh "rake gem"
47
+ end
48
+ end
49
+ end
50
+
51
+ Rake::TestTask.new do |t|
52
+ t.verbose = true
53
+ t.warning = true
54
+ end
@@ -0,0 +1,58 @@
1
+ module Rack
2
+ module Auth
3
+ class Cheat
4
+ # Creates a new Rack::Auth::Cheat object. The +user_field+ and +password_field+
5
+ # are the params looked for in the call method. The defaults are 'username'
6
+ # and 'password', respectively.
7
+ #
8
+ def initialize(app, user_field = 'username', password_field = 'password')
9
+ @app = app
10
+ @user_field = user_field
11
+ @password_field = password_field
12
+ end
13
+
14
+ # The call method we've defined first checks to see if the AUTH_USER
15
+ # environment variable is set. If it is, we assume that the user has
16
+ # already been authenticated and move on.
17
+ #
18
+ # If AUTH_USER is not set, and AUTH_FAIL is not set, we then check whether
19
+ # the username and password match (the "Cheat" authentication method). If
20
+ # they match, AUTH_USER is set to the username.
21
+ #
22
+ # If they don't match then the request is passed on without modification.
23
+ # AUTH_FAIL will not be set by this class.
24
+ #
25
+ # It up to the application to check for the presence of AUTH_USER and/or
26
+ # AUTH_FAIL and act as necessary.
27
+ #
28
+ def call(env)
29
+ request = Rack::Request.new(env)
30
+
31
+ user = request.params[@user_field]
32
+ password = request.params[@password_field]
33
+
34
+ # Only authenticate user if both the username and password fields are present
35
+ unless user && password
36
+ return @app.call(env)
37
+ end
38
+
39
+ # Do not authenticate if either one of these is set
40
+ if env['AUTH_USER'] || env['AUTH_FAIL']
41
+ return @app.call(env)
42
+ end
43
+
44
+ if user == password
45
+ env['AUTH_USER'] = user
46
+ env['AUTH_TYPE'] = "Cheat"
47
+ env['AUTH_TYPE_USER'] = user
48
+ env['AUTH_TYPE_THIS_REQUEST'] = "Cheat"
49
+ env['AUTH_DATETIME'] = Time.now.utc
50
+
51
+ env.delete('AUTH_FAIL')
52
+ end
53
+
54
+ @app.call(env)
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,25 @@
1
+ require 'rubygems'
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = 'rack-auth-cheat'
5
+ gem.version = '0.1.0'
6
+ gem.authors = ["Daniel Berger", "Charlie O'Keefe"]
7
+ gem.email = 'cokeefe@globe.gov'
8
+ gem.homepage = 'http://www.github.com/rack-auth-cheat'
9
+ gem.summary = 'Rack middleware that authenticates requests with matching username and password'
10
+ gem.test_file = 'test/test_rack_auth_cheat.rb'
11
+ gem.files = Dir['**/*'].delete_if{ |item| item.include?('git') }
12
+
13
+ gem.extra_rdoc_files = ['CHANGES', 'README', 'MANIFEST']
14
+
15
+ gem.add_dependency('rack', '>= 1.0.0')
16
+
17
+ gem.description = <<-EOF
18
+ The rack-auth-cheat library provides a Rack middleware interface which authenticates
19
+ any request with a matching username and password. This is useful in development
20
+ environments, especially as a way to stub out an external authentication system. Be
21
+ sure to include this from an environment-specific file such as
22
+ config/environments/development.rb rather than from a global configuration file such
23
+ as config/environment.rb
24
+ EOF
25
+ end
@@ -0,0 +1,22 @@
1
+ require 'test/unit'
2
+ require 'rack/auth/cheat'
3
+
4
+ class TC_Rack_Auth_Cheat < Test::Unit::TestCase
5
+ def setup
6
+ @app = 1 # Placeholder
7
+ @env = 1 # Placeholder
8
+ @rack = Rack::Auth::Cheat.new(@app)
9
+ end
10
+
11
+ def test_constructor_basic
12
+ assert_nothing_raised{ Rack::Auth::Cheat.new(@app) }
13
+ end
14
+
15
+ def test_version
16
+ assert_equal('0.1.0', Rack::Auth::Cheat::VERSION)
17
+ end
18
+
19
+ def teardown
20
+ @rack = nil
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-auth-cheat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Berger
8
+ - Charlie O'Keefe
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-12-21 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rack
18
+ type: :runtime
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 1.0.0
25
+ version:
26
+ description: " The rack-auth-cheat library provides a Rack middleware interface which authenticates\n any request with a matching username and password. This is useful in development\n environments, especially as a way to stub out an external authentication system. Be\n sure to include this from an environment-specific file such as\n config/environments/development.rb rather than from a global configuration file such\n as config/environment.rb\n"
27
+ email: cokeefe@globe.gov
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - CHANGES
34
+ - README
35
+ - MANIFEST
36
+ files:
37
+ - CHANGES
38
+ - lib/rack/auth/cheat.rb
39
+ - MANIFEST
40
+ - rack-auth-cheat.gemspec
41
+ - Rakefile
42
+ - README
43
+ - test/test_rack_auth_cheat.rb
44
+ has_rdoc: true
45
+ homepage: http://www.github.com/rack-auth-cheat
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options: []
50
+
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ requirements: []
66
+
67
+ rubyforge_project:
68
+ rubygems_version: 1.3.5
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Rack middleware that authenticates requests with matching username and password
72
+ test_files:
73
+ - test/test_rack_auth_cheat.rb