# Core (Authz)

OpenAPI document for this API: https://docs.neobits.ai/openapi/core-authz.json

## OAuth 2.0 Authorization Server

The Authentication API is a full-featured OAuth 2.0 server implementation that specializes in secure machine-to-machine (M2M) authentication using the client credentials back channel flow. This flow is optimized for server-side applications that need to securely communicate with APIs without user interaction.

## Key Capabilities

As a complete OAuth 2.0 authorization server, the API provides:

-   **Token Endpoint** `/v1/oauth2/token` — Issues access tokens to clients using the client credentials grant type, supporting secure back channel communication
-   **Token Introspection** `/v1/oauth2/introspect` — Validates tokens and returns metadata about their scope and validity
-   **Client Management** — Endpoints for creating, listing, retrieving and deleting OAuth clients, with configurable scopes and credentials
-   **Subscriptions and Products** — Endpoints for reading your tenant's subscriptions and the products they include, which decide the data Fuse returns
-   **Legacy Basic Introspection** `/v1/basic/introspect` — Checks the HTTP Basic credentials of older clients with the legacy `basic` auth method. New clients use OAuth 2.0 and check tokens with `/v1/oauth2/introspect`
-   **Standard Discovery** — OpenID Connect discovery endpoints for automated configuration

## Getting Started

To use the APIs you need a client. To get your first one, send an email to [hello@neobits.no](mailto:hello@neobits.no) with details about your integration needs. A client has three values:

-   `id`: a UUID that identifies the client. Its tokens carry it as `sub` and `client_id`.
-   `username`: a generated name such as `violent-boa-8290`.
-   `password`: the client secret. It is returned only when the client is created, so store it then.

Core is served from your tenant's host: `https://{tenant}.core.server.test.neobits.ai` for test and `https://{tenant}.core.server.prod.neobits.ai` for production.

For OAuth 2.0 authentication, your server application should:

1.  Request an access token via the back channel at the `/v1/oauth2/token` endpoint. Authenticate with HTTP Basic, sending your client's `username` and `password` as `Authorization: Basic base64(username:password)`. Use the password exactly as you received it.
2.  Use that token in the Authorization header for subsequent API requests

## Access Tokens

-   Access tokens are RS256-signed JWTs. The `expires_in` field of the token response gives the lifetime in seconds.
-   Each client has a `max_session_duration`. It defaults to 28800 seconds (8 hours), which is also the maximum.
-   Send `session_duration` with the token request to get a shorter-lived token. The issued lifetime is the lower of the two values.
-   The client credentials grant issues no refresh token. Request a new token before the current one expires. Expired tokens are rejected with `401`.
-   The `resource` parameter becomes the token's audience (for example `https://your-tenant.fuse.server.prod.neobits.ai`). A token is only accepted by the API it was issued for. You can request your tenant's host of each API your client has a scope for.
-   Each tenant's tokens are signed with the tenant's own keys, and test and production are separate environments with their own issuers. Tokens from one tenant or environment are not accepted by another.

## Scopes

Scopes are assigned to your client when it is created. A token carries only the scopes you request in the `scope` parameter (or the scopes of the requested `role`), so request every scope the endpoints you call need. Requesting a scope that is not assigned to your client fails with `invalid_scope`, and calling an endpoint without its scope returns `403`.

Core client management scopes:

-   List clients: `clients:read`
-   Get a client: `client:read`
-   Create a client: `client:write`, plus `subscription:read` to set its `subscription_id`
-   Delete a client: `client:delete`

Core subscription and product scopes:

-   List subscriptions: `subscriptions:read`
-   Get a subscription: `subscription:read`
-   List products: `products:read`

