aboutsummaryrefslogtreecommitdiff
path: root/lib/mauve/auth_bytemark.rb
blob: c0834e04425c0fea07095091d61d443f93e3c56a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# encoding: UTF-8
require 'sha1'
require 'xmlrpc/client'
require 'timeout'

class AuthBytemark 

  def initialize (srv='auth.bytemark.co.uk', port=443)
    raise ArgumentError.new("Server must be a String, not a #{srv.class}") if String != srv.class
    raise ArgumentError.new("Port must be a Fixnum, not a #{port.class}") if Fixnum != port.class
    @srv = srv
    @port = port
    @timeout = 7
    @logger = Log4r::Logger.new(self.class.to_s)
  end

  ## Not really needed.
  def ping ()
    begin
      MauveTimeout.timeout(@timeout) do
        s = TCPSocket.open(@srv, @port)
        s.close()
        return true
      end
    rescue MauveTimeout::Error => ex
      return false
    rescue => ex 
      return false
    end
    return false
  end

  def authenticate(login, password)
    raise ArgumentError.new("Login must be a string, not a #{login.class}") if String != login.class
    raise ArgumentError.new("Password must be a string, not a #{password.class}") if String != password.class
    raise ArgumentError.new("Login or/and password is/are empty.") if login.empty? || password.empty?

    client = XMLRPC::Client.new(@srv,"/",@port,nil,nil,nil,nil,true,@timeout).proxy("bytemark.auth")

    begin
      challenge = client.getChallengeForUser(login)
      response = Digest::SHA1.new.update(challenge).update(password).hexdigest
      client.login(login, response)
      return true
    rescue XMLRPC::FaultException => fault
      Mauve::Server.instance.logger.warn "Fault code is #{fault.faultCode} stating #{fault.faultString}"
      return false
    rescue Exception => ex
      Mauve::Server.instance.logger.error "Caught #{ex.to_s} whilst trying to loging for #{login}"
      Mauve::Server.instance.logger.debug ex.backtrace.join("\n")
      return false
    end
  end

end