Skip to content

Commit fdca4e5

Browse files
🩹 [Patch]: Linting passes on all platforms (#25)
The `Lint-SourceCode` and `Lint-Module` CI jobs now pass on Linux, macOS, and Windows. All `PSAvoidLongLines` violations that blocked the pipeline after PR #18 was merged have been resolved by extracting long error message strings into local variables and splitting long inline conditionals across multiple lines. - Fixes #24 ## Fixed: Linting no longer fails on any platform `PSScriptAnalyzer` reported a `PSAvoidLongLines` violation (line length > 120 characters) in three source files, causing every platform leg of both `Lint-SourceCode` and `Lint-Module` to exit with code 1 and preventing `Get-TestResults` from completing successfully. The affected lines were all `throw` statements with long interpolated error messages and one long inline `if/else` expression used to set an HMAC key. Each has been reformatted — long messages extracted into a `$message` variable, and the inline conditional split across multiple lines. No error types, parameter names, function signatures, or observable behaviors changed. All 31 Pester tests pass locally after the reformatting. ## Technical Details - **`New-Jwt.ps1`** — 7 violations fixed: `throw` messages on lines 80, 90, 101, 106, 128, 144 extracted to `$message` variables; `$hmacsha256.Key = if (...) {...} else {...}` on line 132 split across multiple lines. - **`Test-Jwt.ps1`** — 6 violations fixed: same patterns as `New-Jwt.ps1` (lines 78, 88, 103, 121, 125, 154). - **`ConvertTo-Base64UrlString.ps1`** — 2 violations fixed: `.NOTES` comment wrapped at a word boundary (line 24); `throw` message extracted via an intermediate `$type` variable to stay within 120 chars (line 51). - **Implementation plan progress**: All tasks completed — source fixes in all three files applied, `Invoke-ScriptAnalyzer` returns no `PSAvoidLongLines` results, and all 31 Pester tests pass. --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 383e134 commit fdca4e5

3 files changed

Lines changed: 37 additions & 15 deletions

File tree

src/functions/public/ConvertTo-Base64UrlString.ps1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
System.String
2222
2323
.NOTES
24-
Converts standard base64 output to JWT-safe base64url text by replacing URL-sensitive characters and removing padding.
24+
Converts standard base64 output to JWT-safe base64url text by replacing URL-sensitive
25+
characters and removing padding.
2526
2627
.LINK
2728
https://psmodule.io/Jwt/Functions/ConvertTo-Base64UrlString/
@@ -48,7 +49,9 @@
4849
} elseif ($InputObject -is [byte[]]) {
4950
[Convert]::ToBase64String($InputObject) -replace '\+', '-' -replace '/', '_' -replace '='
5051
} else {
51-
throw [System.ArgumentException]::new("ConvertTo-Base64UrlString requires string or byte array input, received $($InputObject.GetType())")
52+
$type = $InputObject.GetType()
53+
$message = "ConvertTo-Base64UrlString requires string or byte array input, received $type"
54+
throw [System.ArgumentException]::new($message)
5255
}
5356
}
5457

src/functions/public/New-Jwt.ps1

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@
7777
try {
7878
$algorithm = (ConvertFrom-Json -InputObject $Header -ErrorAction Stop).alg
7979
} catch {
80-
throw [System.FormatException]::new("The supplied JWT header is not valid JSON. Header length: $($Header.Length) characters.")
80+
$message = "The supplied JWT header is not valid JSON. Header length: $($Header.Length) characters."
81+
throw [System.FormatException]::new($message)
8182
}
8283
if ([string]::IsNullOrEmpty($algorithm)) {
8384
throw [System.FormatException]::new('The JWT header is missing the required "alg" claim.')
@@ -87,7 +88,8 @@
8788
try {
8889
$null = ConvertFrom-Json -InputObject $PayloadJson -ErrorAction Stop
8990
} catch {
90-
throw [System.FormatException]::new("The supplied JWT payload is not valid JSON. Payload length: $($PayloadJson.Length) characters.")
91+
$message = "The supplied JWT payload is not valid JSON. Payload length: $($PayloadJson.Length) characters."
92+
throw [System.FormatException]::new($message)
9193
}
9294

9395
$encodedHeader = ConvertTo-Base64UrlString $Header
@@ -98,12 +100,14 @@
98100
switch ($algorithm) {
99101
'RS256' {
100102
if (-not $PSBoundParameters.ContainsKey('Cert')) {
101-
throw [System.ArgumentException]::new('RS256 requires a -Cert parameter of type X509Certificate2.', 'Cert')
103+
$message = 'RS256 requires a -Cert parameter of type X509Certificate2.'
104+
throw [System.ArgumentException]::new($message, 'Cert')
102105
}
103106
Write-Verbose "Signing certificate: $($Cert.Subject)"
104107
$rsa = [System.Security.Cryptography.X509Certificates.RSACertificateExtensions]::GetRSAPrivateKey($Cert)
105108
if ($null -eq $rsa) {
106-
throw [System.ArgumentException]::new('The supplied certificate has no RSA private key and cannot be used to sign.', 'Cert')
109+
$message = 'The supplied certificate has no RSA private key and cannot be used to sign.'
110+
throw [System.ArgumentException]::new($message, 'Cert')
107111
} else {
108112
try {
109113
$signature = $rsa.SignData(
@@ -125,11 +129,16 @@
125129
throw [System.ArgumentException]::new('HS256 requires a -Secret parameter.', 'Secret')
126130
}
127131
if ($Secret -isnot [byte[]] -and $Secret -isnot [string]) {
128-
throw [System.ArgumentException]::new("Expected Secret parameter as byte array or string, instead got $($Secret.GetType())", 'Secret')
132+
$message = "Expected Secret parameter as byte array or string, instead got $($Secret.GetType())"
133+
throw [System.ArgumentException]::new($message, 'Secret')
129134
}
130135
$hmacsha256 = [System.Security.Cryptography.HMACSHA256]::new()
131136
try {
132-
$hmacsha256.Key = if ($Secret -is [byte[]]) { $Secret } else { [System.Text.Encoding]::UTF8.GetBytes($Secret) }
137+
$hmacsha256.Key = if ($Secret -is [byte[]]) {
138+
$Secret
139+
} else {
140+
[System.Text.Encoding]::UTF8.GetBytes($Secret)
141+
}
133142
$encodedSignature = ConvertTo-Base64UrlString $hmacsha256.ComputeHash($contentBytes)
134143
} catch {
135144
throw [System.Exception]::new("Signing with HMACSHA256 failed: $_", $_.Exception)
@@ -141,7 +150,8 @@
141150
$encodedSignature = $null
142151
}
143152
default {
144-
throw [System.NotSupportedException]::new('The algorithm is not one of the supported: "RS256", "HS256", "none".')
153+
$message = 'The algorithm is not one of the supported: "RS256", "HS256", "none".'
154+
throw [System.NotSupportedException]::new($message)
145155
}
146156
}
147157

src/functions/public/Test-Jwt.ps1

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ function Test-Jwt {
7575
try {
7676
$algorithm = (ConvertFrom-Json -InputObject $header -ErrorAction Stop).alg
7777
} catch {
78-
throw [System.FormatException]::new("The supplied JWT header segment is not valid JSON. Header length: $($header.Length) characters.")
78+
$message = "The supplied JWT header segment is not valid JSON. Header length: $($header.Length) characters."
79+
throw [System.FormatException]::new($message)
7980
}
8081
if ([string]::IsNullOrEmpty($algorithm)) {
8182
throw [System.FormatException]::new('The JWT header is missing the required "alg" claim.')
@@ -85,7 +86,8 @@ function Test-Jwt {
8586
switch ($algorithm) {
8687
'RS256' {
8788
if (-not $PSBoundParameters.ContainsKey('Cert')) {
88-
throw [System.ArgumentException]::new('RS256 requires a -Cert parameter of type X509Certificate2.', 'Cert')
89+
$message = 'RS256 requires a -Cert parameter of type X509Certificate2.'
90+
throw [System.ArgumentException]::new($message, 'Cert')
8991
}
9092
if ([string]::IsNullOrEmpty($parts[2])) {
9193
return $false
@@ -100,7 +102,8 @@ function Test-Jwt {
100102
$computed = [System.Security.Cryptography.SHA256]::HashData($signedContent)
101103
$rsa = [System.Security.Cryptography.X509Certificates.RSACertificateExtensions]::GetRSAPublicKey($Cert)
102104
if ($null -eq $rsa) {
103-
throw [System.ArgumentException]::new('The supplied certificate has no RSA public key and cannot be used to verify.', 'Cert')
105+
$message = 'The supplied certificate has no RSA public key and cannot be used to verify.'
106+
throw [System.ArgumentException]::new($message, 'Cert')
104107
}
105108
try {
106109
$rsa.VerifyHash(
@@ -118,11 +121,16 @@ function Test-Jwt {
118121
throw [System.ArgumentException]::new('HS256 requires a -Secret parameter.', 'Secret')
119122
}
120123
if ($Secret -isnot [byte[]] -and $Secret -isnot [string]) {
121-
throw [System.ArgumentException]::new("Expected Secret parameter as byte array or string, instead got $($Secret.GetType())", 'Secret')
124+
$message = "Expected Secret parameter as byte array or string, instead got $($Secret.GetType())"
125+
throw [System.ArgumentException]::new($message, 'Secret')
122126
}
123127
$hmacsha256 = [System.Security.Cryptography.HMACSHA256]::new()
124128
try {
125-
$hmacsha256.Key = if ($Secret -is [byte[]]) { $Secret } else { [System.Text.Encoding]::UTF8.GetBytes($Secret) }
129+
$hmacsha256.Key = if ($Secret -is [byte[]]) {
130+
$Secret
131+
} else {
132+
[System.Text.Encoding]::UTF8.GetBytes($Secret)
133+
}
126134
$signedContent = [System.Text.Encoding]::UTF8.GetBytes($parts[0] + '.' + $parts[1])
127135
$signature = $hmacsha256.ComputeHash($signedContent)
128136
if (-not $parts[2]) {
@@ -151,7 +159,8 @@ function Test-Jwt {
151159
$parts[2] -eq ''
152160
}
153161
default {
154-
throw [System.NotSupportedException]::new('The algorithm is not one of the supported: "RS256", "HS256", "none".')
162+
$message = 'The algorithm is not one of the supported: "RS256", "HS256", "none".'
163+
throw [System.NotSupportedException]::new($message)
155164
}
156165
}
157166
}

0 commit comments

Comments
 (0)