{ "$schema":              "https://json-schema.org/draft/2020-12/schema",
  "$id":                  "https://schemas.mpai.community/MMM4/V3.0/data/ServiceAccessToken.json",
  "title":                "Service Access Token V3.0",
  "description":          "Verifiable authorisation for a Subject (e.g., a Process) to access a specific service under the terms of a Service Pricing Model, within temporal, operational and usage constraints. A Service Access Token is accepted only if: the current time is within [NotBefore (if present), Expiry] and not before IssuedAt (temporal validity); Audience matches the addressed service or a permitted audience list (audience binding); Signature verifies against KeyID and SignatureAlgorithm (signature verification); the requested operation is in Scope (scope authorisation); when Limits is present, the usage counters do not exceed Limits.max within Limits.window (limits enforcement).",
  "type":                 "object",
  "additionalProperties": false,
  "required":             [ "Header", "TokenID", "Format", "Issuer", "Subject", "Audience", "ServicePricingModelID", "IssuedAt", "Expiry",
                            "KeyID", "SignatureAlgorithm", "Signature", "Scope" ],
  "properties":           {

    "Header":                { "type": "string", "const": "MMM-SAT-V3.0", "description": "Service Access Token Header." },
    "TokenID":               { "type": "string", "description": "Unique token identifier (e.g., UUID/ULID/URN)." },
    "Format":                { "type": "string", "enum": [ "JWT", "JWS-Compact", "JWS-JSON", "CWT" ],
                               "description": "Token representation format." },
    "Issuer":                { "type": "string", "description": "Entity that issued the token (URL, DID, or authority ID)." },
    "Subject":               { "type": "string", "description": "Subscriber/User identifier (pseudonymous if required by privacy policy)." },
    "Audience":              { "type": "string", "description": "Target service/resource identifier the token is intended for." },
    "ServicePricingModelID": { "type": "string", "description": "Identifier of the governing Service Pricing Model." },

    "IssuedAt":              { "$ref": "https://schemas.mpai.community/OSD/V1.5/data/SimpleTime.json", "description": "Token issuance time." },
    "NotBefore":             { "$ref": "https://schemas.mpai.community/OSD/V1.5/data/SimpleTime.json", "description": "Time before which the token must not be accepted." },
    "Expiry":                { "$ref": "https://schemas.mpai.community/OSD/V1.5/data/SimpleTime.json", "description": "Token expiry time." },
    "Nonce":                 { "type": "string", "description": "Anti-replay nonce bound to the token." },

    "KeyID":                 { "type": "string", "description": "Identifier of the public key used to verify the signature (resolves to a JWKS or DID document)." },
    "SignatureAlgorithm":    { "type": "string", "enum": [ "ES256", "ES384", "ES512", "RS256", "RS384", "RS512", "EdDSA" ],
                               "description": "Digital signature algorithm." },
    "Signature":             { "type": "string", "pattern": "^[A-Za-z0-9_-]*$",
                               "description": "Base64url-encoded signature (or detached signature for JWS-JSON)." },

    "Scope":                 { "type": "array", "minItems": 1, "items": { "type": "string" },
                               "description": "Allowed operations/privileges (e.g., request, render, download, support, admin)." },
    "Limits":                { "type": "object", "additionalProperties": false, "required": [ "meter", "unit", "max", "window" ],
                               "description": "Quantitative constraints aligned with pricing.", "properties": {
      "meter":               { "type": "string", "description": "Metered resource name (e.g., requests, GB, minutes)." },
      "unit":                { "type": "string", "description": "Unit of measure (e.g., call, GB, minute)." },
      "max":                 { "type": "number", "minimum": 0, "description": "Maximum usage allowed within the window." },
      "window":              { "$ref": "https://schemas.mpai.community/OSD/V1.5/data/SimpleTime.json", "description": "Validity/usage window for the limit." } } },
    "Entitlements":          { "type": "array", "items": { "type": "string" },
                               "description": "Named entitlements or tier flags (e.g., tier:Pro, feature:HDR)." },
    "CurrencyObject":        { "$ref": "https://schemas.mpai.community/MMM4/V3.0/data/CurrencyObject.json", "description": "Billing currency object." },
    "Rights":                { "$ref": "https://schemas.mpai.community/MMM4/V3.0/data/Rights.json", "description": "Rights/policy governing usage." },
    "Trace":                 { "type": "object", "additionalProperties": false,
                               "description": "MPAI AIF trace metadata for auditability.", "properties": {
      "SourceAIM":           { "type": "string", "description": "The AIM that produced the token." },
      "Time":                { "$ref": "https://schemas.mpai.community/OSD/V1.5/data/SimpleTime.json", "description": "Time of the trace." } } },

    "DescrMetadata":         { "type": "string", "maxLength": 2048, "description": "Descriptive Metadata." } } }
