Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Common/CommonErrorMessages.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/Common/CommonErrorMessages.resx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@
<value>Failed to load JSON secrets file for service account configuration. See inner exception for details.</value>
<comment>Used by AppConfigBase class when trying to load various settings from the JSON secrets file when using OAuth2 service account flow.</comment>
</data>
<data name="FailedToLoadJsonSecrets" xml:space="preserve">
<value>Failed to load JSON secrets for service account configuration. See inner exception for details.</value>
<comment>Used by AppConfigBase class when trying to load various settings from the JSON secrets when using OAuth2 service account flow.</comment>
</data>
<data name="FailedToParseAuthTokenException" xml:space="preserve">
<value>Failed to parse response from server. See inner exception for more details.</value>
<comment>Used as default error message when auth token error parsing fails.</comment>
Expand Down
5 changes: 5 additions & 0 deletions src/Common/Lib/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public interface AppConfig : ICloneable
/// </remarks>
string OAuth2SecretsJsonPath { get; set; }

/// <summary>
/// contents
/// </summary>
string OAuth2SecretsJson { get; set; }

/// <summary>
/// Gets the OAuth2 private key for service account flow.
/// </summary>
Expand Down
78 changes: 62 additions & 16 deletions src/Common/Lib/AppConfigBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ public class AppConfigBase : AppConfig, INotifyPropertyChanged
private ConfigSetting<string> oAuth2SecretsJsonPath =
new ConfigSetting<string>("OAuth2SecretsJsonPath", "");

private ConfigSetting<string> oAuth2SecretsJson =
new ConfigSetting<string>("OAuth2SecretsJson", "");

/// <summary>
/// OAuth2 scope.
/// </summary>
Expand Down Expand Up @@ -351,6 +354,22 @@ public string OAuth2SecretsJsonPath
}
}

/// <summary>
/// Gets or sets the OAuth2 secrets JSON file path.
/// </summary>
/// <remarks>
/// This setting is applicable only when using OAuth2 service accounts.
/// </remarks>
public string OAuth2SecretsJson
{
get => oAuth2SecretsJson.Value;
set
{
SetPropertyAndNotify(oAuth2SecretsJson, value);
LoadOAuth2SecretsFromString();
}
}

/// <summary>
/// Gets or sets whether usage of various client library features should be
/// tracked.
Expand Down Expand Up @@ -548,6 +567,12 @@ protected virtual void ReadSettings(Dictionary<string, string> settings)
// Read and parse the OAuth2 JSON secrets file if applicable.
ReadSetting(settings, oAuth2SecretsJsonPath);

ReadSetting(settings, oAuth2SecretsJson);
if (!string.IsNullOrEmpty(oAuth2SecretsJson.Value))
{
LoadOAuth2SecretsFromString();
}

if (!string.IsNullOrEmpty(oAuth2SecretsJsonPath.Value))
{
LoadOAuth2SecretsFromFile();
Expand Down Expand Up @@ -608,22 +633,7 @@ private void LoadOAuth2SecretsFromFile()
using (StreamReader reader = new StreamReader(OAuth2SecretsJsonPath))
{
string contents = reader.ReadToEnd();
Dictionary<string, string> config =
JsonConvert.DeserializeObject<Dictionary<string, string>>(contents);

ReadSetting(config, oAuth2ServiceAccountEmail);
if (string.IsNullOrEmpty(this.OAuth2ServiceAccountEmail))
{
throw new AdsOAuthException(CommonErrorMessages
.ClientEmailIsMissingInJsonFile);
}

ReadSetting(config, oAuth2PrivateKey);
if (string.IsNullOrEmpty(this.OAuth2PrivateKey))
{
throw new AdsOAuthException(CommonErrorMessages
.PrivateKeyIsMissingInJsonFile);
}
LoadOAuth2Secrets(contents);
}
}
catch (AdsOAuthException)
Expand All @@ -636,6 +646,42 @@ private void LoadOAuth2SecretsFromFile()
}
}

private void LoadOAuth2SecretsFromString()
{
try
{
LoadOAuth2Secrets(oAuth2SecretsJson.Value);
}
catch (AdsOAuthException)
{
throw;
}
catch (Exception e)
{
throw new AdsOAuthException(CommonErrorMessages.FailedToLoadJsonSecrets, e);
}
}

private void LoadOAuth2Secrets(string contents)
{
Dictionary<string, string> config =
JsonConvert.DeserializeObject<Dictionary<string, string>>(contents);

ReadSetting(config, oAuth2ServiceAccountEmail);
if (string.IsNullOrEmpty(this.OAuth2ServiceAccountEmail))
{
throw new AdsOAuthException(CommonErrorMessages
.ClientEmailIsMissingInJsonFile);
}

ReadSetting(config, oAuth2PrivateKey);
if (string.IsNullOrEmpty(this.OAuth2PrivateKey))
{
throw new AdsOAuthException(CommonErrorMessages
.PrivateKeyIsMissingInJsonFile);
}
}

/// <summary>
/// Reads a setting from a given dictionary.
/// </summary>
Expand Down