XMLHttpRequest: setPrivateToken() method - Web APIs | MDN
Syntax
js
setPrivateToken(privateToken)
Parameters
privateToken-
An object containing options for initiating a private state token operation. Possible properties include:
issuersOptional-
An array of strings containing the URLs of issuers that you want to forward redemption records for. This setting is ignored unless
operationis set tosend-redemption-record, in which case theissuersarray must be included. operation-
A string representing the type of token operation you want to initiate. Possible values are:
token-request-
Initiates a token request operation.
token-redemption-
Initiates a token redemption operation.
send-redemption-record-
Initiates a send redemption record operation.
refreshPolicyOptional-
An enumerated value that specifies the expected behavior when a non-expired redemption record for the current user and site has previously been set. This setting is ignored unless
operationis set totoken-redemption. Possible values are: version-
A number indicating the version of the cryptographic protocol you wish to use when generating a token. Currently this is always set to
1, which is the only version that the specification supports. When specifying theprivateTokenoption, this property is mandatory.
Return value
None (undefined).
Exceptions
InvalidStateErrorDOMException-
Thrown if the associated
XMLHttpRequestis not in an opened state, orsend()has already been called on it. NotAllowedErrorDOMException-
Thrown if use of Private State Token API operations is specifically disallowed by a
private-state-token-issuanceorprivate-state-token-redemptionPermissions Policy. TypeError-
Thrown if the
operationis set tosend-redemption-record, and theissuesarray was empty or not set, or one or more of the specifiedissuersare not trustworthy, HTTPS URLs.
Examples
Issuing a private token
js
const hasToken = await Document.hasPrivateToken(`issuer.example`);
if (!hasToken) {
const request = new XMLHttpRequest();
request.open(
"POST",
"https://issuer.example/.well-known/private-state-token/issuance",
);
request.setPrivateToken({
version: 1,
operation: "token-request",
});
req.send();
}
Redeeming a private token
js
const request = new XMLHttpRequest();
request.open(
"POST",
"https://issuer.example/.well-known/private-state-token/redemption",
);
request.setPrivateToken({
version: 1,
operation: "token-redemption",
refreshPolicy: "none",
});
req.send();
Forwarding a redemption record
js
const hasRR = await Document.hasRedemptionRecord(`issuer.example`);
if (hasRR) {
const request = new XMLHttpRequest();
request.open("POST", "some-resource.example");
request.setPrivateToken({
version: 1,
operation: "send-redemption-record",
issuers: ["https://issuer.example"],
});
req.send();
}
Specifications
| Specification |
|---|
| Private State Token API # dom-xmlhttprequest-setprivatetoken |