From c80a7b520df8782084d218aa8489a15d753d066d Mon Sep 17 00:00:00 2001 From: Jcardif <29174946+Jcardif@users.noreply.github.com> Date: Tue, 10 Sep 2024 15:51:43 +0300 Subject: [PATCH] Authenticate to google cloud --- src/Functions/DeleteCalendarEvent.cs | 6 +++++- src/Functions/SyncNewEvents.cs | 16 ++++++++++------ src/Functions/UpdateCalendarEvent.cs | 6 +++++- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/Functions/DeleteCalendarEvent.cs b/src/Functions/DeleteCalendarEvent.cs index 8a4544f..8f182b3 100644 --- a/src/Functions/DeleteCalendarEvent.cs +++ b/src/Functions/DeleteCalendarEvent.cs @@ -35,9 +35,13 @@ public async Task RunAsync([HttpTrigger(AuthorizationLevel.Fun return response; } + + // Authenticate to Google Cloud Console + var privateKeyFile = await keyVaultService.GetSecretAsync(GOOGLE_CALENDAR_PRIVATE_KEY_SECRET_NAME); + var calendarService = googleCalendarService.AuthenticateAsync(privateKeyFile); // Check if the token was acquired successfully - if (googleCalendarService.CalendarService is null) + if (calendarService is null) { // get response from helper method response = await req.CreateFunctionReturnResponseAsync(HttpStatusCode.Unauthorized, diff --git a/src/Functions/SyncNewEvents.cs b/src/Functions/SyncNewEvents.cs index 62b787c..e69e1a3 100644 --- a/src/Functions/SyncNewEvents.cs +++ b/src/Functions/SyncNewEvents.cs @@ -16,7 +16,7 @@ namespace CalendarSync.Functions; public class SyncNewEvents(ILoggerFactory loggerFactory, AppDbContext context, GoogleCalendarService googleCalendarService, KeyVaultService keyVaultService) { private readonly ILogger _logger = loggerFactory.CreateLogger(); - private List? calendarEvents; + private List? _calendarEvents; [Function("SyncNewEvents")] public async Task RunAsync([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequestData req) @@ -37,10 +37,10 @@ public async Task RunAsync([HttpTrigger(AuthorizationLevel.Fun { if (bodyType == "list") { - calendarEvents = JsonConvert.DeserializeObject>(requestBody); + _calendarEvents = JsonConvert.DeserializeObject>(requestBody); // check if list is null or empty - if (calendarEvents is null || calendarEvents.Count == 0) + if (_calendarEvents is null || _calendarEvents.Count == 0) { // get response from helper method response = await req.CreateFunctionReturnResponseAsync(HttpStatusCode.BadRequest, "No events were passed"); @@ -62,7 +62,7 @@ public async Task RunAsync([HttpTrigger(AuthorizationLevel.Fun return response; } - calendarEvents = new List { calendarEvent }; + _calendarEvents = new List { calendarEvent }; } } @@ -75,8 +75,12 @@ public async Task RunAsync([HttpTrigger(AuthorizationLevel.Fun return response; } + // Authenticate to Google Cloud Console + var privateKeyFile = await keyVaultService.GetSecretAsync(GOOGLE_CALENDAR_PRIVATE_KEY_SECRET_NAME); + var calendarService = googleCalendarService.AuthenticateAsync(privateKeyFile); + // Check if the token was acquired successfully - if (googleCalendarService.CalendarService is null) + if (calendarService is null) { // get response from helper method response = await req.CreateFunctionReturnResponseAsync(HttpStatusCode.Unauthorized, @@ -88,7 +92,7 @@ public async Task RunAsync([HttpTrigger(AuthorizationLevel.Fun // Use EF Core to insert a record into the table var cEvents = new List(); - foreach (var calendarEvent in calendarEvents) + foreach (var calendarEvent in _calendarEvents) { var existingCalendarEvent = context?.CalendarEvents?.Find(calendarEvent.Id); if (existingCalendarEvent != null) diff --git a/src/Functions/UpdateCalendarEvent.cs b/src/Functions/UpdateCalendarEvent.cs index 6cdeeaa..649f273 100644 --- a/src/Functions/UpdateCalendarEvent.cs +++ b/src/Functions/UpdateCalendarEvent.cs @@ -44,9 +44,13 @@ public async Task RunAsync([HttpTrigger(AuthorizationLevel.Fun return response; } + + // Authenticate to Google Cloud Console + var privateKeyFile = await keyVaultService.GetSecretAsync(GOOGLE_CALENDAR_PRIVATE_KEY_SECRET_NAME); + var calendarService = googleCalendarService.AuthenticateAsync(privateKeyFile); // Check if the token was acquired successfully - if (googleCalendarService.CalendarService is null) + if (calendarService is null) { // get response from helper method response = await req.CreateFunctionReturnResponseAsync(HttpStatusCode.Unauthorized,