basica 0.0.1 → 1.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e3327b0583b781fbc1fe0713836e95c3f7274a66
4
- data.tar.gz: 7a5f68e27845f98138b08ffedf0b6fad7fe7b0e2
3
+ metadata.gz: 596643c3a44ac35fd6e6ba9709a306572019250d
4
+ data.tar.gz: 599e38abf05f1cdf834ba289bc26f41554a3ce6f
5
5
  SHA512:
6
- metadata.gz: 40d018c2414e3819961c62181a5a5975e9081081a6f017cd4cf1d1649d500f85bd328ea476a96e9ef63ea9c3de44589b87c8cfc896b7c50e9902cb2b3f3ed432
7
- data.tar.gz: 8bf891f0394218003c2b067b4bcca66a85b61564219f78ddb196a02b4a3261fb7abe24fed29c1189a73d005ba802721631ccf83da2cff2f0c54782abbd6b98fb
6
+ metadata.gz: a24e48bc8a045eec458c091cd264516e7c8699245a79f476c6ae1371ab9a081a2f96827063d1b645c2597508b686b7cafe05b691506a8549a06ae8e1178ecdf4
7
+ data.tar.gz: 1ebaa93abba947828f65e7554c4b0bdfa1247490fb07a4546e77b03a9f9a44811bdfee0f46e766834c01252e800553865dfc6840417c52cecee391c47d7e21d0
@@ -0,0 +1,19 @@
1
+ This code tries to solve a particular problem with a very simple
2
+ implementation. We try to keep the code to a minimum while making
3
+ it as clear as possible. The design is very likely finished, and
4
+ if some feature is missing it is possible that it was left out on
5
+ purpose. That said, new usage patterns may arise, and when that
6
+ happens we are ready to adapt if necessary.
7
+
8
+ A good first step for contributing is to meet us on IRC and discuss
9
+ ideas. We spend a lot of time on #lesscode at freenode, always ready
10
+ to talk about code and simplicity. If connecting to IRC is not an
11
+ option, you can create an issue explaining the proposed change and
12
+ a use case. We pay a lot of attention to use cases, because our
13
+ goal is to keep the code base simple. Usually the result of a
14
+ conversation is the creation of a different tool.
15
+
16
+ Please don't start the conversation with a pull request. The code
17
+ should come at last, and even though it may help to convey an idea,
18
+ more often than not it draws the attention to a particular
19
+ implementation.
data/README.md CHANGED
@@ -6,10 +6,6 @@ Basic authentication library.
6
6
  Description
7
7
  -----------
8
8
 
9
- Basic authentication library suited for Cuba apps.
10
-
11
- ## Usage
12
-
13
9
  Include the Basica module in your application, then call it
14
10
  by passing the `env` hash (should be available in every Rack
15
11
  based application) and a block. If the `env` hash contains the
@@ -31,7 +27,9 @@ include Basica
31
27
 
32
28
  header = "Basic %s" % Base64.encode64("foo:bar")
33
29
 
34
- result = basic_auth("HTTP_AUTHORIZATION" => header) do |user, pass|
30
+ env = { "HTTP_AUTHORIZATION" => header }
31
+
32
+ result = basic_auth(env) do |user, pass|
35
33
  user == "foo" && pass == "bar"
36
34
  end
37
35
 
@@ -47,12 +45,6 @@ Now an example of how to use it with [Cuba][cuba] and
47
45
  Cuba.plugin Basica
48
46
 
49
47
  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
48
  basic_auth(env) do |user, pass|
57
49
  login(User, user, pass)
58
50
  end
@@ -60,6 +52,12 @@ Cuba.define do
60
52
  on authenticated(User) do
61
53
  run Users
62
54
  end
55
+
56
+ on default do
57
+ res.status = 401
58
+ res.headers["WWW-Authenticate"] = 'Basic realm="MyApp"'
59
+ res.write "Unauthorized"
60
+ end
63
61
  end
64
62
  ```
65
63
 
@@ -2,9 +2,9 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "basica"
5
- s.version = "0.0.1"
5
+ s.version = "1.0.0"
6
6
  s.summary = "Basic authentication library."
7
- s.description = "Basic authentication library suited for Cuba apps."
7
+ s.description = "Basic authentication library for Rack applications."
8
8
  s.authors = ["Michel Martens"]
9
9
  s.email = ["michel@soveran.com"]
10
10
  s.homepage = "https://github.com/soveran/basica"
@@ -1,14 +1,16 @@
1
1
  require "base64"
2
2
 
3
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(":")
4
+ HTTP_AUTHORIZATION = "HTTP_AUTHORIZATION".freeze
8
5
 
9
- yield user, pass
10
- else
11
- raise "Bad request"
6
+ def basic_auth(env)
7
+ http_auth = env.fetch(HTTP_AUTHORIZATION) do
8
+ return nil
12
9
  end
10
+
11
+ cred = http_auth.split(" ")[1]
12
+ user, pass = Base64.decode64(cred).split(":")
13
+
14
+ yield(user, pass) || nil
13
15
  end
14
16
  end
@@ -1,4 +1,5 @@
1
- require File.expand_path("../lib/basica", File.dirname(__FILE__))
1
+ require_relative "../lib/basica"
2
+
2
3
  include Basica
3
4
 
4
5
  scope do
@@ -19,14 +20,14 @@ scope do
19
20
  user == "foo" && pass == "baz"
20
21
  end
21
22
 
22
- assert_equal false, result
23
+ assert_equal nil, result
23
24
  end
24
25
 
25
26
  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
27
+ result = basic_auth(Hash.new) do |user, pass|
28
+ user == "foo" && pass == "baz"
30
29
  end
30
+
31
+ assert_equal nil, result
31
32
  end
32
33
  end
metadata CHANGED
@@ -1,23 +1,24 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: basica
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michel Martens
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-03 00:00:00.000000000 Z
11
+ date: 2016-03-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Basic authentication library suited for Cuba apps.
13
+ description: Basic authentication library for Rack applications.
14
14
  email:
15
15
  - michel@soveran.com
16
16
  executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
- - .gems
20
+ - ".gems"
21
+ - CONTRIBUTING
21
22
  - LICENSE
22
23
  - README.md
23
24
  - basica.gemspec
@@ -33,17 +34,17 @@ require_paths:
33
34
  - lib
34
35
  required_ruby_version: !ruby/object:Gem::Requirement
35
36
  requirements:
36
- - - '>='
37
+ - - ">="
37
38
  - !ruby/object:Gem::Version
38
39
  version: '0'
39
40
  required_rubygems_version: !ruby/object:Gem::Requirement
40
41
  requirements:
41
- - - '>='
42
+ - - ">="
42
43
  - !ruby/object:Gem::Version
43
44
  version: '0'
44
45
  requirements: []
45
46
  rubyforge_project:
46
- rubygems_version: 2.0.3
47
+ rubygems_version: 2.4.5.1
47
48
  signing_key:
48
49
  specification_version: 4
49
50
  summary: Basic authentication library.