< Summary

Information
Class: LOCKnet.Core.Security.SessionManager
Assembly: LOCKnet.Core
File(s): /home/runner/work/LOCKnet/LOCKnet/src/LOCKnet.Core/Security/SessionManager.cs
Line coverage
100%
Covered lines: 33
Uncovered lines: 0
Coverable lines: 33
Total lines: 68
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
get_IsUnlocked()100%11100%
GetSessionKey()100%22100%
Open(...)100%44100%
Lock()100%44100%

File(s)

/home/runner/work/LOCKnet/LOCKnet/src/LOCKnet.Core/Security/SessionManager.cs

#LineLine coverage
 1using System.Security.Cryptography;
 2
 3namespace LOCKnet.Core.Security;
 4
 5/// <summary>
 6/// Implementierung von <see cref="ISessionManager"/>.
 7/// Thread-safe: alle Zugriffe auf den Session-Key sind durch ein Lock geschützt.
 8/// </summary>
 9public sealed class SessionManager : ISessionManager
 10{
 11311  private readonly object _lock = new();
 12  private byte[]? _sessionKey;
 13
 14  /// <inheritdoc/>
 15  public bool IsUnlocked
 16  {
 17    get
 1518    {
 1519      lock (_lock)
 1520        return _sessionKey is not null;
 1521    }
 22  }
 23
 24  /// <inheritdoc/>
 25  public event EventHandler? Locked;
 26
 27  /// <inheritdoc/>
 28  public byte[]? GetSessionKey()
 7829  {
 7830    lock (_lock)
 7831      return _sessionKey?.ToArray();
 7832  }
 33
 34  /// <inheritdoc/>
 35  public void Open(byte[] sessionKey)
 5436  {
 5437    ArgumentNullException.ThrowIfNull(sessionKey);
 5338    if (sessionKey.Length != 32)
 139      throw new ArgumentException("Session-Key muss genau 32 Bytes lang sein.", nameof(sessionKey));
 40
 5241    var copy = sessionKey.ToArray();
 5242    CryptographicOperations.ZeroMemory(sessionKey);
 43
 5244    lock (_lock)
 5245    {
 46      // Alten Key sicher überschreiben falls vorhanden
 5247      if (_sessionKey is not null)
 248        CryptographicOperations.ZeroMemory(_sessionKey);
 49
 5250      _sessionKey = copy;
 5251    }
 5252  }
 53
 54  /// <inheritdoc/>
 55  public void Lock()
 4156  {
 4157    lock (_lock)
 4158    {
 4159      if (_sessionKey is not null)
 2360      {
 2361        CryptographicOperations.ZeroMemory(_sessionKey);
 2362        _sessionKey = null;
 2363      }
 4164    }
 65
 4166    Locked?.Invoke(this, EventArgs.Empty);
 4167  }
 68}