Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
LiamMorrow committed Jul 24, 2024
1 parent 687dbc5 commit 5583173
Show file tree
Hide file tree
Showing 12 changed files with 345 additions and 321 deletions.
20 changes: 9 additions & 11 deletions LiftLog.Api/Controllers/EventsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ [FromServices] IValidator<GetEventsRequest> validator
return BadRequest(validationResult.Errors);
}

var validFollowSecrets = await db
.UserFollowSecrets.Where(x =>
request.Users.Select(x => x.FollowSecret).Contains(x.Value)
)
var validFollowSecrets = await db.UserFollowSecrets.Where(x =>
request.Users.Select(x => x.FollowSecret).Contains(x.Value)
)
.ToArrayAsync();
var invalidFollowSecrets = request
.Users.Select(x => x.FollowSecret)
Expand All @@ -40,13 +39,12 @@ [FromServices] IValidator<GetEventsRequest> validator
})
.ToArray();

var events = await db
.UserEvents.Join(
db.UserEventFilterStubDbSet.CreateResultSetFromData(userIdsAndSince),
x => x.UserId,
x => x.UserId,
(Event, Request) => new { Event, Request }
)
var events = await db.UserEvents.Join(
db.UserEventFilterStubDbSet.CreateResultSetFromData(userIdsAndSince),
x => x.UserId,
x => x.UserId,
(Event, Request) => new { Event, Request }
)
.Where(x => x.Event.Expiry > DateTimeOffset.UtcNow)
.Where(x => x.Event.Timestamp > x.Request.Since)
.Select(x => x.Event)
Expand Down
7 changes: 3 additions & 4 deletions LiftLog.Api/Controllers/FollowSecretController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,9 @@ [FromServices] IValidator<DeleteUserFollowSecretRequest> validator
return Unauthorized();
}

var userFollowSecret = await db
.UserFollowSecrets.Where(x =>
x.UserId == request.UserId && x.Value == request.FollowSecret
)
var userFollowSecret = await db.UserFollowSecrets.Where(x =>
x.UserId == request.UserId && x.Value == request.FollowSecret
)
.ToListAsync();
db.UserFollowSecrets.RemoveRange(userFollowSecret);
await db.SaveChangesAsync();
Expand Down
3 changes: 1 addition & 2 deletions LiftLog.Api/Controllers/InboxController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ [FromServices] IValidator<GetInboxMessagesRequest> validator
return Unauthorized();
}

