Class: CZTop::CURVE::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/cztop/curve/auth.rb

Overview

In-memory ZAP authentication handler for CURVE encryption.

Runs a ZAP responder on inproc://zeromq.zap.01 in a background thread, authenticating CURVE clients by their public key. No filesystem access needed.

Examples:

Allow specific clients

auth = CZTop::CURVE::Auth.new(allowed_clients: [client1_pub, client2_pub])

Allow any CURVE client

auth = CZTop::CURVE::Auth.new(allow_any: true)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(allowed_clients: nil, allow_any: false) ⇒ Auth

Returns a new instance of Auth.

Raises:

  • (NotImplementedError)

    if CURVE is not available



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cztop/curve/auth.rb', line 25

def initialize(allowed_clients: nil, allow_any: false)
  raise NotImplementedError, 'CURVE not available in this libzmq build' unless CURVE.available?
  @mutex = Mutex.new
  @allowed = allowed_clients&.map { |k| k.b.freeze }&.then { |keys| Set.new(keys) }
  @allow_any = allow_any
  @zap = CZTop::Socket::REP.new
  @zap.linger = 0
  @zap.bind('inproc://zeromq.zap.01')
  @thread = Thread.new { run }
  ObjectSpace.define_finalizer(self, self.class._poststop(@thread))
end

Class Method Details

._poststop(thread) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



69
70
71
72
73
# File 'lib/cztop/curve/auth.rb', line 69

def self._poststop(thread)
  ->(_id) do
    thread.kill rescue nil
  end
end

Instance Method Details

#allow(pubkey) ⇒ void

This method returns an undefined value.

Adds a client public key to the allowed set.



41
42
43
44
45
46
# File 'lib/cztop/curve/auth.rb', line 41

def allow(pubkey)
  @mutex.synchronize do
    @allowed ||= Set.new
    @allowed.add(pubkey.b.freeze)
  end
end

#deny(pubkey) ⇒ void

This method returns an undefined value.

Removes a client public key from the allowed set.



52
53
54
55
56
# File 'lib/cztop/curve/auth.rb', line 52

def deny(pubkey)
  @mutex.synchronize do
    @allowed&.delete(pubkey.b)
  end
end

#stopvoid

This method returns an undefined value.

Stops the ZAP handler thread and closes the socket.



61
62
63
64
65
# File 'lib/cztop/curve/auth.rb', line 61

def stop
  ObjectSpace.undefine_finalizer(self)
  @zap.close
  @thread.join(1)
end