Class: CZTop::CURVE::Auth
- Inherits:
-
Object
- Object
- CZTop::CURVE::Auth
- 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.
Class Method Summary collapse
- ._poststop(thread) ⇒ Object private
Instance Method Summary collapse
-
#allow(pubkey) ⇒ void
Adds a client public key to the allowed set.
-
#deny(pubkey) ⇒ void
Removes a client public key from the allowed set.
-
#initialize(allowed_clients: nil, allow_any: false) ⇒ Auth
constructor
A new instance of Auth.
-
#stop ⇒ void
Stops the ZAP handler thread and closes the socket.
Constructor Details
#initialize(allowed_clients: nil, allow_any: false) ⇒ Auth
Returns a new instance of Auth.
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 |
#stop ⇒ void
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 |