# Add a model sign-in with an extension

> Let an installed Fungi extension fill an empty model sign-in slot. Declare the login in package.json, export it from ./logins and enable it for your user.

An installed extension can provide a model sign-in that Fungi does not provide
itself. Once you enable it, the sign-in appears in `fungi login`,
`fungi acp login` and `/login`, labelled with the extension that supplied it.
This page describes the contract with a made-up provider, `example-provider`.
The example is a sketch, not a package to install.

Extensions are trusted code that runs as your OS user, and a sign-in handles
refresh tokens. Enable one only if you trust its author. For the rest of the
extension contract, read
[Write a Shiitake extension](/guides/write-an-extension/).

## Declare the login

Name each sign-in in the `fungi` block of `package.json`, and export a
`./logins` entry. A package that declares logins without a built `./logins`
entry is refused when you add it.

```json
{
  "name": "example-provider-login",
  "version": "1.0.0",
  "type": "module",
  "exports": { "./logins": "./logins.js" },
  "fungi": {
    "id": "example-provider-login",
    "logins": [{ "providerId": "example-provider", "method": "subscription" }]
  },
  "dependencies": { "@earendil-works/pi-ai": "<the version Fungi pins>" }
}
```

`method` is `subscription` for an OAuth sign-in or `api_key` for a key. An
extension declares at most eight logins, once each per provider and method.

## Return Pi's auth object

Fungi calls Pi's model library. A login is the same auth object Pi uses, so the
extension returns Pi's `OAuthAuth` or `ApiKeyAuth` from its default export:

```js
export default function logins({ z }) {
  return [
    {
      providerId: "example-provider",
      method: "subscription",
      oauth: {
        name: "Example provider",
        isSubscription: true,
        async login(interaction) {
          // Ask with interaction.prompt, show links with interaction.notify,
          // then return the OAuth credential.
        },
        async refresh(credential, signal) {
          // Exchange the refresh token; throw if it is no longer valid.
        },
        async toAuth(credential) {
          return { apiKey: credential.access };
        },
      },
    },
  ];
}
```

The factory may be async and receives one host port, `z`, Fungi's copy of Zod.
What it returns must match what `package.json` declares, exactly. A module that
throws, returns something else, cannot load or takes longer than five seconds to
load contributes nothing, and Fungi reports why. Built-in sign-ins still work. A
failed extension is never shown as a signed-in login.

Pin the `@earendil-works/pi-ai` version Fungi uses and ship the package
unbundled. Pi loads its sign-in flows by relative import, which a bundle breaks.

## What Fungi accepts

Fungi merges a login only when all of these hold:

- the provider is one Fungi already lists, because an extension adds no
  provider;
- the sign-in slot is empty, so an extension never replaces a login Fungi
  provides;
- Fungi's credential policy allows an extension to fill that slot, on your own
  machine and for you alone, never for a Team or hosted Fungi;
- no other enabled extension claims the same slot. If two do, Fungi refuses both
  and says so rather than picking one.

The subscription kill switch that can turn off a provider's sign-in applies to
extensions too. Each refusal is reported with its reason.

## Enable it

```sh
fungi extension add <git-url | npm-spec | path>
fungi extension enable logins example-provider-login
fungi login --provider example-provider --auth-type oauth
```

`add` lists the logins a package declares. `enable logins` applies to you on
this machine, not to one project or Agent, because a credential belongs to one
person. It takes effect the next time Fungi opens its models, so a running Agent
picks it up when it next starts. `fungi extension disable logins <id>` withdraws
it and keeps any credential already saved. `fungi extension remove` clears it
too.

Over SSH, where a browser cannot reach your machine, `fungi login` picks the
copy-code method when the sign-in offers one. Pass `--method <id>` to choose a
different one.