var inboxItems = await db
.UserInboxItems.Where(x => x.UserId == request.UserId)
var inboxItems = await db.UserInboxItems.Where(x => x.UserId == request.UserId)
.ToArrayAsync();
db.UserInboxItems.RemoveRange(inboxItems);
await db.SaveChangesAsync();
Expand Down
15 changes: 7 additions & 8 deletions LiftLog.Api/Controllers/SharedItemController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,13 @@ [FromServices] IValidator<CreateSharedItemRequest> validator
[HttpGet]
public async Task<IActionResult> GetSharedItem(string id)
{
var sharedItem = await db
.SharedItems.Select(x => new
{
x.Id,
x.User.RsaPublicKey,
x.EncryptedPayload,
x.EncryptionIV
})
var sharedItem = await db.SharedItems.Select(x => new
{
x.Id,
x.User.RsaPublicKey,
x.EncryptedPayload,
x.EncryptionIV
})
.FirstOrDefaultAsync(x => x.Id == id);
if (sharedItem == null)
{
Expand Down
3 changes: 1 addition & 2 deletions LiftLog.Api/Service/CleanupExpiredDataHostedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
using var scope = services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<UserDataContext>();
var now = DateTimeOffset.UtcNow;
var expiredEvents = await db
.UserEvents.Where(e => e.Expiry < now)
var expiredEvents = await db.UserEvents.Where(e => e.Expiry < now)
.ToListAsync(cancellationToken: stoppingToken);
db.UserEvents.RemoveRange(expiredEvents);
await db.SaveChangesAsync(stoppingToken);
Expand Down
24 changes: 12 additions & 12 deletions LiftLog.Api/Service/GptAiWorkoutPlanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,18 @@ Please make me a workout plan.
.Sessions.Select(s => new SessionBlueprint(
s.Name,
s.Exercises.Select(e => new ExerciseBlueprint(
e.Name,
e.Sets,
e.RepsPerSet,
e.WeightIncreaseOnSuccess,
new Rest(
TimeSpan.FromSeconds(e.RestBetweenSets.MinRestSeconds),
TimeSpan.FromSeconds(e.RestBetweenSets.MaxRestSeconds),
TimeSpan.FromSeconds(e.RestBetweenSets.FailureRestSeconds)
),
false,
Notes: ""
))
e.Name,
e.Sets,
e.RepsPerSet,
e.WeightIncreaseOnSuccess,
new Rest(
TimeSpan.FromSeconds(e.RestBetweenSets.MinRestSeconds),
TimeSpan.FromSeconds(e.RestBetweenSets.MaxRestSeconds),
TimeSpan.FromSeconds(e.RestBetweenSets.FailureRestSeconds)
),
false,
Notes: ""
))
.ToImmutableList(),
Notes: s.Description
))
Expand Down
4 changes: 3 additions & 1 deletion LiftLog.Maui/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ private static void HandleLink(string? link)
MainThread.BeginInvokeOnMainThread(() =>
{
Application
.Current?.Handler.MauiContext?.Services.GetRequiredService<Fluxor.IDispatcher>()
.Current?.Handler
.MauiContext?.Services
.GetRequiredService<Fluxor.IDispatcher>()
.Dispatch(new NavigateAction(path + query));
});
}
Expand Down
5 changes: 3 additions & 2 deletions LiftLog.Maui/Platforms/Android/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ protected override void OnCreate(Bundle? savedInstanceState)
base.OnCreate(savedInstanceState);
WindowCompat.SetDecorFitsSystemWindows(Window!, false);
WebViewSoftInputPatch.Initialize();
var insetsManager =
IPlatformApplication.Current?.Services.GetRequiredService<InsetsManager>();
var insetsManager = IPlatformApplication
.Current?.Services
.GetRequiredService<InsetsManager>();
if (insetsManager is not null)
{
insetsManager.SystemSafeInsetBottom =
Expand Down
5 changes: 2 additions & 3 deletions LiftLog.Ui/Models/FeedStateDao.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ value is null
: new Lib.Services.AesKey(value.AesKey.ToByteArray()),
PublicKey: new Lib.Services.RsaPublicKey(value.PublicKey.ToByteArray()),
CurrentPlan: value
.CurrentPlan?.Sessions.Select(sessionBlueprintDao =>
sessionBlueprintDao.ToModel()
)
.CurrentPlan?.Sessions
.Select(sessionBlueprintDao => sessionBlueprintDao.ToModel())
.ToImmutableList() ?? [],
ProfilePicture: value.ProfilePicture.IsEmpty
? null
Expand Down
223 changes: 114 additions & 109 deletions tests/LiftLog.Tests.App/Encryption/OsEncryptionServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,125 +6,130 @@ namespace LiftLog.Tests.Encryption;

public class OsEncryptionServiceTests
{
[Detest]
public static void Spec() =>
Describe("OsEncryptionService")
.As(() =>
{
OsEncryptionService sut = new();
[Detest]
public static void Spec() =>
Describe("OsEncryptionService")
.As(() =>
{
OsEncryptionService sut = new();

Describe("GenerateKeyPair")
.As(() =>
{
It("will generate a key")
.When(async () =>
{
var key = await sut.GenerateAesKeyAsync();
key.Should().NotBeNull();
});
});
Describe("GenerateKeyPair")
.As(() =>
{
It("will generate a key")
.When(async () =>
{
var key = await sut.GenerateAesKeyAsync();
key.Should().NotBeNull();
});
});

Describe("Encrypting then Decrypting")
.As(() =>
{
It("Can encrypt and decrypt a payload")
.When(async () =>
{
var key = await sut.GenerateAesKeyAsync();
var rsaKeyPair = await sut.GenerateRsaKeysAsync();
var data = Encoding.UTF8.GetBytes("Hello, world!");
Describe("Encrypting then Decrypting")
.As(() =>
{
It("Can encrypt and decrypt a payload")
.When(async () =>
{
var key = await sut.GenerateAesKeyAsync();
var rsaKeyPair = await sut.GenerateRsaKeysAsync();
var data = Encoding.UTF8.GetBytes("Hello, world!");

var encryptedData = await sut.SignRsa256PssAndEncryptAesCbcAsync(
data,
key,
rsaKeyPair.PrivateKey
);
var decryptedData = await sut.DecryptAesCbcAndVerifyRsa256PssAsync(
encryptedData,
key,
rsaKeyPair.PublicKey
);
var encryptedData = await sut.SignRsa256PssAndEncryptAesCbcAsync(
data,
key,
rsaKeyPair.PrivateKey
);
var decryptedData = await sut.DecryptAesCbcAndVerifyRsa256PssAsync(
encryptedData,
key,
rsaKeyPair.PublicKey
);

decryptedData.Should().BeEquivalentTo(data);
});
decryptedData.Should().BeEquivalentTo(data);
});

Describe("using a supplied IV")
.As(() =>
{
It("Can encrypt and decrypt a payload")
.When(async () =>
{
// Arrange
var key = await sut.GenerateAesKeyAsync();
var rsaKeyPair = await sut.GenerateRsaKeysAsync();
var data1 = Encoding.UTF8.GetBytes("Hello, world!");
var data2 = Encoding.UTF8.GetBytes("Goodbye, world!");
Describe("using a supplied IV")
.As(() =>
{
It("Can encrypt and decrypt a payload")
.When(async () =>
{
// Arrange
var key = await sut.GenerateAesKeyAsync();
var rsaKeyPair = await sut.GenerateRsaKeysAsync();
var data1 = Encoding.UTF8.GetBytes("Hello, world!");
var data2 = Encoding.UTF8.GetBytes("Goodbye, world!");

// Act
var encryptedData1 = await sut.SignRsa256PssAndEncryptAesCbcAsync(
data1,
key,
rsaKeyPair.PrivateKey
);
var decryptedData1 = await sut.DecryptAesCbcAndVerifyRsa256PssAsync(
encryptedData1,
key,
rsaKeyPair.PublicKey
);
// Act
var encryptedData1 =
await sut.SignRsa256PssAndEncryptAesCbcAsync(
data1,
key,
rsaKeyPair.PrivateKey
);
var decryptedData1 =
await sut.DecryptAesCbcAndVerifyRsa256PssAsync(
encryptedData1,
key,
rsaKeyPair.PublicKey
);

var encryptedData2 = await sut.SignRsa256PssAndEncryptAesCbcAsync(
data2,
key,
rsaKeyPair.PrivateKey,
encryptedData1.IV
);
var decryptedData2 = await sut.DecryptAesCbcAndVerifyRsa256PssAsync(
new AesEncryptedAndRsaSignedData(
encryptedData2.EncryptedPayload,
encryptedData1.IV
),
key,
rsaKeyPair.PublicKey
);
var encryptedData2 =
await sut.SignRsa256PssAndEncryptAesCbcAsync(
data2,
key,
rsaKeyPair.PrivateKey,
encryptedData1.IV
);
var decryptedData2 =
await sut.DecryptAesCbcAndVerifyRsa256PssAsync(
new AesEncryptedAndRsaSignedData(
encryptedData2.EncryptedPayload,
encryptedData1.IV
),
key,
rsaKeyPair.PublicKey
);

// Assert
Assert.Equal(data1, decryptedData1);
Assert.Equal(data2, decryptedData2);
Assert.Equal(encryptedData1.IV, encryptedData2.IV);
});
});
// Assert
Assert.Equal(data1, decryptedData1);
Assert.Equal(data2, decryptedData2);
Assert.Equal(encryptedData1.IV, encryptedData2.IV);
});
});

Describe("when the payload is tampered with")
.As(() =>
{
It("Throws")
.When(async () =>
{
// Arrange
var key = await sut.GenerateAesKeyAsync();
var rsaKeyPair = await sut.GenerateRsaKeysAsync();
var data = Encoding.UTF8.GetBytes("Hello, world!");
Describe("when the payload is tampered with")
.As(() =>
{
It("Throws")
.When(async () =>
{
// Arrange
var key = await sut.GenerateAesKeyAsync();
var rsaKeyPair = await sut.GenerateRsaKeysAsync();
var data = Encoding.UTF8.GetBytes("Hello, world!");

// Act
var encryptedData = await sut.SignRsa256PssAndEncryptAesCbcAsync(
data,
key,
rsaKeyPair.PrivateKey
);
// Act
var encryptedData =
await sut.SignRsa256PssAndEncryptAesCbcAsync(
data,
key,
rsaKeyPair.PrivateKey
);

encryptedData.EncryptedPayload[0] ^= 0xFF;
encryptedData.EncryptedPayload[0] ^= 0xFF;

// Assert
await Assert.ThrowsAsync<SignatureMismatchException>(
async () =>
await sut.DecryptAesCbcAndVerifyRsa256PssAsync(
encryptedData,
key,
rsaKeyPair.PublicKey
)
);
});
});
});
});
// Assert
await Assert.ThrowsAsync<SignatureMismatchException>(
async () =>
await sut.DecryptAesCbcAndVerifyRsa256PssAsync(
encryptedData,
key,
rsaKeyPair.PublicKey
)
);
});
});
});
});
}
Loading

0 comments on commit 5583173

Please sign in to comment.