123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- /*
- Technitium DNS Server
- Copyright (C) 2024 Shreyas Zare (shreyas@technitium.com)
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program 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 General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- using System;
- using System.IO;
- using System.Net;
- using System.Security.Cryptography;
- using TechnitiumLibrary.IO;
- using TechnitiumLibrary.Net;
- namespace DnsServerCore.Auth
- {
- enum UserSessionType : byte
- {
- Unknown = 0,
- Standard = 1,
- ApiToken = 2
- }
- class UserSession : IComparable<UserSession>
- {
- #region variables
- readonly string _token;
- readonly UserSessionType _type;
- readonly string _tokenName;
- readonly User _user;
- DateTime _lastSeen;
- IPAddress _lastSeenRemoteAddress;
- string _lastSeenUserAgent;
- #endregion
- #region constructor
- public UserSession(UserSessionType type, string tokenName, User user, IPAddress remoteAddress, string lastSeenUserAgent)
- {
- if ((tokenName is not null) && (tokenName.Length > 255))
- throw new ArgumentOutOfRangeException(nameof(tokenName), "Token name length cannot exceed 255 characters.");
- if (remoteAddress.IsIPv4MappedToIPv6)
- remoteAddress = remoteAddress.MapToIPv4();
- Span<byte> tokenBytes = stackalloc byte[32];
- RandomNumberGenerator.Fill(tokenBytes);
- _token = Convert.ToHexString(tokenBytes).ToLowerInvariant();
- _type = type;
- _tokenName = tokenName;
- _user = user;
- _lastSeen = DateTime.UtcNow;
- _lastSeenRemoteAddress = remoteAddress;
- _lastSeenUserAgent = lastSeenUserAgent;
- if ((_lastSeenUserAgent is not null) && (_lastSeenUserAgent.Length > 255))
- _lastSeenUserAgent = _lastSeenUserAgent.Substring(0, 255);
- }
- public UserSession(BinaryReader bR, AuthManager authManager)
- {
- switch (bR.ReadByte())
- {
- case 1:
- _token = bR.ReadShortString();
- _type = (UserSessionType)bR.ReadByte();
- _tokenName = bR.ReadShortString();
- if (_tokenName.Length == 0)
- _tokenName = null;
- _user = authManager.GetUser(bR.ReadShortString());
- _lastSeen = bR.ReadDateTime();
- _lastSeenRemoteAddress = IPAddressExtensions.ReadFrom(bR);
- _lastSeenUserAgent = bR.ReadShortString();
- if (_lastSeenUserAgent.Length == 0)
- _lastSeenUserAgent = null;
- break;
- default:
- throw new InvalidDataException("Invalid data or version not supported.");
- }
- }
- #endregion
- #region public
- public void UpdateLastSeen(IPAddress remoteAddress, string lastSeenUserAgent)
- {
- if (remoteAddress.IsIPv4MappedToIPv6)
- remoteAddress = remoteAddress.MapToIPv4();
- _lastSeen = DateTime.UtcNow;
- _lastSeenRemoteAddress = remoteAddress;
- _lastSeenUserAgent = lastSeenUserAgent;
- if ((_lastSeenUserAgent is not null) && (_lastSeenUserAgent.Length > 255))
- _lastSeenUserAgent = _lastSeenUserAgent.Substring(0, 255);
- }
- public bool HasExpired()
- {
- if (_type == UserSessionType.ApiToken)
- return false;
- if (_user.SessionTimeoutSeconds == 0)
- return false;
- return _lastSeen.AddSeconds(_user.SessionTimeoutSeconds) < DateTime.UtcNow;
- }
- public void WriteTo(BinaryWriter bW)
- {
- bW.Write((byte)1);
- bW.WriteShortString(_token);
- bW.Write((byte)_type);
- if (_tokenName is null)
- bW.Write((byte)0);
- else
- bW.WriteShortString(_tokenName);
- bW.WriteShortString(_user.Username);
- bW.Write(_lastSeen);
- _lastSeenRemoteAddress.WriteTo(bW);
- if (_lastSeenUserAgent is null)
- bW.Write((byte)0);
- else
- bW.WriteShortString(_lastSeenUserAgent);
- }
- public int CompareTo(UserSession other)
- {
- return other._lastSeen.CompareTo(_lastSeen);
- }
- #endregion
- #region properties
- public string Token
- { get { return _token; } }
- public UserSessionType Type
- { get { return _type; } }
- public string TokenName
- { get { return _tokenName; } }
- public User User
- { get { return _user; } }
- public DateTime LastSeen
- { get { return _lastSeen; } }
- public IPAddress LastSeenRemoteAddress
- { get { return _lastSeenRemoteAddress; } }
- public string LastSeenUserAgent
- { get { return _lastSeenUserAgent; } }
- #endregion
- }
- }
|