Skip to content

Commit 0b7afdb

Browse files
committed
resolves issue #4
1 parent ff57d35 commit 0b7afdb

7 files changed

Lines changed: 70 additions & 14 deletions

File tree

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,32 @@ To verify the installation run:
4343

4444
## Usage of Salesforce dotnet cli tool
4545

46-
The regular usage of this tool is within the Salesforce project that utilizes this library and contains configuration settings with the minimum of:
46+
There are several ways to run this cli tool.
47+
48+
1. From any location with Consumer Key and Secret provided
49+
50+
```bash
51+
salesforce get-tokens --key:{key} --secret:{secret} --login:https://login.salesforce.com --verbose:information
52+
```
53+
54+
2. Running the tool in the directory that contains `appsettings.json` file
55+
56+
```bash
57+
salesforce get-tokens --section Salesforce
58+
```
59+
Note: required configurations are as follows:
4760

4861
```json
4962
"Salesforce": {
5063
"ClientId": "",
51-
"ClientSecret": ""
64+
"ClientSecret": "",
65+
"LoginUrl": ""
5266
}
5367
````
5468

55-
Or you can specify Azure Key Vault Url:
69+
3. Running with Azure Vault
5670

71+
`appsettings.json
5772
```json
5873
"AzureVault": {
5974
"BaseUrl": "https://{name}.vault.azure.net/"

src/AuthApp/Host/HttpServer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ await auth.WebServerAsync(
9494
_config.ClientSecret,
9595
redirectURI,
9696
code,
97-
$"{_config.LoginUrl}{_config.OAuthTokenUri}");
97+
$"{_config.LoginUrl}{_config.OAuthUri}");
9898

9999
Console.WriteLineFormatted("Access_token = {0}",Color.Green, Color.Yellow, auth.AccessInfo.AccessToken);
100100
Console.WriteLineFormatted("Refresh_token = {0}", Color.Green, Color.Yellow, auth.AccessInfo.RefreshToken);
@@ -122,7 +122,7 @@ private int GetRandomUnusedPort()
122122

123123
private string GetAuthorizationUrl(string redirectURI)
124124
{
125-
var authEndpoint = $"{_config.LoginUrl}{_config.OAuthUri}";
125+
var authEndpoint = $"{_config.LoginUrl}{_config.OAuthorizeUri}";
126126
var url = $"{authEndpoint}?response_type=code&access_type=offline&scope=openid%20profile%20api%20refresh_token%20offline_access&redirect_uri={Uri.EscapeDataString(redirectURI)}&client_id={_config.ClientId}";
127127
return url;
128128
}

src/AuthApp/Host/SfConfig.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public class SfConfig
2626
/// <summary>
2727
/// Default set to /services/oauth2/token.
2828
/// </summary>
29-
public string OAuthTokenUri { get; set; } = "/services/oauth2/token";
29+
public string OAuthUri { get; set; } = "/services/oauth2/token";
3030

31-
public string OAuthUri { get; set; } = "/services/oauth2/authorize";
31+
public string OAuthorizeUri { get; set; } = "/services/oauth2/authorize";
3232
}
3333
}

src/AuthApp/HostBuilderExtensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,21 @@ internal static IHostBuilder CreateDefaultBuilder(HostBuilderOptions options)
6969
config.AddAzureKeyVault(hostingEnviromentName:options.HostingEnviroment, options.UseAzureKeyPrefix);
7070
}
7171

72+
if(!string.IsNullOrWhiteSpace(options.Settings.ClientId)
73+
&& !string.IsNullOrWhiteSpace(options.Settings.ClientSecret))
74+
{
75+
var inputValues = new Dictionary<string, string>
76+
{
77+
{$"{options.SectionName}:ClientId", options.Settings.ClientId },
78+
{$"{options.SectionName}:ClientSecret", options.Settings.ClientSecret },
79+
{$"{options.SectionName}:LoginUrl", options.Settings.LoginUrl },
80+
{$"{options.SectionName}:OAuthUri", options.Settings.OAuthUri },
81+
{$"{options.SectionName}:OAuthorizeUri", options.Settings.OAuthorizeUri },
82+
};
83+
84+
config.AddInMemoryCollection(inputValues);
85+
}
86+
7287
if (options.Verbose && options.Level == LogLevel.Debug
7388
|| options.Level == LogLevel.Trace)
7489
{

src/AuthApp/HostBuilderOptions.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.Extensions.Logging;
1+
using AuthApp.Host;
2+
using Microsoft.Extensions.Logging;
23

34
namespace AuthApp
45
{
@@ -38,5 +39,15 @@ internal class HostBuilderOptions
3839
/// Pass Hosting environment for the context of the application.
3940
/// </summary>
4041
public string HostingEnviroment { get; set; }
42+
43+
/// <summary>
44+
/// Salesforce options.
45+
/// </summary>
46+
public SfConfig Settings { get; set; }
47+
48+
/// <summary>
49+
/// The name of the configuration section for the options.
50+
/// </summary>
51+
public string SectionName { get; set; }
4152
}
4253
}

src/AuthApp/Properties/launchSettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
"AuthApp": {
44
"commandName": "Project",
55
//"commandLineArgs": "get-tokens --azure https://{name}.vault.azure.net/"
6-
"commandLineArgs": "get-tokens --verbose:information --section:sfconfig"
6+
//"commandLineArgs": "get-tokens --verbose:information --section:sfconfig"
77

8+
"commandLineArgs": "get-tokens --key: --secret: --verbose:information --section:Salesforce"
89
}
910
}
1011
}

src/AuthApp/TokenGeneratorCommand.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ namespace AuthApp
1515
ThrowOnUnexpectedArgument = false)]
1616
internal class TokenGeneratorCommand
1717
{
18+
[Option("--key", Description = "The Salesforce Consumer Key.")]
19+
public string ClientId { get; set; }
20+
21+
[Option("--secret", Description = "The Salesforce Consumer Secret.")]
22+
public string ClientSecret { get; set; }
23+
24+
[Option("--login", Description = "The Salesforce login url. The default value is https://login.salesforce.com.")]
25+
public string LoginUrl { get; set; }
26+
1827
[Option("--azure",
1928
Description = "Allows to specify Azure Vault Url. It overrides url specified in the appsetting.json file or any other configuration provider.")]
2029
public string AzureVault { get; set; }
@@ -57,25 +66,30 @@ private async Task<int> OnExecuteAsync()
5766
Verbose = Verbose.HasValue,
5867
Level = Verbose.level,
5968
UserSecrets = UserSecrets,
60-
HostingEnviroment = !string.IsNullOrWhiteSpace(HostingEnviroment) ? HostingEnviroment : "Development"
69+
HostingEnviroment = !string.IsNullOrWhiteSpace(HostingEnviroment) ? HostingEnviroment : "Development",
70+
Settings = new SfConfig
71+
{
72+
ClientId = ClientId,
73+
ClientSecret = ClientSecret,
74+
LoginUrl = !string.IsNullOrWhiteSpace(LoginUrl) ? LoginUrl : "https://login.salesforce.com"
75+
},
76+
SectionName = string.IsNullOrWhiteSpace(SectionName) ? "Salesforce" : SectionName,
6177
};
6278

6379
try
6480
{
6581
var builder = HostBuilderExtensions.CreateDefaultBuilder(builderConfig)
6682
.ConfigureServices((hostingContext, services) =>
6783
{
68-
var configSection = string.IsNullOrWhiteSpace(SectionName) ? "Salesforce" : SectionName;
69-
70-
services.ConfigureWithDataAnnotationsValidation<SfConfig>(hostingContext.Configuration, configSection);
84+
services.ConfigureWithDataAnnotationsValidation<SfConfig>(hostingContext.Configuration, builderConfig.SectionName);
7185
services.AddHostedService<HttpServer>();
7286
});
7387

7488
await builder.RunConsoleAsync();
7589

7690
return 0;
7791
}
78-
catch(Microsoft.Extensions.Options.OptionsValidationException)
92+
catch(Microsoft.Extensions.Options.OptionsValidationException exv)
7993
{
8094
Console.WriteLine("Not all of the required configurations has been provided.", Color.Red);
8195
}

0 commit comments

Comments
 (0)