Skip to content

Commit 9533bc2

Browse files
committed
fix: tenant id detection from factory
1 parent 58aefe6 commit 9533bc2

3 files changed

Lines changed: 49 additions & 27 deletions

File tree

Commands/Public/New-ExoConnection.ps1

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,32 @@ param
6666

6767
process
6868
{
69+
if($authenticationFactory -is [string])
70+
{
71+
$f = Get-AadAuthenticationFactory -Name $authenticationFactory
72+
if($null -eq $f)
73+
{
74+
throw (new-object ExoHelper.ExoException([System.Net.HttpStatusCode]::BadRequest, 'ExoMissingAuthenticationFactory', 'ExoInitializationError', "Factory with name $authenticationFactory not found"))
75+
}
76+
}
77+
else
78+
{
79+
$f = $authenticationFactory
80+
}
81+
if([string]::IsNullOrEmpty($tenantId) )
82+
{
83+
$tenantId = $f.tenantId
84+
}
85+
if([string]::IsNullOrEmpty($TenantId))
86+
{
87+
throw (new-object ExoHelper.ExoException([System.Net.HttpStatusCode]::BadRequest, 'ExoMissingTenantId', 'ExoInitializationError', 'TenantId is not specified and cannot be determined automatically - please specify TenantId parameter'))
88+
}
89+
6990
$Connection = [PSCustomObject]@{
7091
PSTypeName = "ExoHelper.Connection"
71-
AuthenticationFactory = $AuthenticationFactory
92+
AuthenticationFactory = $f
7293
ConnectionId = [Guid]::NewGuid().ToString()
73-
TenantId = $null
94+
TenantId = $tenantId
7495
AnchorMailbox = $null
7596
ConnectionUri = $null
7697
IsIPPS = $IPPS.IsPresent
@@ -79,18 +100,8 @@ param
79100
}
80101
$Connection.HttpClient.DefaultRequestHeaders.Add("User-Agent", "ExoHelper")
81102
$Connection.HttpClient.Timeout = $DefaultTimeout
82-
83103
#explicitly authenticate when establishing connection to catch any authentication problems early
84104
Get-ExoToken -Connection $Connection | Out-Null
85-
if([string]::IsNullOrEmpty($TenantId))
86-
{
87-
$TenantId = $AuthenticationFactory.TenantId
88-
}
89-
if([string]::IsNullOrEmpty($TenantId))
90-
{
91-
throw (new-object ExoHelper.ExoException([System.Net.HttpStatusCode]::BadRequest, 'ExoMissingTenantId', 'ExoInitializationError', 'TenantId is not specified and cannot be determined automatically - please specify TenantId parameter'))
92-
}
93-
$Connection.TenantId = $TenantId
94105

95106
if($IPPS)
96107
{

Module/ExoHelper/ExoHelper.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = 'ExoHelper.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '3.0.4'
15+
ModuleVersion = '3.0.5'
1616

1717
# Supported PSEditions
1818
CompatiblePSEditions = @('Desktop', 'Core')

Module/ExoHelper/ExoHelper.psm1

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ This command creates connection for IPPS REST API, retrieves list of sensitivity
252252
}
253253
else
254254
{
255-
Write-Warning "Received non-JSON response with http status $($response.StatusCode): $($response.content.Headers.ContentType.MediaType)"
255+
Write-Warning "Received non-JSON response with http status $($response.StatusCode): $payload"
256256
$responseData = $payload
257257
}
258258
}
@@ -298,7 +298,7 @@ This command creates connection for IPPS REST API, retrieves list of sensitivity
298298
#request failed
299299
$ex = $null
300300
$exceptionType = $null
301-
$response.Headers.TryGetValues('X-ExceptionType', [ref]$exceptionType)
301+
$response.Headers.TryGetValues('X-ExceptionType', [ref]$exceptionType) | out-null
302302
if($response.StatusCode -notin $RetryableStatusCodes `
303303
-and $exceptionType -notin @('UnableToWriteToAadException') `
304304
-and $responseData -notlike 'You have reached the maximum number of concurrent requests per tenant. Please wait and try again*' `
@@ -458,11 +458,32 @@ param
458458

459459
process
460460
{
461+
if($authenticationFactory -is [string])
462+
{
463+
$f = Get-AadAuthenticationFactory -Name $authenticationFactory
464+
if($null -eq $f)
465+
{
466+
throw (new-object ExoHelper.ExoException([System.Net.HttpStatusCode]::BadRequest, 'ExoMissingAuthenticationFactory', 'ExoInitializationError', "Factory with name $authenticationFactory not found"))
467+
}
468+
}
469+
else
470+
{
471+
$f = $authenticationFactory
472+
}
473+
if([string]::IsNullOrEmpty($tenantId) )
474+
{
475+
$tenantId = $f.tenantId
476+
}
477+
if([string]::IsNullOrEmpty($TenantId))
478+
{
479+
throw (new-object ExoHelper.ExoException([System.Net.HttpStatusCode]::BadRequest, 'ExoMissingTenantId', 'ExoInitializationError', 'TenantId is not specified and cannot be determined automatically - please specify TenantId parameter'))
480+
}
481+
461482
$Connection = [PSCustomObject]@{
462483
PSTypeName = "ExoHelper.Connection"
463-
AuthenticationFactory = $AuthenticationFactory
484+
AuthenticationFactory = $f
464485
ConnectionId = [Guid]::NewGuid().ToString()
465-
TenantId = $null
486+
TenantId = $tenantId
466487
AnchorMailbox = $null
467488
ConnectionUri = $null
468489
IsIPPS = $IPPS.IsPresent
@@ -471,18 +492,8 @@ param
471492
}
472493
$Connection.HttpClient.DefaultRequestHeaders.Add("User-Agent", "ExoHelper")
473494
$Connection.HttpClient.Timeout = $DefaultTimeout
474-
475495
#explicitly authenticate when establishing connection to catch any authentication problems early
476496
Get-ExoToken -Connection $Connection | Out-Null
477-
if([string]::IsNullOrEmpty($TenantId))
478-
{
479-
$TenantId = $AuthenticationFactory.TenantId
480-
}
481-
if([string]::IsNullOrEmpty($TenantId))
482-
{
483-
throw (new-object ExoHelper.ExoException([System.Net.HttpStatusCode]::BadRequest, 'ExoMissingTenantId', 'ExoInitializationError', 'TenantId is not specified and cannot be determined automatically - please specify TenantId parameter'))
484-
}
485-
$Connection.TenantId = $TenantId
486497

487498
if($IPPS)
488499
{

0 commit comments

Comments
 (0)