|
| 1 | +using System; |
| 2 | + |
| 3 | +namespace GitCredentialManager.Authentication.OAuth |
| 4 | +{ |
| 5 | + /// <summary> |
| 6 | + /// Helper class for retrieving OAuth2 configuration settings from environment variables or Git configuration. |
| 7 | + /// </summary> |
| 8 | + public static class OAuth2SettingsHelper |
| 9 | + { |
| 10 | + /// <summary> |
| 11 | + /// Retrieves an OAuth configuration value from settings, with fallback to a default value. |
| 12 | + /// </summary> |
| 13 | + /// <param name="settings">The settings instance to query.</param> |
| 14 | + /// <param name="environmentVariable">The environment variable name.</param> |
| 15 | + /// <param name="configSection">The Git configuration section name.</param> |
| 16 | + /// <param name="configProperty">The Git configuration property name.</param> |
| 17 | + /// <param name="defaultValue">The default value to return if no setting is found.</param> |
| 18 | + /// <returns>The configured value if found, otherwise the default value.</returns> |
| 19 | + public static string GetOAuthConfigValue( |
| 20 | + this ISettings settings, |
| 21 | + string environmentVariable, |
| 22 | + string configSection, |
| 23 | + string configProperty, |
| 24 | + string defaultValue) |
| 25 | + { |
| 26 | + if (settings.TryGetSetting(environmentVariable, configSection, configProperty, out string value)) |
| 27 | + { |
| 28 | + return value; |
| 29 | + } |
| 30 | + |
| 31 | + return defaultValue; |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Retrieves a required OAuth configuration value from settings, throwing an exception if not found. |
| 36 | + /// </summary> |
| 37 | + /// <param name="settings">The settings instance to query.</param> |
| 38 | + /// <param name="environmentVariable">The environment variable name.</param> |
| 39 | + /// <param name="configSection">The Git configuration section name.</param> |
| 40 | + /// <param name="configProperty">The Git configuration property name.</param> |
| 41 | + /// <param name="errorMessage">The error message to include in the exception if the value is not found.</param> |
| 42 | + /// <returns>The configured value.</returns> |
| 43 | + /// <exception cref="ArgumentException">Thrown when the required value is not found.</exception> |
| 44 | + public static string GetRequiredOAuthConfigValue( |
| 45 | + this ISettings settings, |
| 46 | + string environmentVariable, |
| 47 | + string configSection, |
| 48 | + string configProperty, |
| 49 | + string errorMessage) |
| 50 | + { |
| 51 | + if (settings.TryGetSetting(environmentVariable, configSection, configProperty, out string value)) |
| 52 | + { |
| 53 | + return value; |
| 54 | + } |
| 55 | + |
| 56 | + throw new ArgumentException(errorMessage); |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Retrieves an OAuth configuration URI from settings, with fallback to a default value. |
| 61 | + /// </summary> |
| 62 | + /// <param name="settings">The settings instance to query.</param> |
| 63 | + /// <param name="environmentVariable">The environment variable name.</param> |
| 64 | + /// <param name="configSection">The Git configuration section name.</param> |
| 65 | + /// <param name="configProperty">The Git configuration property name.</param> |
| 66 | + /// <param name="defaultValue">The default URI to return if no setting is found.</param> |
| 67 | + /// <returns>The configured URI if found and valid, otherwise the default URI.</returns> |
| 68 | + public static Uri GetOAuthConfigUri( |
| 69 | + this ISettings settings, |
| 70 | + string environmentVariable, |
| 71 | + string configSection, |
| 72 | + string configProperty, |
| 73 | + Uri defaultValue) |
| 74 | + { |
| 75 | + if (settings.TryGetSetting(environmentVariable, configSection, configProperty, out string uriStr) && |
| 76 | + Uri.TryCreate(uriStr, UriKind.Absolute, out Uri uri)) |
| 77 | + { |
| 78 | + return uri; |
| 79 | + } |
| 80 | + |
| 81 | + return defaultValue; |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments