Created
November 23, 2011 09:18
-
-
Save gareth/1388254 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env ruby | |
require 'logger' | |
$stdout.sync = true | |
$stdin.sync = true | |
path = "/usr/local/var/log/ejabberd/auth.log" | |
file = File.open(path, File::WRONLY | File::APPEND | File::CREAT) | |
file.sync = true | |
$logger = Logger.new(file) | |
$logger.level = Logger::DEBUG | |
$logger.info "Starting ejabberd authentication service" | |
require 'net/https' | |
# require 'rubygems' | |
# require 'bundler/setup' | |
def auth(username, password) | |
subdomain = username.split("@").last.split(".").first | |
begin | |
uri = URI("https://#{subdomain}.campfirenow.com/account.xml") | |
http = Net::HTTP.new uri.host, uri.port | |
http.use_ssl = true if uri.scheme == "https" | |
http.start do |http| | |
req = Net::HTTP::Get.new(uri.request_uri) | |
req.basic_auth password, 'X' | |
http_response = http.request(req) | |
case http_response | |
when Net::HTTPSuccess | |
true | |
else | |
false | |
end | |
end | |
rescue Exception => e | |
RAILS_DEFAULT_LOGGER.info "ERROR: #{e}" | |
RAILS_DEFAULT_LOGGER.info e.backtrace.pretty_inspect | |
false | |
end | |
# rescue Tinder::AuthenticationFailed | |
# return false | |
rescue StandardError | |
return false | |
end | |
$logger.info ".Started ejabberd authentication service" | |
loop do | |
begin | |
$stdin.eof? # wait for input | |
start = Time.now | |
msg = $stdin.read(2) | |
length = msg.unpack('n').first | |
msg = $stdin.read(length) | |
cmd, *data = msg.split(":") | |
$logger.info "Incoming Request: '#{cmd}'" | |
success = case cmd | |
when "auth" | |
$logger.info "Authenticating #{data[0]}@#{data[1]}/#{data[2]}" | |
auth(data[0], data[2]) | |
else | |
false | |
end | |
bool = success ? 1 : 0 | |
$stdout.write [2, bool].pack("nn") | |
$logger.info "Response: #{success ? "success" : "failure"}" | |
# rescue => e | |
# $logger.error "#{e.class.name}: #{e.message}" | |
# $logger.error e.backtrace.join("\n\t") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment