add auth parser

master
Kanezoh 2022-01-01 21:20:07 +09:00
parent c46539ea44
commit 603be3ff5b
4 changed files with 71 additions and 34 deletions

View File

@ -30,4 +30,39 @@ describe "Mechanize HTTP Authentication test" do
parser.scanner = StringScanner.new("realm=\"this site\"") parser.scanner = StringScanner.new("realm=\"this site\"")
parser.auth_param.should eq ["realm", "this site"] parser.auth_param.should eq ["realm", "this site"]
end end
it "test parse" do
expect = [Mechanize::HTTP::AuthChallenge.new("Basic", {"realm" => "foo", "qop" => "auth,auth-int"})]
parser = Mechanize::HTTP::WWWAuthenticateParser.new
result = parser.parse("Basic realm=foo, qop=\"auth,auth-int\"")
result[0].scheme.should eq expect[0].scheme
result[0].params.should eq expect[0].params
end
it "test_parse_without_comma_delimiter" do
expect = [challenge("Basic", {"realm" => "foo", "qop" => "auth,auth-int"})]
parser = Mechanize::HTTP::WWWAuthenticateParser.new
result = parser.parse("Basic realm=foo qop=\"auth,auth-int\"")
result[0].scheme.should eq expect[0].scheme
result[0].params.should eq expect[0].params
end
it "test_parse_multiple" do
expect = [
challenge("Basic", {"realm" => "foo"}),
challenge("Digest", {"realm" => "bar"}),
]
parser = Mechanize::HTTP::WWWAuthenticateParser.new
result = parser.parse("Basic realm=foo, Digest realm=bar")
result[0].scheme.should eq expect[0].scheme
result[0].params.should eq expect[0].params
result[1].scheme.should eq expect[1].scheme
result[1].params.should eq expect[1].params
end
end
private def challenge(scheme, params)
Mechanize::HTTP::AuthChallenge.new(scheme, params)
end end

View File

@ -170,7 +170,7 @@ class Mechanize
private def save_response_cookies(response, uri, page) private def save_response_cookies(response, uri, page)
if page.body =~ /Set-Cookie/ if page.body =~ /Set-Cookie/
page.css("head meta[http-equiv=\"Set-Cookie\"]").each do |meta| page.css("head meta[http-equiv=\"Set-Cookie\"]").each do |meta|
cookie = meta["content"].split(";") # [0] cookie = meta["content"].split(";")[0]
key, value = cookie.split("=") key, value = cookie.split("=")
cookie = ::HTTP::Cookie.new(name: key, value: value) cookie = ::HTTP::Cookie.new(name: key, value: value)
save_cookies(uri, [cookie]) save_cookies(uri, [cookie])

View File

@ -5,14 +5,16 @@ class Mechanize
class AuthChallenge class AuthChallenge
property scheme : String? property scheme : String?
property params : String? property params : String? | Hash(String, String)?
def initialize(scheme = nil, params = nil) def initialize(scheme = nil, params = nil)
@scheme = scheme
@params = params
end end
# def [] param def [](param)
# params[param] params[param]
# end end
## ##
# Constructs an AuthRealm for this challenge # Constructs an AuthRealm for this challenge

View File

@ -18,15 +18,16 @@ class Mechanize
# Parsers the header. Returns an Array of challenges as strings # Parsers the header. Returns an Array of challenges as strings
def parse(www_authenticate : String) def parse(www_authenticate : String)
challenges = [Mechanize::HTTP::AuthChallenge] challenges = [] of Mechanize::HTTP::AuthChallenge
scanner = StringScanner.new(www_authenticate) @scanner = StringScanner.new(www_authenticate)
loop do loop do
break if scanner.eos? break if scanner.eos?
start = scanner.pos start = scanner.offset
challenge = Mechanize::HTTP::AuthChallenge.new challenge = Mechanize::HTTP::AuthChallenge.new
scheme = auth_scheme scheme = auth_scheme
if scheme == "Negotiate" if scheme == "Negotiate"
scan_comma_spaces scan_comma_spaces
end end
@ -45,39 +46,38 @@ class Mechanize
challenges << challenge challenges << challenge
next next
else else
scheme.capitalize! scheme = scheme.capitalize
end end
# next unless space next unless space
# params = {} params = Hash(String, String).new
# while true do loop do
# pos = @scanner.pos offset = scanner.offset
# name, value = auth_param param = auth_param
if param
name, value = param
name = name.downcase if name =~ /^realm$/i
params[name] = value
else
challenge.params = params
challenges << challenge
# name.downcase! if name =~ /^realm$/i if scanner.eos?
# challenge.raw = www_authenticate[start, scanner.offset]
break
end
# unless name then scanner.offset = offset # rewind
# challenge.params = params # challenge.raw = www_authenticate[start, scanner.offset].sub(/(,+)? *$/, "")
# challenges << challenge challenge = nil # a token should be next, new challenge
break
end
# if @scanner.eos? then spaces
# challenge.raw = www_authenticate[start, @scanner.pos]
# break
# end
# @scanner.pos = pos # rewind scanner.scan(/(, *)+/)
# challenge.raw = www_authenticate[start, @scanner.pos].sub(/(,+)? *$/, '') end
# challenge = nil # a token should be next, new challenge
# break
# else
# params[name] = value
# end
# spaces
# @scanner.scan(/(, *)+/)
# end
end end
challenges challenges