diff options
author | vnugent <public@vaughnnugent.com> | 2023-01-12 17:47:40 -0500 |
---|---|---|
committer | vnugent <public@vaughnnugent.com> | 2023-01-12 17:47:40 -0500 |
commit | b75668b164d398b99ee942beced06aa27ef65a50 (patch) | |
tree | c1faf6df3caa78083dcc38eb1a7247e456bbe754 /lib/VNLib.Data.Caching.Extensions/src/BrokerRegistrationRequest.cs | |
parent | cea64e619e714f6dbe51d37ca8329b58d8c271fb (diff) |
Large project reorder and consolidation
Diffstat (limited to 'lib/VNLib.Data.Caching.Extensions/src/BrokerRegistrationRequest.cs')
-rw-r--r-- | lib/VNLib.Data.Caching.Extensions/src/BrokerRegistrationRequest.cs | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/lib/VNLib.Data.Caching.Extensions/src/BrokerRegistrationRequest.cs b/lib/VNLib.Data.Caching.Extensions/src/BrokerRegistrationRequest.cs new file mode 100644 index 0000000..4ed4ab7 --- /dev/null +++ b/lib/VNLib.Data.Caching.Extensions/src/BrokerRegistrationRequest.cs @@ -0,0 +1,113 @@ +/* +* Copyright (c) 2022 Vaughn Nugent +* +* Library: VNLib +* Package: VNLib.Data.Caching.Extensions +* File: BrokerRegistrationRequest.cs +* +* BrokerRegistrationRequest.cs is part of VNLib.Data.Caching.Extensions which is part of the larger +* VNLib collection of libraries and utilities. +* +* VNLib.Data.Caching.Extensions is free software: you can redistribute it and/or modify +* it under the terms of the GNU Affero General Public License as +* published by the Free Software Foundation, either version 3 of the +* License, or (at your option) any later version. +* +* VNLib.Data.Caching.Extensions is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Affero General Public License for more details. +* +* You should have received a copy of the GNU Affero General Public License +* along with this program. If not, see https://www.gnu.org/licenses/. +*/ + +using VNLib.Utils; +using VNLib.Hashing.IdentityUtility; + + +namespace VNLib.Data.Caching.Extensions +{ + /// <summary> + /// A broker registration request message in a fluent api + /// format. This message may be disposed when no longer in use + /// </summary> + public sealed class BrokerRegistrationRequest : VnDisposeable + { + private bool ownsKey; + private ReadOnlyJsonWebKey? SigningKey; + + /// <summary> + /// The cache server node id + /// </summary> + public string? NodeId { get; private set; } + /// <summary> + /// The broker server's address + /// </summary> + public Uri? BrokerAddress { get; private set; } + /// <summary> + /// The security token used by the broker server to + /// authenticate during heartbeat connections + /// </summary> + public string? HeartbeatToken { get; private set; } + /// <summary> + /// The address for remote clients to use to + /// connect to this server + /// </summary> + public string? RegistrationAddress { get; private set; } + + /// <summary> + /// Recovers the private key from the supplied certificate + /// </summary> + /// <param name="jwk">The private key used to sign messages</param> + /// <param name="ownsKey">A value that indicates if the current instance owns the key</param> + /// <returns></returns> + /// <exception cref="ArgumentException"></exception> + public BrokerRegistrationRequest WithSigningKey(ReadOnlyJsonWebKey jwk, bool ownsKey) + { + this.ownsKey = ownsKey; + SigningKey = jwk ?? throw new ArgumentNullException(nameof(jwk)); + return this; + } + + public BrokerRegistrationRequest WithBroker(Uri brokerUri) + { + BrokerAddress = brokerUri; + return this; + } + + public BrokerRegistrationRequest WithRegistrationAddress(string address) + { + RegistrationAddress = address; + return this; + } + + public BrokerRegistrationRequest WithHeartbeatToken(string token) + { + HeartbeatToken = token; + return this; + } + + public BrokerRegistrationRequest WithNodeId(string nodeId) + { + NodeId = nodeId; + return this; + } + + internal void SignJwt(JsonWebToken jwt) + { + jwt.SignFromJwk(SigningKey); + } + + internal IReadOnlyDictionary<string, string?> JsonHeader => SigningKey!.JwtHeader; + + ///<inheritdoc/> + protected override void Free() + { + if (ownsKey) + { + SigningKey?.Dispose(); + } + } + } +} |