[#28] Client: Apply code optimizations
Signed-off-by: Pavel Gross <p.gross@yadro.com>
This commit is contained in:
parent
766f61a5f7
commit
749000a090
57 changed files with 845 additions and 1116 deletions
|
@ -1,9 +1,117 @@
|
|||
using System;
|
||||
|
||||
using FrostFS.Refs;
|
||||
|
||||
using FrostFS.SDK.Client;
|
||||
using FrostFS.SDK.Cryptography;
|
||||
using FrostFS.Session;
|
||||
|
||||
using Google.Protobuf;
|
||||
|
||||
namespace FrostFS.SDK;
|
||||
|
||||
public class FrostFsSessionToken(byte[] token, Guid id)
|
||||
public class FrostFsSessionToken
|
||||
{
|
||||
public Guid Id { get; private set; } = id;
|
||||
public byte[] Token { get; private set; } = token;
|
||||
private Guid _id;
|
||||
private ReadOnlyMemory<byte> _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<byte> 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.Body.SessionKey = key.PublicKeyProto;
|
||||
|
||||
sessionToken.Signature = key.ECDsaKey.SignMessagePart(sessionToken.Body);
|
||||
|
||||
return sessionToken;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue