LicenseDesk開発者向けログイン

試験

ライセンスの組み込みを確かめる方法は 2 つあります。

方法 使う人 確かめること
偽のサーバー Node.js・Electron の SDK を使うアプリ 登録・更新・解除・エラー・時間の経過など、アプリの振る舞いの全体
試験の値の組(test-vectors.json) SDK を使わずに、自分で組み込むアプリ(C#・Swift・Python・Go など) トークンの署名の確かめと、状態の判定

偽のサーバー

SDK には、偽のサーバーが付いています。/testing から読み込みます。ネットワークも本物の鍵も使わずに、すべての状態を作れます。

import { createClient } from '@companyllc-japan/licencemanager-client'
import { createFakeServer, memoryStorage } from '@companyllc-japan/licencemanager-client/testing'

let clock = Date.now()
const fake = createFakeServer({ product: 'myapp', now: () => clock })
const lm = createClient({
  url: fake.url,
  product: fake.product,
  publicKeys: fake.publicKeys,
  fetch: fake.fetch,
  storage: memoryStorage(),
  appVersion: '1.0.0',
  now: () => clock,
})

const key = fake.addLicense({ seats: 2, status: 'active' })
await lm.activate(key)

作るときの指定

指定 既定 内容
product testapp 製品の ID
kid test-1 署名の鍵の名前
keyPrefix — キーの接頭辞。指定したときだけ、本物と同じく接頭辞を確かめます。作るキーの既定は TA
graceDays 14 猶予の日数
now — 時計
url https://api.lm.test 偽のサーバーの URL

状態を作る

const key = fake.addLicense({ seats: 2, status: 'active' }) // trialing・past_due・pending_payment・canceled・suspended・expired
fake.updateLicense(key, { revoked: true })                  // 停止
fake.updateLicense(key, { status: 'suspended', suspendReason: 'seller' }) // 一時停止(suspendReason の既定は payment)
fake.updateLicense(key, { status: 'past_due', effectiveEnd: new Date(clock + 14 * 86400_000) })
fake.addLicense({ licenseType: 'perpetual', updatesUntil: new Date('2027-11-10T00:00:00Z') }) // 買い切り
clock += 31 * 86400_000                                     // 時間を進める(期限切れ)
  • 使える操作: addLicense・updateLicense・deleteLicense・activations。
  • effectiveEnd(猶予を含む期限)を省くと、状態から既定の値を決めます(猶予は graceDays)。
  • 一時停止のトークンは、本物と同じく valid_until がすぐ切れ、refresh_after は発行 + 1 時間です。

失敗を作る

fake.setOffline(true)                           // 回線の切断(fetch が投げる)
fake.failNext({ status: 429, retryAfter: 600 }) // 回数の制限
fake.failNext({ status: 503 })                  // サーバーの不調
fake.failNext({ status: 502, html: true })      // CDN が返すような、JSON でない応答
fake.failNext({ hang: true })                   // 応答が返らない(timeoutMs で打ち切られる)
  • failNext の status は 429・500・502・503 です。times で、続けて失敗する回数を決められます。

お支払いと契約のページ

  • /v1/billing-link に答え、<origin>/manage/<product>/s/<リンクの合言葉> を返します。
  • <origin> は、偽のサーバーの url から、SDK の既定と同じ規則で決めます。既定の https://api.lm.test なら https://buy.lm.test、http://127.0.0.1:<port> ならそのままです。
  • 登録が無ければ not_activated です(本物と同じ)。
  • fake.setBillingLinkUrl(url) で、返す URL を差し替えられます。始まりが違うときの server_error を試せます。null で元に戻します。

鍵の入れ替え

fake.rotateKey('test-2') // 以後は新しい kid で署名する
  • publicKeys に新しい公開鍵が足され、古い kid も残ります。
  • fake.currentKid で、今の kid が分かります。
  • fake.sign(payload, kid?) で、好きなトークンを作れます。

本物と同じ規則

偽のサーバーは、本物と同じ規則で断ります。偽のサーバーだけで通る値はありません。

  • 入力の形とキーの正規化
  • キーの接頭辞(keyPrefix を指定したとき)
  • /v1 の下の POST だけ
  • 本文の 8 KB
  • 台数と、同じ端末の扱い
  • license_inactive の本文の status
  • 外す回数(removal_limit)
  • 期限の決め方

試験の値の組(test-vectors.json)

SDK を使わずに組み込むときは、自分の実装の判定を、試験の値の組で確かめます。見本(C#・Swift・Python・Go)は、どれもこの値の組をすべて通します。値の組は、管理画面の「SDK と組み込み」からダウンロードする見本の zip に入っています(docs/integration/test-vectors.json)。下の実行のしかたは、zip を展開したフォルダ(例: licensedesk-python・licensedesk-csharp)で行います。

中身

{
  "description": "…",
  "publicKeys": { "test-1": "MCowBQYDK2VwAyEA3BiVmV3wF6/ikWzgdx+Qx4H0wq+V/CF5GcIIhLUb8x8=" },
  "clockToleranceMs": 3600000,
  "cases": [
    {
      "name": "active",
      "description": "有効なサブスク",
      "input": {
        "token": { "payload": "…", "sig": "…", "kid": "test-1" },
        "product": "testapp",
        "deviceId": "device-1",
        "now": "2026-11-10T00:00:00Z",
        "maxSeen": null
      },
      "expected": {
        "state": "active",
        "reason": null,
        "status": "active",
        "licenseType": "subscription",
        "updatesUntil": null,
        "periodEnd": "2027-09-06T00:00:00Z",
        "validUntil": "2026-12-09T00:00:00Z",
        "activationId": "act_0000000000000001",
        "suspendReason": null
      }
    }
  ]
}
項目 内容
publicKeys 試験専用の公開鍵(test-1)。本物の鍵ではありません
clockToleranceMs 時計の巻き戻しとして許す幅(1 時間)
cases[].input.token 署名したトークン。null なら、トークンが無い
cases[].input.product・deviceId 自分の製品の ID と端末 ID として渡す値
cases[].input.now 今の時刻として渡す値(ISO 8601、UTC)
cases[].input.maxSeen これまでに見た最も新しい時刻。null なら、まだ記録が無い(0 として渡す)
cases[].expected 正しい判定の結果
  • 署名の鍵は、試験専用の test-1 だけです。unknown_kid の件だけは、publicKeys に無い test-9 で署名しています。
  • 注意: 試験用の鍵(test-1・test-9)の秘密は公開されています。本番のアプリの公開鍵に入れると、誰でもトークンを作れます。試験の中だけで使ってください。
  • expected の時刻は、トークンの payload にある文字列のままです。

含む件

name 中身
active 有効なサブスク
trialing 試用中(状態は active として使える)
canceled 解約の予定(期間の終わりまで使える)
past_due 支払いの再試行中(猶予)
pending_payment 請求書の入金待ち(猶予)
offline 更新の時刻を過ぎた(サーバーに届かない間の猶予)
expired_by_time 手元の確認の期限(valid_until)を過ぎた
expired_status 契約が終わった
revoked 販売者が止めた(返金など)
perpetual 買い切り(アップデートの期限つき)
no_new_fields license_type・updates_until が無い、古い形のトークン
unknown_field 知らない項目がある(読まずに受け付ける)
device_mismatch 別の PC のトークン
tampered_payload 署名の後に payload を書き換えた
tampered_sig 署名を壊した
unknown_kid 知らない kid
other_product 別の製品のトークン
clock_rollback 時計が発行の時刻より 1 時間より多く前に戻された
clock_within_tolerance 時計のずれが 1 時間の内(発行の時刻として扱う)
max_seen_ahead 前に見た時刻より、時計が 1 時間より多く前に戻っている
max_seen_within_tolerance 前に見た時刻より時計が少し前(1 時間の内。前に見た時刻で判定する)
no_token 登録していない(トークンが無い)
suspended_payment 一時停止(お支払い)
suspended_seller 一時停止(販売者)
suspended_no_reason 一時停止で suspend_reason が無い(suspendReason は null)
active_with_reason suspended でないときの suspend_reason は読まない
  • 件は、後で足すことがあります。名前で選ばず、cases をすべて回してください。

各言語の試験での使い方

どの言語でも、手順は同じです。

  1. test-vectors.json を読みます。
  2. cases の 1 件ずつ、自分の判定の関数に、input.token・publicKeys・input.product・input.deviceId・input.now・input.maxSeen を渡します。
  3. 結果の 9 つの項目(state・reason・status・licenseType・updatesUntil・periodEnd・validUntil・activationId・suspendReason)が、expected とすべて同じかを比べます。
  4. expected に項目が無いときは、null として比べます。
言語 判定の関数(見本) 実行のしかた
Python evaluate(token, public_keys, product, device_id, now_ms, max_seen_ms) cd examples/python && python -m venv .venv && .venv/bin/pip install -r requirements.txt && .venv/bin/python -m unittest -v
Swift LicenseDesk の判定(vector-check で確かめる) cd examples/swift && swift run vector-check
C# LicenseDesk の判定(VectorCheck で確かめる) dotnet run --project examples/csharp/VectorCheck(既定で docs/integration/test-vectors.json を読みます。ほかの場所なら -- <場所> を足します)
Go Evaluate(token, publicKeys, product, deviceID, nowMs, maxSeenMs) cd examples/go && go test ./...

Python の例です。zip の licensedesk-python/examples/python/ に置き、licensedesk-python/docs/integration/test-vectors.json を読みます(zip の test_vectors.py と同じ作りです)。

import json
import unittest
from datetime import datetime
from pathlib import Path

from licensedesk import evaluate


def ms(s: str) -> int:
    return int(datetime.fromisoformat(s.replace("Z", "+00:00")).timestamp() * 1000)


V = json.loads((Path(__file__).resolve().parents[2] / "docs/integration/test-vectors.json").read_text(encoding="utf-8"))
FIELDS = ["state", "reason", "status", "licenseType", "updatesUntil", "periodEnd", "validUntil", "activationId", "suspendReason"]


class TestVectors(unittest.TestCase):
    def test_all_cases(self) -> None:
        for c in V["cases"]:
            with self.subTest(c["name"]):
                i = c["input"]
                s = evaluate(i["token"], V["publicKeys"], i["product"], i["deviceId"], ms(i["now"]), ms(i["maxSeen"]) if i["maxSeen"] else 0)
                got = dict(zip(FIELDS, [s.state, s.reason, s.status, s.license_type, s.updates_until, s.period_end, s.valid_until, s.activation_id, s.suspend_reason]))
                self.assertEqual(got, {f: c["expected"].get(f) for f in FIELDS})
  • 試験の値の組の鍵は、試験専用です。アプリの本番の版には組み込まないでください。
  • 本物のサーバーでの確かめは、staging の製品で行います(組み込みの手引き の「試すときの注意」)。