Created
April 21, 2022 16:14
-
-
Save w3bward/0364bce98bd8a8d0123539694d6ff907 to your computer and use it in GitHub Desktop.
A PowerShell class to implement the interface Microsoft.SqlServer.Management.Common.IRenewableToken
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ResourceUrl = 'https://database.windows.net' | |
class SmoRenewableToken : Microsoft.SqlServer.Management.Common.IRenewableToken { | |
[string]$Resource | |
[string]$Tenant | |
[DateTimeOffset]$TokenExpiry | |
[string]$UserId | |
[string] hidden $Token | |
SmoRenewableToken( | |
[System.Uri]$ResourceUrl | |
) { | |
$AzAccessToken = Get-AzAccessToken -ResourceUrl $ResourceUrl | |
$this.Tenant = $AzAccessToken.TenantId | |
$this.Resource = $ResourceUrl | |
$this.TokenExpiry = $AzAccessToken.ExpiresOn | |
$this.UserId = $AzAccessToken.UserId | |
$this.Token = $AzAccessToken.Token | |
} | |
[string]GetAccessToken() { | |
if ($this.TokenExpiry.AddMinutes(-10) -gt (Get-Date)) { | |
return $this.Token | |
} | |
$AzAccessToken = Get-AzAccessToken -ResourceUrl $this.Resource | |
$this.TokenExpiry = $AzAccessToken.ExpiresOn | |
$this.Tenant = $AzAccessToken.TenantId | |
$this.UserId = $AzAccessToken.UserId | |
return $AzAccessToken.Token | |
} | |
<# | |
Despite some documentation stating otherwise, it appears that PowerShell v5.1 does not automatically implement accessors (get/set) that the interface | |
recognizes. When the following methods were omitted, this function threw errors along the lines of "get_TokenExpiry... does not have an implementation" | |
At some point, this issue was fixed in PowerShell Core, and testing in 7.2.1 revealed that these lines could be safely omitted, but their presence does | |
not appear to cause any problems either. For the sake of compatibility, they stay. | |
#> | |
[DateTimeOffset]get_TokenExpiry() { | |
return $this.TokenExpiry | |
} | |
[string]get_Resource() { | |
return $this.Resource | |
} | |
[string]get_Tenant() { | |
return $this.Tenant | |
} | |
[string]get_UserId() { | |
return $this.UserId | |
} | |
} | |
New-Object SmoRenewableToken -ArgumentList $ResourceUrl |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment