2424
2525namespace MyApp ;
2626
27- public static class TokenTool
27+ public class TokenTool
2828{
29- public static async Task RunAsync ( )
29+ public async Task RunAsync ( MyApp . IOConsole ioConsole )
3030 {
3131 var tenant = "common" ; // Damit auch externe Konten wie @live.de funktionieren
3232 var clientId = "865f6ac0-cdbc-44c6-98cc-3e35c39ecb6e" ; // aus der App-Registrierung
@@ -41,13 +41,13 @@ public static async Task RunAsync()
4141 ] ;
4242 for ( var i = 0 ; i < configUrlList . Count ; i ++ )
4343 {
44- Console . WriteLine ( i + ": " + configUrlList [ i ] ) ;
44+ ioConsole . WriteLine ( i + ": " + configUrlList [ i ] ) ;
4545 }
4646 var input = "0" ;
4747 if ( configUrlList . Count > 1 )
4848 {
49- Console . WriteLine ( "Enter index: " ) ;
50- input = Console . ReadLine ( ) ;
49+ ioConsole . WriteLine ( "Enter index: " ) ;
50+ input = ioConsole . ReadLine ( ) ;
5151 }
5252 var configUrl = configUrlList [ Convert . ToInt32 ( input ?? "0" ) ] ;
5353
@@ -56,10 +56,10 @@ public static async Task RunAsync()
5656 {
5757 text = "Enter character: (C)ertificateStore or (F)ile or (E)ntraID or (I)nteractive Entra or (S)ecret + optional Token(X)Change + optional Double(2)XChange" ;
5858 }
59- Console . WriteLine ( text ) ;
59+ ioConsole . WriteLine ( text ) ;
6060
6161
62- input = ( Console . ReadLine ( ) ?? "" ) . ToLower ( ) ;
62+ input = ( ioConsole . ReadLine ( ) ?? "" ) . ToLower ( ) ;
6363
6464 var exchange = "0" ;
6565 if ( input . EndsWith ( "x" ) || input . EndsWith ( "1" ) || input . EndsWith ( "2" ) )
@@ -79,16 +79,16 @@ public static async Task RunAsync()
7979
8080 if ( input == "s" )
8181 {
82- Console . WriteLine ( "Client ID (YourEmail or empty): " ) ;
83- var id = Console . ReadLine ( ) ;
84- Console . WriteLine ( "Client Secret (YourEmail-secret or empty): " ) ;
85- var secret = Console . ReadLine ( ) ;
82+ ioConsole . WriteLine ( "Client ID (YourEmail or empty): " ) ;
83+ var id = ioConsole . ReadLine ( ) ;
84+ ioConsole . WriteLine ( "Client Secret (YourEmail-secret or empty): " ) ;
85+ var secret = ioConsole . ReadLine ( ) ;
8686 if ( id == "" && secret == "" )
8787 {
8888 id = "aorzelski@phoenixcontact.com" ;
8989 secret = "aorzelski@phoenixcontact.com-secret" ;
90- Console . WriteLine ( $ "Client ID: { id } ") ;
91- Console . WriteLine ( $ "Client Secret: { secret } ") ;
90+ ioConsole . WriteLine ( $ "Client ID: { id } ") ;
91+ ioConsole . WriteLine ( $ "Client Secret: { secret } ") ;
9292 }
9393
9494 var request1 = new HttpRequestMessage ( HttpMethod . Post , configUrl ) ;
@@ -109,7 +109,7 @@ public static async Task RunAsync()
109109 doc = JsonDocument . Parse ( json ) ;
110110 accessToken = doc . RootElement . GetProperty ( "access_token" ) . GetString ( ) ;
111111
112- Console . WriteLine ( "Access Token : " + accessToken ) ;
112+ ioConsole . WriteLine ( "Access Token : " + accessToken ) ;
113113 }
114114 else
115115 {
@@ -136,22 +136,22 @@ public static async Task RunAsync()
136136 switch ( input )
137137 {
138138 case "c" :
139- certificate = SelectCertificateWithUI ( rootCertSubjects ) ;
139+ certificate = SelectCertificateWithUI ( ioConsole , rootCertSubjects ) ;
140140 x5c = BuildChainX5C ( certificate ) ;
141141 break ;
142142
143143 case "f" :
144- certificate = LoadCertificateFromPfx ( IOConsole . UploadedPfxBytes , IOConsole . PfxPassword ) ;
144+ certificate = LoadCertificateFromPfx ( ioConsole . UploadedPfxBytes , ioConsole . PfxPassword ) ;
145145 x5c = BuildChainX5C ( certificate ) ;
146146 break ;
147147
148148 case "e" :
149- Console . WriteLine ( "Entra ID (from https://entraid.aas-voyager.com/)?" ) ;
150- entraid = Console . ReadLine ( ) ?? "" ;
149+ ioConsole . WriteLine ( "Entra ID (from https://entraid.aas-voyager.com/)?" ) ;
150+ entraid = ioConsole . ReadLine ( ) ?? "" ;
151151 break ;
152152
153153 case "i" :
154- entraid = await AcquireEntraIdInteractiveAsync ( clientId , tenant , scopes ) ;
154+ entraid = await AcquireEntraIdInteractiveAsync ( ioConsole , clientId , tenant , scopes ) ;
155155 break ;
156156 }
157157
@@ -165,26 +165,26 @@ public static async Task RunAsync()
165165 }
166166
167167 // JWT erstellen und Access-Token anfordern
168- var ( jwt , accessTok ) = await BuildAndRequestTokenAsync ( client , tokenEndpoint , entraid , x5c , certificate , email ) ;
168+ var ( jwt , accessTok ) = await BuildAndRequestTokenAsync ( ioConsole , client , tokenEndpoint , entraid , x5c , certificate , email ) ;
169169 accessToken = accessTok ;
170170 }
171171
172172 var target = "" ;
173173 if ( exchange != "0" )
174174 {
175- Console . WriteLine ( "Token Exchange" ) ;
175+ ioConsole . WriteLine ( "Token Exchange" ) ;
176176 configUrlList = [
177177 "https://iam-security-training.com/sts"
178178 ] ;
179179 for ( var i = 0 ; i < configUrlList . Count ; i ++ )
180180 {
181- Console . WriteLine ( i + ": " + configUrlList [ i ] ) ;
181+ ioConsole . WriteLine ( i + ": " + configUrlList [ i ] ) ;
182182 }
183183 input = "0" ;
184184 if ( configUrlList . Count > 1 )
185185 {
186- Console . WriteLine ( "Enter index: " ) ;
187- input = Console . ReadLine ( ) ;
186+ ioConsole . WriteLine ( "Enter index: " ) ;
187+ input = ioConsole . ReadLine ( ) ;
188188 }
189189 configUrl = configUrlList [ Convert . ToInt32 ( input ?? "0" ) ] ;
190190
@@ -196,13 +196,13 @@ public static async Task RunAsync()
196196 ] ;
197197 for ( var i = 0 ; i < configUrlList . Count ; i ++ )
198198 {
199- Console . WriteLine ( i + ": " + configUrlList [ i ] ) ;
199+ ioConsole . WriteLine ( i + ": " + configUrlList [ i ] ) ;
200200 }
201201 input = "0" ;
202202 if ( configUrlList . Count > 1 )
203203 {
204- Console . WriteLine ( "Enter index: " ) ;
205- input = Console . ReadLine ( ) ;
204+ ioConsole . WriteLine ( "Enter index: " ) ;
205+ input = ioConsole . ReadLine ( ) ;
206206 }
207207 target = configUrlList [ Convert . ToInt32 ( input ?? "0" ) ] ;
208208
@@ -231,7 +231,7 @@ public static async Task RunAsync()
231231 if ( doc . RootElement . TryGetProperty ( "access_token" , out var tokenElement ) )
232232 {
233233 accessToken = tokenElement . GetString ( ) ;
234- Console . WriteLine ( "Access Token: " + accessToken ) ;
234+ ioConsole . WriteLine ( "Access Token: " + accessToken ) ;
235235
236236 using var httpClient = new HttpClient ( handler ) ;
237237 var jwksJson = await httpClient . GetStringAsync ( $ "{ configUrl } /jwks") ;
@@ -264,11 +264,11 @@ public static async Task RunAsync()
264264 try
265265 {
266266 handler2 . ValidateToken ( accessToken , validationParams , out _ ) ;
267- Console . WriteLine ( "Token is valid" ) ;
267+ ioConsole . WriteLine ( "Token is valid" ) ;
268268 }
269269 catch ( Exception ex )
270270 {
271- Console . WriteLine ( $ "Validation failed: { ex . Message } ") ;
271+ ioConsole . WriteLine ( $ "Validation failed: { ex . Message } ") ;
272272 }
273273 }
274274 }
@@ -278,20 +278,20 @@ public static async Task RunAsync()
278278 handler = new HttpClientHandler { DefaultProxyCredentials = CredentialCache . DefaultCredentials } ;
279279 client = new HttpClient ( handler ) ;
280280
281- Console . WriteLine ( "Token Exchange" ) ;
281+ ioConsole . WriteLine ( "Token Exchange" ) ;
282282 configUrlList = [
283283 "https://demo2.digital-twin.host/identity-management/realms/D4E/protocol/openid-connect" ,
284284 "https://integration.assetfox.apps.siemens.cloud/auth/realms/assetfox/protocol/openid-connect"
285285 ] ;
286286 for ( var i = 0 ; i < configUrlList . Count ; i ++ )
287287 {
288- Console . WriteLine ( i + ": " + configUrlList [ i ] ) ;
288+ ioConsole . WriteLine ( i + ": " + configUrlList [ i ] ) ;
289289 }
290290 input = "0" ;
291291 if ( configUrlList . Count > 1 )
292292 {
293- Console . WriteLine ( "Enter index: " ) ;
294- input = Console . ReadLine ( ) ;
293+ ioConsole . WriteLine ( "Enter index: " ) ;
294+ input = ioConsole . ReadLine ( ) ;
295295 }
296296 configUrl = configUrlList [ Convert . ToInt32 ( input ?? "0" ) ] ;
297297
@@ -320,16 +320,16 @@ public static async Task RunAsync()
320320 if ( doc . RootElement . TryGetProperty ( "access_token" , out var tokenElement ) )
321321 {
322322 accessToken = tokenElement . GetString ( ) ;
323- Console . WriteLine ( "Access Token: " + accessToken ) ;
323+ ioConsole . WriteLine ( "Access Token: " + accessToken ) ;
324324 }
325325 }
326326 }
327327
328- private static X509Certificate2 ? SelectCertificateWithUI ( List < string > rootCertSubjects )
328+ private X509Certificate2 ? SelectCertificateWithUI ( MyApp . IOConsole ioConsole , List < string > rootCertSubjects )
329329 {
330330 if ( ! OperatingSystem . IsWindows ( ) )
331331 {
332- Console . WriteLine ( "Certificate UI is only available on Windows. Please use mode 'f' (PFX)." ) ;
332+ ioConsole . WriteLine ( "Certificate UI is only available on Windows. Please use mode 'f' (PFX)." ) ;
333333 return null ;
334334 }
335335
@@ -371,7 +371,7 @@ public static async Task RunAsync()
371371 return null ;
372372 }
373373
374- private static X509Certificate2 ? LoadCertificateFromPfx ( byte [ ] ? uploadedPfx , string ? password )
374+ private X509Certificate2 ? LoadCertificateFromPfx ( byte [ ] ? uploadedPfx , string ? password )
375375 {
376376 if ( uploadedPfx is { Length : > 0 } && password != "" )
377377 return new X509Certificate2 ( uploadedPfx , password ?? "" ) ;
@@ -395,7 +395,7 @@ public static async Task RunAsync()
395395 return xc ;
396396 }
397397
398- private static string [ ] ? BuildChainX5C ( X509Certificate2 ? certificate )
398+ private string [ ] ? BuildChainX5C ( X509Certificate2 ? certificate )
399399 {
400400 if ( certificate == null ) return null ;
401401
@@ -416,11 +416,11 @@ public static async Task RunAsync()
416416 return x5c . ToArray ( ) ;
417417 }
418418
419- private static async Task < string > AcquireEntraIdInteractiveAsync ( string clientId , string tenant , string [ ] scopes )
419+ private async Task < string > AcquireEntraIdInteractiveAsync ( MyApp . IOConsole ioConsole , string clientId , string tenant , string [ ] scopes )
420420 {
421421 if ( ! OperatingSystem . IsWindows ( ) )
422422 {
423- Console . WriteLine ( "Interaktive EntraID is only available on Windows; please use mode 'e' and copy token." ) ;
423+ ioConsole . WriteLine ( "Interaktive EntraID is only available on Windows; please use mode 'e' and copy token." ) ;
424424 return "" ;
425425 }
426426
@@ -438,7 +438,7 @@ private static async Task<string> AcquireEntraIdInteractiveAsync(string clientId
438438 return result . IdToken ;
439439 }
440440
441- private static string ? ExtractEmailFromCert ( X509Certificate2 certificate )
441+ private string ? ExtractEmailFromCert ( X509Certificate2 certificate )
442442 {
443443 var email = certificate . GetNameInfo ( X509NameType . EmailName , false ) ;
444444 if ( ! string . IsNullOrEmpty ( email ) ) return email ;
@@ -448,7 +448,8 @@ private static async Task<string> AcquireEntraIdInteractiveAsync(string clientId
448448 return match ? . Split ( '=' ) [ 1 ] ;
449449 }
450450
451- private static async Task < ( string jwt , string accessToken ) > BuildAndRequestTokenAsync (
451+ private async Task < ( string jwt , string accessToken ) > BuildAndRequestTokenAsync (
452+ MyApp . IOConsole ioConsole ,
452453 HttpClient client ,
453454 string ? tokenEndpoint ,
454455 string entraid ,
@@ -518,8 +519,8 @@ private static async Task<string> AcquireEntraIdInteractiveAsync(string clientId
518519 if ( doc . RootElement . TryGetProperty ( "access_token" , out var tokenElement ) )
519520 {
520521 accessToken = tokenElement . GetString ( ) ;
521- Console . WriteLine ( "Access Token: " + accessToken ) ;
522- Console . WriteLine ( "" ) ;
522+ ioConsole . WriteLine ( "Access Token: " + accessToken ) ;
523+ ioConsole . WriteLine ( "" ) ;
523524 }
524525
525526 return ( jwt , accessToken ?? "" ) ;
0 commit comments