// Copyright (C) 2025, The Duplicati Team // https://duplicati.com, hello@duplicati.com // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. #nullable enable using System; using System.Collections.Generic; using Duplicati.Library.Interface; namespace Duplicati.Library.Utility.Options; /// /// Helper class for unified use of authentication settings /// public static class AuthIdOptionsHelper { /// /// The default URL to use for the OAuth login hosted on GAE. /// private const string DEFAULT_DUPLICATI_OAUTH_SERVICE = "https://duplicati-oauth-handler.appspot.com/refresh"; /// /// The default URL to use for the new hosted OAuth login server. /// private const string DEFAULT_DUPLICATI_OAUTH_SERVICE_NEW = "https://oauth-service.duplicati.com/refresh"; /// /// Helper method to get an environment variable with a default value. /// /// The name of the environment variable to retrieve. /// The default value to return if the environment variable is not set or is empty. /// The value of the environment variable or the default value if it is not set. private static string GetEnvVar(string varName, string defaultValue) { var value = Environment.GetEnvironmentVariable(varName); return string.IsNullOrEmpty(value) ? defaultValue : value; } /// /// The URL to use for the OAuth service /// public static readonly string DUPLICATI_OAUTH_SERVICE = GetEnvVar("DUPLICATI_OAUTH_SERVICE", DEFAULT_DUPLICATI_OAUTH_SERVICE); /// /// The URL to use for the new OAuth service /// public static readonly string DUPLICATI_OAUTH_SERVICE_NEW = GetEnvVar("DUPLICATI_OAUTH_SERVICE", DEFAULT_DUPLICATI_OAUTH_SERVICE_NEW); /// /// Returns the URL to use for obtaining an OAuth token for the given module. /// /// The name of the module to use. /// The URL to use for obtaining an OAuth token. public static string GetOAuthLoginUrl(string modulename, string? oauthurl) { if (string.IsNullOrWhiteSpace(oauthurl)) oauthurl = DUPLICATI_OAUTH_SERVICE; var u = new Uri(oauthurl); var addr = u.SetPath("").SetQuery((u.Query ?? "") + (string.IsNullOrWhiteSpace(u.Query) ? "" : "&") + "type={0}"); return string.Format(addr.ToString(), modulename); } /// /// Returns the URL to use for obtaining an OAuth token for the given module, defaulting to the new server. /// /// The name of the module to use. public static string GetOAuthLoginUrlNew(string modulename, string? oauthurl) { if (string.IsNullOrWhiteSpace(oauthurl)) oauthurl = DUPLICATI_OAUTH_SERVICE_NEW; var u = new Uri(oauthurl); var addr = u.SetPath("").SetQuery((u.Query ?? "") + (string.IsNullOrWhiteSpace(u.Query) ? "" : "&") + "type={0}"); return string.Format(addr.ToString(), modulename); } /// /// The authentication ID option, without a prefix /// public const string AuthIdOption = "authid"; /// /// The OAuth server URL option, without a prefix /// public const string OAuthUrlOption = "oauth-url"; /// /// Gets the authentication options /// /// The URL to use for the token /// An optional prefix for the options /// The authentication options public static CommandLineArgument[] GetOptions(string tokenurl, string? prefix = null) => [ new CommandLineArgument($"{prefix}{AuthIdOption}", CommandLineArgument.ArgumentType.Password, Strings.AuthIdSettingsHelper.AuthidShort, Strings.AuthIdSettingsHelper.AuthidLong(tokenurl)), .. GetServerOnlyOptions(prefix) ]; /// /// Gets the server-only authentication options /// /// An optional prefix for the options /// The server-only authentication options public static CommandLineArgument[] GetServerOnlyOptions(string? prefix = null) => [ new CommandLineArgument($"{prefix}{OAuthUrlOption}", CommandLineArgument.ArgumentType.String, Strings.AuthIdSettingsHelper.OauthurlShort, Strings.AuthIdSettingsHelper.OauthurlLong) ]; /// /// Parses the authentication options from a dictionary /// /// The dictionary to parse /// An optional prefix for the options /// The parsed authentication options public static AuthIdOptions Parse(IReadOnlyDictionary options, string? prefix = null) { var oauthUrl = options.GetValueOrDefault($"{prefix}{OAuthUrlOption}"); if (string.IsNullOrEmpty(oauthUrl)) oauthUrl = DUPLICATI_OAUTH_SERVICE; return new AuthIdOptions(options.GetValueOrDefault($"{prefix}{AuthIdOption}"), oauthUrl); } /// /// Structure to hold the authentication options /// /// The Auth ID public sealed record AuthIdOptions(string? AuthId, string OAuthUrl) { /// /// Checks if the authid is set /// public bool IsValid() => !string.IsNullOrEmpty(AuthId); /// /// Throws an exception if the username and password are not set /// /// The URL to use for the token public AuthIdOptions RequireCredentials(string tokenurl) { if (!IsValid()) throw new UserInformationException(Strings.AuthIdSettingsHelper.MissingAuthID(tokenurl), "MissingAuthID"); return this; } } }