Skip to content

Commit

Permalink
Merge pull request #119 from malcolmstill/malcolm/nodejs-doc-fixes
Browse files Browse the repository at this point in the history
Fix node.js examples
  • Loading branch information
divarvel authored Sep 25, 2023
2 parents 360c562 + c592416 commit 54e4659
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions docs/src/usage/nodejs.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,32 @@ const root = new KeyPair();
## Create a token

```javascript
const { biscuit, KeyPair } = require('@biscuit-auth/biscuit-wasm');
const { biscuit, PrivateKey } = require('@biscuit-auth/biscuit-wasm');

const root = new KeyPair();
const userId = "1234";
// a token can be created from a datalog snippet
const biscuitBuilder = biscuit`
const builder = biscuit`
user(${userId});
check if resource("file1");
`;

// facts, checks and rules can be added one by one on an existing builder.
for (let right of ["read", "write"]) {
biscuitBuilder.addFact(fact`right(${right})`);
builder.addFact(fact`right(${right})`);
}

const token = builder.build(root);
const privateKey = PrivateKey.fromString("<private key>");
const token = builder.build(privateKey);
console.log(token.toBase64());
```

## Authorize a token

```javascript
const { authorizer, Biscuit } = require('@biscuit-auth/biscuit-wasm');
const { authorizer, Biscuit, PublicKey } = require('@biscuit-auth/biscuit-wasm');

const token = Biscuit.fromBase64("<base64 string>");
const publicKey = PublicKey.fromString("<public key>");
const token = Biscuit.fromBase64("<base64 string>", publicKey);

const userId = "1234";
const auth = authorizer`
Expand All @@ -91,9 +92,10 @@ const acceptedPolicyCustomLimits = authorizer.authorizeWithLimits({
## Attenuate a token

```javascript
const { block, Biscuit } = require('@biscuit-auth/biscuit-wasm');
const { block, Biscuit, PublicKey } = require('@biscuit-auth/biscuit-wasm');

const token = Biscuit.fromBase64("<base64 string>");
const publicKey = PublicKey.fromString("<public key>");
const token = Biscuit.fromBase64("<base64 string>", publicKey);

// restrict to read only
const attenuatedToken = token.append(block`check if operation("read")`);
Expand All @@ -105,9 +107,10 @@ console.log(attenuatedToken.toBase64());
A sealed token cannot be attenuated further.

```javascript
const { Biscuit } = require('@biscuit-auth/biscuit-wasm');
const { Biscuit, PublicKey } = require('@biscuit-auth/biscuit-wasm');

const token = Biscuit.fromBase64("<base64 string>");
const publicKey = PublicKey.fromString("<public key>");
const token = Biscuit.fromBase64("<base64 string>", publicKey);

const sealedToken = token.sealToken();
```
Expand All @@ -116,9 +119,10 @@ const sealedToken = token.sealToken();


```javascript
const { Biscuit } = require('@biscuit-auth/biscuit-wasm');
const { Biscuit, PublicKey } = require('@biscuit-auth/biscuit-wasm');

const token = Biscuit.fromBase64("<base64 string>");
const publicKey = PublicKey.fromString("<public key>");
const token = Biscuit.fromBase64("<base64 string>", publicKey);

// revocationIds is a list of hex-encoded revocation identifiers,
// one per block
Expand All @@ -133,9 +137,10 @@ if (containsRevokedIds(revocationIds)) {
## Query data from the authorizer

```javascript
const { authorizer, rule, Biscuit } = require('@biscuit-auth/biscuit-wasm');
const { authorizer, rule, Biscuit, PublicKey } = require('@biscuit-auth/biscuit-wasm');

const token = Biscuit.fromBase64("<base64 string>");
const publicKey = PublicKey.fromString("<public key>");
const token = Biscuit.fromBase64("<base64 string>", publicKey);

const userId = "1234";
const auth = authorizer`
Expand Down

0 comments on commit 54e4659

Please sign in to comment.