Class Permit

The Permit class represents the main entry point for interacting with the Permit.io SDK. The SDK constructor expects an object implementing the IPermitConfig interface.

Example usage:

import { Permit } from 'permitio';

const permit = new Permit({
// this is typically the same API Key you would use for the PDP container
token: "[YOUR_API_KEY]",
// in production, you might need to change this url to fit your deployment
pdp: "http://localhost:7766",
...
});

// creates (or updates) a user on that can be assigned roles and permissions
const { user } = await permit.api.users.sync({
// the user key must be a unique id of the user
key: 'auth0|elon',
// optional params
email: 'elonmusk@tesla.com',
first_name: 'Elon',
last_name: 'Musk',
// user attributes can be used in attribute-based access-control policies
attributes: {
age: 50,
favoriteColor: 'red',
},
});

// 'document' is the protected resource we are enforcing access to
const resource = 'document';
// the action the user is trying to do on the resource
const action = 'read';

const permitted = await permit.check(user, action, resource);
if (permitted) {
console.log('User is authorized to read a document.');
} else {
console.log('User is not authorized to read a document.');
}

Hierarchy

  • Permit

Implements

Constructors

  • Constructs a new instance of the Permit class with the specified configuration.

    Parameters

    • config: RecursivePartial<IPermitConfig>

      The configuration for the Permit SDK.

    Returns Permit

Properties

Access the Permit REST API using this property.

Usage example:

const permit = new Permit(config);
permit.api.roles.create(...);

Access the SDK configuration using this property. Once the SDK is initialized, the configuration is read-only.

Usage example:

const permit = new Permit(config);
const pdpUrl = permit.config.pdp;

Access the Permit Elements API using this property.

Usage example:

const permit = new Permit(config);
permit.elements.loginAs(user, tenant);

Methods

  • Checks multiple requests within the specified context.

    Parameters

    • checks: ICheckQuery[]

      The check requests.

    • Optional context: Context

      The context object representing the context in which the action is performed.

    • Optional config: CheckConfig

    Returns Promise<boolean[]>

    array containing true if the user is authorized, false otherwise for each check request.

    Throws

    PermitConnectionError if an error occurs while sending the authorization request to the PDP.

    Throws

    PermitPDPStatusError if received a response with unexpected status code from the PDP.

  • Checks if a user is authorized to perform an action on a resource within the specified context.

    Parameters

    • user: string | IUser

      The user object representing the user.

    • action: string

      The action to be performed on the resource.

    • resource: string | IResource

      The resource object representing the resource.

    • Optional context: Context

      The context object representing the context in which the action is performed.

    • Optional config: CheckConfig

    Returns Promise<boolean>

    true if the user is authorized, false otherwise.

    Throws

    PermitConnectionError if an error occurs while sending the authorization request to the PDP.

    Throws

    PermitPDPStatusError if received a response with unexpected status code from the PDP.

  • Get all tenants available in the system.

    Parameters

    • user: string | IUser
    • action: string
    • resource: string | IResource
    • Optional context: Context
    • Optional sdk: string

    Returns Promise<TenantDetails[]>

    An array of TenantDetails representing all tenants.

  • Get all permissions for the specified user.

    Parameters

    • user: string | IUser

      The user object representing the user.

    • Optional tenants: string[]

      The list of tenants to filter the permissions on ( given by roles ).

    • Optional resources: string[]

      The list of resources to filter the permissions on ( given by resource roles ).

    • Optional resource_types: string[]

      The list of resource types to filter the permissions on ( given by resource roles ).

    • Optional config: CheckConfig

    Returns Promise<IUserPermissions>

    object with key as the resource identifier and value as the resource details and permissions.

    Throws

    PermitConnectionError if an error occurs while sending the authorization request to the PDP.

    Throws

    PermitPDPStatusError if received a response with unexpected status code from the PDP.

Generated using TypeDoc