AD4M Connection Library and Authentication Wizard
A powerful library that simplifies connecting applications to AD4M executors by handling:
- Executor discovery and connection
- Capability-based authentication
- Token management and storage
- Connection state management
Overview
AD4M uses a capability-based security model to protect user data and control access to agent functionality. Every application that wants to interact with an AD4M executor needs to request specific capabilities, which are then granted (or denied) by the user.
What are Capabilities?
Capabilities in AD4M are like permission tokens that:
- Define what data an application can access (which perspectives)
- Specify what operations an application can perform
- Are scoped to specific domains and functions
- Can be revoked by the user at any time
A capability token might grant permissions like:
- Read access to specific perspectives
- Write access to create or modify links
- Ability to create new perspectives
- Permission to manage agent relationships
- Access to specific language functions
Installation
npm install -s @coasys/ad4m-connect
Authentication Flow
When an application wants to connect to an AD4M executor, it goes through a secure authentication handshake:
- The application requests access with specific capabilities
- The AD4M executor generates a random verification code
- The user must confirm the request and enter the code
- Upon successful verification, a capability token is issued
Visual Flow
Usage
In the Browser
import Ad4mConnectUI from "@coasys/ad4m-connect";
const ui = Ad4mConnect({
appName: "My AD4M App",
appDesc: "A description of what your app does",
appDomain: "myapp.com",
capabilities: [{
with: { domain: "*", pointers: ["*"] },
can: ["*"]
}],
appIconPath: "https://myapp.com/icon.png",
port: 12345,
token: "existing-token",
url: "custom-executor-url"
});
ui.addEventListener("authstatechange", (e) => {
switch(e.detail) {
case "authenticated":
console.log("Successfully authenticated");
break;
case "unauthenticated":
console.log("Not authenticated");
break;
case "locked":
console.log("Agent is locked");
break;
}
});
ui.connect().then((client) => {
console.log("Connected with capabilities");
});
In Node.js / Electron
const { ad4mConnect } = require("@coasys/ad4m-connect/electron");
ad4mConnect({
appName: "Perspect3ve",
appIconPath: path.join(__dirname, "graphics", "Logo.png"),
capabilities: [{ with: { domain: "*", pointers: ["*"] }, can: ["*"] }],
dataPath: path.join(homedir(), ".perspect3ve"),
})
.then(({ client, capabilityToken, executorUrl }) => {
createWindow(client);
})
.catch(() => {
console.log("User closed AD4M connection wizard. Exiting...");
app.exit(0);
process.exit(0);
});
Capability Specification
When requesting capabilities, specify:
{
with: {
domain: string | "*",
pointers: string[] | "*"
},
can: string[] | "*"
}
Examples:
{ with: { domain: "*", pointers: ["*"] }, can: ["*"] }
{ with: { domain: "perspective-uuid", pointers: ["*"] }, can: ["read"] }
{ with: { domain: "friends", pointers: ["*"] }, can: ["read", "add", "remove"] }
Events
The library emits various events to help track connection state:
Mobile Integration
Android Setup
Add to android/app/src/main/AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
+ xmlns:tools="http://schemas.android.com/tools"
package="com.example">
<application
+ android:hardwareAccelerated="true"
>
</application>
+ <uses-permission android:name="android.permission.CAMERA" />
+ <uses-sdk tools:overrideLibrary="com.google.zxing.client.android" />
</manifest>
iOS Setup
Add to Info.plist:
<dict>
+ <key>NSCameraUsageDescription</key>
+ <string>To be able to scan barcodes</string>
</dict>
After changes, run:
npx cap sync
npx cap build
Best Practices
More Information
For more details about AD4M authentication and capabilities: