Add licensing to a Go app
At the end, your Go app activates a licence key and unlocks its features offline.
From your product page in PermitPlane (app.permitplane.com/ops/vendor/), click Download permitplane.json and Create test key.
1. Install
Download permitplane-go-0.1.0.zip and compare its sha256 with manifest.json. The package uses cgo, so you need a C compiler such as MinGW-w64. In your module folder, run:
Get-FileHash -Algorithm SHA256 .\permitplane-go-0.1.0.zip
Expand-Archive .\permitplane-go-0.1.0.zip -DestinationPath C:\libs
$pp = "C:/libs/permitplane-go-0.1.0/sdk/go"
go mod edit -require permitplane.com/sdk/go@v0.0.0
go mod edit -replace "permitplane.com/sdk/go=$pp"
$env:CGO_ENABLED = "1"
$env:CGO_LDFLAGS = "-L$pp/lib"
$env:PATH = "$pp/lib;$env:PATH"2. Activate a licence key
Load the config, create the client and activate once:
package main
import (
"context"
"fmt"
"log"
"os"
permitplane "permitplane.com/sdk/go"
)
func main() {
ctx := context.Background()
config, err := os.ReadFile("permitplane.json")
if err != nil {
log.Fatal(err)
}
// Loads the saved state, so a restart needs no request.
host := permitplane.NewCompositeHost(nil, nil, nil, nil)
client, err := permitplane.New(string(config), host)
if err != nil {
log.Fatal(err)
}
defer client.Close()
if st, _ := client.Status(); st.Status == "free" {
// paste your test key here; in your real app, ask the user for it
err = client.ActivateWithKey(ctx,
"PP-7KQ4M-X9T2H-B6WRN-3FJ8D", "Office PC")
if err != nil {
log.Print(err)
}
}
// ... unlock features here (step 3) ...
fmt.Println("ready")
}3. Unlock features in your app
Turn parts of your app on or off based on what the customer's licence includes. The check reads the signed grant and works offline up to your policy's offline days.
if ok, _ := client.HasFeature("export"); ok {
enableExport()
} else {
showUpgradeOffer()
}seats, _ := client.FeatureValue("seats")
// seats.Unlimited, or seats.Value
st, _ := client.Status()
// st.Status: free, trial, paid, online_required, expired,
// recovery_required or seat_unavailable
fmt.Println(seats.Value, st.Status)4. Keep it current
- Call
client.Refresh(ctx)on start and every hour. - Call
client.Persist()now and then and before exit. - Call
client.Deactivate(ctx)to release the device.
Errors
Every failure is a *permitplane.Error.
| Error | What to do |
|---|---|
Code is not_a_license_key | The key is mistyped. Ask the user to check it. |
APICode is invalid_license_key | The key is unknown or revoked. Ask for the key from the purchase email. |
APICode is device_limit | Every device slot is taken. Deactivate another device first. |
APICode is license_expired, license_suspended or license_revoked | Show a renew or contact link. |
Code is transport | The network failed. Retry later. Unlocked features keep working. |
var e *permitplane.Error
if errors.As(err, &e) {
log.Println(e.Code, e.APICode, e.HTTPStatus, e.RetryAfter)
}Going further
- Unattended and offline activation: request and response files for air-gapped machines.
- Floating licences: concurrent seats with leases.
- Network requirements: what your customer's firewall must allow.
- Reference: platform services, floating seats, offline files and threading.
Reference
Package contents
- The
permitplaneGo package as source. It needs Go 1.26 or newer. permitplane.dllfor Windows x86_64 insdk/go/lib.- Downloads need no sign-in. On Linux or macOS, check a file with
sha256sum <file>. - Go fetches modules by path, so use the
replacedirective above. - You can also copy
sdk/gointo yourvendortree. - Linux and macOS libraries are coming. The manifest lists them under
coming. - Serving the module through the Go proxy is a later option.
Platform services
NewCompositeHost builds four services from the defaults. Pass your own to replace any one.
| Service | Default | Behaviour |
|---|---|---|
StateStore | FileStateStore | Atomic file in the user or machine directory. DPAPI on Windows, mode 0600 elsewhere. It holds a bearer installation token. |
KeyStore | FileKeyStore | One ECDSA P-256 key per namespace. DPAPI on Windows, a 0600 file elsewhere. |
Clock | SystemClock | Monotonic time that includes suspend, plus a boot id. Reports Confident: false where it cannot guarantee both. |
Transport | HTTPTransport | net/http, https only, no redirects, 1 MiB response cap. |
- The client copies an
http.Clientyou pass in, with redirects and cookies switched off.
Machine directory rules
- The machine directory is
%ProgramData%\PermitPlane. - The client creates it with a locked-down ACL: SYSTEM, Administrators and the creating user.
- It checks the directory on every save, load and key creation.
- It refuses a directory that another account owns or can read or write.
- The error says "machine directory ... is not private".
- Activate under the service account that will use the store.
- Installers create
<Vendor>\<Product>for SYSTEM and Administrators only. - The
icaclsrecipe is in Unattended and offline activation.
Upgrade notes
- An existing machine directory with default permissions is now refused.
- A machine directory whose parent other accounts can modify is now refused.
Error fields
| Field | Holds |
|---|---|
Status, Code | The library status and its short name. |
APICode, HTTPStatus | The service error code and HTTP status, when the service answered. |
RetryAfter | The service's Retry-After, or 0. |
LastError | The raw error JSON. |
Unwrap() | The error or panic from one of your own services. |
Floating seats
CheckoutSeat(ctx, 0)takes an online seat.- Call
RenewSeat(ctx)at about half ofStatus().Lease.TTLSeconds. CheckoutSeat(ctx, hours)with 1 to 504 hours borrows a seat for offline use.ReleaseSeat(ctx)returns an online seat early.- No free seat:
APICodeisseats_exhaustedandRetryAftersays when to retry. - The status then becomes
seat_unavailable.
Offline files
CreateOfflineRequest(permitplane.OfflineActivate, "PC")returns a request file for the vendor.ImportOfflineResponse(text)stores the vendor's answer to it.permitplane.OfflineDeactivateends local rights at once. Keep its file until the vendor processes it.
Your own services
- Implement
permitplane.Host, or pass your own services toNewCompositeHost. - A service must not call back into the same client. That call fails with
StatusBusy. - The native boundary catches errors and panics from your services.
- They surface as a
Hosterror, or asSignerfor an error fromKeyStore.Sign.
Threading
- You can share one client between goroutines.
- The library serialises calls on one client.
- Cancelling a context stops a call that has not started.
- It does not stop a call that is already running.
Good to know
- The client never stores the licence key.
Persist()saves the clock high-water mark.Deactivate(ctx)also clears the local state.