See the [Fuse](https://docs.neobits.ai/api-reference/tag/fuse.md) and [Logic](https://docs.neobits.ai/api-reference/tag/logic.md) introductions for their scopes.

## Roles

A client holds either a `scope` or a `role`. A role grants a bundle of scopes. From most to least privileged, the roles are `owner`, `admin`, `member`, `viewer` and `guest`.

-   A token requested with `role` carries every scope of the role. A token requested with `scope` carries only the scopes you name, which must be among the role's.
-   Only a client with a role can request a token with `role`, for its own role or a less privileged one.
-   When you create a client, you can assign your token's role or a less privileged one, never a more privileged one.

Of the scopes in these docs, each role grants:

-   **owner** and **admin**: `clients:read`, `client:read`, `client:write`, `client:delete`, `subscription:read`, `subscriptions:read`, `products:read`, every Fuse scope and every Logic scope.
-   **member**: `clients:read`, `client:read`, `client:write`, `products:read`; Fuse's `inquiry:write`, `inquiry:read`, `person:read` and `terms:read`; Logic's `environments:read`, `environment:read`, `deployments:read` and `deployment:read`.
-   **viewer**: `clients:read`, `client:read`, `products:read`; Fuse's `inquiry:read`, `person:read` and `terms:read`; Logic's `environments:read`, `environment:read`, `deployments:read` and `deployment:read`.
-   **guest**: none of them.

## Your Client's Scopes and Role

To see what your own client holds:

1.  Request a token for your Core host with `scope=client:read`.
2.  Introspect it at `/v1/oauth2/introspect`. The response's `sub` is your client's `id`.
3.  Call `GET /v1/clients/{id}` with the token. The response has your client's `scope` or its `role`. A client with a role holds the role's scopes, listed under [Roles](#roles).

```bash
curl --request POST 'https://your-tenant.core.server.test.neobits.ai/v1/oauth2/token' \
  --user "$CLIENT_USERNAME:$CLIENT_PASSWORD" \
  --data-urlencode 'grant_type=client_credentials' \
  --data-urlencode 'resource=https://your-tenant.core.server.test.neobits.ai' \
  --data-urlencode 'scope=client:read'

curl --request POST 'https://your-tenant.core.server.test.neobits.ai/v1/oauth2/introspect' \
  --user "$CLIENT_USERNAME:$CLIENT_PASSWORD" \
  --data-urlencode "token=$ACCESS_TOKEN"

curl "https://your-tenant.core.server.test.neobits.ai/v1/clients/$CLIENT_ID" \
  --header "Authorization: Bearer $ACCESS_TOKEN"
```

If the token request fails with `invalid_scope`, your client doesn't hold `client:read`. Ask whoever created the client, or Neobits, which scopes it holds.

## Your Subscription and Products

Fuse serves your client under its subscription, and returns only the data that subscription's products include. If Get a person leaves out fields you expect, such as income, check your products. To see them:

1.  Find your client's `subscription_id` with `GET /v1/clients/{id}`, as in [Your Client's Scopes and Role](#your-client). If it is null, Fuse answers your client with `403`.
2.  Request a token for your Core host with `scope=subscription:read products:read`.
3.  Call `GET /v1/subscriptions/{subscription_id}` for the subscription's plan and dates.
4.  Call `GET /v1/products` for the products of your tenant's active subscriptions, such as `Fuse inntektsinformasjon: A-meldingen`. With one subscription, these are its products.

```bash
curl --request POST 'https://your-tenant.core.server.test.neobits.ai/v1/oauth2/token' \
  --user "$CLIENT_USERNAME:$CLIENT_PASSWORD" \
  --data-urlencode 'grant_type=client_credentials' \
  --data-urlencode 'resource=https://your-tenant.core.server.test.neobits.ai' \
  --data-urlencode 'scope=subscription:read products:read'

curl "https://your-tenant.core.server.test.neobits.ai/v1/subscriptions/$SUBSCRIPTION_ID" \
  --header "Authorization: Bearer $ACCESS_TOKEN"

curl 'https://your-tenant.core.server.test.neobits.ai/v1/products' \
  --header "Authorization: Bearer $ACCESS_TOKEN"
```

The `owner` and `admin` roles grant both scopes, and `member` and `viewer` grant only `products:read`. If the token request fails with `invalid_scope`, its `error_description` names the scope your client isn't assigned: leave it out, or ask whoever created the client, or Neobits.

## Signing Keys

Your tenant's tokens are signed with your tenant's own keys, published at `/.well-known/jwks.json` on your tenant's host. Neobits APIs validate the tokens they receive. If your own services also verify Neobits tokens, use the published keys:

1.  Read `jwks_uri` and `issuer` from `/.well-known/openid-configuration`.
2.  Fetch the key set from `/.well-known/jwks.json`, and cache it no longer than its `Cache-Control: max-age`.
3.  Select the key whose `kid` matches the `kid` in the token header, and verify the RS256 signature.
4.  Check that `iss` equals the issuer, `aud` contains your resource, and `exp` is in the future.

If a token has a `kid` that is not in your cached key set, fetch the key set again before rejecting the token. Never hard-code a key. You can also check a token with the `/v1/oauth2/introspect` endpoint instead of verifying it yourself.

Neobits generates and holds your tenant's keys: a private key is never sent to or returned by the API. The key set can hold several keys. When Neobits replaces a key, the new one is published at least an hour before it signs, and tokens signed with the keys still in the set keep verifying.

## Endpoints

- [OpenID Connect Configuration](https://docs.neobits.ai/api-reference/operation/getOpenIdConfiguration/method/get.md): `GET /.well-known/openid-configuration`
- [JSON Web Key Set](https://docs.neobits.ai/api-reference/operation/getJwks/method/get.md): `GET /.well-known/jwks.json`
- [Get an OAuth 2.0 token](https://docs.neobits.ai/api-reference/operation/createToken/method/post.md): `POST /v1/oauth2/token`
- [Introspect an OAuth 2.0 token](https://docs.neobits.ai/api-reference/operation/introspectOauthToken/method/post.md): `POST /v1/oauth2/introspect`
- [Check legacy Basic credentials](https://docs.neobits.ai/api-reference/operation/introspectBasicToken/method/post.md): `POST /v1/basic/introspect`
- [List Clients](https://docs.neobits.ai/api-reference/operation/getClients/method/get.md): `GET /v1/clients`
- [Create Client](https://docs.neobits.ai/api-reference/operation/createClient/method/post.md): `POST /v1/clients`
- [Get Client](https://docs.neobits.ai/api-reference/operation/getClient/method/get.md): `GET /v1/clients/{client_id}`
- [Delete Client](https://docs.neobits.ai/api-reference/operation/deleteClient/method/delete.md): `DELETE /v1/clients/{client_id}`
- [List Subscriptions](https://docs.neobits.ai/api-reference/operation/getSubscriptions/method/get.md): `GET /v1/subscriptions`
- [Get Subscription](https://docs.neobits.ai/api-reference/operation/getSubscription/method/get.md): `GET /v1/subscriptions/{subscription_id}`
- [List Products](https://docs.neobits.ai/api-reference/operation/getProducts/method/get.md): `GET /v1/products`