2022-01-10 16:58:16 +01:00
|
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Tokens;
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2022-01-10 16:58:16 +01:00
|
|
|
|
public class Token
|
|
|
|
|
{
|
|
|
|
|
private readonly string _token;
|
2022-08-29 20:53:16 +02:00
|
|
|
|
|
2022-01-10 16:58:16 +01:00
|
|
|
|
public Token(string token)
|
|
|
|
|
{
|
|
|
|
|
_token = token;
|
2022-08-29 20:53:16 +02:00
|
|
|
|
}
|
2022-01-10 16:58:16 +01:00
|
|
|
|
|
|
|
|
|
public Token WithPrefix(string prefix)
|
|
|
|
|
{
|
|
|
|
|
return new Token($"{prefix}{_token}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Token RemovePrefix(string expectedPrefix)
|
2022-08-29 20:53:16 +02:00
|
|
|
|
{
|
2022-01-10 16:58:16 +01:00
|
|
|
|
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;
|
|
|
|
|
}
|