From 67051da8769b93559f2c10533b3f2248bf030c7b Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Sat, 1 Jan 2022 13:27:20 +0900 Subject: [PATCH] add credentials_for --- spec/http_auth_store_spec.cr | 15 +++++++++++++-- src/mechanize/http/auth_store.cr | 21 +++++++++++++-------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/spec/http_auth_store_spec.cr b/spec/http_auth_store_spec.cr index 6f566a4..e52f8a7 100644 --- a/spec/http_auth_store_spec.cr +++ b/spec/http_auth_store_spec.cr @@ -6,8 +6,19 @@ describe "Mechanize AuthStore test" do url = URI.parse("http://example.com/") user = "kanezoh" password = "password" + realm = "" auth_store.add_auth(url, user, password) - auth_store.auth_accounts.size.should eq 1 - auth_store.auth_accounts[url].should eq({"kanezoh", "password"}) + auth_store.auth_accounts[url].size.should eq 1 + auth_store.auth_accounts[url][realm].should eq(["kanezoh", "password", nil]) + end + + it "credentials_for" do + auth_store = Mechanize::HTTP::AuthStore.new + url = URI.parse("http://example.com/") + user = "kanezoh" + password = "password" + realm = "" + auth_store.add_auth(url, user, password) + auth_store.credentials_for(url, realm).should eq ["kanezoh", "password", nil] end end diff --git a/src/mechanize/http/auth_store.cr b/src/mechanize/http/auth_store.cr index ad9976a..d40000b 100644 --- a/src/mechanize/http/auth_store.cr +++ b/src/mechanize/http/auth_store.cr @@ -2,25 +2,28 @@ class Mechanize module HTTP # This class store info for HTTP Authentication. class AuthStore - getter auth_accounts : Hash(URI, Tuple(String, String)) + getter auth_accounts : Hash(URI, Hash(String, Array(String?))) def initialize - @auth_accounts = Hash(URI, Tuple(String, String)).new + @auth_accounts = Hash(URI, Hash(String, Array(String?))).new end - def add_auth(uri, user, pass) + def add_auth(uri : String | URI, user : String, pass : String, realm : String? = nil, domain : String? = nil) unless uri.is_a?(URI) uri = URI.new(uri) end # uri += '/' uri.user = nil uri.password = nil - - auth_accounts[uri] = {user, pass} - end ## + if realm.nil? + realm = "" + end + realm_hash = {realm => [user, pass, domain]} + auth_accounts[uri] = realm_hash + end # Retrieves credentials for +realm+ on the server at +uri+. - def credentials_for(uri, realm) : Tuple(String) + def credentials_for(uri : String | URI, realm : String) : Array(String?) unless uri.is_a?(URI) uri = URI.new(uri) end @@ -28,7 +31,9 @@ class Mechanize uri.user = nil uri.password = nil - auth_accounts[uri] + realms = auth_accounts[uri] + + realms[realm] end end end