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.

Important note: when you construct the Zas::Client it is your responsibility to shut down cleanly. To do this call client.shutdown when your consumer exits.



24
25
26
27
28
29
30
# File 'lib/zas/client.rb', line 24

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
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.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/zas/client.rb', line 44

def authenticate(credentials)
  begin
    raise("Send failed") unless 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}"
  rescue => e
    logger.error "Error occurred: #{e.message}"
    raise
  end
end

#closeObject

Public: Close the context.



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

def close
  if @context
    puts "Closing context #{@context.inspect}"
    @context.close
    puts "Closed context #{@context.inspect}"
  end
  @context = nil
rescue Exception => e
  puts "Exception is close(): #{e.message}"
  raise
end

#disconnectObject

Public: Disconnect the socket.



65
66
67
68
69
70
71
72
73
74
# File 'lib/zas/client.rb', line 65

def disconnect
  if @socket
    @socket.close 
    logger.debug "Closed socket #{@socket.inspect}"
  end
  @socket = nil
rescue Exception => e
  puts "Exception in disconnect(): #{e.message}"
  raise
end

#shutdownObject

Public: shut down the client (disconnect any open sockets and close the context.



91
92
93
94
95
96
97
98
99
100
# File 'lib/zas/client.rb', line 91

def shutdown
  puts "Disconnecting"
  disconnect
  puts "Closing"
  close
  puts "Done"
rescue Exception => e
  puts "Exception in shutdown(): #{e.message}"
  raise
end