From 1c5948fa2b97848b9ce42c62ddc27e15ccddde2b Mon Sep 17 00:00:00 2001 From: Dan Anstis Date: Wed, 7 Nov 2018 12:44:32 +1000 Subject: [PATCH 1/6] Add new function --- PSF.psd1 | 2 +- PSF.psm1 | 1 + functions/Connect-AadWithRmContext.ps1 | 26 ++++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 functions/Connect-AadWithRmContext.ps1 diff --git a/PSF.psd1 b/PSF.psd1 index eba8631..c1c4161 100644 --- a/PSF.psd1 +++ b/PSF.psd1 @@ -69,7 +69,7 @@ Description = 'PowerShell Functions Module' # NestedModules = @() # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. -FunctionsToExport = @('Get-oAuthToken', 'Get-RandomPassword') +FunctionsToExport = @('Get-oAuthToken', 'Get-RandomPassword', 'Connect-AadWithRmContext') # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. CmdletsToExport = @() diff --git a/PSF.psm1 b/PSF.psm1 index e2120f1..e0df1d3 100644 --- a/PSF.psm1 +++ b/PSF.psm1 @@ -2,3 +2,4 @@ . $PSScriptRoot\functions\Get-oAuthToken.ps1 . $PSScriptRoot\functions\Get-RandomPassword.ps1 +. $PSScriptRoot\functions\Connect-AadWithRmContext.ps1 diff --git a/functions/Connect-AadWithRmContext.ps1 b/functions/Connect-AadWithRmContext.ps1 new file mode 100644 index 0000000..afb5829 --- /dev/null +++ b/functions/Connect-AadWithRmContext.ps1 @@ -0,0 +1,26 @@ +function Connect-AadWithRmContext { + <# + .SYNOPSIS + Connect to Azure AD using an existing Azure RM Context. + .DESCRIPTION + This Function connects to the Microsoft AAD using an existing Azure RM context by + leveraging the exiting oAuth token for Azure RM. + .EXAMPLE + Connect-AadWithRmContext + .NOTES + Version 1.0.1 + #> + + $Context = Get-AzureRmContext + # Force context to grab a token for GraphAPI + Get-AzureRmADUser -UserPrincipalName $Context.Account.Id + + $CacheItems = $Context.TokenCache.ReadItems() + + $Token = ($CacheItems | Where-Object { $_.Resource -eq "https://graph.windows.net/" -and $_.TenantId -eq $Context.Tenant.Id }) + if ($Token.ExpiresOn -le [System.DateTime]::UtcNow) { + $AC = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext]::new("$($Context.Environment.ActiveDirectoryAuthority)$($Context.Tenant.Id)", $Token) + $Token = $AC.AcquireTokenByRefreshToken($Token.RefreshToken, "1950a258-227b-4e31-a9cf-717495945fc2", "https://graph.windows.net") + } + Connect-AzureAD -AadAccessToken $Token.AccessToken -AccountId $Context.Account.Id -TenantId $Context.Tenant.Id | Out-Null +} From 4f9f3a9e8a2c579bff7bfde74afb0e0394c0ca0f Mon Sep 17 00:00:00 2001 From: Dan Anstis Date: Wed, 7 Nov 2018 13:58:15 +1000 Subject: [PATCH 2/6] Update build script --- appveyor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index 0a809b6..a1ddf14 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -4,7 +4,7 @@ environment: secure: v711B0A/aGDr4wVCW3sYtaJJljC+rXipS2PENvcBC+qtOs4VXS9QWDTMJyQIKLTv install: - - choco install gitversion.portable -pre -y + - choco install gitversion.portable -y - rm C:\Tools\GitVersion\GitVersion.exe - ps: Install-PackageProvider -Name NuGet -MinimumVersion '2.8.5.201' -Force From 7ef3c355035457e47862ec05854586e01d1aa517 Mon Sep 17 00:00:00 2001 From: Dan Anstis Date: Wed, 7 Nov 2018 14:51:28 +1000 Subject: [PATCH 3/6] Check Gitversion version --- appveyor.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index a1ddf14..ffac007 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -4,8 +4,10 @@ environment: secure: v711B0A/aGDr4wVCW3sYtaJJljC+rXipS2PENvcBC+qtOs4VXS9QWDTMJyQIKLTv install: - - choco install gitversion.portable -y - - rm C:\Tools\GitVersion\GitVersion.exe + - gitversion /version + # - choco install gitversion.portable -y + # - rm C:\Tools\GitVersion\GitVersion.exe + # - gitversion /version - ps: Install-PackageProvider -Name NuGet -MinimumVersion '2.8.5.201' -Force before_build: From 521c5394e609339a38eb8c825ae0e6bd0971ad69 Mon Sep 17 00:00:00 2001 From: Dan Anstis Date: Mon, 7 Jan 2019 18:31:14 +1000 Subject: [PATCH 4/6] Add new Get-PublicIPAddress function --- PSF.psd1 | 2 +- PSF.psm1 | 1 + functions/Get-PublicIPAddress.ps1 | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 functions/Get-PublicIPAddress.ps1 diff --git a/PSF.psd1 b/PSF.psd1 index c1c4161..284ef56 100644 --- a/PSF.psd1 +++ b/PSF.psd1 @@ -69,7 +69,7 @@ Description = 'PowerShell Functions Module' # NestedModules = @() # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. -FunctionsToExport = @('Get-oAuthToken', 'Get-RandomPassword', 'Connect-AadWithRmContext') +FunctionsToExport = @('Get-oAuthToken', 'Get-RandomPassword', 'Connect-AadWithRmContext', 'Get-PublicIPAddress') # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. CmdletsToExport = @() diff --git a/PSF.psm1 b/PSF.psm1 index e0df1d3..008d540 100644 --- a/PSF.psm1 +++ b/PSF.psm1 @@ -3,3 +3,4 @@ . $PSScriptRoot\functions\Get-oAuthToken.ps1 . $PSScriptRoot\functions\Get-RandomPassword.ps1 . $PSScriptRoot\functions\Connect-AadWithRmContext.ps1 +. $PSScriptRoot\functions\Get-PublicIPAddress.ps1 diff --git a/functions/Get-PublicIPAddress.ps1 b/functions/Get-PublicIPAddress.ps1 new file mode 100644 index 0000000..85b2958 --- /dev/null +++ b/functions/Get-PublicIPAddress.ps1 @@ -0,0 +1,14 @@ +function Get-PublicIPAddress { + <# + .SYNOPSIS + Returns the local machines Public IP address. + .DESCRIPTION + Returns the local machines Public IP address. + .EXAMPLE + Get-PublicIPAddress + .NOTES + Version 1.0.0 + #> + + return (Resolve-DnsName -Name myip.opendns.com -Server resolver1.opendns.com).IPAddress +} From a8c540de6078bb6251d152f901ee2b34ce6ed0dc Mon Sep 17 00:00:00 2001 From: Dan Anstis Date: Mon, 7 Jan 2019 18:37:52 +1000 Subject: [PATCH 5/6] Bump version +semver: feature --- PSF.psd1 | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/PSF.psd1 b/PSF.psd1 index 284ef56..15b5eaa 100644 --- a/PSF.psd1 +++ b/PSF.psd1 @@ -3,7 +3,7 @@ # # Generated by: Colin Roche, Dan Anstis # -# Generated on: 13/08/2018 +# Generated on: 7/01/2019 # @{ @@ -12,7 +12,7 @@ RootModule = '.\PSF.psm1' # Version number of this module. -ModuleVersion = '0.1.0' +ModuleVersion = '0.4.1' # Supported PSEditions # CompatiblePSEditions = @() @@ -32,13 +32,13 @@ Copyright = '(c) 2018 Colin Roche, Dan Anstis. All rights reserved.' # Description of the functionality provided by this module Description = 'PowerShell Functions Module' -# Minimum version of the Windows PowerShell engine required by this module +# Minimum version of the PowerShell engine required by this module # PowerShellVersion = '' -# Name of the Windows PowerShell host required by this module +# Name of the PowerShell host required by this module # PowerShellHostName = '' -# Minimum version of the Windows PowerShell host required by this module +# Minimum version of the PowerShell host required by this module # PowerShellHostVersion = '' # Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only. @@ -69,7 +69,8 @@ Description = 'PowerShell Functions Module' # NestedModules = @() # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. -FunctionsToExport = @('Get-oAuthToken', 'Get-RandomPassword', 'Connect-AadWithRmContext', 'Get-PublicIPAddress') +FunctionsToExport = 'Get-oAuthToken', 'Get-RandomPassword', 'Connect-AadWithRmContext', + 'Get-PublicIPAddress' # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. CmdletsToExport = @() @@ -78,7 +79,7 @@ CmdletsToExport = @() # VariablesToExport = @() # Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. -AliasesToExport = @('gpw') +AliasesToExport = 'gpw' # DSC resources to export from this module # DscResourcesToExport = @() @@ -109,12 +110,18 @@ PrivateData = @{ # ReleaseNotes of this module # ReleaseNotes = '' + # Prerelease string of this module + # Prerelease = '' + + # Flag to indicate whether the module requires explicit user acceptance for install/update + # RequireLicenseAcceptance = $false + # External dependent modules of this module - # ExternalModuleDependencies = '' + # ExternalModuleDependencies = @() } # End of PSData hashtable -} # End of PrivateData hashtable + } # End of PrivateData hashtable # HelpInfo URI of this module # HelpInfoURI = '' From c6817e64c5155a60878a4cb97ca6b694a189fb8b Mon Sep 17 00:00:00 2001 From: Dan Anstis Date: Mon, 16 Dec 2019 15:57:00 +1000 Subject: [PATCH 6/6] Add test for oAuth --- tests/Get-oAuthToken.Tests.ps1 | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 tests/Get-oAuthToken.Tests.ps1 diff --git a/tests/Get-oAuthToken.Tests.ps1 b/tests/Get-oAuthToken.Tests.ps1 new file mode 100644 index 0000000..45553c0 --- /dev/null +++ b/tests/Get-oAuthToken.Tests.ps1 @@ -0,0 +1,35 @@ +#Requires -Modules Pester + +Import-Module $PSScriptRoot\..\PSF.psm1 -Force + +Describe 'Get-oAuthToken' { + + Context "When called normally" { + Mock -ModuleName PSF Invoke-RestMethod { return [PSCustomObject]@{ token_type = "Bearer"; access_token = "StandardEndpoint" } } -ParameterFilter { $Uri.AbsoluteUri.StartsWith("https://login.windows.net") } # Mock the standard endpoint calls. + + $Result = Get-oAuthToken -ApplicationId "00000000-0000-0000-0000-000000000000" -ApplicationKey "Test" -TenantId "00000000-0000-0000-0000-000000000000" -ResourceName "https://test" + + It "Returns a valid token from the correct endpoint" { + $Result | Should -Be "Bearer StandardEndpoint" + } + + It "Calls calls the oAuth endpoint" { + Assert-MockCalled -ModuleName PSF -CommandName Invoke-RestMethod -Times 1 + } + } + + Context "When called with ChinaAuth" { + Mock -ModuleName PSF Invoke-RestMethod { return [PSCustomObject]@{ token_type = "Bearer"; access_token = "ChinaEndpoint" } } -ParameterFilter { $Uri.AbsoluteUri.StartsWith("https://login.chinacloudapi.cn") } # Mock the china endpoint calls. + + $Result = Get-oAuthToken -ApplicationId "00000000-0000-0000-0000-000000000000" -ApplicationKey "Test" -TenantId "00000000-0000-0000-0000-000000000000" -ResourceName "https://test" -ChinaAuth $true + + It "Returns a valid token from the correct endpoint" { + $Result | Should -Be "Bearer ChinaEndpoint" + } + + It "Calls calls the oAuth endpoint" { + Assert-MockCalled -ModuleName PSF -CommandName Invoke-RestMethod -Times 1 + } + } + +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy