Tests/New-JWT.Tests.ps1
Describe "New-JWT" { BeforeAll { Import-Module -Name D:\a\jwtPS\jwtPS\jwtPS.psd1 } Context "Verify parameter" { $mandatoryParameter = @( @{ parameter = 'PrivateKey' }, @{ parameter = 'Payload' } ) It "<parameter> is mandatory" -TestCases $mandatoryParameter { param($parameter) $command = Get-Command -Name New-JWT $command.Parameters[$parameter].Attributes.Mandatory | Should -BeTrue } } Context "Verify payload" { $claims = @( @{ payload = @{ aud = "jwtPS" iss = "DigitalAXPP" sub = "RS256 Test" nbf = "0" exp = ([System.DateTimeOffset]::Now.AddHours(3)).ToUnixTimeSeconds() iat = ([System.DateTimeOffset]::Now).ToUnixTimeSeconds() } match = 'jti' total = 6 }, @{ payload = @{ aud = "jwtPS" iss = "DigitalAXPP" sub = "RS256 Test" nbf = "0" exp = ([System.DateTimeOffset]::Now.AddHours(3)).ToUnixTimeSeconds() } match = 'jti', 'iat' total = 5 }, @{ payload = @{ aud = "jwtPS" iss = "DigitalAXPP" sub = "RS256 Test" nbf = "0" } match = 'jti', 'iat', 'exp' total = 4 } ) It "With <total> properties" -TestCases $claims { param($payload, $match) $jwt = New-JWT -Algorithm HS256 -PrivateKey "P@ssw0rd" -Payload $payload -VerifyPayload $match | ForEach-Object { [bool]($jwt -match $_) | Should -BeTrue } } } Context "Creating RSA signature" { BeforeEach { $key = "-----BEGIN PRIVATE KEY-----`r`nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC4yIC+wJeDpeBr`r`n/RuXyCNuAZPj/qSCaBIV9qr0yjNV6VJsH2ULdvv9qSWTvcja2wMCzcYNUCtlwFLn`r`nwCIahP7rKiB8jMsrONhub1qX175Z1XjTxNurIRHt5v5nuuOxO8AmcptKrtJGbvb8`r`nQPQ6+BCJdc+M/3kRYDAPiju0iUmWCveK5DwTnorkevyexEVRUsoIkMhBVzD6LZy/`r`nbOucp4oC7sXJ/HHaJ8/3/i6HekiPPe90w7QzJ5Un/zxlwbCd0T5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IKXhjLusTM9Yc4aR2EQDYknzK4pyC1wKBH6u7`r`nhfd1yfH3vcuVja/xmsORb8PI0q6MgHHonoiwoxwBMSP8E1max8jcooGruwLAs+Vt`r`ntDkhw/OqDoDPCcNdXlAtc7IvBHj55CCp63HFphkCgYEA3eHDbrPLKZToKvcJ7TI4`r`n4azujEBqfq2Vc7vDn5T7fjPcUq8ZthdZADs6uL4EDdmCvjTmP68ndoJRWQiXfFQV`r`nddtmbgQcESw40/0fvd2NRABSZ/xbrKCFRiG6od0y9WSw1Kl0chMLlWhN1osbqbXZ`r`n5h9Ey+dTqd9d5+lIRxlBjoQ=`r`n-----END PRIVATE KEY-----`r`n" $claim = @{ aud = "jwtPS" iss = "DigitalAXPP" sub = "RS256 Test" nbf = "0" exp = ([System.DateTimeOffset]::Now.AddHours(3)).ToUnixTimeSeconds() iat = ([System.DateTimeOffset]::Now).ToUnixTimeSeconds() jti = [guid]::NewGuid() } } It "With SHA256" { $jwt = New-JWT -PrivateKey $key -Algorithm RS256 -Payload $claim $jwt | Should -Match -RegularExpression '(^[\w-]*\.[\w-]*\.[\w-]*$)' } It "With SHA384" { $jwt = New-JWT -PrivateKey $key -Algorithm RS384 -Payload $claim $jwt | Should -Match -RegularExpression '(^[\w-]*\.[\w-]*\.[\w-]*$)' } It "With SHA512" { $jwt = New-JWT -PrivateKey $key -Algorithm RS512 -Payload $claim $jwt | Should -Match -RegularExpression '(^[\w-]*\.[\w-]*\.[\w-]*$)' } } Context "Creating ECDSA signature" { BeforeEach { $key_256 = "-----BEGIN EC PRIVATE KEY-----`r`nMHcCAQEEIO9Xgf50T8VO6GkncN1Q2oF0kq3IBrbkI+SSphg98VE2oAoGCCqGSM49`r`nAwEHoUQDQgAEN9S07l/929SmRhf0yTvTykjwJd/QJXARITRQ5B8e00aSKR7uuguy`r`nfeGQEbNDmL21aAhy7RqmQBhx3ZcO71apFA==`r`n-----END EC PRIVATE KEY-----`r`n" $key_384 = "-----BEGIN EC PRIVATE KEY-----`r`nMIGkAgEBBDDIBWp8sZe1ff5kmLHS3RFd1pHxOimPnO1vfrydzlm8UlYNBFnj0lrI`r`nCoTPd1tg8HugBwYFK4EEACKhZANiAARtMhih0x3xd4OaZKXw64GApFQv2tPylyao`r`n3gpcxbq62o6o0sk734KOwJTKkOVBElOJlAWRtkplBc9UkS7wQv7zo5cBwDO0v+nt`r`nEzDFGAoqOg1lfMW22hDoyMCGywxdGhs=`r`n-----END EC PRIVATE KEY-----`r`n" $key_512 = "-----BEGIN EC PRIVATE KEY-----`r`nMIHcAgEBBEIB383k8S7qBj3/wbufXKbnuXKVLhlZ+Rpzeox3Dc9phmLaKHKggePA`r`nSivMyCaR7MZMWsYJ5UdG/covRbXxuQaenQqgBwYFK4EEACOhgYkDgYYABAFBKL3L`r`nsMgI9Xc443ef8I63bS5hz703VtroGvOBQv4zuY2V8y3amqdgjas7FQlI4ZNQBohs`r`nLHIRTaJy/uqpi3T3JAHLriR1QzEQ5S/WUiKx0iPUcM6ItuMaByaZGb11YMw/ygIy`r`n+mpcE0LEEtuVsSuzuSSc5nnvgreD6h+mhHzKNxVOog==`r`n-----END EC PRIVATE KEY-----`r`n" $claim = @{ aud = "jwtPS" iss = "DigitalAXPP" sub = "ES Test" nbf = "0" exp = ([System.DateTimeOffset]::Now.AddHours(3)).ToUnixTimeSeconds() iat = ([System.DateTimeOffset]::Now).ToUnixTimeSeconds() jti = [guid]::NewGuid() } } It "With SHA256" { $jwt = New-JWT -PrivateKey $key_256 -Algorithm ES256 -Payload $claim $jwt | Should -Match -RegularExpression '(^[\w-]*\.[\w-]*\.[\w-]*$)' } It "With SHA384" { $jwt = New-JWT -PrivateKey $key_384 -Algorithm ES384 -Payload $claim $jwt | Should -Match -RegularExpression '(^[\w-]*\.[\w-]*\.[\w-]*$)' } It "With SHA512" { $jwt = New-JWT -PrivateKey $key_512 -Algorithm ES512 -Payload $claim $jwt | Should -Match -RegularExpression '(^[\w-]*\.[\w-]*\.[\w-]*$)' } } Context "Creating HMAC signature" { BeforeEach { $claim = @{ aud = "jwtPS" iss = "DigitalAXPP" sub = "HS256 Test" nbf = "0" exp = ([System.DateTimeOffset]::Now.AddHours(3)).ToUnixTimeSeconds() iat = ([System.DateTimeOffset]::Now).ToUnixTimeSeconds() jti = [guid]::NewGuid() } } It "With SHA256" { $jwt = New-JWT -Algorithm HS256 -Payload $claim -PrivateKey 'P@ssw0rd' $jwt | Should -Match -RegularExpression '(^[\w-]*\.[\w-]*\.[\w-]*$)' } It "With SHA384" { $jwt = New-JWT -Algorithm HS384 -Payload $claim -PrivateKey 'P@ssw0rd' $jwt | Should -Match -RegularExpression '(^[\w-]*\.[\w-]*\.[\w-]*$)' } It "With SHA512" { $jwt = New-JWT -Algorithm HS512 -Payload $claim -PrivateKey 'P@ssw0rd' $jwt | Should -Match -RegularExpression '(^[\w-]*\.[\w-]*\.[\w-]*$)' } } } |