# Getting started

## Install

**Rust**

```text title="Terminal"
cargo add strig --features dsl,dsl-fs
cargo add serde_json
```

**Node**

```text title="Terminal"
npm install @the-hades-organization/strig
```

**Python**

```text title="Terminal"
pip install strig-auth
```

**Go**

```text title="Terminal"
go get github.com/The-Hades-Organization/Strig/bindings/go@v0.10.0
```

**Java**

```xml title="pom.xml"
<dependency>
  <groupId>io.github.the-hades-organization</groupId>
  <artifactId>strig</artifactId>
  <version>0.10.0</version>
</dependency>
```

| Language | Needs |
| --- | --- |
| Rust | 1.88 or later |
| Node, Bun, browser | Node 18.17, Bun 1.1 or a current browser; one package, native module or WebAssembly |
| Python | 3.9 or later; one wheel per platform |
| Go | 1.22 or later; no cgo |
| Java, Kotlin | 17 or later; no native code |

All five run the same engine and read the same policy file. The examples on this page run against Strig 0.10 in Rust and Node; [Python](https://strig.vercel.app/docs/integrations/python.md), [Go](https://strig.vercel.app/docs/integrations/go.md) and [Java](https://strig.vercel.app/docs/integrations/jvm.md) have their own pages.

## Write the smallest useful policy

Save this as `policy.strig`. One file holds the catalog, the roles, who has them and the grants.

```rules title="policy.strig"
language rules

kinds project, document
broad org

permission document.view on project, document
permission document.edit on project, document depends_on document.view

role viewer
role editor inherits viewer

user:ana has role editor in project:apollo

g-view: allow role viewer to document.view on project in org:acme
g-edit: allow role editor to document.edit on project in org:acme
```

Ana edits in Apollo. An editor is also a viewer, and `document.edit` needs `document.view` on the same resource. Both grants sit on `project` inside `org:acme`.

## Ask the first question

Can Ana edit the document `spec`, in Apollo, in Acme? The program compiles the file, builds the engine, finds Ana and asks. The Node tab does the same in JavaScript.

**Rust**

```rust title="src/main.rs"
use strig::dsl::fs::read_file;
use strig::{Context, Request, Resource};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let permission = std::env::args().nth(1).expect("permission");
    let policy = read_file("policy.strig")?.policy;
    let engine = policy.engine_spec().build()?;
    let context = Context::new(&policy.grants);

    let ana = policy.subjects.iter().find(|s| s.id.as_deref() == Some("ana")).expect("ana");
    let document = Resource::new("document", "spec").within("project", "apollo").within("org", "acme");

    let decision = engine.decide(&context, &Request::new(ana, &permission, &document));
    println!("{}", serde_json::to_string_pretty(&decision)?);
    Ok(())
}
```

```text title="Terminal"
cargo run -- document.edit
```

```json title="Output"
{
  "effect": "allow",
  "reason": {
    "kind": "grant",
    "grant_id": "g-edit",
    "permission": "document.edit",
    "rank": 1,
    "strategy": "most_specific"
  },
  "matched": [
    "g-edit"
  ]
}
```

**Node**

```ts title="ask.mjs"
import { readFileSync } from 'node:fs';
import { load } from '@the-hades-organization/strig';

const { compile, Engine, Context } = await load();
const policy = compile(readFileSync('policy.strig', 'utf8'), { file: 'policy.strig' });
const { catalog, roles, strategy } = policy;
const engine = new Engine({ catalog, roles, strategy });
const context = new Context({ grants: policy.grants });

const subject = policy.subjects.find((s) => s.id === 'ana');
const resource = {
  path: [
    { kind: 'document', id: 'spec' },
    { kind: 'project', id: 'apollo' },
    { kind: 'org', id: 'acme' },
  ],
};

const decision = engine.decide(context, { subject, permission: process.argv[2], resource });
console.log(JSON.stringify(decision, null, 2));
```

```text title="Terminal"
node ask.mjs document.edit
```

```json title="Output"
{
  "effect": "allow",
  "matched": [
    "g-edit"
  ],
  "reason": {
    "grantId": "g-edit",
    "kind": "grant",
    "permission": "document.edit",
    "rank": 1,
    "strategy": "most_specific"
  }
}
```

**allow**

## Read the answer

| Field | What it says |
| --- | --- |
| `effect` | The verdict: `allow` or `deny`. |
| `reason.kind` | Why: `grant` here. Other values appear below. |
| `reason.grant_id` | The grant that decided. Node spells it `grantId`. |
| `reason.strategy` | The rule that picked it: `most_specific`, the default. |
| `matched` | Every grant that applied to the question, in source order. |

`document.view` was not asked, but `document.edit` depends on it, and Ana holds it through `g-view`: she is an editor, and an editor is a viewer.

## Deny something

Add one line to `policy.strig`. It freezes editing for everyone in Acme.

```rules title="policy.strig"
g-freeze: deny anyone to document.edit on project in org:acme
```

Run the same question again.

**Rust**

```json title="Output"
{
  "effect": "deny",
  "reason": {
    "kind": "grant",
    "grant_id": "g-freeze",
    "permission": "document.edit",
    "rank": 1,
    "strategy": "most_specific"
  },
  "matched": [
    "g-edit",
    "g-freeze"
  ]
}
```

**Node**

```json title="Output"
{
  "effect": "deny",
  "matched": [
    "g-edit",
    "g-freeze"
  ],
  "reason": {
    "grantId": "g-freeze",
    "kind": "grant",
    "permission": "document.edit",
    "rank": 1,
    "strategy": "most_specific"
  }
}
```

**deny**

Both grants applied, on the same step of the path. With `most_specific`, a `deny` wins there. Viewing is untouched: `cargo run -- document.view` still answers `allow`, by `g-view`.

## See it fail closed

Ask for a permission the catalog does not declare. Remove `g-freeze` first, so the question stands on its own.

```text title="Terminal"
cargo run -- document.delete
```

```json title="Output"
{
  "effect": "deny",
  "reason": {
    "kind": "unknown_permission"
  }
}
```

Now make `g-edit` depend on something the engine cannot see. Replace its line with this one, and ask for `document.edit` again. Ana has no `mfa` attribute.

```rules title="policy.strig"
g-edit: allow role editor to document.edit on project in org:acme when subject.mfa == true
```

```json title="Output"
{
  "effect": "deny",
  "reason": {
    "kind": "no_applicable_grant"
  },
  "warnings": [
    "grant g-edit: atributo ausente: subject.mfa"
  ]
}
```

A condition the engine cannot evaluate never opens access. The `allow` is dropped, nothing else applies, and the answer is `deny` with the cause in `warnings`.

> **Note**
>
> The text of a warning is in Portuguese. Branch on `reason.kind`, not on the warning text.

Rust and Node print the same for these two.

## Next steps

- [Concepts](https://strig.vercel.app/docs/concepts.md): Catalog, grants, scope and the strategies that combine them.

- [Policy language](https://strig.vercel.app/docs/policy-language.md): The same policy in Blocks, Rules and Actors.

- [A multi-tenant SaaS](https://strig.vercel.app/docs/guides/saas-multi-tenant.md): A complete policy, from the catalog to the checks in a service.
