簡介
若要在存取影片庫時增加額外層級的保護,或為您的內容套用使用者層級限制,您可以將呼叫JSON Web Token (JWT) 傳遞給 Brightcove 播放 API。若要建立權杖,請依照下列步驟執行:
產生公開私密金鑰組
發行者將產生公開私密金鑰組,並將公開金鑰提供給 Brightcove。私密金鑰是由發行者用來簽署令牌,並且不會與 Brightcove 共用。
有很多方法可以產生公開私密金鑰組。以下是一些範例:
bash 腳本示例:
產生金鑰配對的範例指令碼:
#!/bin/bash
set -euo pipefail
NAME=${1:-}
test -z "${NAME:-}" && NAME="brightcove-playback-auth-key-$(date +%s)"
mkdir "$NAME"
PRIVATE_PEM="./$NAME/private.pem"
PUBLIC_PEM="./$NAME/public.pem"
PUBLIC_TXT="./$NAME/public_key.txt"
ssh-keygen -t rsa -b 2048 -m PEM -f "$PRIVATE_PEM" -q -N ""
openssl rsa -in "$PRIVATE_PEM" -pubout -outform PEM -out "$PUBLIC_PEM" 2>/dev/null
openssl rsa -in "$PRIVATE_PEM" -pubout -outform DER | base64 > "$PUBLIC_TXT"
rm "$PRIVATE_PEM".pub
echo "Public key to saved in $PUBLIC_TXT"
執行指令碼:
$ bash keygen.sh
使用範例Go
使用Go編程語言生成密鑰對的示例:
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"flag"
"fmt"
"io/ioutil"
"os"
"path"
"strconv"
"time"
)
func main() {
var out string
flag.StringVar(&out, "output-dir", "", "Output directory to write files into")
flag.Parse()
if out == "" {
out = "rsa-key_" + strconv.FormatInt(time.Now().Unix(), 10)
}
if err := os.MkdirAll(out, os.ModePerm); err != nil {
panic(err.Error())
}
priv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
panic(err.Error())
}
privBytes := x509.MarshalPKCS1PrivateKey(priv)
pubBytes, err := x509.MarshalPKIXPublicKey(priv.Public())
if err != nil {
panic(err.Error())
}
privOut, err := os.OpenFile(path.Join(out, "private.pem"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
panic(err.Error())
}
if err := pem.Encode(privOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: privBytes}); err != nil {
panic(err.Error())
}
pubOut, err := os.OpenFile(path.Join(out, "public.pem"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
panic(err.Error())
}
if err := pem.Encode(pubOut, &pem.Block{Type: "PUBLIC KEY", Bytes: pubBytes}); err != nil {
panic(err.Error())
}
var pubEnc = base64.StdEncoding.EncodeToString(pubBytes)
var pubEncOut = path.Join(out, "public_key.txt")
if err := ioutil.WriteFile(pubEncOut, []byte(pubEnc+"\n"), 0600); err != nil {
panic(err.Error())
}
fmt.Println("Public key saved in " + pubEncOut)
}
使用 node.js 的範例
使用 node.js 生成密鑰對的示例:
var crypto = require("crypto");
var fs = require("fs");
var now = Math.floor(new Date() / 1000);
var dir = "rsa-key_" + now;
fs.mkdirSync(dir);
crypto.generateKeyPair(
"rsa",
{modulusLength: 2048},
(err, publicKey, privateKey) => {
fs.writeFile(
dir + "/public.pem",
publicKey.export({ type: "spki", format: "pem" }),
err => {}
);
fs.writeFile(
dir + "/public_key.txt",
publicKey.export({ type: "spki", format: "der" }).toString("base64") +
"\n",
err => {}
);
fs.writeFile(
dir + "/private.pem",
privateKey.export({ type: "pkcs1", format: "pem" }),
err => {}
);
}
);
console.log("Public key saved in " + dir + "/public_key.txt");
註冊公開金鑰
您將使用密鑰 API 來註冊您的公鑰與布萊特灣。
關鍵 API
密鑰 API 用於管理您的公鑰與布萊特灣。
基本網址
API 的基本網址是:
https://playback-auth.api.brightcove.com
帳號路徑
在所有情況下,我們都會針對特定的視訊雲端帳戶提出要求。因此,您將始終將術語帳戶後面跟著您的帳戶 ID 添加到基本 URL 中:
https://playback-auth.api.brightcove.com/v1/accounts/{accountID}
授權
請求的訪問令牌是必需的,並且必須出現在授權頭።
Authorization: Bearer {access_token}
訪問令牌是一個臨時的 OAuth2 訪問令牌,必須從布萊特灣 OAuth 服務獲得。如需有關如何取得用戶端認證並使用它們來擷取存取權杖的詳細資訊,請參閱 Brightcove OAuth 概觀。
權限
對金鑰 API 的要求必須從具有下列權限的用戶端認證提出:
-
video-cloud/playback-auth/key/read
-
video-cloud/playback-auth/key/write
管理金鑰
關鍵 API 支援下列要求:
註冊一個新的金鑰:
將您的公鑰的值放在 API 請求主體中。你可以在 public_key.txt 文件中找到密鑰。
請求
POST /v1/accounts/{accountID}/keys
Content-Type: application/json
Body: {"value": "MFkwEwYHKoZIzj0CAQYIKoZIzj...MyeQviqploA=="}
使用捲曲
響應
{
"id": "{your_public_key_id}",
"type": "public",
"algorithm": "rsa",
"value": "{your_public_key_value}",
"createdAt": "2020-01-03T20:30:36.488Z"
}
列出鍵:
取得帳戶中的公開金鑰清單。
GET /v1/accounts/{accountID}/keys
取得一個金鑰:
取得帳戶中公開金鑰的詳細資料。
GET /v1/accounts/{accountID}/keys/{key_Id}
刪除一個鍵:
刪除帳戶中的公開金鑰。
DELETE /v1/accounts/{accountID}/keys/{key_Id}
建立JSON Web Token
發行者創建一個JSON Web Token(JWT)。使用 SHA-256 雜湊演算法 (在 JWT 規格中識別為 " RS256 」) 使用 RSA 演算法簽署該令牌不會支援其他 JWT 演算法。
JSON Web Token claims將使用標準的一部分,以及 Brightcove 定義的一些私人索賠。您將創建一個用您的私鑰JSON Web Token簽名。
靜態 URL 傳遞的宣告
以下聲明可以與布萊特灣的靜態 URL 傳遞一起使用。
欄位 | 類型 | 必填 | 描述 |
---|---|---|---|
accid |
字串 | 擁有播放內容的帳號 ID | |
drules |
串[] | 要套用的傳遞規則動作識別碼清單,請參閱實行傳送規則以取得詳細資訊。如果 config_id 查詢參數也被設置,它將被忽略,因為此聲明會覆蓋它。 | |
exp |
整數 | 這個令牌的時間將不再有效,以秒為單位,自紀元以來。必須不超過 30 天iat |
|
iat |
整數 | 這個令牌發出的時間,以秒為單位,自紀元 | |
conid |
字串 | 如果存在,此權杖只會授權擷取特定 Video Cloud 視訊 ID 的授權。 必須是有效的影片 ID。 |
|
pro |
字串 | 在單一視訊可用多個情況下,指定保護類型。 值:
|
|
vod |
物件 | 包含視訊隨選視訊的特定組態選項。 | |
ssai |
字串 | 您的伺服器端廣告插入 (SSAI) 組態識別碼。擷取 HLS 或破折號 VMAP 需要此宣告。 |
以下是您可能使用的JSON Web Token(JWT)聲明的示例:
{
// account id: JWT is only valid for this accounts
"accid":"4590388311111",
// drules: list of delivery rule IDs to be applied
"drules": ["0758da1f-e913-4f30-a587-181db8b1e4eb"]
// expires: timestamp when JWT expires
"exp":1577989732,
// issued at: timestamp when the JWT was created
"iat":1575484132,
// content id: JWT is only valid for video id or reference id
"conid":"5805807122222",
// protection: specify a protection type in the case where multiple are available for a single video
"pro":"aes128",
// VOD specific configuration options
"vod":{
// SSAI configuration to apply
"ssai":"efcc566-b44b-5a77-a0e2-d33333333333"
}
}
產生權杖
庫通常可用於生成 JWT 令牌。有關詳細信息,請參閱JSON Web Tokens網站。
bash 腳本示例:
生成 JWT 令牌的示例腳本:
#! /usr/bin/env bash
# Static header fields.
HEADER='{
"type": "JWT",
"alg": "RS256"
}'
payload='{
"pkid": "{your_public_key_id}",
"accid": "{your_account_id}"
}'
# Use jq to set the dynamic `iat` and `exp`
# fields on the payload using the current time.
# `iat` is set to now, and `exp` is now + 1 second.
PAYLOAD=$(
echo "${payload}" | jq --arg time_str "$(date +%s)" \
'
($time_str | tonumber) as $time_num
| .iat=$time_num
| .exp=($time_num + 60 * 60)
'
)
function b64enc() { openssl enc -base64 -A | tr '+/' '-_' | tr -d '='; }
function rs_sign() { openssl dgst -binary -sha256 -sign playback-auth-keys/private.pem ; }
JWT_HDR_B64="$(echo -n "$HEADER" | b64enc)"
JWT_PAY_B64="$(echo -n "$PAYLOAD" | b64enc)"
UNSIGNED_JWT="$JWT_HDR_B64.$JWT_PAY_B64"
SIGNATURE=$(echo -n "$UNSIGNED_JWT" | rs_sign | b64enc)
echo "$UNSIGNED_JWT.$SIGNATURE"
執行指令碼:
$ bash jwtgen.sh
使用範例Go
結果
以下是一個使用 https://JWT.io 指定完整的聲明集的解碼令牌的示例:
標頭:
{
"alg": "RS256",
"type": "JWT"
}
有效載荷:
{
"accid": "1100863500123",
"conid": "51141412620123",
"exp": 1554200832,
"iat": 1554199032,
"maxip": 10,
"maxu": 10,
"ua": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
}
測試播放
雖然不是必要的,但您可能想要在設定播放器之前先測試視訊播放。
要求播放:
curl -X GET \
-H 'Authorization: Bearer {JWT}' \
https://edge-auth.api.brightcove.com/playback/v1/accounts/{your_account_id}/videos/{your_video_id}