Skip to content

Commit 16ed464

Browse files
committed
Windows authentication
1 parent 0be059b commit 16ed464

7 files changed

+106
-32
lines changed

Generate-CmdletDocs.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ foreach($cmdlet in $cmdlets)
1212
{
1313
$file = "docs\$($cmdlet.Name).md"
1414
"``````PowerShell" | Out-File $file -Encoding utf8
15-
Get-Help $cmdlet.Name | Out-File $file -Encoding utf8 -Append
15+
Get-Help $cmdlet.Name -example | Out-File $file -Encoding utf8 -Append
1616
"``````" | Out-File $file -Encoding utf8 -Append
1717
}

UiPath.PowerShell/Cmdlets/GetAuthToken.cs

+51-10
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,73 @@ namespace UiPath.PowerShell.Cmdlets
1111
/// <summary>
1212
/// <para type="synopsis">Obtains an UiPath authentication token</para>
1313
/// <para type="description">The authentication token is needed for other UiPath Powershell cmdlets.</para>
14+
/// <example>
15+
/// <code>Get-UiPathAuthToken -URL https://platform.uipath.com -Username &lt;myuser&gt; -Password &lt;mypassword&gt;</code>
16+
/// <para>Connect to UiPath public platform Orchestrator, using user name and password.</para>
17+
/// </example>
18+
/// <example>
19+
/// <code>Get-UiPathAuthToken -URL https://platform.uipath.com -Username &lt;myuser&gt; -Password &lt;mypassword&gt; -Session</code>
20+
/// <para>Connect to UiPath public platform Orchestrator, using user name and password and save the token for the current session.</para>
21+
/// </example>
22+
/// <example>
23+
/// <code>Get-UiPathAuthToken -URL https://uipath.corpnet -WindowsCredentials -Session</code>
24+
/// <para>Connect to a private Orchestrator with Windows enabled, using current Windows credentials and save the token for current session.</para>
25+
/// </example>
1426
/// </summary>
1527
[Cmdlet(VerbsCommon.Get, Nouns.AuthToken)]
1628
public class GetAuthToken: UiPathCmdlet
1729
{
30+
private const string UserPasswordSet = "UserPassword";
31+
private const string WindowsCredentialsSet = "WindowsCredentials";
32+
1833
[Parameter(Mandatory = true, Position = 0)]
1934
public string URL { get; set; }
2035

2136
[Parameter(Mandatory = false)]
2237
public string TenantName { get; set; }
2338

24-
[Parameter(Mandatory = true)]
39+
[Parameter(Mandatory = true, ParameterSetName = UserPasswordSet)]
2540
public string Username { get; set; }
2641

27-
[Parameter(Mandatory = true)]
42+
[Parameter(Mandatory = true, ParameterSetName = UserPasswordSet)]
2843
public string Password { get; set; }
2944

45+
[Parameter(Mandatory = true, ParameterSetName = WindowsCredentialsSet)]
46+
public SwitchParameter WindowsCredentials { get; set; }
47+
3048
[Parameter]
3149
public SwitchParameter Session { get; set; }
3250

3351
protected override void ProcessRecord()
3452
{
53+
AuthToken authToken = null;
54+
if (ParameterSetName == UserPasswordSet)
55+
{
56+
authToken = GetUserToken();
57+
}
58+
else if (ParameterSetName == WindowsCredentialsSet)
59+
{
60+
authToken = GetWindowsToken();
61+
}
62+
if (Session.IsPresent)
63+
{
64+
AuthenticatedCmdlet.SetAuthToken(authToken);
65+
}
66+
67+
WriteObject(authToken);
68+
}
69+
70+
private AuthToken GetWindowsToken()
71+
{
72+
return new AuthToken
73+
{
74+
URL = URL,
75+
WindowsCredentials = true
76+
};
77+
}
78+
79+
private AuthToken GetUserToken()
80+
{
3581
var creds = new BasicAuthenticationCredentials();
3682
using (var client = new UiPathWebApi(creds)
3783
{
@@ -47,18 +93,13 @@ protected override void ProcessRecord()
4793
var response = HandleHttpOperationException(() => client.Account.Authenticate(loginModel));
4894
var token = (string) response.Result;
4995

50-
var authToken = new AuthToken
96+
return new AuthToken
5197
{
5298
Token = token,
53-
URL = URL
99+
URL = URL,
100+
WindowsCredentials = false
54101
};
55102

56-
if (Session.IsPresent)
57-
{
58-
AuthenticatedCmdlet.SetAuthToken(authToken);
59-
}
60-
61-
WriteObject(authToken);
62103
}
63104
}
64105
}

UiPath.PowerShell/Models/AuthToken.cs

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
7-
namespace UiPath.PowerShell.Models
1+
namespace UiPath.PowerShell.Models
82
{
93
/// <summary>
104
/// The Token needed to authenticate UiPath cmdlets
@@ -14,5 +8,7 @@ public class AuthToken
148
public string URL { get; internal set; }
159

1610
public string Token { get; internal set; }
11+
12+
public bool WindowsCredentials { get; internal set; }
1713
}
1814
}

