1
0
mirror of https://github.com/bitwarden/server synced 2025-12-25 12:43:14 +00: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,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;
}
}
}
}