using System; using System.Diagnostics; using FrostFS.Refs; using FrostFS.SDK.Client; using FrostFS.SDK.Cryptography; using FrostFS.Session; using Google.Protobuf; namespace FrostFS.SDK; public class FrostFsSessionToken { private Guid _id; private ReadOnlyMemory _sessionKey; private readonly SessionToken.Types.Body _body; private FrostFsSessionToken() { ProtoId = ByteString.Empty; ProtoSessionKey = ByteString.Empty; _body = new SessionToken.Types.Body(); } internal FrostFsSessionToken(SessionToken token) { ProtoId = token.Body.Id; ProtoSessionKey = token.Body.SessionKey; _body = token.Body; } public Guid Id { get { if (_id == Guid.Empty) { _id = ProtoId.ToUuid(); } return _id; } } public ReadOnlyMemory SessionKey { get { if (_sessionKey.IsEmpty) { _sessionKey = ProtoSessionKey.Memory; } return _sessionKey; } } internal ByteString ProtoId { get; } internal ByteString ProtoSessionKey { get; } public SessionToken CreateContainerToken(ContainerID? containerId, ContainerSessionContext.Types.Verb verb, ClientKey key) { if (key is null) { throw new ArgumentNullException(nameof(key)); } SessionToken sessionToken = new() { Body = _body.Clone() }; sessionToken.Body.Container = new() { Verb = verb }; if (containerId != null) { sessionToken.Body.Container.ContainerId = containerId; } else { sessionToken.Body.Container.Wildcard = true; } sessionToken.Body.SessionKey = key.PublicKeyProto; sessionToken.Signature = key.ECDsaKey.SignMessagePart(sessionToken.Body); return sessionToken; } public SessionToken CreateObjectTokenContext(Address address, ObjectSessionContext.Types.Verb verb, ClientKey key) { if (address is null) { throw new ArgumentNullException(nameof(address)); } if (key is null) { throw new ArgumentNullException(nameof(key)); } SessionToken sessionToken = new() { Body = _body.Clone() }; ObjectSessionContext.Types.Target target = new() { Container = address.ContainerId }; if (address.ObjectId != null) { target.Objects.Add(address.ObjectId); } sessionToken.Body.Object = new() { Target = target, Verb = verb }; sessionToken.Signature = key.ECDsaKey.SignMessagePart(sessionToken.Body); return sessionToken; } }