@manta-eu/auth 0.1.10 → 0.2.0

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 CHANGED
@@ -117,6 +117,37 @@ const getAccessToken = cachedToken(() => mintWithClientCredentials({ baseUrl, re
117
117
 
118
118
  Keep the secret on a server; never ship it to a browser.
119
119
 
120
+ ## Tokens for your app's own users
121
+
122
+ A custom app with machine access gets a token for one of its own users, by the app's id for them, or
123
+ for a visitor it has not identified, by its session id for them. Manta keeps their chats, and no
124
+ memories:
125
+
126
+ ```ts
127
+ import { cachedToken, customAppUserAccessToken } from "@manta-eu/auth/web";
128
+
129
+ // One cached getter per person, so each reuses their token until it nears expiry. Evict idle entries in a real app.
130
+ const tokens = new Map<string, () => Promise<string>>();
131
+ const tokenFor = (userId: string) => {
132
+ let get = tokens.get(userId);
133
+ if (get === undefined) {
134
+ get = cachedToken(() =>
135
+ customAppUserAccessToken({ clientId, clientSecret, userId, name, email, teams: ["/Sales/EU"] }),
136
+ );
137
+ tokens.set(userId, get);
138
+ }
139
+ return get;
140
+ };
141
+ ```
142
+
143
+ A visitor works the same way, keyed by your session id and with `anonymousId: sessionId` in place of `userId`.
144
+
145
+ It sends the app's secret, so it runs on the app's server only; the token it returns may go to the
146
+ browser. `datastores` narrows the token to some of the datastores the app reaches. `teams` places
147
+ the person in those teams of the app's organization: an empty list places them in none, and leaving
148
+ it out places them in every team the app reaches. A refusal throws `RefusedRequestError`, whose
149
+ `status` tells `isTransientStatus` whether a retry can help (429, 503) or not (401, 403).
150
+
120
151
  ## Issuer and discovery
121
152
 
122
153
  `issuerUrl({ baseUrl, realm })` names the realm's issuer, and `discoveryUrl({ baseUrl, realm })` its
@@ -0,0 +1,33 @@
1
+ import { type CustomAppUserProfile } from "@manta-eu/lib/custom-app";
2
+ import { type FetchLike } from "./http.js";
3
+ export type CustomAppUserGrantBase = {
4
+ /** The custom app's client ID and secret, held only by the app's server. */
5
+ clientId: string;
6
+ clientSecret: string;
7
+ /** Narrows the token to these datastores; leave out to reach every datastore the app reaches. */
8
+ datastores?: string[];
9
+ /**
10
+ * Places the person in these teams of the app's organization, by path like /Sales/EU, as a member of them. An empty
11
+ * list places them in no team; leaving it out places them in every team the app reaches.
12
+ */
13
+ teams?: string[];
14
+ /** Manta's address, https://app.hiremanta.com unless you name another deployment. */
15
+ apiUrl?: string;
16
+ };
17
+ /** A person your app knows: Manta keeps their chats under your id for them, and no memories. */
18
+ export type IdentifiedCustomAppUser = CustomAppUserGrantBase & CustomAppUserProfile & {
19
+ userId: string;
20
+ anonymousId?: never;
21
+ };
22
+ /** A visitor your app has not identified, by your session id for them: they chat, and Manta keeps no memories. */
23
+ export type AnonymousCustomAppVisitor = CustomAppUserGrantBase & {
24
+ anonymousId: string;
25
+ userId?: never;
26
+ };
27
+ export type CustomAppUserAccessTokenOptions = IdentifiedCustomAppUser | AnonymousCustomAppVisitor;
28
+ /**
29
+ * Gets a Manta access token for one of the app's own users, or for an anonymous visitor. It sends the app's client
30
+ * secret, so call it on the app's server only; the token it returns may go to the browser.
31
+ */
32
+ export declare function customAppUserAccessToken(options: CustomAppUserAccessTokenOptions, fetchImpl?: FetchLike): Promise<string>;
33
+ //# sourceMappingURL=custom-app-user-token.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"custom-app-user-token.d.ts","sourceRoot":"","sources":["../src/custom-app-user-token.ts"],"names":[],"mappings":"AAAA,OAAO,EAOL,KAAK,oBAAoB,EAE1B,MAAM,0BAA0B,CAAC;AAGlC,OAAO,EAAE,KAAK,SAAS,EAAyC,MAAM,WAAW,CAAC;AAElF,MAAM,MAAM,sBAAsB,GAAG;IACnC,4EAA4E;IAC5E,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,iGAAiG;IACjG,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IACtB;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IACjB,qFAAqF;IACrF,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,gGAAgG;AAChG,MAAM,MAAM,uBAAuB,GAAG,sBAAsB,GAC1D,oBAAoB,GAAG;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,WAAW,CAAC,EAAE,KAAK,CAAA;CAAE,CAAC;AAEjE,kHAAkH;AAClH,MAAM,MAAM,yBAAyB,GAAG,sBAAsB,GAAG;IAAE,WAAW,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,KAAK,CAAA;CAAE,CAAC;AAEzG,MAAM,MAAM,+BAA+B,GAAG,uBAAuB,GAAG,yBAAyB,CAAC;AAoBlG;;;GAGG;AACH,wBAAsB,wBAAwB,CAC5C,OAAO,EAAE,+BAA+B,EACxC,SAAS,GAAE,SAAiB,GAC3B,OAAO,CAAC,MAAM,CAAC,CAoCjB"}
@@ -0,0 +1,56 @@
1
+ import { customAppClientIdProblem, customAppUserDatastoresProblem, customAppUserIdProblem, customAppUserProfileProblem, customAppUserTeamsProblem, customAppUserTokenPath, } from "@manta-eu/lib/custom-app";
2
+ import { isPresent } from "@manta-eu/lib/present";
3
+ import { defaultDeployment } from "./default-deployment.js";
4
+ import { postTo, readJson, RefusedRequestError } from "./http.js";
5
+ function requestFor(options) {
6
+ const { clientId, clientSecret, datastores, teams } = options;
7
+ const narrowed = { ...(datastores === undefined ? {} : { datastores }), ...(teams === undefined ? {} : { teams }) };
8
+ if (options.userId === undefined) {
9
+ return { client_id: clientId, client_secret: clientSecret, anonymous_id: options.anonymousId, ...narrowed };
10
+ }
11
+ const { userId, name, email, fields } = options;
12
+ return {
13
+ client_id: clientId,
14
+ client_secret: clientSecret,
15
+ user_id: userId,
16
+ ...(name === undefined ? {} : { name }),
17
+ ...(email === undefined ? {} : { email }),
18
+ ...(fields === undefined ? {} : { fields }),
19
+ ...narrowed,
20
+ };
21
+ }
22
+ /**
23
+ * Gets a Manta access token for one of the app's own users, or for an anonymous visitor. It sends the app's client
24
+ * secret, so call it on the app's server only; the token it returns may go to the browser.
25
+ */
26
+ export async function customAppUserAccessToken(options, fetchImpl = fetch) {
27
+ if (typeof globalThis.document !== "undefined") {
28
+ throw new Error("customAppUserAccessToken runs on your server only: it sends the app's client secret");
29
+ }
30
+ const clientIdProblem = customAppClientIdProblem(options.clientId);
31
+ if (clientIdProblem !== undefined)
32
+ throw new Error(clientIdProblem);
33
+ if (!isPresent(options.clientSecret))
34
+ throw new Error("The app's client secret is required");
35
+ const problem = customAppUserIdProblem(options.userId ?? options.anonymousId) ??
36
+ customAppUserDatastoresProblem(options.datastores) ??
37
+ customAppUserTeamsProblem(options.teams) ??
38
+ (options.userId === undefined ? undefined : customAppUserProfileProblem(options));
39
+ if (problem !== undefined)
40
+ throw new Error(problem);
41
+ const endpoint = new URL(customAppUserTokenPath, options.apiUrl ?? defaultDeployment.apiUrl).toString();
42
+ const response = await postTo(endpoint, {
43
+ method: "POST",
44
+ headers: { "Content-Type": "application/json", Accept: "application/json" },
45
+ body: JSON.stringify(requestFor(options)),
46
+ }, fetchImpl);
47
+ if (!response.ok) {
48
+ const refusal = await readJson(response, endpoint).catch(() => ({}));
49
+ throw new RefusedRequestError(`Manta refused the custom app user token (${response.status}). ${refusal.error_description ?? ""}`.trim(), response.status);
50
+ }
51
+ const body = await readJson(response, endpoint);
52
+ if (typeof body.access_token !== "string")
53
+ throw new Error("Manta answered without an access token");
54
+ return body.access_token;
55
+ }
56
+ //# sourceMappingURL=custom-app-user-token.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"custom-app-user-token.js","sourceRoot":"","sources":["../src/custom-app-user-token.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,wBAAwB,EACxB,8BAA8B,EAC9B,sBAAsB,EACtB,2BAA2B,EAC3B,yBAAyB,EACzB,sBAAsB,GAGvB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAClD,OAAO,EAAE,iBAAiB,E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QACjB,MAAM,OAAO,GAAG,MAAM,QAAQ,CAAiC,QAAQ,EAAE,QAAQ,CAAC,CAAC,KAAK,CACtF,GAAmC,EAAE,CAAC,CAAC,EAAE,CAAC,CAC3C,CAAC;QACF,MAAM,IAAI,mBAAmB,CAC3B,4CAA4C,QAAQ,CAAC,MAAM,MAAM,OAAO,CAAC,iBAAiB,IAAI,EAAE,EAAE,CAAC,IAAI,EAAE,EACzG,QAAQ,CAAC,MAAM,CAChB,CAAC;IACJ,CAAC;IACD,MAAM,IAAI,GAAG,MAAM,QAAQ,CAA4B,QAAQ,EAAE,QAAQ,CAAC,CAAC;IAC3E,IAAI,OAAO,IAAI,CAAC,YAAY,KAAK,QAAQ;QAAE,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;IACrG,OAAO,IAAI,CAAC,YAAY,CAAC;AAC3B,CAAC"}
package/dist/index.d.ts CHANGED
@@ -9,4 +9,5 @@ export { cachedToken, mintWithClientCredentials, withRenewal, type ClientCredent
9
9
  export { discoveryUrl, issuerUrl, type OidcClient, type Realm } from "./oidc.js";
10
10
  export { endWebLogin, finishWebLogin, refreshWebLogin, startWebLogin, webLogoutUrl, type StartWebLoginOptions, type WebLoginStart, type WebLogoutOptions, type WebTokens, } from "./web-login.js";
11
11
  export { webLoginRoutes, type WebLoginRoutes, type WebLoginRoutesOptions, type WebSession } from "./web-routes.js";
12
+ export { customAppUserAccessToken, type AnonymousCustomAppVisitor, type CustomAppUserAccessTokenOptions, type CustomAppUserGrantBase, type IdentifiedCustomAppUser, } from "./custom-app-user-token.js";
12
13
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,WAAW,EACX,YAAY,EACZ,gBAAgB,EAChB,KAAK,EACL,WAAW,EACX,MAAM,EACN,gBAAgB,EAChB,oBAAoB,EACpB,kBAAkB,EAClB,eAAe,EAC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K,gBAAgB,EACrB,KAAK,SAAS,GACf,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,cAAc,EAAE,KAAK,cAAc,EAAE,KAAK,qBAAqB,EAAE,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAC;AACnH,OAAO,EACL,wBAAwB,EACxB,KAAK,yBAAyB,EAC9B,KAAK,+BAA+B,EACpC,KAAK,sBAAsB,EAC3B,KAAK,uBAAuB,GAC7B,MAAM,4BAA4B,CAAC"}
package/dist/index.js CHANGED
@@ -9,4 +9,5 @@ export { cachedToken, mintWithClientCredentials, withRenewal, } from "./token.js
9
9
  export { discoveryUrl, issuerUrl } from "./oidc.js";
10
10
  export { endWebLogin, finishWebLogin, refreshWebLogin, startWebLogin, webLogoutUrl, } from "./web-login.js";
11
11
  export { webLoginRoutes } from "./web-routes.js";
12
+ export { customAppUserAccessToken, } from "./custom-app-user-token.js";
12
13
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,WAAW,EACX,YAAY,EACZ,gBAAgB,EAChB,KAAK,EACL,WAAW,EACX,MAAM,EACN,gBAAgB,EAChB,oBAAoB,EACpB,kBAAkB,EAClB,eAAe,GAGhB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAC9D,OAAO,EACL,sBAAsB,EACtB,mBAAmB,EACnB,uBAAuB,EACvB,wBAAwB,GAKzB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EACL,mBAAmB,EACnB,kBAAkB,EAClB,gBAAgB,GAMjB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,EAAE,QAAQ,EAAE,mBAAmB,EA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E;AAED,wBAAgB,mBAAmB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAK3E;AAED,wBAAgB,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAE/D;AAqBD,wBAAsB,yBAAyB,CAC7C,WAAW,EAAE,iBAAiB,EAC9B,SAAS,GAAE,SAAiB,GAC3B,OAAO,CAAC,MAAM,CAAC,CAgBjB;AAED,wBAAgB,WAAW,CAAC,IAAI,EAAE,WAAW,EAAE,KAAK,GAAE,MAAM,MAAiB,GAAG,oBAAoB,CA6BnG"}
package/dist/token.js CHANGED
@@ -77,8 +77,11 @@ export async function mintWithClientCredentials(credentials, fetchImpl = fetch)
77
77
  const endpoint = tokenEndpoint(baseUrl, realm);
78
78
  const response = await postTo(endpoint, clientCredentialsRequest(credentials), fetchImpl);
79
79
  if (!response.ok) {
80
+ const { error } = (await response.json().catch(() => ({})));
80
81
  throw new Error(`Keycloak refused the client credentials for realm ${realm} with status ${response.status}. ` +
81
- `Check the client id and the secret against ${endpoint}.`);
82
+ (error === "unauthorized_client"
83
+ ? "This client may not mint tokens on its own: for a custom app, turn on Machine access."
84
+ : `Check the client id and the secret against ${endpoint}.`));
82
85
  }
83
86
  return readAccessToken(await response.json(), realm);
84
87
  }
package/dist/token.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"token.js","sourceRoot":"","sources":["../src/token.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,cAAc,EAAE,MAAM,yBAAyB,CAAC;AACrE,OAAO,EAAkB,WAAW,EAAE,MAAM,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAarF,SAAS,WAAW,CAAC,KAAkB;IACrC,OAAO,OAAQ,KAAuC,CAAC,KAAK,KAAK,UAAU,CAAC;AAC9E,CAAC;AAED,kHAAkH;AAClH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAI,KAAkB,EAAE,IAAyC;IAChG,IAAI,WAA+B,CA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M,KAAK,GAAG,MAAM,IAAI,EAAE,CAAC;QAC3B,mFAAmF;QACnF,IAAI,GAAG,EAAE,KAAK,EAAE,SAAS,EAAE,oBAAoB,CAAC,KAAK,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC;QAElE,OAAO,KAAK,CAAC;IACf,CAAC,CAAC;IAEF,MAAM,MAAM,GAAG,KAAK,IAAI,EAAE;QACxB,IAAI,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,SAAS,EAAE,KAAK,EAAE,CAAC;YAAE,OAAO,IAAI,CAAC,KAAK,CAAC;QAEhE,MAAM,OAAO,GAAG,CAAC,OAAO,KAAK,QAAQ,EAAE,CAAC,OAAO,CAAC,GAAG,EAAE;YACnD,IAAI,OAAO,KAAK,OAAO;gBAAE,OAAO,GAAG,SAAS,CAAC;QAC/C,CAAC,CAAC,CAAC,CAAC;QAEJ,OAAO,OAAO,CAAC;IACjB,CAAC,CAAC;IAEF,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE;QAC3B,2FAA2F;QAC3F,KAAK,EAAE,CAAC,QAAiB,EAAE,EAAE;YAC3B,IAAI,QAAQ,KAAK,SAAS,IAAI,IAAI,EAAE,KAAK,KAAK,QAAQ;gBAAE,IAAI,GAAG,SAAS,CAAC;QAC3E,CAAC;KACF,CAAC,CAAC;AACL,CAAC"}
package/dist/web.d.ts CHANGED
@@ -5,4 +5,5 @@ export { discoveryUrl, issuerUrl, type OidcClient, type Realm } from "./oidc.js"
5
5
  export { accessTokenExpiresAt, cachedToken, mintWithClientCredentials, withRenewal, type ClientCredentials, type RenewableTokenSource, type TokenSource, } from "./token.js";
6
6
  export { endWebLogin, finishWebLogin, refreshWebLogin, startWebLogin, webLogoutUrl, type StartWebLoginOptions, type WebLoginStart, type WebLogoutOptions, type WebTokens, } from "./web-login.js";
7
7
  export { webLoginRoutes, type WebLoginRoutes, type WebLoginRoutesOptions, type WebSession } from "./web-routes.js";
8
+ export { customAppUserAccessToken, type AnonymousCustomAppVisitor, type CustomAppUserAccessTokenOptions, type CustomAppUserGrantBase, type IdentifiedCustomAppUser, } from "./custom-app-user-token.js";
8
9
  //# sourceMappingURL=web.d.ts.map
package/dist/web.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"web.d.ts","sourceRoot":"","sources":["../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,YAAY,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,KAAK,SAAS,EAAE,MAAM,WAAW,CAAC;AAClG,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,KAAK,UAAU,EAAE,KAAK,KAAK,EAAE,MAAM,WAAW,CAAC;AACjF,OAAO,EACL,oBAAoB,EACpB,WAAW,EACX,yBAAyB,EACzB,WAAW,EACX,KAAK,iBAAiB,EACtB,KAAK,oBAAoB,EACzB,KAAK,WAAW,GACjB,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,WAAW,EACX,cAAc,EACd,eAAe,EACf,aAAa,EACb,YAAY,EACZ,KAAK,oBAAoB,EACzB,KAAK,aAAa,EAClB,KAAK,gBAAgB,EACrB,KAAK,SAAS,GACf,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,cAAc,EAAE,KAAK,cAAc,EAAE,KAAK,qBAAqB,EAAE,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAC"}
1
+ {"version":3,"file":"web.d.ts","sourceRoot":"","sources":["../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAC5D,YAAY,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,KAAK,SAAS,EAAE,MAAM,WAAW,CAAC;AAClG,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,KAAK,UAAU,EAAE,KAAK,KAAK,EAAE,MAAM,WAAW,CAAC;AACjF,OAAO,EACL,oBAAoB,EACpB,WAAW,EACX,yBAAyB,EACzB,WAAW,EACX,KAAK,iBAAiB,EACtB,KAAK,oBAAoB,EACzB,KAAK,WAAW,GACjB,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,WAAW,EACX,cAAc,EACd,eAAe,EACf,aAAa,EACb,YAAY,EACZ,KAAK,oBAAoB,EACzB,KAAK,aAAa,EAClB,KAAK,gBAAgB,EACrB,KAAK,SAAS,GACf,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,cAAc,EAAE,KAAK,cAAc,EAAE,KAAK,qBAAqB,EAAE,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAC;AACnH,OAAO,EACL,wBAAwB,EACxB,KAAK,yBAAyB,EAC9B,KAAK,+BAA+B,EACpC,KAAK,sBAAsB,EAC3B,KAAK,uBAAuB,GAC7B,MAAM,4BAA4B,CAAC"}
package/dist/web.js CHANGED
@@ -4,4 +4,5 @@ export { discoveryUrl, issuerUrl } from "./oidc.js";
4
4
  export { accessTokenExpiresAt, cachedToken, mintWithClientCredentials, withRenewal, } from "./token.js";
5
5
  export { endWebLogin, finishWebLogin, refreshWebLogin, startWebLogin, webLogoutUrl, } from "./web-login.js";
6
6
  export { webLoginRoutes } from "./web-routes.js";
7
+ export { customAppUserAccessToken, } from "./custom-app-user-token.js";
7
8
  //# sourceMappingURL=web.js.map
package/dist/web.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"web.js","sourceRoot":"","sources":["../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAE5D,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,mBAAmB,EAAkB,MAAM,WAAW,CAAC;AAClG,OAAO,EAAE,YAAY,EAAE,SAAS,EAA+B,MAAM,WAAW,CAAC;AACjF,OAAO,EACL,oBAAoB,EACpB,WAAW,EACX,yBAAyB,EACzB,WAAW,GAIZ,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,WAAW,EACX,cAAc,EACd,eAAe,EACf,aAAa,EACb,YAAY,GAKb,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,cAAc,EAAoE,MAAM,iBAAiB,CAAC"}
1
+ {"version":3,"file":"web.js","sourceRoot":"","sources":["../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAE5D,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,mBAAmB,EAAkB,MAAM,WAAW,CAAC;AAClG,OAAO,EAAE,YAAY,EAAE,SAAS,EAA+B,MAAM,WAAW,CAAC;AACjF,OAAO,EACL,oBAAoB,EACpB,WAAW,EACX,yBAAyB,EACzB,WAAW,GAIZ,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,WAAW,EACX,cAAc,EACd,eAAe,EACf,aAAa,EACb,YAAY,GAKb,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,cAAc,EAAoE,MAAM,iBAAiB,CAAC;AACnH,OAAO,EACL,wBAAwB,GAKzB,MAAM,4BAA4B,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@manta-eu/auth",
3
- "version": "0.1.10",
3
+ "version": "0.2.0",
4
4
  "description": "Keycloak login for the SDK CLIs: browser and device-code login, silent token renewal, and credential storage; plus the default deployment the SDK targets",
5
5
  "homepage": "https://developers.manta.eu",
6
6
  "keywords": [
@@ -30,7 +30,8 @@
30
30
  "dependencies": {
31
31
  "commander": "^15.0.0",
32
32
  "jose": "^6.2.12",
33
- "openid-client": "^6.8.8"
33
+ "openid-client": "^6.8.8",
34
+ "@manta-eu/lib": "0.2.0"
34
35
  },
35
36
  "devDependencies": {
36
37
  "@types/node": "^24.13.3",
@@ -0,0 +1,100 @@
1
+ import {
2
+ customAppClientIdProblem,
3
+ customAppUserDatastoresProblem,
4
+ customAppUserIdProblem,
5
+ customAppUserProfileProblem,
6
+ customAppUserTeamsProblem,
7
+ customAppUserTokenPath,
8
+ type CustomAppUserProfile,
9
+ type CustomAppUserTokenRequest,
10
+ } from "@manta-eu/lib/custom-app";
11
+ import { isPresent } from "@manta-eu/lib/present";
12
+ import { defaultDeployment } from "./default-deployment.js";
13
+ import { type FetchLike, postTo, readJson, RefusedRequestError } from "./http.js";
14
+
15
+ export type CustomAppUserGrantBase = {
16
+ /** The custom app's client ID and secret, held only by the app's server. */
17
+ clientId: string;
18
+ clientSecret: string;
19
+ /** Narrows the token to these datastores; leave out to reach every datastore the app reaches. */
20
+ datastores?: string[];
21
+ /**
22
+ * Places the person in these teams of the app's organization, by path like /Sales/EU, as a member of them. An empty
23
+ * list places them in no team; leaving it out places them in every team the app reaches.
24
+ */
25
+ teams?: string[];
26
+ /** Manta's address, https://app.hiremanta.com unless you name another deployment. */
27
+ apiUrl?: string;
28
+ };
29
+
30
+ /** A person your app knows: Manta keeps their chats under your id for them, and no memories. */
31
+ export type IdentifiedCustomAppUser = CustomAppUserGrantBase &
32
+ CustomAppUserProfile & { userId: string; anonymousId?: never };
33
+
34
+ /** A visitor your app has not identified, by your session id for them: they chat, and Manta keeps no memories. */
35
+ export type AnonymousCustomAppVisitor = CustomAppUserGrantBase & { anonymousId: string; userId?: never };
36
+
37
+ export type CustomAppUserAccessTokenOptions = IdentifiedCustomAppUser | AnonymousCustomAppVisitor;
38
+
39
+ function requestFor(options: CustomAppUserAccessTokenOptions): CustomAppUserTokenRequest {
40
+ const { clientId, clientSecret, datastores, teams } = options;
41
+ const narrowed = { ...(datastores === undefined ? {} : { datastores }), ...(teams === undefined ? {} : { teams }) };
42
+ if (options.userId === undefined) {
43
+ return { client_id: clientId, client_secret: clientSecret, anonymous_id: options.anonymousId, ...narrowed };
44
+ }
45
+ const { userId, name, email, fields } = options;
46
+ return {
47
+ client_id: clientId,
48
+ client_secret: clientSecret,
49
+ user_id: userId,
50
+ ...(name === undefined ? {} : { name }),
51
+ ...(email === undefined ? {} : { email }),
52
+ ...(fields === undefined ? {} : { fields }),
53
+ ...narrowed,
54
+ };
55
+ }
56
+
57
+ /**
58
+ * Gets a Manta access token for one of the app's own users, or for an anonymous visitor. It sends the app's client
59
+ * secret, so call it on the app's server only; the token it returns may go to the browser.
60
+ */
61
+ export async function customAppUserAccessToken(
62
+ options: CustomAppUserAccessTokenOptions,
63
+ fetchImpl: FetchLike = fetch,
64
+ ): Promise<string> {
65
+ if (typeof (globalThis as { document?: unknown }).document !== "undefined") {
66
+ throw new Error("customAppUserAccessToken runs on your server only: it sends the app's client secret");
67
+ }
68
+ const clientIdProblem = customAppClientIdProblem(options.clientId);
69
+ if (clientIdProblem !== undefined) throw new Error(clientIdProblem);
70
+ if (!isPresent(options.clientSecret)) throw new Error("The app's client secret is required");
71
+ const problem =
72
+ customAppUserIdProblem(options.userId ?? options.anonymousId) ??
73
+ customAppUserDatastoresProblem(options.datastores) ??
74
+ customAppUserTeamsProblem(options.teams) ??
75
+ (options.userId === undefined ? undefined : customAppUserProfileProblem(options));
76
+ if (problem !== undefined) throw new Error(problem);
77
+
78
+ const endpoint = new URL(customAppUserTokenPath, options.apiUrl ?? defaultDeployment.apiUrl).toString();
79
+ const response = await postTo(
80
+ endpoint,
81
+ {
82
+ method: "POST",
83
+ headers: { "Content-Type": "application/json", Accept: "application/json" },
84
+ body: JSON.stringify(requestFor(options)),
85
+ },
86
+ fetchImpl,
87
+ );
88
+ if (!response.ok) {
89
+ const refusal = await readJson<{ error_description?: string }>(response, endpoint).catch(
90
+ (): { error_description?: string } => ({}),
91
+ );
92
+ throw new RefusedRequestError(
93
+ `Manta refused the custom app user token (${response.status}). ${refusal.error_description ?? ""}`.trim(),
94
+ response.status,
95
+ );
96
+ }
97
+ const body = await readJson<{ access_token?: string }>(response, endpoint);
98
+ if (typeof body.access_token !== "string") throw new Error("Manta answered without an access token");
99
+ return body.access_token;
100
+ }
package/src/index.ts CHANGED
@@ -58,3 +58,10 @@ export {
58
58
  type WebTokens,
59
59
  } from "./web-login.js";
60
60
  export { webLoginRoutes, type WebLoginRoutes, type WebLoginRoutesOptions, type WebSession } from "./web-routes.js";
61
+ export {
62
+ customAppUserAccessToken,
63
+ type AnonymousCustomAppVisitor,
64
+ type CustomAppUserAccessTokenOptions,
65
+ type CustomAppUserGrantBase,
66
+ type IdentifiedCustomAppUser,
67
+ } from "./custom-app-user-token.js";
package/src/token.ts CHANGED
@@ -110,9 +110,12 @@ export async function mintWithClientCredentials(
110
110
  const response = await postTo(endpoint, clientCredentialsRequest(credentials), fetchImpl);
111
111
 
112
112
  if (!response.ok) {
113
+ const { error } = (await response.json().catch(() => ({}))) as { error?: unknown };
113
114
  throw new Error(
114
115
  `Keycloak refused the client credentials for realm ${realm} with status ${response.status}. ` +
115
- `Check the client id and the secret against ${endpoint}.`,
116
+ (error === "unauthorized_client"
117
+ ? "This client may not mint tokens on its own: for a custom app, turn on Machine access."
118
+ : `Check the client id and the secret against ${endpoint}.`),
116
119
  );
117
120
  }
118
121
 
package/src/web.ts CHANGED
@@ -23,3 +23,10 @@ export {
23
23
  type WebTokens,
24
24
  } from "./web-login.js";
25
25
  export { webLoginRoutes, type WebLoginRoutes, type WebLoginRoutesOptions, type WebSession } from "./web-routes.js";
26
+ export {
27
+ customAppUserAccessToken,
28
+ type AnonymousCustomAppVisitor,
29
+ type CustomAppUserAccessTokenOptions,
30
+ type CustomAppUserGrantBase,
31
+ type IdentifiedCustomAppUser,
32
+ } from "./custom-app-user-token.js";