Skip to content

Commit 2dff074

Browse files
committed
Credentials now accumulates entries
1 parent 3efb379 commit 2dff074

File tree

5 files changed

+85
-12
lines changed

5 files changed

+85
-12
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ get "/" do |env|
3636
end
3737
```
3838

39+
### accumulated entries
40+
41+
When `basic_auth` is called in several times, the credentials are accumulated and shared by all pages.
42+
43+
```crystal
44+
basic_auth "user1", "123"
45+
get "/members" do |env|
46+
"restricted page" # both `user1` and `guest` can see this page.
47+
end
48+
49+
basic_auth "guest", "temp"
50+
get "/trial" do |env|
51+
"restricted page" # both `user1` and `guest` can see this page.
52+
end
53+
```
54+
3955
## Contributing
4056

4157
1. Fork it ( https://github.com/kemalcr/kemal-basic-auth/fork )

spec/credentials_spec.cr

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,35 @@ describe "HTTPBasicAuth::Credentials" do
66
"serdar" => "123",
77
"dogruyol" => "abc",
88
}
9-
crendentials = HTTPBasicAuth::Credentials.new(entries)
9+
credentials = HTTPBasicAuth::Credentials.new(entries)
1010

11-
crendentials.authorize?("serdar" , "123").should eq("serdar")
12-
crendentials.authorize?("serdar" , "xxx").should eq(nil)
13-
crendentials.authorize?("dogruyol", "abc").should eq("dogruyol")
14-
crendentials.authorize?("dogruyol", "xxx").should eq(nil)
15-
crendentials.authorize?("foo" , "bar").should eq(nil)
11+
credentials.authorize?("serdar" , "123").should eq("serdar")
12+
credentials.authorize?("serdar" , "xxx").should eq(nil)
13+
credentials.authorize?("dogruyol", "abc").should eq("dogruyol")
14+
credentials.authorize?("dogruyol", "xxx").should eq(nil)
15+
credentials.authorize?("foo" , "bar").should eq(nil)
16+
end
17+
18+
describe "#update" do
19+
credentials = HTTPBasicAuth::Credentials.new
20+
21+
it "(String, String) adds a new entry" do
22+
credentials.authorize?("serdar", "123").should eq(nil)
23+
credentials.update("serdar", "123")
24+
credentials.authorize?("serdar", "123").should eq("serdar")
25+
credentials.authorize?("serdar", "xxx").should eq(nil)
26+
end
27+
28+
it "(Hash) adds new entries" do
29+
credentials.update({"a" => "1", "b" => "2"})
30+
credentials.authorize?("a", "1").should eq("a")
31+
credentials.authorize?("a", "x").should eq(nil)
32+
credentials.authorize?("b", "2").should eq("b")
33+
credentials.authorize?("c", "3").should eq(nil)
34+
end
35+
36+
it "preserves accumulated entries" do
37+
credentials.authorize?("serdar", "123").should eq("serdar")
38+
end
1639
end
1740
end

spec/kemal-basic-auth_spec.cr

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,23 @@ describe "HTTPBasicAuth" do
2828
context.kemal_authorized_username?.should eq(nil)
2929
end
3030

31-
it "adds HTTPBasicAuthHandler" do
31+
it "adds HTTPBasicAuthHandler at most once" do
3232
basic_auth "serdar", "123"
3333
Kemal.config.handlers.size.should eq 6
34+
35+
basic_auth "dogruyol", "abc"
36+
Kemal.config.handlers.size.should eq 6
37+
end
38+
39+
describe ".runtime" do
40+
it "returns singleton instance" do
41+
HTTPBasicAuth.runtime.should be_a(HTTPBasicAuth)
42+
end
43+
44+
it "is affected by `basic_auth`" do
45+
HTTPBasicAuth.runtime.credentials.authorize?("a", "1").should eq(nil)
46+
basic_auth "a", "1"
47+
HTTPBasicAuth.runtime.credentials.authorize?("a", "1").should eq("a")
48+
end
3449
end
3550
end

src/kemal-basic-auth.cr

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,16 @@ class HTTPBasicAuth
2020
AUTH_MESSAGE = "Could not verify your access level for that URL.\nYou have to login with proper credentials"
2121
HEADER_LOGIN_REQUIRED = "Basic realm=\"Login Required\""
2222

23-
def initialize(@credentials : Credentials)
23+
# a lazy singleton instance which is automatically added to handler in first access
24+
@@runtime : self?
25+
def self.runtime
26+
@@runtime ||= new.tap{|handler| add_handler handler}
27+
@@runtime.not_nil!
28+
end
29+
30+
getter credentials
31+
32+
def initialize(@credentials : Credentials = Credentials.new)
2433
end
2534

2635
# backward compatibility
@@ -57,9 +66,9 @@ end
5766

5867
# Helper to easily add HTTP Basic Auth support.
5968
def basic_auth(username, password)
60-
add_handler HTTPBasicAuth.new(username, password)
69+
HTTPBasicAuth.runtime.credentials.update(username, password)
6170
end
6271

6372
def basic_auth(crendentials : Hash(String, String))
64-
add_handler HTTPBasicAuth.new(crendentials)
73+
HTTPBasicAuth.runtime.credentials.update(crendentials)
6574
end

src/kemal-basic-auth/credentials.cr

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
class HTTPBasicAuth
1+
class HTTPBasicAuth
22
class Credentials
3-
def initialize(@entries : Hash(String, String) = Hash(String, String).new)
3+
alias Entries = Hash(String, String)
4+
5+
def initialize(@entries : Entries = Entries.new)
46
end
57

68
def authorize?(username : String, password : String) : String?
@@ -10,5 +12,13 @@ class HTTPBasicAuth
1012
nil
1113
end
1214
end
15+
16+
def update(username : String, password : String)
17+
@entries[username] = password
18+
end
19+
20+
def update(other : Entries)
21+
@entries.merge!(other)
22+
end
1323
end
1424
end

0 commit comments

Comments
 (0)