11using Microsoft . Rest ;
22using System ;
3+ using System . Linq ;
34using System . Management . Automation ;
5+ using System . Net . Http ;
46using UiPath . PowerShell . Models ;
57using UiPath . PowerShell . Util ;
68using UiPath . Web . Client ;
@@ -29,6 +31,7 @@ public class GetAuthToken: UiPathCmdlet
2931 {
3032 private const string UserPasswordSet = "UserPassword" ;
3133 private const string WindowsCredentialsSet = "WindowsCredentials" ;
34+ private const string UnauthenticatedSet = "Unauthenticated" ;
3235
3336 [ Parameter ( Mandatory = true , Position = 0 ) ]
3437 public string URL { get ; set ; }
@@ -45,34 +48,74 @@ public class GetAuthToken: UiPathCmdlet
4548 [ Parameter ( Mandatory = true , ParameterSetName = WindowsCredentialsSet ) ]
4649 public SwitchParameter WindowsCredentials { get ; set ; }
4750
51+ [ Parameter ( Mandatory = true , ParameterSetName = UnauthenticatedSet ) ]
52+ public SwitchParameter Unauthenticated { get ; set ; }
53+
4854 [ Parameter ]
4955 public SwitchParameter Session { get ; set ; }
5056
5157 protected override void ProcessRecord ( )
5258 {
53- AuthToken authToken = null ;
54- if ( ParameterSetName == UserPasswordSet )
59+ try
5560 {
56- authToken = GetUserToken ( ) ;
61+ AuthToken authToken = null ;
62+ if ( ParameterSetName == UserPasswordSet )
63+ {
64+ authToken = GetUserToken ( ) ;
65+ }
66+ else if ( ParameterSetName == WindowsCredentialsSet )
67+ {
68+ authToken = GetWindowsToken ( ) ;
69+ }
70+ else if ( ParameterSetName == UnauthenticatedSet )
71+ {
72+ authToken = GetUnauthenticatedToken ( ) ;
73+ }
74+
75+ GetServerVersion ( authToken ) ;
76+
77+ if ( Session . IsPresent )
78+ {
79+ AuthenticatedCmdlet . SetAuthToken ( authToken ) ;
80+ }
81+
82+ WriteObject ( authToken ) ;
5783 }
58- else if ( ParameterSetName == WindowsCredentialsSet )
84+ catch ( Exception e )
5985 {
60- authToken = GetWindowsToken ( ) ;
86+ WriteVerbose ( e . ToString ( ) ) ;
6187 }
62- if ( Session . IsPresent )
88+ }
89+
90+ private void GetServerVersion ( AuthToken authToken )
91+ {
92+ using ( var api = AuthenticatedCmdlet . MakeApi ( authToken ) )
6393 {
64- AuthenticatedCmdlet . SetAuthToken ( authToken ) ;
94+ api . MakeHttpRequest ( HttpMethod . Get , "/odata/$metadata" , null , out var response , out var headers ) ;
95+ if ( headers . TryGetValues ( "api-supported-versions" , out var values ) )
96+ {
97+ authToken . ApiVersion = values . First ( ) ;
98+ }
6599 }
100+ }
66101
67- WriteObject ( authToken ) ;
102+ private AuthToken GetUnauthenticatedToken ( )
103+ {
104+ return new AuthToken
105+ {
106+ URL = URL ,
107+ WindowsCredentials = false ,
108+ Authenticated = false
109+ } ;
68110 }
69111
70112 private AuthToken GetWindowsToken ( )
71113 {
72114 return new AuthToken
73115 {
74116 URL = URL ,
75- WindowsCredentials = true
117+ WindowsCredentials = true ,
118+ Authenticated = true
76119 } ;
77120 }
78121
@@ -97,7 +140,8 @@ private AuthToken GetUserToken()
97140 {
98141 Token = token ,
99142 URL = URL ,
100- WindowsCredentials = false
143+ WindowsCredentials = false ,
144+ Authenticated = true
101145 } ;
102146
103147 }
0 commit comments