mirror of
https://github.com/bitwarden/server.git
synced 2024-11-25 12:45:18 +01:00
e2c6fc81f4
* 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>
73 lines
1.9 KiB
C#
73 lines
1.9 KiB
C#
using System;
|
|
using System.Text.Json;
|
|
using AutoFixture.Xunit2;
|
|
using Bit.Core.Utilities;
|
|
using Xunit;
|
|
|
|
namespace Bit.Core.Test.Tokens
|
|
{
|
|
public class ExpiringTokenTests
|
|
{
|
|
[Theory, AutoData]
|
|
public void ExpirationSerializesToEpochMilliseconds(DateTime expirationDate)
|
|
{
|
|
var sut = new TestExpiringTokenable
|
|
{
|
|
ExpirationDate = expirationDate
|
|
};
|
|
|
|
var result = JsonSerializer.Serialize(sut);
|
|
var expectedDate = CoreHelpers.ToEpocMilliseconds(expirationDate);
|
|
|
|
Assert.Contains($"\"ExpirationDate\":{expectedDate}", result);
|
|
}
|
|
|
|
[Theory, AutoData]
|
|
public void ExpirationSerializationRoundTrip(DateTime expirationDate)
|
|
{
|
|
var sut = new TestExpiringTokenable
|
|
{
|
|
ExpirationDate = expirationDate
|
|
};
|
|
|
|
var intermediate = JsonSerializer.Serialize(sut);
|
|
var result = JsonSerializer.Deserialize<TestExpiringTokenable>(intermediate);
|
|
|
|
Assert.Equal(sut.ExpirationDate, result.ExpirationDate, TimeSpan.FromMilliseconds(100));
|
|
}
|
|
|
|
[Fact]
|
|
public void InvalidIfPastExpiryDate()
|
|
{
|
|
var sut = new TestExpiringTokenable
|
|
{
|
|
ExpirationDate = DateTime.UtcNow.AddHours(-1)
|
|
};
|
|
|
|
Assert.False(sut.Valid);
|
|
}
|
|
|
|
[Fact]
|
|
public void ValidIfWithinExpirationAndTokenReportsValid()
|
|
{
|
|
var sut = new TestExpiringTokenable
|
|
{
|
|
ExpirationDate = DateTime.UtcNow.AddHours(1)
|
|
};
|
|
|
|
Assert.True(sut.Valid);
|
|
}
|
|
|
|
[Fact]
|
|
public void HonorsTokenIsValidAbstractMember()
|
|
{
|
|
var sut = new TestExpiringTokenable(forceInvalid: true)
|
|
{
|
|
ExpirationDate = DateTime.UtcNow.AddHours(1)
|
|
};
|
|
|
|
Assert.False(sut.Valid);
|
|
}
|
|
}
|
|
}
|