diff --git a/PSF.psd1 b/PSF.psd1 index eba8631..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') +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 = '' diff --git a/PSF.psm1 b/PSF.psm1 index e2120f1..008d540 100644 --- a/PSF.psm1 +++ b/PSF.psm1 @@ -2,3 +2,5 @@ . $PSScriptRoot\functions\Get-oAuthToken.ps1 . $PSScriptRoot\functions\Get-RandomPassword.ps1 +. $PSScriptRoot\functions\Connect-AadWithRmContext.ps1 +. $PSScriptRoot\functions\Get-PublicIPAddress.ps1 diff --git a/appveyor.yml b/appveyor.yml index 0a809b6..ffac007 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -4,8 +4,10 @@ environment: secure: v711B0A/aGDr4wVCW3sYtaJJljC+rXipS2PENvcBC+qtOs4VXS9QWDTMJyQIKLTv install: - - choco install gitversion.portable -pre -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: 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 +} 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 +} 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