mirror of
https://github.com/bitwarden/server.git
synced 2025-07-02 16:42:50 -05:00
Run formatting (#2230)
This commit is contained in:
@ -1,13 +1,12 @@
|
||||
namespace Bit.Core.Tokens
|
||||
{
|
||||
public class BadTokenException : Exception
|
||||
{
|
||||
public BadTokenException()
|
||||
{
|
||||
}
|
||||
namespace Bit.Core.Tokens;
|
||||
|
||||
public BadTokenException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
public class BadTokenException : Exception
|
||||
{
|
||||
public BadTokenException()
|
||||
{
|
||||
}
|
||||
|
||||
public BadTokenException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
@ -1,55 +1,54 @@
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
|
||||
namespace Bit.Core.Tokens
|
||||
namespace Bit.Core.Tokens;
|
||||
|
||||
public class DataProtectorTokenFactory<T> : IDataProtectorTokenFactory<T> where T : Tokenable
|
||||
{
|
||||
public class DataProtectorTokenFactory<T> : IDataProtectorTokenFactory<T> where T : Tokenable
|
||||
private readonly IDataProtector _dataProtector;
|
||||
private readonly string _clearTextPrefix;
|
||||
|
||||
public DataProtectorTokenFactory(string clearTextPrefix, string purpose, IDataProtectionProvider dataProtectionProvider)
|
||||
{
|
||||
private readonly IDataProtector _dataProtector;
|
||||
private readonly string _clearTextPrefix;
|
||||
_dataProtector = dataProtectionProvider.CreateProtector(purpose);
|
||||
_clearTextPrefix = clearTextPrefix;
|
||||
}
|
||||
|
||||
public DataProtectorTokenFactory(string clearTextPrefix, string purpose, IDataProtectionProvider dataProtectionProvider)
|
||||
public string Protect(T data) =>
|
||||
data.ToToken().ProtectWith(_dataProtector).WithPrefix(_clearTextPrefix).ToString();
|
||||
|
||||
/// <summary>
|
||||
/// Unprotect token
|
||||
/// </summary>
|
||||
/// <param name="token">The token to parse</param>
|
||||
/// <typeparam name="T">The tokenable type to parse to</typeparam>
|
||||
/// <returns>The parsed tokenable</returns>
|
||||
/// <exception>Throws CryptographicException if fails to unprotect</exception>
|
||||
public T Unprotect(string token) =>
|
||||
Tokenable.FromToken<T>(new Token(token).RemovePrefix(_clearTextPrefix).UnprotectWith(_dataProtector).ToString());
|
||||
|
||||
public bool TokenValid(string token)
|
||||
{
|
||||
try
|
||||
{
|
||||
_dataProtector = dataProtectionProvider.CreateProtector(purpose);
|
||||
_clearTextPrefix = clearTextPrefix;
|
||||
return Unprotect(token).Valid;
|
||||
}
|
||||
|
||||
public string Protect(T data) =>
|
||||
data.ToToken().ProtectWith(_dataProtector).WithPrefix(_clearTextPrefix).ToString();
|
||||
|
||||
/// <summary>
|
||||
/// Unprotect token
|
||||
/// </summary>
|
||||
/// <param name="token">The token to parse</param>
|
||||
/// <typeparam name="T">The tokenable type to parse to</typeparam>
|
||||
/// <returns>The parsed tokenable</returns>
|
||||
/// <exception>Throws CryptographicException if fails to unprotect</exception>
|
||||
public T Unprotect(string token) =>
|
||||
Tokenable.FromToken<T>(new Token(token).RemovePrefix(_clearTextPrefix).UnprotectWith(_dataProtector).ToString());
|
||||
|
||||
public bool TokenValid(string token)
|
||||
catch
|
||||
{
|
||||
try
|
||||
{
|
||||
return Unprotect(token).Valid;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryUnprotect(string token, out T data)
|
||||
public bool TryUnprotect(string token, out T data)
|
||||
{
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
data = Unprotect(token);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
data = default;
|
||||
return false;
|
||||
}
|
||||
data = Unprotect(token);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
data = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,13 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.Core.Tokens
|
||||
{
|
||||
public abstract class ExpiringTokenable : Tokenable
|
||||
{
|
||||
[JsonConverter(typeof(EpochDateTimeJsonConverter))]
|
||||
public DateTime ExpirationDate { get; set; }
|
||||
public override bool Valid => ExpirationDate > DateTime.UtcNow && TokenIsValid();
|
||||
namespace Bit.Core.Tokens;
|
||||
|
||||
protected abstract bool TokenIsValid();
|
||||
}
|
||||
public abstract class ExpiringTokenable : Tokenable
|
||||
{
|
||||
[JsonConverter(typeof(EpochDateTimeJsonConverter))]
|
||||
public DateTime ExpirationDate { get; set; }
|
||||
public override bool Valid => ExpirationDate > DateTime.UtcNow && TokenIsValid();
|
||||
|
||||
protected abstract bool TokenIsValid();
|
||||
}
|
||||
|
@ -1,8 +1,7 @@
|
||||
namespace Bit.Core.Tokens
|
||||
namespace Bit.Core.Tokens;
|
||||
|
||||
public interface IBillingSyncTokenable
|
||||
{
|
||||
public interface IBillingSyncTokenable
|
||||
{
|
||||
public Guid OrganizationId { get; set; }
|
||||
public string BillingSyncKey { get; set; }
|
||||
}
|
||||
public Guid OrganizationId { get; set; }
|
||||
public string BillingSyncKey { get; set; }
|
||||
}
|
||||
|
@ -1,10 +1,9 @@
|
||||
namespace Bit.Core.Tokens
|
||||
namespace Bit.Core.Tokens;
|
||||
|
||||
public interface IDataProtectorTokenFactory<T> where T : Tokenable
|
||||
{
|
||||
public interface IDataProtectorTokenFactory<T> where T : Tokenable
|
||||
{
|
||||
string Protect(T data);
|
||||
T Unprotect(string token);
|
||||
bool TryUnprotect(string token, out T data);
|
||||
bool TokenValid(string token);
|
||||
}
|
||||
string Protect(T data);
|
||||
T Unprotect(string token);
|
||||
bool TryUnprotect(string token, out T data);
|
||||
bool TokenValid(string token);
|
||||
}
|
||||
|
@ -1,37 +1,36 @@
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
|
||||
namespace Bit.Core.Tokens
|
||||
namespace Bit.Core.Tokens;
|
||||
|
||||
public class Token
|
||||
{
|
||||
public class Token
|
||||
private readonly string _token;
|
||||
|
||||
public Token(string token)
|
||||
{
|
||||
private readonly string _token;
|
||||
|
||||
public Token(string token)
|
||||
{
|
||||
_token = token;
|
||||
}
|
||||
|
||||
public Token WithPrefix(string prefix)
|
||||
{
|
||||
return new Token($"{prefix}{_token}");
|
||||
}
|
||||
|
||||
public Token RemovePrefix(string expectedPrefix)
|
||||
{
|
||||
if (!_token.StartsWith(expectedPrefix))
|
||||
{
|
||||
throw new BadTokenException($"Expected prefix, {expectedPrefix}, was not present.");
|
||||
}
|
||||
|
||||
return new Token(_token[expectedPrefix.Length..]);
|
||||
}
|
||||
|
||||
public Token ProtectWith(IDataProtector dataProtector) =>
|
||||
new(dataProtector.Protect(ToString()));
|
||||
|
||||
public Token UnprotectWith(IDataProtector dataProtector) =>
|
||||
new(dataProtector.Unprotect(ToString()));
|
||||
|
||||
public override string ToString() => _token;
|
||||
_token = token;
|
||||
}
|
||||
|
||||
public Token WithPrefix(string prefix)
|
||||
{
|
||||
return new Token($"{prefix}{_token}");
|
||||
}
|
||||
|
||||
public Token RemovePrefix(string expectedPrefix)
|
||||
{
|
||||
if (!_token.StartsWith(expectedPrefix))
|
||||
{
|
||||
throw new BadTokenException($"Expected prefix, {expectedPrefix}, was not present.");
|
||||
}
|
||||
|
||||
return new Token(_token[expectedPrefix.Length..]);
|
||||
}
|
||||
|
||||
public Token ProtectWith(IDataProtector dataProtector) =>
|
||||
new(dataProtector.Protect(ToString()));
|
||||
|
||||
public Token UnprotectWith(IDataProtector dataProtector) =>
|
||||
new(dataProtector.Unprotect(ToString()));
|
||||
|
||||
public override string ToString() => _token;
|
||||
}
|
||||
|
@ -1,20 +1,19 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Bit.Core.Tokens
|
||||
namespace Bit.Core.Tokens;
|
||||
|
||||
public abstract class Tokenable
|
||||
{
|
||||
public abstract class Tokenable
|
||||
public abstract bool Valid { get; }
|
||||
|
||||
public Token ToToken()
|
||||
{
|
||||
public abstract bool Valid { get; }
|
||||
return new Token(JsonSerializer.Serialize(this, this.GetType()));
|
||||
}
|
||||
|
||||
public Token ToToken()
|
||||
{
|
||||
return new Token(JsonSerializer.Serialize(this, this.GetType()));
|
||||
}
|
||||
|
||||
public static T FromToken<T>(string token) => FromToken<T>(new Token(token));
|
||||
public static T FromToken<T>(Token token)
|
||||
{
|
||||
return JsonSerializer.Deserialize<T>(token.ToString());
|
||||
}
|
||||
public static T FromToken<T>(string token) => FromToken<T>(new Token(token));
|
||||
public static T FromToken<T>(Token token)
|
||||
{
|
||||
return JsonSerializer.Deserialize<T>(token.ToString());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user