Skip to content
LogoLogo

AccessKeyTokenSource

AccessKeyTokenSource is a WalletApi2TokenSource for an access key (pxk_…) that an organisation admin issued in the wallet UI. It exchanges the key at the wallet UI's API gateway (POST auth/access-key/token) for access tokens (pxt_…). It authorizes each wallet call with Authorization: DPoP pxt_… and a fresh DPoP proof.

You rarely make one yourself. WalletApi2CredentialProvider.withAccessKey and WalletApi2VcSigner.withAccessKey make one and expose it as tokenSource. The guide Use an access key in scripts, CI and MCP covers setup and CI.

Usage

const  = new ({
  : 'https://wallet.example.org/api/gateway',
  : .. as string
})
 
const  = await .() // asks GET auth/account what the key may do
.(., .)
await .() // ends the token. The key keeps working.
 
// Or read PONTUSX_ACCESS_KEY or PONTUSX_ACCESS_KEY_FILE, and PONTUSX_WALLET_URL
const  = .()

Options

OptionTypePurpose
gatewaystringThe API gateway, <wallet URL>/api/gateway (AccessKeyTokenSource.gatewayUrl(walletUrl) builds it). Required. It must use https:// and be written as a URL parser reads it.
accessKeystringThe key, pxk_…. nautilus checks its form and CRC-32 offline. Give this or accessKeyFile.
accessKeyFilestringA file that holds the key (a mounted secret). nautilus reads and trims it at once. It reads the file again when the gateway refuses the key, so it picks up a rotated key.
scopes('present' | 'sign_ddo')[]Defaults to ['present'].
walletId, addressstringThe wallet to act from, for a key with several wallets. The provider defaults address to the consumer.
organizationstringThe organisation the key must belong to. nautilus checks it on every token and in check().
resourcestringThe RFC 8707 resource, which is wallet-api2's ACCESS_KEYS_PUBLIC_URL. Defaults to gateway.
actorstring | () => string | undefined | Promise<…>An opaque end-user id that nautilus sends as X-Access-Key-Actor on wallet calls, for the audit log. Printable ASCII (0x20 to 0x7e, spaces allowed), 1 to 128 characters, not blank. nautilus refuses a value that contains pxk_ or pxt_ (any case) or a JWT with invalid_request before it sends anything. A function may return undefined for no actor. It only attributes calls and must never hold an inbound token.
dpopSignerDpopSigner{ jwk, sign(input) } for a DPoP key held elsewhere (HSM, KMS, a key pinned to the access key). Defaults to a new non-extractable P-256 key in memory. A signer that throws or answers a signature that is not 64 bytes fails with dpop_signer_failed.
allowInsecureTransportbooleanAccept http:// on localhost, 127.0.0.1 or ::1 (a local stack). Other hosts always need https://.
timeoutMsnumberThe timeout of each request, 1 to 2147483647 ms. Defaults to 30 s.
fetchImpltypeof fetchA fetch implementation for tests.

Methods and properties

MemberWhat it does
getToken(signal?)Returns a current token, the cached one or a new one. Concurrent callers share one request.
authorize({ method, url, signal })Returns the Authorization and DPoP headers, and X-Access-Key-Actor, for one request to the gateway. Refuses any other URL with wrong_endpoint.
invalidate(token?)Forgets the token, if it is still the current one.
check(signal?)Calls GET auth/account and returns the key id and name, organisation, wallet and address, scopes, expiry, credential ids, presentAsOrg, ddoSigning, unboundRequests and the DDO signing identity (signing: { keyId, did }). It first forgets the refusals the source answers from memory for 60 seconds, so it asks the gateway again. For a key with several wallets, pin walletId or address, or the gateway answers wallet_required.
revoke(signal?)Ends the current token at the gateway, with its proof. Waits for a pending token request first. Also ends the tokens of earlier revocations that failed for a passing reason, and those of the per-address sources the provider made.
revokeKey(signal?)Revokes the key itself and all its tokens. You cannot undo it.
keyIdThe key id, ak_…. It is not secret.
walletId, address, organizationThe values of the current token, once the gateway issued one.
gateway, endpoint, scopes, resourceThe configured values.
toJSON(), toString(), util.inspectShow the key id and the display form pxk_<id>…<last 4>, never the key or a token. The last 4 are the last 4 characters of the full key, as the wallet UI shows them.

parseAccessKey(value) checks a key offline and returns { keyId, display }.

Token handling

  • nautilus sends the key only in the form body of POST auth/access-key/token (with grant_type, resource, scope and wallet_id or address) and of revokeKey(), with redirect: 'error'. It never puts the key in a header, a URL or a log, and never sends it to a wallet route.
  • nautilus checks each token answer for the pxt_ form, the type DPoP, a lifetime of 1 s to 24 h, the scopes asked for, the key id, and the expected organisation, address and wallet.
  • nautilus uses a token until 30 seconds before it expires, or until half its lifetime when that is under a minute. Then it renews the token once for all callers. If a renewal fails for a passing reason (rate_limited, a network error), nautilus keeps the old token while it has more than 5 seconds left. It revokes a renewed token's predecessor in the background.
  • When a wallet call gets token_invalid or token_expired, nautilus gets one new token and sends the call once more. After dpop_invalid, it sends the call once more with a fresh proof.
  • A coded 401 is retried by its code only. dpop_required or access_key_not_accepted_here never fetch a new token, even when WWW-Authenticate says invalid_token.
  • The source answers a refusal that a new request would only repeat from memory, so it does not send the key again. invalid_client, key_revoked and key_expired stay for the life of the source. key_suspended, key_disabled, access_keys_not_allowed_for_organization, ip_not_allowed, invalid_target, invalid_scope and organization_mismatch stay for 60 seconds, or until check(). The wallet_* refusals and consumer_mismatch stay for 60 seconds for the address asked for. With accessKeyFile the source reads the file again meanwhile and uses a changed key at once.
  • After a 429 the source asks for no token until Retry-After has passed (seconds or an HTTP date) and at least a backoff of 1 s, doubled per 429 in a row up to 60 s. It uses a token that still works meanwhile. retryAfter on the error is in seconds.
  • A token that the source refuses after the gateway issued it (another organisation, wallet, address or scope) is revoked in the background, so it does not hold a live-token slot.
  • One source holds one token and one DPoP key, so it is safe in a long-running server. Each process or replica makes its own source. Together they must stay under the key's maxLiveTokens. A provider made by withAccessKey without walletId or address holds one token per consumer address on the same DPoP key, so concurrent orders for different addresses never share a token.

Errors

The source throws WaltIdWalletError with code and hint (who fixes it), and with status and retryAfter when they are known. The guide's error table lists the codes and their causes.