-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from Team-Wilhelm/jwt-claims
Jwt claims + SAS tokens for Blobs
- Loading branch information
Showing
24 changed files
with
171 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System.Net; | ||
using Azure.Storage.Blobs; | ||
using Azure.Storage.Sas; | ||
using Core.Options; | ||
using Microsoft.Extensions.Options; | ||
using Shared.Exceptions; | ||
|
||
namespace Core.Services.External.BlobStorage; | ||
|
||
public class BlobStorageService(IOptions<AzureBlobStorageOptions> azureBlobStorageOptions) : IBlobStorageService | ||
{ | ||
public async Task<string> SaveImageToBlobStorage(string base64Image, string userEmail, string? blobUrl = null) | ||
{ | ||
var imageBytes = Convert.FromBase64String(base64Image); | ||
var blobName = blobUrl is not null | ||
? GetBlobNameFromUrl(blobUrl) | ||
: userEmail + "_" + Guid.NewGuid(); | ||
|
||
var blobClient = new BlobClient(azureBlobStorageOptions.Value.ConnectionString, azureBlobStorageOptions.Value.PlantImagesContainer, blobName); | ||
var binaryData = new BinaryData(imageBytes); | ||
await blobClient.UploadAsync(binaryData, true); | ||
return WebUtility.UrlDecode(blobClient.Uri.ToString()); | ||
} | ||
|
||
public async Task<bool> DeleteImageFromBlobStorage(string imageUrl) | ||
{ | ||
var blobClient = new BlobClient(new Uri(imageUrl)); | ||
return await blobClient.DeleteIfExistsAsync(); | ||
} | ||
|
||
public async Task<string> GetImageFromBlobStorage(string imageUrl) | ||
{ | ||
if (string.IsNullOrEmpty(imageUrl)) return ""; | ||
|
||
var blobClient = new BlobClient(new Uri(imageUrl)); | ||
if (await blobClient.ExistsAsync() == false) throw new NotFoundException("Image not found"); | ||
|
||
using var memoryStream = new MemoryStream(); | ||
await blobClient.DownloadToAsync(memoryStream); | ||
var imageBytes = memoryStream.ToArray(); | ||
return Convert.ToBase64String(imageBytes); | ||
} | ||
|
||
public string GenerateSasUri(string blobUrl) | ||
{ | ||
var blobServiceClient = new BlobServiceClient(azureBlobStorageOptions.Value.ConnectionString); | ||
var blobContainerClient = blobServiceClient.GetBlobContainerClient(azureBlobStorageOptions.Value.PlantImagesContainer); | ||
var blobClient = blobContainerClient.GetBlobClient(GetBlobNameFromUrl(blobUrl)); | ||
|
||
var blobSasBuilder = new BlobSasBuilder | ||
{ | ||
BlobContainerName = blobContainerClient.Name, | ||
BlobName = blobClient.Name, | ||
Resource = "b", | ||
StartsOn = DateTimeOffset.UtcNow.AddMinutes(-5), | ||
ExpiresOn = DateTimeOffset.UtcNow.AddHours(1), | ||
}; | ||
blobSasBuilder.SetPermissions(BlobSasPermissions.Read); | ||
return blobClient.GenerateSasUri(blobSasBuilder).ToString(); | ||
} | ||
|
||
public string GetBlobUrlFromSasUri(string sasUri) | ||
{ | ||
var blobUriBuilder = new BlobUriBuilder(new Uri(sasUri)) | ||
{ | ||
Query = string.Empty | ||
}; | ||
return blobUriBuilder.ToString(); | ||
} | ||
|
||
private string GetBlobNameFromUrl(string blobUrl) | ||
{ | ||
return WebUtility.UrlDecode(new Uri(blobUrl).AbsolutePath.Substring(azureBlobStorageOptions.Value.PlantImagesContainer.Length + 2)); | ||
} | ||
} |
38 changes: 0 additions & 38 deletions
38
Core/Services/External/BlobStorage/BlobStorageServiceService.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
using Azure.Storage.Blobs; | ||
|
||
namespace Core.Services.External.BlobStorage; | ||
|
||
public interface IBlobStorageService | ||
{ | ||
public Task<string> SaveImageToBlobStorage(string base64Image, string userEmail, string? blobUrl = null); | ||
public Task<bool> DeleteImageFromBlobStorage(string imageUrl); | ||
public Task<string> GetImageFromBlobStorage(string imageUrl); | ||
public string GenerateSasUri(string blobUrl); | ||
public string GetBlobUrlFromSasUri(string sasUri); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using api.Events.Auth.Server; | ||
using api.Extensions; | ||
using Fleck; | ||
using lib; | ||
using Shared.Models; | ||
|
||
namespace api.Events.Auth.Client; | ||
|
||
public class ClientWantsToCheckJwtValidityDto : BaseDtoWithJwt; | ||
|
||
/// <summary> | ||
/// The token's validation is actually checked in Program.cs, but this event is used to request the validation. | ||
/// If the token is not valid, an exception will be thrown, and the GlobalExceptionHandler will catch it, and send a | ||
/// corresponding message to the client. | ||
/// </summary> | ||
public class ClientWantsToCheckJwtValidity : BaseEventHandler<ClientWantsToCheckJwtValidityDto> | ||
{ | ||
public override Task Handle(ClientWantsToCheckJwtValidityDto dto, IWebSocketConnection socket) | ||
{ | ||
socket.SendDto(new ServerAuthenticatesUser | ||
{ | ||
Jwt = dto.Jwt, | ||
}); | ||
return Task.CompletedTask; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.