1
0
mirror of https://github.com/bitwarden/server synced 2025-12-31 15:43:16 +00:00

Turn on file scoped namespaces (#2225)

This commit is contained in:
Justin Baur
2022-08-29 14:53:16 -04:00
committed by GitHub
parent 7c4521e0b4
commit 34fb4cca2a
1206 changed files with 73816 additions and 75022 deletions

View File

@@ -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;
}
}
}