Skip to content

Commit a782998

Browse files
committed
Detect when not running as an admin during Initialize-Blogger
1 parent 5f1fb3d commit a782998

3 files changed

Lines changed: 46 additions & 6 deletions

File tree

src/private/ModuleInitHelpers.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function Test-IsAdmin
2+
{
3+
$currentIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
4+
$principal = New-Object System.Security.Principal.WindowsPrincipal($currentIdentity)
5+
return $principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
6+
}

src/public/Initialize-Blogger.ps1

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Initiate a login flow with Google
2121
2222
Initialize-Blogger
2323
24+
.NOTES
25+
Note that this function requires administrator permissions to support the authentication flow.
2426
#>
2527
Function Initialize-Blogger {
2628
[CmdletBinding()]
@@ -34,8 +36,17 @@ Function Initialize-Blogger {
3436
[Parameter(HelpMessage = "Redirect Uri specified in Google API Consent Form")]
3537
[string]$RedirectUri = "http://localhost/oauth2callback"
3638
)
39+
40+
# Check that we're running as an admin
41+
if (-not (Test-IsAdmin)) {
42+
Write-Warning "Administrator privileges are required to initialize Blogger authentication."
43+
Write-Warning "Please restart PowerShell as Administrator and try again."
44+
return
45+
}
3746

3847
$ErrorActionPreference = 'Stop'
48+
49+
# Show warning to developers if they attempt to use the neutered credentials by mistake
3950
if ($env:PSBLOGGER_CLIENT_ID -and !$PSBoundParameters.ContainsKey("ClientId"))
4051
{
4152
Write-Verbose "Using environment variable PSBLOGGER_CLIENT_ID for ClientId"

src/tests/Initialize-Blogger.Tests.ps1

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@ Describe "Initialize-Blogger" {
44
Import-Module $PSScriptRoot\..\PSBlogger.psm1 -Force
55
}
66

7-
# Context "Try it" {
8-
# It "Should launch browser and authenticate" {
9-
# Initialize-Blogger
10-
# }
11-
# }
12-
137
Context "User provides AuthCode" {
148

159
BeforeEach {
1610
InModuleScope -ModuleName PSBlogger {
11+
# simulate running as admin
12+
Mock Test-IsAdmin { $true }
13+
1714
# simulate valid auth token
1815
Mock Get-GoogleAccessToken { return @{ refresh_token = "refresh_token" } }
1916
# simulate valid offline token
@@ -72,4 +69,30 @@ Describe "Initialize-Blogger" {
7269

7370
}
7471

72+
Context "Running as non-admin" {
73+
BeforeEach {
74+
InModuleScope -ModuleName PSBlogger {
75+
# simulate running as non-admin
76+
Mock Test-IsAdmin { $false }
77+
78+
# ensure that we don't launch browser or admin features
79+
Mock Start-Process { throw "Unexpected call to start-process"}
80+
}
81+
}
82+
83+
It "Should show warning and exit when not admin" {
84+
InModuleScope -ModuleName PSBlogger {
85+
# arrange
86+
$initArgs = @{ ClientId="dummy"; ClientSecret="dummy" }
87+
Mock Write-Warning {} -Verifiable
88+
89+
# act & assert
90+
{ Initialize-Blogger @initArgs } | Should -Not -Throw
91+
92+
# The function should exit early, so we can verify it doesn't try to do auth
93+
Assert-MockCalled Test-IsAdmin -Times 1
94+
Should -InvokeVerifiable
95+
}
96+
}
97+
}
7598
}

0 commit comments

Comments
 (0)