1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-02 16:42:50 -05:00

Revert filescoped (#2227)

* Revert "Add git blame entry (#2226)"

This reverts commit 239286737d.

* Revert "Turn on file scoped namespaces (#2225)"

This reverts commit 34fb4cca2a.
This commit is contained in:
Justin Baur
2022-08-29 15:53:48 -04:00
committed by GitHub
parent 239286737d
commit bae03feffe
1208 changed files with 74317 additions and 73126 deletions

View File

@ -1,12 +1,13 @@
namespace Bit.Core.Tokens;
public class BadTokenException : Exception
namespace Bit.Core.Tokens
{
public BadTokenException()
public class BadTokenException : Exception
{
}
public BadTokenException()
{
}
public BadTokenException(string message) : base(message)
{
public BadTokenException(string message) : base(message)
{
}
}
}

View File

@ -1,54 +1,55 @@
using Microsoft.AspNetCore.DataProtection;
namespace Bit.Core.Tokens;
public class DataProtectorTokenFactory<T> : IDataProtectorTokenFactory<T> where T : Tokenable
namespace Bit.Core.Tokens
{
private readonly IDataProtector _dataProtector;
private readonly string _clearTextPrefix;
public DataProtectorTokenFactory(string clearTextPrefix, string purpose, IDataProtectionProvider dataProtectionProvider)
public class DataProtectorTokenFactory<T> : IDataProtectorTokenFactory<T> where T : Tokenable
{
_dataProtector = dataProtectionProvider.CreateProtector(purpose);
_clearTextPrefix = clearTextPrefix;
}
private readonly IDataProtector _dataProtector;
private readonly string _clearTextPrefix;
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
public DataProtectorTokenFactory(string clearTextPrefix, string purpose, IDataProtectionProvider dataProtectionProvider)
{
return Unprotect(token).Valid;
_dataProtector = dataProtectionProvider.CreateProtector(purpose);
_clearTextPrefix = clearTextPrefix;
}
catch
{
return false;
}
}
public bool TryUnprotect(string token, out T data)
{
try
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)
{
data = Unprotect(token);
return true;
try
{
return Unprotect(token).Valid;
}
catch
{
return false;
}
}
catch
public bool TryUnprotect(string token, out T data)
{
data = default;
return false;
try
{
data = Unprotect(token);
return true;
}
catch
{
data = default;
return false;
}
}
}
}

View File

@ -1,13 +1,14 @@
using System.Text.Json.Serialization;
using Bit.Core.Utilities;
namespace Bit.Core.Tokens;
public abstract class ExpiringTokenable : Tokenable
namespace Bit.Core.Tokens
{
[JsonConverter(typeof(EpochDateTimeJsonConverter))]
public DateTime ExpirationDate { get; set; }
public override bool Valid => ExpirationDate > DateTime.UtcNow && 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();
protected abstract bool TokenIsValid();
}
}

View File

@ -1,7 +1,8 @@
namespace Bit.Core.Tokens;
public interface IBillingSyncTokenable
namespace Bit.Core.Tokens
{
public Guid OrganizationId { get; set; }
public string BillingSyncKey { get; set; }
public interface IBillingSyncTokenable
{
public Guid OrganizationId { get; set; }
public string BillingSyncKey { get; set; }
}
}

View File

@ -1,9 +1,10 @@
namespace Bit.Core.Tokens;
public interface IDataProtectorTokenFactory<T> where T : Tokenable
namespace Bit.Core.Tokens
{
string Protect(T data);
T Unprotect(string token);
bool TryUnprotect(string token, out T data);
bool TokenValid(string token);
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);
}
}

View File

@ -1,36 +1,37 @@
using Microsoft.AspNetCore.DataProtection;
namespace Bit.Core.Tokens;
public class Token
namespace Bit.Core.Tokens
{
private readonly string _token;
public Token(string token)
public class Token
{
_token = token;
}
private readonly string _token;
public Token WithPrefix(string prefix)
{
return new Token($"{prefix}{_token}");
}
public Token RemovePrefix(string expectedPrefix)
{
if (!_token.StartsWith(expectedPrefix))
public Token(string token)
{
throw new BadTokenException($"Expected prefix, {expectedPrefix}, was not present.");
_token = token;
}
return new Token(_token[expectedPrefix.Length..]);
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;
}
public Token ProtectWith(IDataProtector dataProtector) =>
new(dataProtector.Protect(ToString()));
public Token UnprotectWith(IDataProtector dataProtector) =>
new(dataProtector.Unprotect(ToString()));
public override string ToString() => _token;
}

View File

@ -1,19 +1,20 @@
using System.Text.Json;
namespace Bit.Core.Tokens;
public abstract class Tokenable
namespace Bit.Core.Tokens
{
public abstract bool Valid { get; }
public Token ToToken()
public abstract class Tokenable
{
return new Token(JsonSerializer.Serialize(this, this.GetType()));
}
public abstract bool Valid { get; }
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 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());
}
}
}