mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 08:02:49 -05:00
Feature/token service (#1785)
* Implement draft token service * Add tokenizer and factory * Handle expiring tokens through base class * Allow direct token validity checks * Add safe unprotect to tokenizer * Add interface to tokenizer factory * Use tokenizer * Fix rebase * Handle cleartext prefix in tokenizer base * Use epoch milliseconds for expiration in tokens * Use tokenizers * Test tokens * Linter fixes * Add TokenizerFactory to DI services * Test epoch milliseconds deserialization * Use separate injectables for each token type * Fix directory * Add functional unprotect to token * Fix namespace and correct object names * Remove Tokenable interface * Test remaining Tokens classes * Dotnet format * Fix sut provider errors with update * Remove useless property Co-authored-by: Hinton <oscar@oscarhinton.com>
This commit is contained in:
37
src/Core/Tokens/Token.cs
Normal file
37
src/Core/Tokens/Token.cs
Normal file
@ -0,0 +1,37 @@
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
|
||||
namespace Bit.Core.Tokens
|
||||
{
|
||||
public class 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user