PermitPlane Docs
All docs pages

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:

PowerShell
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:

Go
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.

Go
if ok, _ := client.HasFeature("export"); ok {
    enableExport()
} else {
    showUpgradeOffer()
}
Go
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.

ErrorWhat to do
Code is not_a_license_keyThe key is mistyped. Ask the user to check it.
APICode is invalid_license_keyThe key is unknown or revoked. Ask for the key from the purchase email.
APICode is device_limitEvery device slot is taken. Deactivate another device first.
APICode is license_expired, license_suspended or license_revokedShow a renew or contact link.
Code is transportThe network failed. Retry later. Unlocked features keep working.
Go
var e *permitplane.Error
if errors.As(err, &e) {
    log.Println(e.Code, e.APICode, e.HTTPStatus, e.RetryAfter)
}

Going further

Reference

Package contents

  • The permitplane Go package as source. It needs Go 1.26 or newer.
  • permitplane.dll for Windows x86_64 in sdk/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 replace directive above.
  • You can also copy sdk/go into your vendor tree.
  • 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.

ServiceDefaultBehaviour
StateStoreFileStateStoreAtomic file in the user or machine directory. DPAPI on Windows, mode 0600 elsewhere. It holds a bearer installation token.
KeyStoreFileKeyStoreOne ECDSA P-256 key per namespace. DPAPI on Windows, a 0600 file elsewhere.
ClockSystemClockMonotonic time that includes suspend, plus a boot id. Reports Confident: false where it cannot guarantee both.
TransportHTTPTransportnet/http, https only, no redirects, 1 MiB response cap.
  • The client copies an http.Client you 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 icacls recipe 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

FieldHolds
Status, CodeThe library status and its short name.
APICode, HTTPStatusThe service error code and HTTP status, when the service answered.
RetryAfterThe service's Retry-After, or 0.
LastErrorThe 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 of Status().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: APICode is seats_exhausted and RetryAfter says 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.OfflineDeactivate ends local rights at once. Keep its file until the vendor processes it.

Your own services

  • Implement permitplane.Host, or pass your own services to NewCompositeHost.
  • 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 Host error, or as Signer for an error from KeyStore.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.