basica 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e3327b0583b781fbc1fe0713836e95c3f7274a66
4
+ data.tar.gz: 7a5f68e27845f98138b08ffedf0b6fad7fe7b0e2
5
+ SHA512:
6
+ metadata.gz: 40d018c2414e3819961c62181a5a5975e9081081a6f017cd4cf1d1649d500f85bd328ea476a96e9ef63ea9c3de44589b87c8cfc896b7c50e9902cb2b3f3ed432
7
+ data.tar.gz: 8bf891f0394218003c2b067b4bcca66a85b61564219f78ddb196a02b4a3261fb7abe24fed29c1189a73d005ba802721631ccf83da2cff2f0c54782abbd6b98fb
data/.gems ADDED
@@ -0,0 +1 @@
1
+ cutest -v 1.2.0
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2013 Michel Martens
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,78 @@
1
+ basica
2
+ ======
3
+
4
+ Basic authentication library.
5
+
6
+ Description
7
+ -----------
8
+
9
+ Basic authentication library suited for Cuba apps.
10
+
11
+ ## Usage
12
+
13
+ Include the Basica module in your application, then call it
14
+ by passing the `env` hash (should be available in every Rack
15
+ based application) and a block. If the `env` hash contains the
16
+ `HTTP_AUTHORIZATION` header, then the username and password will
17
+ be passed to the block.
18
+
19
+ The result of the method call to `basic_auth` will be that of
20
+ the block. If the `HTTP_AUTHORIZATION` header is not found, a
21
+ `RuntimeError` is raised.
22
+
23
+ ## Example
24
+
25
+ First, an example of Basica in the wild:
26
+
27
+ ```ruby
28
+ require "basica"
29
+
30
+ include Basica
31
+
32
+ header = "Basic %s" % Base64.encode64("foo:bar")
33
+
34
+ result = basic_auth("HTTP_AUTHORIZATION" => header) do |user, pass|
35
+ user == "foo" && pass == "bar"
36
+ end
37
+
38
+ if result
39
+ # The right credentials were provided.
40
+ end
41
+ ```
42
+
43
+ Now an example of how to use it with [Cuba][cuba] and
44
+ [Shield][shield]:
45
+
46
+ ```ruby
47
+ Cuba.plugin Basica
48
+
49
+ Cuba.define do
50
+ on env["HTTP_AUTHORIZATION"].nil? do
51
+ res.status = 401
52
+ res.headers["WWW-Authenticate"] = 'Basic realm="MyApp"'
53
+ res.write "Unauthorized"
54
+ end
55
+
56
+ basic_auth(env) do |user, pass|
57
+ login(User, user, pass)
58
+ end
59
+
60
+ on authenticated(User) do
61
+ run Users
62
+ end
63
+ end
64
+ ```
65
+
66
+ Note that the previous example assumes you have already required
67
+ Cuba, Shield, Basica, and that you have a User model defined.
68
+
69
+ [cuba]: http://cuba.is
70
+ [shield]: http://cyx.github.io/shield/
71
+
72
+ ## Installation
73
+
74
+ Install it using rubygems.
75
+
76
+ ```
77
+ $ gem install basica
78
+ ```
@@ -0,0 +1,13 @@
1
+ # encoding: utf-8
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "basica"
5
+ s.version = "0.0.1"
6
+ s.summary = "Basic authentication library."
7
+ s.description = "Basic authentication library suited for Cuba apps."
8
+ s.authors = ["Michel Martens"]
9
+ s.email = ["michel@soveran.com"]
10
+ s.homepage = "https://github.com/soveran/basica"
11
+ s.files = `git ls-files`.split("\n")
12
+ s.license = "MIT"
13
+ end
@@ -0,0 +1,14 @@
1
+ require "base64"
2
+
3
+ module Basica
4
+ def basic_auth(env)
5
+ if env["HTTP_AUTHORIZATION"]
6
+ auth = env["HTTP_AUTHORIZATION"].split(" ")[1]
7
+ user, pass = Base64.decode64(auth).split(":")
8
+
9
+ yield user, pass
10
+ else
11
+ raise "Bad request"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,32 @@
1
+ require File.expand_path("../lib/basica", File.dirname(__FILE__))
2
+ include Basica
3
+
4
+ scope do
5
+ setup do
6
+ "Basic %s" % Base64.encode64("foo:bar")
7
+ end
8
+
9
+ test "correct credentials" do |header|
10
+ result = basic_auth("HTTP_AUTHORIZATION" => header) do |user, pass|
11
+ user == "foo" && pass == "bar"
12
+ end
13
+
14
+ assert_equal true, result
15
+ end
16
+
17
+ test "incorrect credentials" do |header|
18
+ result = basic_auth("HTTP_AUTHORIZATION" => header) do |user, pass|
19
+ user == "foo" && pass == "baz"
20
+ end
21
+
22
+ assert_equal false, result
23
+ end
24
+
25
+ test "bad request" do
26
+ assert_raise RuntimeError do
27
+ result = basic_auth(Hash.new) do |user, pass|
28
+ user == "foo" && pass == "baz"
29
+ end
30
+ end
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: basica
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michel Martens
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Basic authentication library suited for Cuba apps.
14
+ email:
15
+ - michel@soveran.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - .gems
21
+ - LICENSE
22
+ - README.md
23
+ - basica.gemspec
24
+ - lib/basica.rb
25
+ - test/basica_test.rb
26
+ homepage: https://github.com/soveran/basica
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
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
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.0.3
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: Basic authentication library.
50
+ test_files: []