簡體   English   中英

在 PowerShell 中為 Ghost 創建 JWT 令牌

[英]Create JWT token in PowerShell for Ghost

更新


使用GhostSharp中的此方法設法解決了我的問題:

    public static class ByteConvertor
    {
        public static byte[] StringToByteArray(string hex)
        {
            int NumberChars = hex.Length;
            byte[] bytes = new byte[NumberChars / 2];
            for (int i = 0; i < NumberChars; i += 2)
                bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
            return bytes;
        }
    }

在 C# 或 PowerShell 中有沒有更好的方法來完成同樣的事情? 我是否遺漏了一些可以幫助我解決此問題的編碼或轉換方法?


我正在嘗試構建一個 JWT 令牌來針對我的幽靈博客進行身份驗證,但他們的所有樣本都在 Javascript、Ruby 和 Python 中。C# 或 PowerShell 中沒有任何內容,我似乎無法進行正確的轉換。

這是我現在的代碼:

$parts = $adminToken -split ":"
$id = $parts[0]
$secret= $parts[1]

$secret= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($secret)) | ConvertFrom-Base64UrlString | ConvertTo-Base64UrlString

Install-Module -Name JWT

$jwtToken = New-Jwt -Header (@{
    "alg" = "HS256"
    "kid" = $id
    "typ" = "JWT"
}| ConvertTo-Json) -PayloadJson (@{
    "exp" = ([DateTimeOffset](Get-date).AddMinutes(5)).ToUnixTimeSeconds()
    "iat" = ([DateTimeOffset](Get-date)).ToUnixTimeSeconds()
    "aud" = "/admin/"
} | ConvertTo-Json) -Secret $secret

Invoke-RestMethod -Uri "https://scrumbug.ghost.io/ghost/api/admin/pages/$($trainingPage.id)" -Method PUT -Body ($trainingPage | ConvertTo-Json) -Headers @{Authorization="Ghost $jwtToken "}

但我不斷收到 Ghost 的投訴,說我的令牌不正確:

  50 |  Invoke-RestMethod -Uri "https://scrumbug.ghost.io/ghost/api/admin/pag …
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | {"errors":[{"message":"Invalid token: invalid
     | signature","context":null,"type":"UnauthorizedError","details":null,"property":null,"help":null,"code":"INVALID_JWT","id":"069932a0-4009-11ed-afce-656a161cb24c","ghostErrorCode":null}]}

有許多不同語言的樣本,但我看不出我遺漏了什么。

我懷疑我的錯誤在這一行:

$key = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($secret)) | ConvertFrom-Base64UrlString | ConvertTo-Base64UrlString

這應該等同於:

[secret].pack('H*')

或者:

printf '%s' "${input}" | base64 | tr -d '=' | tr '+' '-' | tr '/' '_'

Ghost 在管理面板中提供的用於構造 JWT 令牌的示例令牌看起來像$id:$secret

THIS TOKEN HAS BEEN REVOKED AND IS NOT VALID.
644599e8733df7003d6fa66e:9aff4fdd6bb58957da5688a9cf0046a76bcc39b6b9ab16261123927f1caf5c94

確實是從令牌到 byte[] 的轉換實現了魔術。

感謝一位同事,我正在尋找的神奇代碼行是:

[Convert]::FromHexString($secret)

哪個神奇。 僅適用於 PowerShell Core 和 .NET 5+。

我的問題的完整解決方案:

Install-Module -Name JWT

$token = "id:secret" # Your token here
$parts = $token -split ":"
$id = $parts[0]
$secretKey = $parts[1]
$secretBytes = [Convert]::FromHexString($secretKey)

$jwtToken = New-Jwt -Header (@{
    "alg" = "HS256"
    "kid" = $id
    "typ" = "JWT"
}| ConvertTo-Json) -PayloadJson (@{
    "exp" = ([DateTimeOffset](Get-date).AddMinutes(5)).ToUnixTimeSeconds()
    "iat" = ([DateTimeOffset](Get-date)).ToUnixTimeSeconds()
    "aud" = "/admin/"
} | ConvertTo-Json) -Secret $secretBytes

Invoke-RestMethod -Uri "https://scrumbug.ghost.io/ghost/api/admin/pages/$($trainingPage.id)" `
    -Method PUT `
    -Body ($pageJson | ConvertTo-Json) `
    -Headers @{Authorization="Ghost $jwtToken "}

有關更完整的演練,請參閱我的博客

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM