Last active
August 29, 2015 14:16
-
-
Save tk3/1ddf679430165a908cca to your computer and use it in GitHub Desktop.
Trusterd で Basic 認証 (骨組み)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SERVER_NAME = "Trusterd" | |
SERVER_VERSION = "0.0.1" | |
SERVER_DESCRIPTION = "#{SERVER_NAME}/#{SERVER_VERSION}" | |
root_dir = "/home/vagrant/trusterd" | |
s = HTTP2::Server.new({ | |
:port => 8080, | |
:document_root => "#{root_dir}/htdocs", | |
:server_name => SERVER_DESCRIPTION, | |
:worker => "auto", | |
:key => "#{root_dir}/ssl/server.key", | |
:crt => "#{root_dir}/ssl/server.crt", | |
#:dh_params_file => "#{root_dir}/ssl/dh.pem", | |
:tls => true, | |
:callback => true, | |
}) | |
module HTTP2 | |
class Server | |
class BasicAuth | |
def initialize(config) | |
@config = { | |
:realm_name => "Private page", | |
}.merge(config) | |
raise ArgumentError unless @config.key?(:htpasswd) | |
@users = {} | |
IO.open(IO.sysopen(@config[:htpasswd])) do |io| | |
io.each do |line| | |
params = line.chomp.split("\t") | |
@users[params[0]] = params[1] if 1 < params.size | |
end | |
end | |
end | |
def authn(s) | |
if s.r.headers_in["authorization"].nil? | |
s.r.headers_out["www-authenticate"] = %Q(Basic realm="#{@config[:realm_name]}") | |
s.set_status 401 | |
return false | |
else | |
auth = s.r.headers_in["authorization"] | |
params = auth.split(" ")[1].unpack("m")[0].split(":") | |
unless @users.key?(params[0]) && @users[params[0]] == params[1] | |
s.r.headers_out["www-authenticate"] = %Q(Basic realm="#{@config[:realm_name]}") | |
s.set_status 401 | |
return false | |
end | |
return true | |
end | |
end | |
end | |
end | |
end | |
basic = HTTP2::Server::BasicAuth.new({ | |
:realm_name => "Private Area", | |
:htpasswd => "/home/vagrant/trusterd/user.list", | |
}) | |
s.set_access_checker_cb { | |
basic.authn(s) | |
} | |
s.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment