Skip to content

cerbos/cerbos-epdp-react-native

Repository files navigation

Reference Implementation of a Cerbos ePDP in React Native

This is an standard Expo project created with create-expo-app. Added to this is a reference implementation of calling the checkResources call on an ePDP from Cerbos Hub which is cached locally.

Unfortunately, React Native does not have native support for WebAssembly so in order to use this a react-native-webview is created and the ePDP is loaded into it. Expo takes care of the bundling this local HTML file with the correct SDK implemeented. The CerbosProvider create a context which handles marshalling messages between the RN app and the webview. It also exposed the onDecision callback for capturing the decision logs.

Offline support is handled via caching the latest retrieved ePDP stored on device in async storage. By default a check will be made every 5 minutes for a new version, but the app will continue to serve the last avaliable local version should a network connection be unavailable.

An example call to the ePDP using the useCerbos hook requires passing in the principal, resources and actions to check and then the response. More details can be found in the @cerbos/embedded repo.

const { checkResources, isLoaded } = useCerbos(); // Access Cerbos context
const [result, setResult] = useState<CheckResourcesResponse | null>(null);

// Function to check permissions call when needed
const checkAccess = async () => {
  try {
    const result = await checkResources({
      principal: {
        id: "alice",
        roles: ["USER"],
        attr: { department: "IT" },
      },
      resources: [
        {
          resource: {
            kind: "resource",
            id: "doc1",
            attr: { ownerId: "sally", status: "published" },
          },
          actions: ["view"],
        },
      ],
    });
    console.log("[App] Auth check result:", JSON.stringify(result));
    setResult(result);
  } catch (err) {
    console.error("[App] Auth check failed:", err);
    setResult(null);
  }
};

Every React Native implementation has its own quirks, so this reference project should be adapted to best fit into your application - please get in touch with us for any questions or additional use cases.

Get started

  1. Install dependencies

    npm install
  2. Update ./app/_layout.tsx with the URL of the Cerbos ePDP WASM file - this can be fouund in the Decsion Points section of your Cerbos Hub account under the Embedded tab. It will begin with https://lite.cerbos.cloud/bundle?workspace=...

  3. Start the app

     npx expo start

Further plans

  • Expose the decision audit logs generated by the ePDP back to the application
  • Implement the checkResource helper function to ease implementation.
  • Investigate the best way to do E2E testing with simulators

About

No description, website, or topics provided.

Resources

Code of conduct

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •