Skip to content

Commit cdcd983

Browse files
author
Greg Brownstein
committed
add vc access token paramset
1 parent b9dd57d commit cdcd983

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

VenafiPS/Public/New-VenafiSession.ps1

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ function New-VenafiSession {
3939
You can either provide a String, SecureString, or PSCredential.
4040
If providing a credential, the username is not used.
4141
42-
.PARAMETER Endpoint
43-
4442
.PARAMETER Jwt
4543
JSON web token.
4644
Available in TLSPDC v22.4 and later.
@@ -79,6 +77,11 @@ function New-VenafiSession {
7977
You can either provide a String, SecureString, or PSCredential.
8078
If providing a credential, the username is not used.
8179
80+
.PARAMETER VcAccessToken
81+
Provide an existing access token to create a TLSPC session.
82+
You can either provide a String, SecureString, or PSCredential.
83+
If providing a credential, the username is not used.
84+
8285
.PARAMETER VcRegion
8386
TLSPC region to connect to, tab-ahead values provided. Defaults to 'us'.
8487
@@ -307,6 +310,7 @@ function New-VenafiSession {
307310
[psobject] $VcKey,
308311

309312
[Parameter(ParameterSetName = 'Vc')]
313+
[Parameter(ParameterSetName = 'VcAccessToken')]
310314
[ValidateScript(
311315
{
312316
if ( $_ -notin ($script:VcRegions).Keys ) {
@@ -317,6 +321,10 @@ function New-VenafiSession {
317321
)]
318322
[string] $VcRegion = 'us',
319323

324+
[Parameter(Mandatory, ParameterSetName = 'VcAccessToken')]
325+
[ValidateNotNullOrEmpty()]
326+
[psobject] $VcAccessToken,
327+
320328
[Parameter(Mandatory, ParameterSetName = 'VcToken')]
321329
[string] $VcEndpoint,
322330

@@ -555,6 +563,20 @@ function New-VenafiSession {
555563
}
556564
}
557565

566+
'VcAccessToken' {
567+
$newSession.Platform = 'VC'
568+
$newSession.Server = ($script:VcRegions).$VcRegion
569+
$newSession | Add-Member @{'Token' = [PSCustomObject]@{
570+
AccessToken = $null
571+
}
572+
}
573+
574+
$newSession.Token.AccessToken = if ( $VcAccessToken -is [string] ) { New-Object System.Management.Automation.PSCredential('AccessToken', ($VcAccessToken | ConvertTo-SecureString -AsPlainText -Force)) }
575+
elseif ($VcAccessToken -is [pscredential]) { $VcAccessToken }
576+
elseif ($VcAccessToken -is [securestring]) { New-Object System.Management.Automation.PSCredential('AccessToken', $VcAccessToken) }
577+
else { throw 'Unsupported type for -VcAccessToken. Provide either a String, SecureString, or PSCredential.' }
578+
}
579+
558580
'VaultVcKey' {
559581
$keySecret = Get-Secret -Name $VaultVcKeyName -Vault 'VenafiPS' -ErrorAction SilentlyContinue
560582
if ( -not $keySecret ) {
@@ -590,10 +612,10 @@ function New-VenafiSession {
590612
else {
591613

592614
# user might not have access to this api, eg. service account
593-
$user = Invoke-VenafiRestMethod -UriLeaf 'useraccounts' -VenafiSession $newSession -ErrorAction SilentlyContinue
594-
if ( $user ) {
615+
$me = Invoke-VenafiRestMethod -UriLeaf 'useraccounts' -VenafiSession $newSession -ErrorAction SilentlyContinue
616+
if ( $me.user ) {
595617
$newSession | Add-Member @{
596-
User = $user | Select-Object -ExpandProperty user | Select-Object @{
618+
User = $me.user | Select-Object @{
597619
'n' = 'userId'
598620
'e' = {
599621
$_.Id

0 commit comments

Comments
 (0)