From 19c27c8ef0a7e4140512cd305f8faf9f77e22245 Mon Sep 17 00:00:00 2001 From: Leon Hoppe Date: Sat, 17 Jan 2026 18:32:57 +0100 Subject: [PATCH] Switched to Device code authentication --- ConfigData.cs | 10 ---------- Dockerfile | 1 - OneDriveClient.cs | 19 +++++++++++++++---- Worker.cs | 2 ++ 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/ConfigData.cs b/ConfigData.cs index 3df3f80..c7df2f1 100644 --- a/ConfigData.cs +++ b/ConfigData.cs @@ -7,21 +7,11 @@ public sealed class ConfigData { public string IncludeFile { get; } public int KeepLast { get; } - public string TenantId { get; } - public string ClientId { get; } - public string ClientSecret { get; } - public string UserId { get; } - public ConfigData(IConfiguration config) { Schedule = config["Schedule"]!; BackupUploadRoot = config["UploadRoot"]!; LocalRoot = config["LocalRoot"]!; IncludeFile = config["IncludeFile"]!; KeepLast = int.Parse(config["KeepLast"]!); - - TenantId = config["TenantId"]!; - ClientId = config["ClientId"]!; - ClientSecret = config["ClientSecret"]!; - UserId = config["UserId"]!; } } \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 6432bff..2574c6a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,4 @@ FROM mcr.microsoft.com/dotnet/runtime:10.0 AS base -RUN apt-get update && apt-get install -y tar && rm /var/lib/apt/lists/* USER $APP_UID WORKDIR /app diff --git a/OneDriveClient.cs b/OneDriveClient.cs index 3e2d3ca..79e8186 100644 --- a/OneDriveClient.cs +++ b/OneDriveClient.cs @@ -13,15 +13,26 @@ public class OneDriveClient { public OneDriveClient(ConfigData config) { _config = config; - var credential = new ClientSecretCredential(_config.TenantId, _config.ClientId, _config.ClientSecret); - _client = new GraphServiceClient(credential); + var options = new DeviceCodeCredentialOptions { + TenantId = "consumers", + DeviceCodeCallback = (code, _) => { + Console.WriteLine(code.Message); + return Task.CompletedTask; + } + }; + + _client = new GraphServiceClient(new DeviceCodeCredential(options), ["Files.ReadWrite.All"]); + } + + public async Task EnsureAuthenticated(CancellationToken token) { + await _client.Me.Drive.GetAsync(cancellationToken: token); } public async Task> UploadFile(string filePath, CancellationToken token) { var fileName = Path.GetFileName(filePath); var remoteFilePath = _config.BackupUploadRoot.Trim('/') + '/' + fileName; - var defaultDrive = await _client.Users[_config.UserId].Drive.GetAsync(cancellationToken: token); + var defaultDrive = await _client.Me.Drive.GetAsync(cancellationToken: token); var driveFile = _client.Drives[defaultDrive!.Id].Items[$"root:/{remoteFilePath}:"]!; var uploadSession = await driveFile.CreateUploadSession.PostAsync(new CreateUploadSessionPostRequestBody { @@ -40,7 +51,7 @@ public class OneDriveClient { } public async Task DeleteOldFiles(CancellationToken token) { - var defaultDrive = await _client.Users[_config.UserId].Drive.GetAsync(cancellationToken: token); + var defaultDrive = await _client.Me.Drive.GetAsync(cancellationToken: token); var remoteFolder = _config.BackupUploadRoot.Trim('/'); var backupFiles = await _client.Drives[defaultDrive!.Id] diff --git a/Worker.cs b/Worker.cs index d59f83f..2e311d6 100644 --- a/Worker.cs +++ b/Worker.cs @@ -5,6 +5,8 @@ namespace OneDriveBackupService; public class Worker(ILogger logger, ConfigData config, OneDriveClient client) : BackgroundService { protected override async Task ExecuteAsync(CancellationToken stoppingToken) { + await client.EnsureAuthenticated(stoppingToken); + if (Environment.GetCommandLineArgs().Contains("--run-once")) { logger.LogInformation("Manual backup triggered"); await RunBackup(DateTime.Now, stoppingToken);