Class: Zas::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/zas/client.rb

Overview

Public: A client for the Zero Authentication Service.

Instance Method Summary collapse

Constructor Details

#initialize(config = ClientConfiguration.new) ⇒ Client

Public: Initialize the client with the given configuration.

config - Configuration spec for the client.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/zas/client.rb', line 21

def initialize(config=ClientConfiguration.new)
  self.host = config.host
  self.port = config.port
  self.timeout = config.timeout
  self.logger = config.logger
  self.context = ZMQ::Context.new

  Signal.trap(:INT) { 
    begin
      puts "Trapped INT in Zas client"
      disconnect
      close
    rescue => e
      puts "Error while trying to shut down zas client: #{e.message}"
    end
  }
end

Instance Method Details

#authenticate(credentials) ⇒ Object

Public: Authenticate the given credentials.

credentials - The credentials. The credentials may be any object that implements the #to_wire method. This method must construct a JSON data structure with at least the following two keys:

strategy - The name of the strategy to use
credentials - A data structure with the credentials

Returns the results of the authentication.

Raises a Zas::TimeoutError if the authentication service does not respond within the timeout as specified the configuration.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/zas/client.rb', line 51

def authenticate(credentials)
  begin
    socket.send credentials.to_wire
    logger.debug "Selecting on socket #{socket.inspect} with timeout #{timeout}"
    if ZMQ.select([socket], nil, nil, timeout)
      logger.debug("Receiving from socket #{socket.inspect}")
      Hashie::Mash.new(Yajl::Parser.parse(socket.recv))
    else
      logger.debug("Failed to select on socket #{socket.inspect}")
      disconnect
      raise Zas::TimeoutError, "Response from authentication service not received in time"
    end
  rescue IOError => e
    logger.error "IO error occurred: #{e.message}" 
  end 
end

#closeObject

Public: Close the context.



78
79
80
81
82
83
84
# File 'lib/zas/client.rb', line 78

def close
  if context
    logger.debug "Closing context #{context.inspect}"
    context.close
    context = nil
  end
end

#disconnectObject

Public: Disconnect the socket.



69
70
71
72
73
74
75
# File 'lib/zas/client.rb', line 69

def disconnect
  if socket
    logger.debug "Closing socket #{socket.inspect}"
    socket.close
  end
  @socket = nil
end