UiPath.PowerShell/UiPath.PowerShell.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<Reference Include="RestSharp, Version=106.2.1.0, Culture=neutral, PublicKeyToken=598062e77f915f75, processorArchitecture=MSIL">
4545
<HintPath>..\packages\RestSharp.106.2.1\lib\net452\RestSharp.dll</HintPath>
4646
</Reference>
47+
<Reference Include="System.Net.Http.WebRequest" />
4748
<Reference Include="System.ValueTuple, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
4849
<HintPath>..\packages\System.ValueTuple.4.4.0\lib\net461\System.ValueTuple.dll</HintPath>
4950
</Reference>
@@ -121,6 +122,7 @@
121122
<Compile Include="Util\FilteredBaseCmdlet.cs" />
122123
<Compile Include="Util\FilteredIdCmdlet.cs" />
123124
<Compile Include="Util\EditCmdlet.cs" />
125+
<Compile Include="Util\NetworkAuthenticationCredentials.cs" />
124126
<Compile Include="Util\SetParameterAttribute.cs" />
125127
<Compile Include="Util\UiPathCmdlet.cs" />
126128
<Compile Include="Models\Asset.cs" />

UiPath.PowerShell/Util/AuthenticatedCmdlet.cs

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using Microsoft.Rest;
22
using System;
33
using System.Management.Automation;
4+
using System.Net;
45
using UiPath.PowerShell.Models;
5-
using UiPath.PowerShell.Util;
66
using UiPath.Web.Client;
77

88
namespace UiPath.PowerShell.Util
@@ -27,7 +27,21 @@ protected UiPathWebApi Api
2727
{
2828
throw new Exception("An authentication token is required. Specify a value for -AuthToken on the cmdlet or call Set-Authentication for the session");
2929
}
30-
_api = new UiPathWebApi(new TokenCredentials(authToken.Token))
30+
31+
ServiceClientCredentials creds = null;
32+
if (authToken.WindowsCredentials)
33+
{
34+
creds = new NetworkAuthenticationCredentials
35+
{
36+
Credentials = CredentialCache.DefaultNetworkCredentials
37+
};
38+
}
39+
else
40+
{
41+
creds = new TokenCredentials(authToken.Token);
42+
}
43+
44+
_api = new UiPathWebApi(creds)
3145
{
3246
BaseUri = new Uri(authToken.URL)
3347
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Microsoft.Rest;
2+
using System.Linq;
3+
using System.Net;
4+
using System.Net.Http;
5+
6+
namespace UiPath.PowerShell.Util
7+
{
8+
internal class NetworkAuthenticationCredentials: ServiceClientCredentials
9+
{
10+
public ICredentials Credentials { get; set; }
11+
12+
public override void InitializeServiceClient<T>(ServiceClient<T> client)
13+
{
14+
var handler = client.HttpMessageHandlers.First(h => h is WebRequestHandler) as WebRequestHandler;
15+
16+
handler.Credentials = Credentials;
17+
}
18+
}
19+
}

docs/Get-UiPathAuthToken.md

+14-12
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,23 @@ SYNOPSIS
77
Obtains an UiPath authentication token
88
99
10-
SYNTAX
11-
Get-UiPathAuthToken [-URL] <string> -Password <string> -Username <string> [-Session <SwitchParameter>]
12-
[-TenantName <string>] [<CommonParameters>]
10+
---------- EXAMPLE 1 ----------
1311
12+
Get-UiPathAuthToken -URL https://platform.uipath.com -Username <myuser> -Password <mypassword>
1413
15-
DESCRIPTION
16-
The authentication token is needed for other UiPath Powershell cmdlets.
14+
Connect to UiPath public platform Orchestrator, using user name and password.
15+
---------- EXAMPLE 2 ----------
1716
18-
19-
RELATED LINKS
20-
21-
REMARKS
22-
To see the examples, type: "get-help Get-UiPathAuthToken -examples".
23-
For more information, type: "get-help Get-UiPathAuthToken -detailed".
24-
For technical information, type: "get-help Get-UiPathAuthToken -full".
17+
Get-UiPathAuthToken -URL https://platform.uipath.com -Username <myuser> -Password <mypassword> -Session
18+
19+
Connect to UiPath public platform Orchestrator, using user name and password and save the token for the current
20+
session.
21+
---------- EXAMPLE 3 ----------
22+
23+
Get-UiPathAuthToken -URL https://uipath.corpnet -WindowsCredentials -Session
24+
25+
Connect to a private Orchestrator with Windows enabled, using current Windows credentials and save the token for
26+
current session.
2527
2628
2729

0 commit comments

Comments
 (0)