# Personal credentials

> Give each employee's agent its own tokens, such as a git token or a kubeconfig. Set variables by hand in the console, or let a hook on your side issue them when a person is created and revoke them when the person is removed.

Source: https://eggent.ai/docs/enterprise-credentials/

Each person's agent works under that person's name, so it needs that person's credentials: a token for your git server, a kubeconfig for their namespace, an account in an internal system. Eggent puts them into the person's workspace as environment variables, stored encrypted. There are two ways to provide them.

## By hand {#manual}

Section **Personal credentials** in the [admin console](https://eggent.ai/docs/enterprise-console.md): choose a person, add a variable (`GIT_TOKEN`, `KUBECONFIG_B64`) and its value. Values are one line; encode a file in base64. The variable reaches the workspace's environment on its next start.

## A hook {#hook}

A hook is an address on your side. Eggent calls it when something happens to a person, and you answer with that person's variables.

**Events.** `member.created`, `member.teams_changed`, `member.deleted`.

**Request.** A `POST` with JSON, signed with HMAC-SHA256 using the secret shown in the console. The signature travels in a header; its name and format are in the contract shown in the console next to the hook's address.

```json
{
  "event": "member.created",
  "member": { "id": "acme-anna", "name": "Anna", "email": "anna@company.tld", "teams": ["Backend"] }
}
```

**Answer.** The variables for this person:

```json
{ "env": { "GIT_TOKEN": "…", "GIT_USER": "anna" } }
```

Eggent puts them into the workspace before it starts for the first time. On `member.teams_changed` you can answer with a new set, for example a token with different rights. On `member.deleted` revoke the person's tokens on your side.

## An example hook {#example}

A hook in Node.js that creates a user on your git server and issues a personal token. Replace the calls to your git server's API with its real ones.

```js
import http from "node:http";
import crypto from "node:crypto";

const SECRET = process.env.EGGENT_HOOK_SECRET;
const SIGNATURE_HEADER = "x-…"; // the header named in the console's contract
const GIT = "https://git.company.tld/api";
const ADMIN = { Authorization: `Bearer ${process.env.GIT_ADMIN_TOKEN}`, "Content-Type": "application/json" };

http.createServer(async (req, res) => {
  let body = "";
  for await (const chunk of req) body += chunk;
  const expected = crypto.createHmac("sha256", SECRET).update(body).digest("hex");
  const given = String(req.headers[SIGNATURE_HEADER] || "");
  if (given.length !== expected.length || !crypto.timingSafeEqual(Buffer.from(given), Buffer.from(expected))) {
    res.writeHead(401).end();
    return;
  }
  const { event, member } = JSON.parse(body);
  if (event === "member.created") {
    await fetch(`${GIT}/users`, { method: "POST", headers: ADMIN, body: JSON.stringify({ login: member.id, email: member.email }) });
    const token = await (await fetch(`${GIT}/users/${member.id}/tokens`, { method: "POST", headers: ADMIN })).json();
    res.writeHead(200, { "Content-Type": "application/json" }).end(JSON.stringify({ env: { GIT_TOKEN: token.value } }));
    return;
  }
  if (event === "member.deleted") await fetch(`${GIT}/users/${member.id}`, { method: "DELETE", headers: ADMIN });
  res.writeHead(200, { "Content-Type": "application/json" }).end("{}");
}).listen(8099);
```

## Which value wins {#priority}

A value set by hand in the console wins over the same variable from the hook. Remove the hand-set value to let the hook's value through.

## Security {#security}

The agent can read its own environment, so it can read its own tokens. Give each token the narrowest rights the person's work needs: their own group on the git server, their own namespace in the cluster, read-only where reading is enough.
