@faable/auth-sdk 2.9.12 → 2.9.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -2
- package/dist/FaableAuthApi.js +11 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/management_errors.d.ts +17 -0
- package/dist/management_errors.js +68 -0
- package/dist/version.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -17,12 +17,12 @@ Programmatically manage FaableAuth users, teams, connections and clients through
|
|
|
17
17
|
> SDK itself has no Node-only dependencies and bundles for the browser. What
|
|
18
18
|
> you must never ship to a browser or any untrusted runtime is a
|
|
19
19
|
> **`client_secret`** or an **API key** — those grant management scopes over
|
|
20
|
-
> the
|
|
20
|
+
> the tenant. A user's own bearer token or the browser's session cookie
|
|
21
21
|
> only grant what that user can do, and those are fine client-side.
|
|
22
22
|
|
|
23
23
|
| Strategy | Credential | Grants | Where |
|
|
24
24
|
| ----------------------- | --------------------------------- | ------------------------------------------------------------------ | --------------------------------------- |
|
|
25
|
-
| `authClientCredentials` | `client_id` + **`client_secret`** | The
|
|
25
|
+
| `authClientCredentials` | `client_id` + **`client_secret`** | The client's Management API scopes (its allow-list; none by default) | **Server only** |
|
|
26
26
|
| `authApikey` | **API key** (`fak_…`) | The management scopes of the key | **Server only** |
|
|
27
27
|
| `authBearer` | A token the caller already holds | Whatever that token's scopes and subject allow | Anywhere the token itself may live |
|
|
28
28
|
| `authCookie` | The browser's session cookie | The signed-in user's own session (`/me`, MFA, passkeys…) | Browser, same-origin with the auth host |
|
|
@@ -66,6 +66,16 @@ const api = FaableAuthApi.create({
|
|
|
66
66
|
});
|
|
67
67
|
```
|
|
68
68
|
|
|
69
|
+
**Allow the client its Management API scopes first.** The token carries at most
|
|
70
|
+
the client's allow-list (`management_scopes`): with no `scope` in `auth`, the
|
|
71
|
+
whole list; with `scope`, the requested ones that are on it. A new client's list
|
|
72
|
+
is empty, so until you allow scopes (dashboard → your Auth tenant → Clients →
|
|
73
|
+
the client → OAuth → **Management API scopes**) every call fails with a
|
|
74
|
+
`FaableApiError` whose message names `management_scopes_empty` and says where to
|
|
75
|
+
fix it; asking only for scopes outside the list names
|
|
76
|
+
`management_scope_not_allowed`. See
|
|
77
|
+
[Calling the Faable Management API](https://faable.com/docs/auth/oauth-flows/client-credentials#management-api-scopes).
|
|
78
|
+
|
|
69
79
|
By default the token is requested from `<domain>/oauth/token`. If your API host
|
|
70
80
|
is **not** the auth server, override it with `auth.domain`:
|
|
71
81
|
|
package/dist/FaableAuthApi.js
CHANGED
|
@@ -3,13 +3,16 @@ import { FaableApiError } from "@faable/sdk-base";
|
|
|
3
3
|
import { GeneratedFaableAuthApi } from "./api/generated-client.js";
|
|
4
4
|
import { authClientCredentials } from "@faable/sdk-base";
|
|
5
5
|
import { SDK_CLIENT } from "./version.js";
|
|
6
|
+
import { withManagementTokenErrors } from "./management_errors.js";
|
|
6
7
|
// auth-sdk exists to drive the Faable management API, so a client_credentials
|
|
7
8
|
// client should "just work" against it from `domain` alone. The management API
|
|
8
9
|
// audience is `faable:management:<account_id>`, where `<account_id>` is this
|
|
9
10
|
// host's tenant. We discover it from the public OIDC discovery document (which
|
|
10
11
|
// resolves the account by host and exposes its id as `account`) lazily on the
|
|
11
|
-
// first token request, and memoize it. Omitting `scope` then yields the
|
|
12
|
-
//
|
|
12
|
+
// first token request, and memoize it. Omitting `scope` then yields the
|
|
13
|
+
// client's own Management API allow-list (`management_scopes`) server-side —
|
|
14
|
+
// not the whole catalog, and nothing at all while that list is empty; the two
|
|
15
|
+
// refusals get an actionable message (management_errors.ts).
|
|
13
16
|
//
|
|
14
17
|
// The memo is module-level, keyed by host. It used to be a closure created per
|
|
15
18
|
// constructor, so it only ever helped an instance that outlived several token
|
|
@@ -86,6 +89,12 @@ export class FaableAuthApi extends GeneratedFaableAuthApi {
|
|
|
86
89
|
baseURL,
|
|
87
90
|
...params,
|
|
88
91
|
auth,
|
|
92
|
+
// Same strategy, with the Management API allow-list refusals of the
|
|
93
|
+
// token endpoint explained (management_errors.ts). Only for
|
|
94
|
+
// client_credentials: other strategies never call /oauth/token.
|
|
95
|
+
...(usesClientCredentials && {
|
|
96
|
+
authStrategy: withManagementTokenErrors(params.authStrategy ?? authClientCredentials),
|
|
97
|
+
}),
|
|
89
98
|
// Identify ourselves as a first-party client (`x-faable-client:
|
|
90
99
|
// auth-sdk/<version>+<sha>`, surfaced as the `client`/`client_version`/
|
|
91
100
|
// `client_sha` tags), and forward any consumer-supplied `name`/`instance`
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export * from "./FaableAuthApi.js";
|
|
2
2
|
export { SDK_CLIENT, commit, version } from "./version.js";
|
|
3
|
+
export { MANAGEMENT_SCOPES_DOCS } from "./management_errors.js";
|
|
3
4
|
export * from "./api/generated-client.js";
|
|
4
5
|
export * from "./api/api-types.js";
|
|
5
6
|
export * from "./api/types.js";
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export * from "./FaableAuthApi.js";
|
|
2
2
|
export { SDK_CLIENT, commit, version } from "./version.js";
|
|
3
|
+
// The Management API allow-list refusals of the token endpoint, explained.
|
|
4
|
+
export { MANAGEMENT_SCOPES_DOCS } from "./management_errors.js";
|
|
3
5
|
export * from "./api/generated-client.js";
|
|
4
6
|
export * from "./api/api-types.js";
|
|
5
7
|
export * from "./api/types.js";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { FaableApiError } from "@faable/sdk-base";
|
|
2
|
+
import type { AuthStrategyBuilder } from "@faable/sdk-base";
|
|
3
|
+
export declare const MANAGEMENT_SCOPES_DOCS = "https://faable.com/docs/auth/oauth-flows/client-credentials#management-api-scopes";
|
|
4
|
+
/**
|
|
5
|
+
* The same error with an actionable message when it is one of the two
|
|
6
|
+
* allow-list refusals of the token endpoint; `undefined` otherwise.
|
|
7
|
+
*/
|
|
8
|
+
export declare const managementTokenError: (err: unknown, client_id?: string, scope?: string | string[]) => FaableApiError | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* Wraps a client_credentials strategy so a refused Management API token
|
|
11
|
+
* surfaces as {@link managementTokenError}. Every other outcome — tokens,
|
|
12
|
+
* other errors, the 401 renewal — is the wrapped strategy's own.
|
|
13
|
+
*/
|
|
14
|
+
export declare const withManagementTokenErrors: <TConfig extends {
|
|
15
|
+
client_id?: string | undefined;
|
|
16
|
+
scope?: string | string[] | undefined;
|
|
17
|
+
}>(builder: AuthStrategyBuilder<TConfig>) => AuthStrategyBuilder<TConfig>;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { FaableApiError } from "@faable/sdk-base";
|
|
2
|
+
// The Management API token a client_credentials client obtains is bounded by
|
|
3
|
+
// the client's own allow-list (`Client.management_scopes`, auth ≥ the release
|
|
4
|
+
// that introduced it): a request with no `scope` — what this SDK sends by
|
|
5
|
+
// default — gets the whole list, and an EMPTY list is refused. The token
|
|
6
|
+
// endpoint answers with RFC 6749 errors, whose body has no `message`, so the
|
|
7
|
+
// sdk-base error would read "ERR_BAD_REQUEST (status=403, …)". These two
|
|
8
|
+
// codes get a sentence that says what to change and where. Nothing else is
|
|
9
|
+
// touched: the original response stays on the error (`error_code`, `status`).
|
|
10
|
+
export const MANAGEMENT_SCOPES_DOCS = "https://faable.com/docs/auth/oauth-flows/client-credentials#management-api-scopes";
|
|
11
|
+
const WHERE = "In the Faable dashboard open your Auth tenant → Clients → this client → OAuth → " +
|
|
12
|
+
"Management API scopes";
|
|
13
|
+
const explain = (code, client_id, scope) => {
|
|
14
|
+
const client = client_id ? `client ${client_id}` : "this client";
|
|
15
|
+
if (code === "management_scopes_empty") {
|
|
16
|
+
return (`FaableAuthApi: auth refused a Management API token to ${client} because ` +
|
|
17
|
+
"it is not allowed any Management API scopes (management_scopes_empty). " +
|
|
18
|
+
`${WHERE} and allow the scopes this backend uses (e.g. read:users), or send ` +
|
|
19
|
+
`\`management_scopes\` on POST /client/:id. See ${MANAGEMENT_SCOPES_DOCS}`);
|
|
20
|
+
}
|
|
21
|
+
if (code === "management_scope_not_allowed") {
|
|
22
|
+
return (`FaableAuthApi: none of the requested scopes${scope ? ` (${scope})` : ""} ` +
|
|
23
|
+
`is in the Management API scopes of ${client} (management_scope_not_allowed). ` +
|
|
24
|
+
`Request scopes that are on its list, or ${WHERE.charAt(0).toLowerCase()}${WHERE.slice(1)} ` +
|
|
25
|
+
`and add them. See ${MANAGEMENT_SCOPES_DOCS}`);
|
|
26
|
+
}
|
|
27
|
+
return undefined;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* The same error with an actionable message when it is one of the two
|
|
31
|
+
* allow-list refusals of the token endpoint; `undefined` otherwise.
|
|
32
|
+
*/
|
|
33
|
+
export const managementTokenError = (err, client_id, scope) => {
|
|
34
|
+
if (!(err instanceof FaableApiError))
|
|
35
|
+
return undefined;
|
|
36
|
+
const message = explain(err.error_code, client_id, Array.isArray(scope) ? scope.join(" ") : scope);
|
|
37
|
+
if (!message)
|
|
38
|
+
return undefined;
|
|
39
|
+
// The response is kept so `error_code`, `isErrorCode()` and `isStatus()`
|
|
40
|
+
// answer as before; only the sentence changes.
|
|
41
|
+
const explained = new FaableApiError(message, err.response);
|
|
42
|
+
// FaableApiError prefers the body's `message` over the one passed in; this
|
|
43
|
+
// sentence is the point, so it wins.
|
|
44
|
+
const status = err.response?.status;
|
|
45
|
+
explained.message = status ? `${message} (status=${status})` : message;
|
|
46
|
+
explained.code = err.code;
|
|
47
|
+
explained.cause = err;
|
|
48
|
+
return explained;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Wraps a client_credentials strategy so a refused Management API token
|
|
52
|
+
* surfaces as {@link managementTokenError}. Every other outcome — tokens,
|
|
53
|
+
* other errors, the 401 renewal — is the wrapped strategy's own.
|
|
54
|
+
*/
|
|
55
|
+
export const withManagementTokenErrors = (builder) => (config, context) => {
|
|
56
|
+
const strategy = builder(config, context);
|
|
57
|
+
return {
|
|
58
|
+
...strategy,
|
|
59
|
+
headers: async () => {
|
|
60
|
+
try {
|
|
61
|
+
return await strategy.headers();
|
|
62
|
+
}
|
|
63
|
+
catch (err) {
|
|
64
|
+
throw (managementTokenError(err, config?.client_id, config?.scope) ?? err);
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
};
|
package/dist/version.js
CHANGED
|
@@ -9,13 +9,13 @@
|
|
|
9
9
|
// login pages could not import the SDK at all. Same pattern as auth-js.
|
|
10
10
|
//
|
|
11
11
|
// The sentinels MUST stay byte-identical to the `from` values in `.releaserc`.
|
|
12
|
-
export const version = "2.9.
|
|
12
|
+
export const version = "2.9.13";
|
|
13
13
|
// Short git SHA of the released commit. The version dates a build; this names
|
|
14
14
|
// the exact tree, so a canonical log line or an audit entry leads straight to
|
|
15
15
|
// `git show <sha>`. Deliberately NOT hex: an unreleased build (dev, a local
|
|
16
16
|
// link) cannot be mistaken for a real commit — auth only records values that
|
|
17
17
|
// look like a SHA, and this one never will.
|
|
18
|
-
export const commit = "
|
|
18
|
+
export const commit = "e26f6c9";
|
|
19
19
|
// What this SDK writes in `x-faable-client`. Exported for a consumer that
|
|
20
20
|
// builds a strategy on its own (outside `FaableAuthApi`) and still wants the
|
|
21
21
|
// token request attributed — a bare `authClientCredentials` stamps only what
|