11using System ;
22using System . Collections . Generic ;
3- using System . Diagnostics ;
4- using System . Dynamic ;
53using System . Reflection ;
64using System . IO ;
75using System . Linq ;
@@ -17,6 +15,15 @@ namespace NetCoreForce.ModelGenerator
1715{
1816 class Program
1917 {
18+ /// <summary>
19+ /// Gets a human-readable string for which value maps to which auth method type
20+ /// </summary>
21+ /// <returns>
22+ /// For example: <c>1 = UsernamePassword, 2 = ClientCredentials</c>
23+ /// </returns>>
24+ private static string ValidAuthTypesInputString =>
25+ string . Join ( ", " , Enum . GetValues ( typeof ( AuthInfo . AuthMethodType ) ) . Cast < AuthInfo . AuthMethodType > ( ) . Select ( v => $ "{ ( int ) v } = { v } ") ) ;
26+
2027 const string defaultConfigFilename = "modelgenerator_config.json" ;
2128
2229 static void Main ( string [ ] args )
@@ -45,7 +52,7 @@ static void Main(string[] args)
4552 "You can supply the API credentials either in the config file, the command parameters, or wait to be prompted for that information." + Environment . NewLine +
4653 "If you choose to save the config file, be careful with it as it may contain your API credentials." ;
4754
48- //Authentication
55+ //Authentication options
4956 var clientIdOption = command . Option ( "--client-id" ,
5057 "API Client ID, a.k.a. Consumer Key" ,
5158 CommandOptionType . SingleValue ) ;
@@ -62,6 +69,15 @@ static void Main(string[] args)
6269 "API Password" ,
6370 CommandOptionType . SingleValue ) ;
6471
72+ var authMethodOption = command . Option ( "--auth-method" ,
73+ $ "Auth Method, Valid inputs: { ValidAuthTypesInputString } ",
74+ CommandOptionType . SingleValue ) ;
75+
76+ var tokenRequestEndpointOption = command . Option ( "--token-request-endpoint" ,
77+ $ "Token Request endpoint default: { GenConfig . DefaultTokenRequestEndpoint } ",
78+ CommandOptionType . SingleValue ) ;
79+
80+ //Config options
6581 var configFileOption = command . Option ( "--config-file" ,
6682 "Config file path" ,
6783 CommandOptionType . SingleValue ) ;
@@ -130,6 +146,22 @@ static void Main(string[] args)
130146 config . AuthInfo . Password = passwordOption . Value ( ) ;
131147 }
132148
149+ if ( authMethodOption . HasValue ( ) )
150+ {
151+ if ( Enum . TryParse ( authMethodOption . Value ( ) , ignoreCase : true , out AuthInfo . AuthMethodType authMethod ) )
152+ config . AuthInfo . AuthMethod = authMethod ;
153+ else
154+ {
155+ Console . WriteLine ( $ "Invalid auth method input, valid inputs: { ValidAuthTypesInputString } ") ;
156+ return - 1 ;
157+ }
158+ }
159+
160+ if ( tokenRequestEndpointOption . HasValue ( ) )
161+ {
162+ config . AuthInfo . TokenRequestEndpoint = tokenRequestEndpointOption . Value ( ) ;
163+ }
164+
133165 if ( customOption . HasValue ( ) )
134166 {
135167 config . IncludeCustom = customOption . HasValue ( ) ;
@@ -209,6 +241,30 @@ static void Main(string[] args)
209241 private static GenConfig CheckOptions ( GenConfig config )
210242 {
211243 //check required auth options
244+ while ( config . AuthInfo . AuthMethod == null )
245+ {
246+ Console . WriteLine ( "Enter Auth Method:" ) ;
247+ string consoleReadLine = Console . ReadLine ( ) ;
248+
249+ if ( Enum . TryParse ( consoleReadLine , ignoreCase : true , out AuthInfo . AuthMethodType authMethod ) )
250+ config . AuthInfo . AuthMethod = authMethod ;
251+ else
252+ Console . WriteLine ( $ "Invalid input, valid inputs: { ValidAuthTypesInputString } ") ;
253+
254+ Console . WriteLine ( ) ;
255+ }
256+
257+ while ( (
258+ string . IsNullOrEmpty ( config . AuthInfo . TokenRequestEndpoint ) ||
259+ config . AuthInfo . TokenRequestEndpoint == GenConfig . DefaultTokenRequestEndpoint
260+ ) &&
261+ config . AuthInfo . AuthMethod == AuthInfo . AuthMethodType . ClientCredentials )
262+ {
263+ Console . WriteLine ( "Enter Token Request Endpoint:" ) ;
264+ config . AuthInfo . TokenRequestEndpoint = Console . ReadLine ( ) ;
265+ Console . WriteLine ( ) ;
266+ }
267+
212268 while ( string . IsNullOrEmpty ( config . AuthInfo . ClientId ) )
213269 {
214270 Console . WriteLine ( "Enter API Client ID:" ) ;
@@ -223,14 +279,14 @@ private static GenConfig CheckOptions(GenConfig config)
223279 Console . WriteLine ( ) ;
224280 }
225281
226- while ( string . IsNullOrEmpty ( config . AuthInfo . Username ) )
282+ while ( string . IsNullOrEmpty ( config . AuthInfo . Username ) && config . AuthInfo . AuthMethod == AuthInfo . AuthMethodType . UsernamePassword )
227283 {
228284 Console . WriteLine ( "Enter API username:" ) ;
229285 config . AuthInfo . Username = Console . ReadLine ( ) ;
230286 Console . WriteLine ( ) ;
231287 }
232288
233- while ( string . IsNullOrEmpty ( config . AuthInfo . Password ) )
289+ while ( string . IsNullOrEmpty ( config . AuthInfo . Password ) && config . AuthInfo . AuthMethod == AuthInfo . AuthMethodType . UsernamePassword )
234290 {
235291 Console . WriteLine ( "Enter API password:" ) ;
236292 config . AuthInfo . Password = Console . ReadLine ( ) ;
@@ -338,8 +394,19 @@ private static async Task<ForceClient> Login(GenConfig config)
338394 AuthenticationClient auth = new AuthenticationClient ( config . AuthInfo . ApiVersion ) ;
339395 try
340396 {
341- await auth . UsernamePasswordAsync ( config . AuthInfo . ClientId , config . AuthInfo . ClientSecret ,
342- config . AuthInfo . Username , config . AuthInfo . Password , config . AuthInfo . TokenRequestEndpoint ) ;
397+ switch ( config . AuthInfo . AuthMethod )
398+ {
399+ case AuthInfo . AuthMethodType . UsernamePassword :
400+ await auth . UsernamePasswordAsync ( config . AuthInfo . ClientId , config . AuthInfo . ClientSecret ,
401+ config . AuthInfo . Username , config . AuthInfo . Password , config . AuthInfo . TokenRequestEndpoint ) ;
402+ break ;
403+ case AuthInfo . AuthMethodType . ClientCredentials :
404+ await auth . ClientCredentialsAsync ( config . AuthInfo . ClientId , config . AuthInfo . ClientSecret ,
405+ config . AuthInfo . TokenRequestEndpoint ) ;
406+ break ;
407+ default :
408+ throw new ArgumentOutOfRangeException ( nameof ( config . AuthInfo . AuthMethod ) , $ "authMethodInt is out of range, value: { ( int ) config . AuthInfo . AuthMethod } ") ;
409+ }
343410
344411 Console . WriteLine ( "Connected to Salesforce" ) ;
345412 }
@@ -499,7 +566,7 @@ public static async Task<string> GenClass(ForceClient client, string objectName,
499566 if ( field . Custom && ! config . IncludeCustom )
500567 {
501568 continue ;
502- }
569+ }
503570
504571 gen . AppendLine ( "\t \t ///<summary>" ) ;
505572 gen . AppendLine ( "\t \t /// " + WebUtility . HtmlEncode ( field . Label ) ) ;
0 commit comments