aboutsummaryrefslogtreecommitdiff
path: root/lib/mauve/auth_bytemark.rb
blob: 52fa610fb1c58d851540372249565b3f2bf7d909 (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
# 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?

    return false if ENV['RACK_ENV'].to_s == "development"

    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)
    rescue Exception => ex
      return false
    end
  end

end