// A shop that sells licences, and an app that checks them without calling home.
//
//   quillo run license_keys.qwe      then open http://localhost:8080/buy

use "license_keys.qwer"

// LICENSE_PRIVATE_KEY lives on the shop's server only; the app ships with the public key.
let PAIR = license_keypair()
let PRIVATE = env("LICENSE_PRIVATE_KEY") ?? PAIR.private
let PUBLIC = env("LICENSE_PUBLIC_KEY") ?? PAIR.public

route GET /buy {
    let key = license_make(PRIVATE, "Acme Ltd", ["pro", "export"], "2099-12-31")?
    { key, check: license_check(key, PUBLIC)?, can_export: license_has(key, PUBLIC, "export")?, can_sso: license_has(key, PUBLIC, "sso")? }
}

route GET /checks {
    let good = license_make(PRIVATE, "Acme Ltd", ["pro"], "")?
    let old = license_make(PRIVATE, "Old Customer", ["pro"], "2020-01-31")?
    let forged = license_make(license_keypair().private, "Acme Ltd", ["pro", "sso"], "")?
    {
        pasted_lower_case: license_check(lower(good), PUBLIC)?.valid,
        typo: license_check(replace(good, slice(good, 10, 11), slice(good, 10, 11) == "A" ? "B" : "A"), PUBLIC)?.reason,
        expired: license_check(old, PUBLIC)?.reason,
        forged: license_check(forged, PUBLIC)?.reason,
    }
}
