UserSession.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2024 Shreyas Zare (shreyas@technitium.com)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. using System;
  16. using System.IO;
  17. using System.Net;
  18. using System.Security.Cryptography;
  19. using TechnitiumLibrary.IO;
  20. using TechnitiumLibrary.Net;
  21. namespace DnsServerCore.Auth
  22. {
  23. enum UserSessionType : byte
  24. {
  25. Unknown = 0,
  26. Standard = 1,
  27. ApiToken = 2
  28. }
  29. class UserSession : IComparable<UserSession>
  30. {
  31. #region variables
  32. readonly string _token;
  33. readonly UserSessionType _type;
  34. readonly string _tokenName;
  35. readonly User _user;
  36. DateTime _lastSeen;
  37. IPAddress _lastSeenRemoteAddress;
  38. string _lastSeenUserAgent;
  39. #endregion
  40. #region constructor
  41. public UserSession(UserSessionType type, string tokenName, User user, IPAddress remoteAddress, string lastSeenUserAgent)
  42. {
  43. if ((tokenName is not null) && (tokenName.Length > 255))
  44. throw new ArgumentOutOfRangeException(nameof(tokenName), "Token name length cannot exceed 255 characters.");
  45. if (remoteAddress.IsIPv4MappedToIPv6)
  46. remoteAddress = remoteAddress.MapToIPv4();
  47. Span<byte> tokenBytes = stackalloc byte[32];
  48. RandomNumberGenerator.Fill(tokenBytes);
  49. _token = Convert.ToHexString(tokenBytes).ToLowerInvariant();
  50. _type = type;
  51. _tokenName = tokenName;
  52. _user = user;
  53. _lastSeen = DateTime.UtcNow;
  54. _lastSeenRemoteAddress = remoteAddress;
  55. _lastSeenUserAgent = lastSeenUserAgent;
  56. if ((_lastSeenUserAgent is not null) && (_lastSeenUserAgent.Length > 255))
  57. _lastSeenUserAgent = _lastSeenUserAgent.Substring(0, 255);
  58. }
  59. public UserSession(BinaryReader bR, AuthManager authManager)
  60. {
  61. switch (bR.ReadByte())
  62. {
  63. case 1:
  64. _token = bR.ReadShortString();
  65. _type = (UserSessionType)bR.ReadByte();
  66. _tokenName = bR.ReadShortString();
  67. if (_tokenName.Length == 0)
  68. _tokenName = null;
  69. _user = authManager.GetUser(bR.ReadShortString());
  70. _lastSeen = bR.ReadDateTime();
  71. _lastSeenRemoteAddress = IPAddressExtensions.ReadFrom(bR);
  72. _lastSeenUserAgent = bR.ReadShortString();
  73. if (_lastSeenUserAgent.Length == 0)
  74. _lastSeenUserAgent = null;
  75. break;
  76. default:
  77. throw new InvalidDataException("Invalid data or version not supported.");
  78. }
  79. }
  80. #endregion
  81. #region public
  82. public void UpdateLastSeen(IPAddress remoteAddress, string lastSeenUserAgent)
  83. {
  84. if (remoteAddress.IsIPv4MappedToIPv6)
  85. remoteAddress = remoteAddress.MapToIPv4();
  86. _lastSeen = DateTime.UtcNow;
  87. _lastSeenRemoteAddress = remoteAddress;
  88. _lastSeenUserAgent = lastSeenUserAgent;
  89. if ((_lastSeenUserAgent is not null) && (_lastSeenUserAgent.Length > 255))
  90. _lastSeenUserAgent = _lastSeenUserAgent.Substring(0, 255);
  91. }
  92. public bool HasExpired()
  93. {
  94. if (_type == UserSessionType.ApiToken)
  95. return false;
  96. if (_user.SessionTimeoutSeconds == 0)
  97. return false;
  98. return _lastSeen.AddSeconds(_user.SessionTimeoutSeconds) < DateTime.UtcNow;
  99. }
  100. public void WriteTo(BinaryWriter bW)
  101. {
  102. bW.Write((byte)1);
  103. bW.WriteShortString(_token);
  104. bW.Write((byte)_type);
  105. if (_tokenName is null)
  106. bW.Write((byte)0);
  107. else
  108. bW.WriteShortString(_tokenName);
  109. bW.WriteShortString(_user.Username);
  110. bW.Write(_lastSeen);
  111. _lastSeenRemoteAddress.WriteTo(bW);
  112. if (_lastSeenUserAgent is null)
  113. bW.Write((byte)0);
  114. else
  115. bW.WriteShortString(_lastSeenUserAgent);
  116. }
  117. public int CompareTo(UserSession other)
  118. {
  119. return other._lastSeen.CompareTo(_lastSeen);
  120. }
  121. #endregion
  122. #region properties
  123. public string Token
  124. { get { return _token; } }
  125. public UserSessionType Type
  126. { get { return _type; } }
  127. public string TokenName
  128. { get { return _tokenName; } }
  129. public User User
  130. { get { return _user; } }
  131. public DateTime LastSeen
  132. { get { return _lastSeen; } }
  133. public IPAddress LastSeenRemoteAddress
  134. { get { return _lastSeenRemoteAddress; } }
  135. public string LastSeenUserAgent
  136. { get { return _lastSeenUserAgent; } }
  137. #endregion
  138. }
  139. }