# authdb-client

Native Node.js client bindings for **AuthDB** – an authentication database with a JSON REST API.

## Installation

```bash
npm install authdb-client
```

The package ships prebuilt native binaries for a range of Node.js ABI versions on Linux / FreeBSD.
If no prebuild is available for your version, see *Building from source* below.

## Usage

```js
const { ClientConnection, Client } = require('authdb-client');

// 1. Create a connection
const con = new ClientConnection();
con.setUrl('https://authdb.example.com:9090');
con.setClientName('my-app');
con.setClientSecret('s3cret');
con.setUsername('admin');
con.setUserPassword('password');

// 2. Create a client and authenticate
const client = new Client(con);
const ok = client.userAuth();
console.log('Authenticated:', ok);

// 3. Query session info
const info = client.sessionInfo();
console.log(info);
// { sessionId: '...', userId: '...', username: '...', domainId: '...' }

// 4. GPO checks
const allowed = client.gpoCheck('<gpo-uuid>');

// 5. Logout
client.remSession();
```

## API

### `ClientConnection`

| Method                        | Description                           |
| ----------------------------- | ------------------------------------- |
| `setUrl(url)`                 | Set the AuthDB server URL             |
| `setClientName(name)`         | Set the client application name       |
| `setClientSecret(secret)`     | Set the client application secret     |
| `setUsername(username)`        | Set the username for authentication   |
| `setUserPassword(password)`   | Set the user password                 |
| `getClientId()`               | Get the assigned client UUID          |
| `getSessionID()`              | Get the current session UUID          |
| `setSessionID(sessionId)`     | Restore a previously obtained session |
| `reConnect()`                 | Reconnect to the server               |

### `Client`

| Method                                      | Returns           | Description                              |
| ------------------------------------------- | ----------------- | ---------------------------------------- |
| `userAuth()`                                | `boolean`         | Authenticate with user credentials       |
| `clientAuth()`                              | `boolean`         | Authenticate with client credentials     |
| `addConnection(name, secret)`               | `string` (UUID)   | Register a new client connection         |
| `remConnection(clientId)`                   | –                 | Remove a client connection               |
| `editConnection(clientId, name, secret)`    | –                 | Edit an existing client connection       |
| `sessionInfo()`                             | `SessionInfo`     | Get current session details              |
| `remSession()`                              | `boolean`         | Invalidate the current session           |
| `gpoCheck(gpoId)`                           | `boolean`         | Check a GPO permission                   |
| `gpoExists(gpoId)`                          | `boolean`         | Check if a GPO exists                    |
| `changeUserPw(oldPw, newPw)`               | –                 | Change the current user's password       |

## Building from Source

Requires the full authdb repository and its C++ dependencies (see the project README).

```bash
git clone <repo-url> authdb
cd authdb
mkdir build && cd build
cmake .. && make
```

Then copy the prebuilt binaries into the npm package:

```bash
node bindings/nodejs/copy-prebuilds.js
```

## Publishing

```bash
cd bindings/nodejs
npm publish
```

## License

BSD-3-Clause – see [LICENSE.txt](LICENSE.txt).
