@owlmeans/client-iam 0.1.18-rc.7 → 0.1.18-rc.70

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.
Files changed (51) hide show
  1. package/README.md +185 -0
  2. package/agent-meta/manifest.json +16 -0
  3. package/agent-meta/skills/client-iam/SKILL.md +131 -0
  4. package/build/append.d.ts +7 -6
  5. package/build/append.d.ts.map +1 -1
  6. package/build/append.js +7 -1
  7. package/build/append.js.map +1 -1
  8. package/build/consent.d.ts +28 -0
  9. package/build/consent.d.ts.map +1 -0
  10. package/build/consent.js +47 -0
  11. package/build/consent.js.map +1 -0
  12. package/build/consts.d.ts +1 -0
  13. package/build/consts.d.ts.map +1 -1
  14. package/build/consts.js +1 -0
  15. package/build/consts.js.map +1 -1
  16. package/build/index.d.ts +7 -0
  17. package/build/index.d.ts.map +1 -1
  18. package/build/index.js +4 -0
  19. package/build/index.js.map +1 -1
  20. package/build/login.d.ts +19 -0
  21. package/build/login.d.ts.map +1 -0
  22. package/build/login.js +18 -0
  23. package/build/login.js.map +1 -0
  24. package/build/organization/types.d.ts +23 -0
  25. package/build/organization/types.d.ts.map +1 -0
  26. package/build/organization/types.js +2 -0
  27. package/build/organization/types.js.map +1 -0
  28. package/build/organization.d.ts +11 -0
  29. package/build/organization.d.ts.map +1 -0
  30. package/build/organization.js +18 -0
  31. package/build/organization.js.map +1 -0
  32. package/build/types.d.ts +11 -0
  33. package/build/types.d.ts.map +1 -0
  34. package/build/types.js +2 -0
  35. package/build/types.js.map +1 -0
  36. package/build/types.local.d.ts +6 -0
  37. package/build/types.local.d.ts.map +1 -0
  38. package/build/types.local.js +2 -0
  39. package/build/types.local.js.map +1 -0
  40. package/package.json +10 -7
  41. package/src/append.ts +10 -5
  42. package/src/consent.ts +53 -0
  43. package/src/consts.ts +2 -0
  44. package/src/index.ts +7 -0
  45. package/src/login.ts +22 -0
  46. package/src/organization/types.ts +23 -0
  47. package/src/organization.ts +29 -0
  48. package/src/types.local.ts +6 -0
  49. package/src/types.ts +13 -0
  50. package/tests/organization.spec.ts +85 -0
  51. package/tests/tsconfig.json +12 -0
package/README.md ADDED
@@ -0,0 +1,185 @@
1
+ # @owlmeans/client-iam
2
+
3
+ The browser half of the OwlMeans IAM in one import, for a web app that signs its people in through
4
+ an OwlMeans OIDC provider. `appendIam` wires the OIDC relying-party guard and puts a consent
5
+ precondition in front of every sign-in; the package also re-exports the sign-in surface
6
+ (`useLogin`, `useLogout`, `withIamGuard`, `iamEntrypoints`), lists the session's organizations and
7
+ switches between them, and re-exports `hasPermission` for showing or hiding a control. It decides
8
+ nothing: permissions are enforced by the app's server (`@owlmeans/server-iam`). It is browser-only
9
+ (it depends on `@owlmeans/web-client` and `@owlmeans/web-oidc-rp`); server code uses
10
+ `@owlmeans/server-iam` and the shared permission model lives in `@owlmeans/iam`.
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ bun add @owlmeans/client-iam@^0.1.18-rc.70
16
+ ```
17
+
18
+ ## Concepts
19
+
20
+ - **Relying-party guard** — `appendOidcGuard` from `@owlmeans/web-oidc-rp`, installed by
21
+ `appendIam`. The provider list is read from `cfg.oidc.providers`.
22
+ - **Consent precondition** — a `LoginService` precondition (`'consent-before-login'`, priority 100)
23
+ that refuses to start a sign-in until the essential consent category is granted, and opens the
24
+ consent dialog in the same gesture.
25
+ - **Acting organization** — the session acts in one organization entity at a time. That is session
26
+ state held by the app's server, moved with `switchOrganization(entitySlug)`, never a request
27
+ parameter.
28
+ - **Organization item** — `OidcOrganizationItem`: `{ entitySlug, title?, owner, groups?, home?, acting }`.
29
+ - **Permission check** — `hasPermission` runs the same check the server gate runs; in a screen it
30
+ only decides whether a control is visible.
31
+
32
+ ## Usage
33
+
34
+ ### Wire sign-in into a web app
35
+
36
+ ```typescript
37
+ import { bindAll } from '@owlmeans/client-entrypoint'
38
+ import { appendIam, iamEntrypoints, withIamGuard } from '@owlmeans/client-iam'
39
+
40
+ appendIam(context) // OIDC guard + consent precondition
41
+
42
+ export const protocols = withIamGuard(appProtocols) // decorate the immutable tree once
43
+ export const bindings = [
44
+ ...bindAll(protocols.api),
45
+ ...iamEntrypoints(), // sign-in pair, organization switch, dispatcher
46
+ ]
47
+ ```
48
+
49
+ An app that sets no cookie at all turns the consent precondition off:
50
+
51
+ ```typescript
52
+ appendIam(context, { consent: { disabled: true } })
53
+ ```
54
+
55
+ A context wired without `appendIam` adds the precondition on its own:
56
+
57
+ ```typescript
58
+ import { requireConsentForLogin } from '@owlmeans/client-iam'
59
+
60
+ requireConsentForLogin(context, { category: 'essential' })
61
+ ```
62
+
63
+ ### Sign-in and sign-out controls
64
+
65
+ ```tsx
66
+ import { useLogin, useLogout } from '@owlmeans/client-iam'
67
+
68
+ export const AccountControls = () => {
69
+ const [href, onLogin] = useLogin('/projects') // land on /projects after signing in
70
+ const onLogout = useLogout()
71
+
72
+ return (
73
+ <>
74
+ <a href={href} onClick={onLogin}>Log in</a>
75
+ <button onClick={onLogout}>Log out</button>
76
+ </>
77
+ )
78
+ }
79
+ ```
80
+
81
+ When consent is missing, the sign-in resolves as `LoginOutcome.Gesture` and the consent dialog
82
+ opens; the person presses "Log in" again after accepting, because the redirect or popup must start
83
+ from a real gesture.
84
+
85
+ ### Organizations of the session
86
+
87
+ ```typescript
88
+ import { organizationSwitchOf, ORGANIZATION_REFUSAL } from '@owlmeans/client-iam'
89
+
90
+ const organizations = organizationSwitchOf(context)
91
+ const items = await organizations.listOrganizations() // the acting one has `acting: true`
92
+
93
+ try {
94
+ await organizations.switchOrganization('hiking-club')
95
+ // every later request, and every screen reading `auth`, acts in hiking-club
96
+ } catch (error) {
97
+ // AuthForbidden(ORGANIZATION_REFUSAL): not one of the person's organizations; the token stays
98
+ }
99
+ ```
100
+
101
+ Both calls go to the app's own server behind `OIDC_GUARD`, so they need the bindings
102
+ `iamEntrypoints()` spreads. A successful switch adopts the re-signed token through `adoptToken`
103
+ from `@owlmeans/client-auth/login`.
104
+
105
+ ### Permissions in a screen
106
+
107
+ ```typescript
108
+ import { hasPermission } from '@owlmeans/client-iam'
109
+
110
+ const canApprove = hasPermission(context.auth().user(), 'loan--approve', { resourceId: loanId })
111
+ ```
112
+
113
+ ## API
114
+
115
+ | Symbol | Kind | Purpose |
116
+ |---|---|---|
117
+ | `appendIam(context, opts?)` | function | `appendOidcGuard` plus `requireConsentForLogin`; `opts` is `AppendIamOptions` |
118
+ | `requireConsentForLogin(ctx, opts?)` | function | Registers the consent precondition on the login service |
119
+ | `organizationSwitchOf(ctx)` | function | Memoized `OrganizationSwitchHelper` of a context |
120
+ | `makeOrganizationSwitchHelper(ctx)` | function | Builds an `OrganizationSwitchHelper` (unmemoized) |
121
+ | `listOrganizations(ctx)`, `switchOrganization(ctx, slug)` | function | Deprecated wrappers over `organizationSwitchOf(ctx)` |
122
+ | `withIamGuard(tree, coguards?)` | function | `withOidcGuard` of `@owlmeans/oidc` — decorated copy of a protocol tree |
123
+ | `iamEntrypoints(dispatcherProps?)` | function | `oidcEntrypoints` of `@owlmeans/web-oidc-rp` |
124
+ | `useLogin(target?)` | hook | `[href, onClick]` for a sign-in control |
125
+ | `useLogout(target?)` | hook | `onClick` for a sign-out control |
126
+ | `LoginOutcome`, `LoginIntent` | enum | Re-exported from `@owlmeans/client-auth/login` |
127
+ | `hasPermission(auth, permission, opts?)` | function | Re-exported from `@owlmeans/iam`; `opts` is `{ scope?, resourceId?, entitySlug? }` |
128
+ | `ORGANIZATION_REFUSAL`, `ORGANIZATION_OWNER_REFUSAL` | const | `AuthForbidden` reasons for a non-member and a non-owner |
129
+ | `CONSENT_LOGIN_PRECONDITION` | const | `'consent-before-login'` |
130
+ | `CLIENT_IAM_SERVICE` | const | `'client-iam-service'` — an alias constant nothing registers |
131
+
132
+ ### Types
133
+
134
+ | Symbol | Purpose |
135
+ |---|---|
136
+ | `AppendIamOptions` | `{ consent?: ConsentLoginOptions }` |
137
+ | `ConsentLoginOptions` | `{ category?: string, disabled?: boolean }`; `category` defaults to `essential` |
138
+ | `OrganizationSwitchHelper` | `{ listOrganizations(), switchOrganization(entitySlug) }` |
139
+ | `OidcOrganizationItem` | One organization of the session |
140
+ | `LoginPlugin`, `LoginRequest`, `LogoutRequest`, `LoginService`, `LoginPrecondition`, `LoginMethod`, `LoginMethodSource`, `LoginScreenProps` | Re-exported from `@owlmeans/client-auth/login` |
141
+ | every `@owlmeans/iam` type | Re-exported with `export type *` |
142
+
143
+ ## Common pitfalls
144
+
145
+ - Decorate the protocol tree with `withIamGuard` before binding it; the helpers never append
146
+ declarations to an already flattened entrypoint list.
147
+ - Organization calls fail without the `iamEntrypoints()` bindings.
148
+ - Never write `auth` or the stored token by hand after a switch — `switchOrganization` already adopts
149
+ the re-signed token; re-read `auth` afterwards.
150
+ - Organizations travel by `entitySlug` only; never send an organization's `entityId` from the
151
+ browser.
152
+ - `ORGANIZATION_REFUSAL` / `ORGANIZATION_OWNER_REFUSAL` are reasons, not messages — phrase them for
153
+ the person.
154
+ - `hasPermission` in a screen hides or shows a control; the server gate is the decision.
155
+ - A client without the provider's `organizations` scope lists no organizations and refuses every
156
+ switch.
157
+
158
+ The `client-iam` skill covers this package; `login-plugins`, `web-oidc-rp` and `server-iam` cover
159
+ the machinery underneath and the server side.
160
+
161
+ ## Related packages
162
+
163
+ - [`@owlmeans/web-oidc-rp`](../web-oidc-rp) — the OIDC guard, bindings and dispatcher
164
+ - [`@owlmeans/oidc`](../oidc) — shared OIDC protocols, `withOidcGuard`, organization types
165
+ - [`@owlmeans/client-auth`](../client-auth) — login service, plugins and `adoptToken`
166
+ - [`@owlmeans/consent`](../consent) — the consent store the precondition reads
167
+ - [`@owlmeans/web-consent`](../web-consent) — the consent dialog the precondition opens
168
+ - [`@owlmeans/iam`](../iam) — the permission model and `hasPermission`
169
+ - [`@owlmeans/server-iam`](../server-iam) — the server gate and organization management
170
+
171
+ <!-- owlmeans:agent-guidance:start -->
172
+ ## Agent guidance
173
+
174
+ This package ships embedded agent skills under `agent-meta/`. After installing your
175
+ `@owlmeans/*` packages, run the OwlMeans agent-skills installer to place them into
176
+ your project's skill store (`.agents/skills/`):
177
+
178
+ ```sh
179
+ npx @owlmeans/agent-skills@^0.1.18-rc.57
180
+ ```
181
+
182
+ The embedded files are version-matched to this package release. Do not edit them
183
+ directly — they are regenerated on each publish. To contribute guidance edits,
184
+ open a PR against the source monorepo.
185
+ <!-- owlmeans:agent-guidance:end -->
@@ -0,0 +1,16 @@
1
+ {
2
+ "schemaVersion": 3,
3
+ "package": "@owlmeans/client-iam",
4
+ "version": "0.1.18-rc.70",
5
+ "generatedAt": "2026-10-20T23:19:58.638Z",
6
+ "canonicalRepo": "https://github.com/owlmeans/common",
7
+ "entries": [
8
+ {
9
+ "kind": "skill",
10
+ "name": "client-iam",
11
+ "category": "package-specific",
12
+ "file": "skills/client-iam/SKILL.md",
13
+ "canonicalPath": ".agents/skills/client-iam/SKILL.md"
14
+ }
15
+ ]
16
+ }
@@ -0,0 +1,131 @@
1
+ ---
2
+ name: client-iam
3
+ description: "How to use @owlmeans/client-iam — the browser half of the OwlMeans IAM in one import: appendIam (OIDC guard plus the consent-before-sign-in precondition), withIamGuard / iamEntrypoints, the re-exported sign-in surface (useLogin, useLogout), the session's organizations (organizationSwitchOf(ctx).listOrganizations) and the organization switch (organizationSwitchOf(ctx).switchOrganization, which adopts the re-signed token), hasPermission for showing or hiding a control, and the refusal reasons ORGANIZATION_REFUSAL / ORGANIZATION_OWNER_REFUSAL. Auto-invoked when wiring sign-in into a web app, rendering an organization switcher, or reading permissions in a screen."
4
+ user-invocable: false
5
+ ---
6
+ <!-- AUTO-GENERATED — do not edit. Regenerate via sync-agent-meta. -->
7
+
8
+ # @owlmeans/client-iam
9
+
10
+ **Layer:** Client (browser-only — it depends on `@owlmeans/web-client` and `@owlmeans/web-oidc-rp`)
11
+ **Install:** `"@owlmeans/client-iam": "^0.1.18-rc.70"` in `dependencies`
12
+
13
+ The browser side of the IAM for an application that signs its people in through an OwlMeans OIDC
14
+ provider. One import wires the relying party, puts the consent precondition in front of every
15
+ sign-in, lists the organizations of the session and moves the session between them. It decides
16
+ nothing: every permission is enforced by the application's server (`@owlmeans/server-iam`); a screen
17
+ reads permissions only to show or hide a control.
18
+
19
+ ## Key Exports
20
+
21
+ | Export | Description |
22
+ |--------|-------------|
23
+ | `appendIam(context, opts?)` | `appendOidcGuard` of `@owlmeans/web-oidc-rp` plus `requireConsentForLogin`. `opts.consent` is `ConsentLoginOptions` — `{ disabled: true }` for an application that sets no cookie at all |
24
+ | `withIamGuard(tree, coguards?)` | `withOidcGuard` of `@owlmeans/oidc`: a decorated copy of an immutable protocol tree with `OIDC_GUARD` ahead of each selected guard (default `DEFAULT_GUARD`) |
25
+ | `iamEntrypoints(dispatcherProps?)` | `oidcEntrypoints` of `@owlmeans/web-oidc-rp`: the browser bindings of the four shared OIDC protocols (sign-in pair and organization switch) and the dispatcher screen |
26
+ | `organizationSwitchOf(ctx).listOrganizations()` | `OidcOrganizationItem[]` of the session — `{ entitySlug, title?, owner, groups?, home?, acting }` |
27
+ | `organizationSwitchOf(ctx).switchOrganization(entitySlug)` | Moves the session into another of its organizations and adopts the re-signed token with `loginTokenOf(ctx).adoptToken` |
28
+ | `requireConsentForLogin(ctx, opts?)`, `CONSENT_LOGIN_PRECONDITION`, `ConsentLoginOptions` | The consent precondition on its own (`'consent-before-login'`, priority 100), for a context wired another way |
29
+ | `useLogin` / `useLogout`, `LoginOutcome`, `LoginIntent` and the login types | Re-exported from `@owlmeans/client-auth/login`, so an application has one IAM import. `useLogin(target)` signs in first and lands on `target` after, consent and other post-sign-in steps included (`client-auth`) |
30
+ | `hasPermission(auth, permission, { scope?, resourceId?, entitySlug? }?)` | Re-exported from `@owlmeans/iam` — the same check the server gate runs |
31
+ | `ORGANIZATION_REFUSAL`, `ORGANIZATION_OWNER_REFUSAL` | `'organization'` / `'organization:owner'` — the `AuthForbidden` reasons of a non-member and of a non-owner |
32
+ | `OidcOrganizationItem` and every `@owlmeans/iam` type | Re-exported types |
33
+ | `CLIENT_IAM_SERVICE` | `'client-iam-service'` — an alias constant nothing registers |
34
+
35
+ ## Wiring
36
+
37
+ ```typescript
38
+ import { bindAll } from '@owlmeans/client-entrypoint'
39
+ import { appendIam, iamEntrypoints, withIamGuard } from '@owlmeans/client-iam'
40
+
41
+ appendIam(context) // guard + consent precondition
42
+
43
+ export const protocols = withIamGuard(appProtocols) // decorate the tree once
44
+ export const bindings = [
45
+ ...bindAll(protocols.api),
46
+ ...iamEntrypoints(), // spread once
47
+ ]
48
+ ```
49
+
50
+ `appendIam` reads the provider list from `cfg.oidc.providers`; an application whose browser is given
51
+ none still offers one generic sign-in method, and its server's default provider decides the issuer
52
+ (`web-oidc-rp`). Decorate the tree before binding it — the helpers never append declarations to a
53
+ flattened entrypoint list.
54
+
55
+ The consent precondition sits on `LoginService.begin`, the one funnel every sign-in mechanic passes
56
+ through and the place the user's gesture is still live: a refusal resolves as
57
+ `LoginOutcome.Gesture` and opens the consent preferences window in the same gesture, and the person
58
+ presses "Log in" again after accepting (`login-plugins`, `web-consent`). A visitor decided for
59
+ automatically (located outside the consent countries — the `consent` skill's geo gate) already has a
60
+ record and passes.
61
+
62
+ ## Organizations of the session
63
+
64
+ A person is one account; an application sees it through its rows, one per organization the person
65
+ belongs to in that application. With the provider's `organizations` scope the session acts in ONE
66
+ of them at a time, and that is session state held by the application's server — never a parameter
67
+ of a request:
68
+
69
+ ```typescript
70
+ import { organizationSwitchOf, ORGANIZATION_REFUSAL } from '@owlmeans/client-iam'
71
+
72
+ const organizations = organizationSwitchOf(context)
73
+ const items = await organizations.listOrganizations() // the acting one carries `acting: true`
74
+ try {
75
+ await organizations.switchOrganization('hiking-club')
76
+ // every request from here on, and every screen reading `auth`, acts in hiking-club
77
+ } catch (error) {
78
+ // AuthForbidden(ORGANIZATION_REFUSAL): not one of the person's organizations — the token stays
79
+ }
80
+ ```
81
+
82
+ - Both calls go to the application's OWN server (`GET /authenticate/oidc/organizations`,
83
+ `POST /authenticate/oidc/organization`), behind `OIDC_GUARD`, so they need the bindings
84
+ `iamEntrypoints()` spreads. The server side — the session record, re-picking, refusals — is
85
+ governed by `server-oidc-rp` → "Organizations".
86
+ - The re-signed token carries the new organization's `entitySlug`, its groups and only its
87
+ permission sets. Re-read `auth` after a switch; nothing else needs resetting.
88
+ - There is no per-request organization selector: a screen offering "act in another organization"
89
+ calls `organizationSwitchOf(ctx).switchOrganization`. A client without the `organizations` scope
90
+ lists none and refuses every switch.
91
+
92
+ Managing organizations — creating one, inviting members, granting in it — is the server's job
93
+ through `makeIamRuntimeClient(context, request)` (`@owlmeans/server-iam`); the browser calls the
94
+ application's own endpoints for it, never the provider.
95
+
96
+ ## Permissions in a screen
97
+
98
+ ```typescript
99
+ import { hasPermission } from '@owlmeans/client-iam'
100
+
101
+ const canApprove = hasPermission(context.auth().user(), 'loan--approve', { resourceId: loanId })
102
+ ```
103
+
104
+ The browser token holds the unbound sets plus the acting organization's sets with their binding
105
+ already stripped, so a screen needs no `entitySlug`. A set without `resources` covers every id. Use
106
+ the answer to hide or show a control only — the server gate decides.
107
+
108
+ ## Rules
109
+
110
+ - One IAM import: take sign-in, organizations and permission checks from here, not from
111
+ `@owlmeans/web-oidc-rp`, `@owlmeans/client-auth/login` and `@owlmeans/iam` separately.
112
+ - Adopt a token only through `loginTokenOf(ctx).adoptToken` / `context.login().adopt(token)` —
113
+ `organizationSwitchOf(ctx).switchOrganization` already does; never write `auth` or the stored
114
+ token by hand.
115
+ - Phrase `ORGANIZATION_REFUSAL` and `ORGANIZATION_OWNER_REFUSAL` as sentences for the person; they
116
+ are reasons, not messages.
117
+ - Never send an organization id from the browser: organizations travel by `entitySlug` only.
118
+
119
+ ## Depends On
120
+
121
+ - `@owlmeans/web-oidc-rp`, `@owlmeans/oidc` — the guard, the bindings and the switch protocols
122
+ - `@owlmeans/client-auth` — the login host and `loginTokenOf(ctx).adoptToken`
123
+ - `@owlmeans/consent` — the consent store the precondition reads
124
+ - `@owlmeans/iam` — types and `hasPermission`
125
+ - `@owlmeans/context`, `@owlmeans/entrypoint`, `@owlmeans/web-client`
126
+
127
+ ## Related
128
+
129
+ - `server-iam` — the gate, the session's organizations on the server, the runtime IAM client
130
+ - `iam` — the permission model (kinds, default classes) and the `IamService`
131
+ - `web-oidc-rp`, `login-plugins`, `login-methods` — the sign-in machinery underneath
package/build/append.d.ts CHANGED
@@ -1,7 +1,5 @@
1
- import type { AppConfig, AppContext } from '@owlmeans/web-client';
2
- import type { WithSharedConfig } from '@owlmeans/oidc';
3
- type IamClientConfig = AppConfig & WithSharedConfig;
4
- type IamClientContext<C extends IamClientConfig = IamClientConfig> = AppContext<C>;
1
+ import type { IamClientConfig, IamClientContext } from './types.local.js';
2
+ import type { AppendIamOptions } from './types.js';
5
3
  /**
6
4
  * One-call OIDC RP setup for a web client context.
7
5
  *
@@ -10,7 +8,10 @@ type IamClientContext<C extends IamClientConfig = IamClientConfig> = AppContext<
10
8
  *
11
9
  * The OIDC provider config (discovery URL, clientId, secret) is read from
12
10
  * context.cfg.oidc.providers[] — populate it before calling appendIam().
11
+ *
12
+ * It also installs the consent precondition, so every application gets the "confirm essential
13
+ * cookies before signing in" rule from the line it already writes. An application that sets no
14
+ * cookie at all passes `{ consent: { disabled: true } }`.
13
15
  */
14
- export declare const appendIam: <C extends IamClientConfig, T extends IamClientContext<C>>(context: T) => T;
15
- export {};
16
+ export declare const appendIam: <C extends IamClientConfig, T extends IamClientContext<C>>(context: T, opts?: AppendIamOptions) => T;
16
17
  //# sourceMappingURL=append.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"append.d.ts","sourceRoot":"","sources":["../src/append.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AACjE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAA;AAGtD,KAAK,eAAe,GAAG,SAAS,GAAG,gBAAgB,CAAA;AACnD,KAAK,gBAAgB,CAAC,CAAC,SAAS,eAAe,GAAG,eAAe,IAAI,UAAU,CAAC,CAAC,CAAC,CAAA;AAElF;;;;;;;;GAQG;AACH,eAAO,MAAM,SAAS,GAAI,CAAC,SAAS,eAAe,EAAE,CAAC,SAAS,gBAAgB,CAAC,CAAC,CAAC,WACvE,CAAC,KACT,CAGF,CAAA"}
1
+ {"version":3,"file":"append.d.ts","sourceRoot":"","sources":["../src/append.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAA;AACzE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAGlD;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,SAAS,GAAI,CAAC,SAAS,eAAe,EAAE,CAAC,SAAS,gBAAgB,CAAC,CAAC,CAAC,WACvE,CAAC,SAAS,gBAAgB,KAClC,CAKF,CAAA"}
package/build/append.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { appendOidcGuard } from '@owlmeans/web-oidc-rp';
2
+ import { requireConsentForLogin } from './consent.js';
2
3
  /**
3
4
  * One-call OIDC RP setup for a web client context.
4
5
  *
@@ -7,9 +8,14 @@ import { appendOidcGuard } from '@owlmeans/web-oidc-rp';
7
8
  *
8
9
  * The OIDC provider config (discovery URL, clientId, secret) is read from
9
10
  * context.cfg.oidc.providers[] — populate it before calling appendIam().
11
+ *
12
+ * It also installs the consent precondition, so every application gets the "confirm essential
13
+ * cookies before signing in" rule from the line it already writes. An application that sets no
14
+ * cookie at all passes `{ consent: { disabled: true } }`.
10
15
  */
11
- export const appendIam = (context) => {
16
+ export const appendIam = (context, opts) => {
12
17
  appendOidcGuard(context);
18
+ requireConsentForLogin(context, opts?.consent);
13
19
  return context;
14
20
  };
15
21
  //# sourceMappingURL=append.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"append.js","sourceRoot":"","sources":["../src/append.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AAKvD;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,CACvB,OAAU,EACP,EAAE;IACL,eAAe,CAAO,OAAO,CAAC,CAAA;IAC9B,OAAO,OAAO,CAAA;AAChB,CAAC,CAAA"}
1
+ {"version":3,"file":"append.js","sourceRoot":"","sources":["../src/append.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AACvD,OAAO,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAA;AAKrD;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,CACvB,OAAU,EAAE,IAAuB,EAChC,EAAE;IACL,eAAe,CAAO,OAAO,CAAC,CAAA;IAC9B,sBAAsB,CAAC,OAAO,EAAE,IAAI,EAAE,OAAO,CAAC,CAAA;IAE9C,OAAO,OAAO,CAAA;AAChB,CAAC,CAAA"}
@@ -0,0 +1,28 @@
1
+ import type { BasicContext } from '@owlmeans/context';
2
+ import type { ConsentLoginOptions } from './types.js';
3
+ /**
4
+ * Refuse to start a sign-in flow until the required consent category is granted, and open the
5
+ * consent dialog in the same gesture so the user can grant it.
6
+ *
7
+ * It sits on `LoginService.begin` rather than on `useLogin`, on a screen, or in a login plugin,
8
+ * and each of those alternatives is wrong for its own reason:
9
+ *
10
+ * - `useLogin` is one of four call sites (a header control, a sign-in-required panel, the
11
+ * dispatcher, and the surrogate screen). Enforcing there means enforcing four times, and the
12
+ * fifth call site somebody adds is unguarded.
13
+ * - a `LoginPlugin` is SELECTED, not chained — `plugin(env)` returns exactly one — so a "consent
14
+ * plugin" would displace the redirect or surrogate plugin rather than run before it, and the
15
+ * flow it was guarding would stop happening at all.
16
+ * - `begin` is the single funnel every mechanic passes through, and it is the one place where the
17
+ * user's gesture is still live, which is what lets the dialog open without being blocked.
18
+ *
19
+ * The check is synchronous because `begin` is: crossing a microtask before a plugin's
20
+ * `window.open` hands the window to the popup blocker.
21
+ *
22
+ * A refusal resolves `begin` as `LoginOutcome.Gesture`, which already means "cannot proceed
23
+ * without a fresh user gesture — render a control". After accepting, the user presses Log in
24
+ * again; that second press is deliberate, not an oversight, because the redirect or popup must
25
+ * originate from a real gesture.
26
+ */
27
+ export declare const requireConsentForLogin: (ctx: BasicContext<any>, opts?: ConsentLoginOptions) => void;
28
+ //# sourceMappingURL=consent.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"consent.d.ts","sourceRoot":"","sources":["../src/consent.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAErD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAErD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,eAAO,MAAM,sBAAsB,QAC5B,YAAY,CAAC,GAAG,CAAC,SAAS,mBAAmB,KACjD,IAoBF,CAAA"}
@@ -0,0 +1,47 @@
1
+ import { CONSENT_ESSENTIAL, consentStore } from '@owlmeans/consent';
2
+ import { ensureLoginService } from '@owlmeans/client-auth/login';
3
+ import { CONSENT_LOGIN_PRECONDITION } from './consts.js';
4
+ /**
5
+ * Refuse to start a sign-in flow until the required consent category is granted, and open the
6
+ * consent dialog in the same gesture so the user can grant it.
7
+ *
8
+ * It sits on `LoginService.begin` rather than on `useLogin`, on a screen, or in a login plugin,
9
+ * and each of those alternatives is wrong for its own reason:
10
+ *
11
+ * - `useLogin` is one of four call sites (a header control, a sign-in-required panel, the
12
+ * dispatcher, and the surrogate screen). Enforcing there means enforcing four times, and the
13
+ * fifth call site somebody adds is unguarded.
14
+ * - a `LoginPlugin` is SELECTED, not chained — `plugin(env)` returns exactly one — so a "consent
15
+ * plugin" would displace the redirect or surrogate plugin rather than run before it, and the
16
+ * flow it was guarding would stop happening at all.
17
+ * - `begin` is the single funnel every mechanic passes through, and it is the one place where the
18
+ * user's gesture is still live, which is what lets the dialog open without being blocked.
19
+ *
20
+ * The check is synchronous because `begin` is: crossing a microtask before a plugin's
21
+ * `window.open` hands the window to the popup blocker.
22
+ *
23
+ * A refusal resolves `begin` as `LoginOutcome.Gesture`, which already means "cannot proceed
24
+ * without a fresh user gesture — render a control". After accepting, the user presses Log in
25
+ * again; that second press is deliberate, not an oversight, because the redirect or popup must
26
+ * originate from a real gesture.
27
+ */
28
+ export const requireConsentForLogin = (ctx, opts) => {
29
+ if (opts?.disabled === true) {
30
+ return;
31
+ }
32
+ const category = opts?.category ?? CONSENT_ESSENTIAL;
33
+ ensureLoginService(ctx).registerPrecondition({
34
+ alias: CONSENT_LOGIN_PRECONDITION,
35
+ // Above anything an application adds by default, so a user is never asked to satisfy some
36
+ // other precondition before being told about the one that is actually blocking them.
37
+ priority: 100,
38
+ check: () => {
39
+ if (consentStore.granted(category) && consentStore.get().record != null) {
40
+ return true;
41
+ }
42
+ consentStore.open('login');
43
+ return false;
44
+ },
45
+ });
46
+ };
47
+ //# sourceMappingURL=consent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"consent.js","sourceRoot":"","sources":["../src/consent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AACnE,OAAO,EAAE,kBAAkB,EAAE,MAAM,6BAA6B,CAAA;AAEhE,OAAO,EAAE,0BAA0B,EAAE,MAAM,aAAa,CAAA;AAGxD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,CACpC,GAAsB,EAAE,IAA0B,EAC5C,EAAE;IACR,IAAI,IAAI,EAAE,QAAQ,KAAK,IAAI,EAAE,CAAC;QAC5B,OAAM;IACR,CAAC;IACD,MAAM,QAAQ,GAAG,IAAI,EAAE,QAAQ,IAAI,iBAAiB,CAAA;IAEpD,kBAAkB,CAAC,GAAG,CAAC,CAAC,oBAAoB,CAAC;QAC3C,KAAK,EAAE,0BAA0B;QACjC,0FAA0F;QAC1F,qFAAqF;QACrF,QAAQ,EAAE,GAAG;QACb,KAAK,EAAE,GAAG,EAAE;YACV,IAAI,YAAY,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,YAAY,CAAC,GAAG,EAAE,CAAC,MAAM,IAAI,IAAI,EAAE,CAAC;gBACxE,OAAO,IAAI,CAAA;YACb,CAAC;YACD,YAAY,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;YAE1B,OAAO,KAAK,CAAA;QACd,CAAC;KACF,CAAC,CAAA;AACJ,CAAC,CAAA"}
package/build/consts.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export declare const CLIENT_IAM_SERVICE = "client-iam-service";
2
+ export declare const CONSENT_LOGIN_PRECONDITION = "consent-before-login";
2
3
  //# sourceMappingURL=consts.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"consts.d.ts","sourceRoot":"","sources":["../src/consts.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,kBAAkB,uBAAuB,CAAA"}
1
+ {"version":3,"file":"consts.d.ts","sourceRoot":"","sources":["../src/consts.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,kBAAkB,uBAAuB,CAAA;AAEtD,eAAO,MAAM,0BAA0B,yBAAyB,CAAA"}
package/build/consts.js CHANGED
@@ -1,2 +1,3 @@
1
1
  export const CLIENT_IAM_SERVICE = 'client-iam-service';
2
+ export const CONSENT_LOGIN_PRECONDITION = 'consent-before-login';
2
3
  //# sourceMappingURL=consts.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"consts.js","sourceRoot":"","sources":["../src/consts.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,kBAAkB,GAAG,oBAAoB,CAAA"}
1
+ {"version":3,"file":"consts.js","sourceRoot":"","sources":["../src/consts.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,kBAAkB,GAAG,oBAAoB,CAAA;AAEtD,MAAM,CAAC,MAAM,0BAA0B,GAAG,sBAAsB,CAAA"}
package/build/index.d.ts CHANGED
@@ -1,5 +1,12 @@
1
1
  export type * from '@owlmeans/iam';
2
2
  export { hasPermission } from '@owlmeans/iam';
3
+ export { ORGANIZATION_REFUSAL, ORGANIZATION_OWNER_REFUSAL } from '@owlmeans/oidc';
4
+ export type { OidcOrganizationItem } from '@owlmeans/oidc';
3
5
  export * from './consts.js';
4
6
  export * from './append.js';
7
+ export type * from './types.js';
8
+ export type * from './organization/types.js';
9
+ export * from './consent.js';
10
+ export * from './login.js';
11
+ export * from './organization.js';
5
12
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,mBAAmB,eAAe,CAAA;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAC7C,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,mBAAmB,eAAe,CAAA;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAC7C,OAAO,EAAE,oBAAoB,EAAE,0BAA0B,EAAE,MAAM,gBAAgB,CAAA;AACjF,YAAY,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAA;AAC1D,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,mBAAmB,YAAY,CAAA;AAC/B,mBAAmB,yBAAyB,CAAA;AAC5C,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA;AAC1B,cAAc,mBAAmB,CAAA"}
package/build/index.js CHANGED
@@ -1,4 +1,8 @@
1
1
  export { hasPermission } from '@owlmeans/iam';
2
+ export { ORGANIZATION_REFUSAL, ORGANIZATION_OWNER_REFUSAL } from '@owlmeans/oidc';
2
3
  export * from './consts.js';
3
4
  export * from './append.js';
5
+ export * from './consent.js';
6
+ export * from './login.js';
7
+ export * from './organization.js';
4
8
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAC7C,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAC7C,OAAO,EAAE,oBAAoB,EAAE,0BAA0B,EAAE,MAAM,gBAAgB,CAAA;AAEjF,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAG3B,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA;AAC1B,cAAc,mBAAmB,CAAA"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * IAM sign-in is assembled from immutable declarations: decorate the app's protocol tree with
3
+ * {@link withIamGuard}, then append the browser-local {@link iamEntrypoints} bindings.
4
+ */
5
+ export { withOidcGuard as withIamGuard } from '@owlmeans/oidc';
6
+ export { oidcEntrypoints as iamEntrypoints } from '@owlmeans/web-oidc-rp';
7
+ /**
8
+ * How a user signs in and out.
9
+ *
10
+ * Re-exported here so an app has one IAM import rather than three, and so the control it renders
11
+ * carries no knowledge of the browsing context it happens to be in — whether the flow can redirect
12
+ * from where it is, has to run one window up, or is already answered by a session this document
13
+ * holds, is decided by the login plugin the framework selected. See the `login-plugins` guidance
14
+ * for what that means and how to add a mechanic.
15
+ */
16
+ export { useLogin, useLogout } from '@owlmeans/client-auth/login';
17
+ export { LoginOutcome, LoginIntent } from '@owlmeans/client-auth/login';
18
+ export type { LoginPlugin, LoginRequest, LogoutRequest, LoginService, LoginPrecondition, LoginMethod, LoginMethodSource, LoginScreenProps, } from '@owlmeans/client-auth/login';
19
+ //# sourceMappingURL=login.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"login.d.ts","sourceRoot":"","sources":["../src/login.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,aAAa,IAAI,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC9D,OAAO,EAAE,eAAe,IAAI,cAAc,EAAE,MAAM,uBAAuB,CAAA;AAEzE;;;;;;;;GAQG;AACH,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAA;AACjE,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAA;AACvE,YAAY,EACV,WAAW,EAAE,YAAY,EAAE,aAAa,EAAE,YAAY,EAAE,iBAAiB,EACzE,WAAW,EAAE,iBAAiB,EAAE,gBAAgB,GACjD,MAAM,6BAA6B,CAAA"}
package/build/login.js ADDED
@@ -0,0 +1,18 @@
1
+ /**
2
+ * IAM sign-in is assembled from immutable declarations: decorate the app's protocol tree with
3
+ * {@link withIamGuard}, then append the browser-local {@link iamEntrypoints} bindings.
4
+ */
5
+ export { withOidcGuard as withIamGuard } from '@owlmeans/oidc';
6
+ export { oidcEntrypoints as iamEntrypoints } from '@owlmeans/web-oidc-rp';
7
+ /**
8
+ * How a user signs in and out.
9
+ *
10
+ * Re-exported here so an app has one IAM import rather than three, and so the control it renders
11
+ * carries no knowledge of the browsing context it happens to be in — whether the flow can redirect
12
+ * from where it is, has to run one window up, or is already answered by a session this document
13
+ * holds, is decided by the login plugin the framework selected. See the `login-plugins` guidance
14
+ * for what that means and how to add a mechanic.
15
+ */
16
+ export { useLogin, useLogout } from '@owlmeans/client-auth/login';
17
+ export { LoginOutcome, LoginIntent } from '@owlmeans/client-auth/login';
18
+ //# sourceMappingURL=login.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"login.js","sourceRoot":"","sources":["../src/login.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,aAAa,IAAI,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC9D,OAAO,EAAE,eAAe,IAAI,cAAc,EAAE,MAAM,uBAAuB,CAAA;AAEzE;;;;;;;;GAQG;AACH,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAA;AACjE,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAA"}
@@ -0,0 +1,23 @@
1
+ import type { OidcOrganizationItem } from '@owlmeans/oidc';
2
+ /** The organizations of the signed-in subject, and the move between them, from the browser's side. */
3
+ export interface OrganizationSwitchHelper {
4
+ /**
5
+ * The organizations of the signed-in subject, the one the session acts in marked `acting`.
6
+ *
7
+ * Answered by the application's own server from the session it holds — refreshed against the
8
+ * provider by the guard first — so a person just removed from an organization no longer sees it.
9
+ * A session of a client without the organizations scope has none. Needs the bindings of
10
+ * `iamEntrypoints()`.
11
+ */
12
+ listOrganizations: () => Promise<OidcOrganizationItem[]>;
13
+ /**
14
+ * Moves the session into another organization of its subject and adopts the re-signed token, so
15
+ * every request after it — and every screen reading `auth` — acts in that organization.
16
+ *
17
+ * There is no per-request organization selector: the session, not the request, decides where a
18
+ * request acts. An organization the subject is not in is refused (`ORGANIZATION_REFUSAL`) and the
19
+ * current token stays.
20
+ */
21
+ switchOrganization: (entitySlug: string) => Promise<void>;
22
+ }
23
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/organization/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAA;AAE1D,sGAAsG;AACtG,MAAM,WAAW,wBAAwB;IACvC;;;;;;;OAOG;IACH,iBAAiB,EAAE,MAAM,OAAO,CAAC,oBAAoB,EAAE,CAAC,CAAA;IACxD;;;;;;;OAOG;IACH,kBAAkB,EAAE,CAAC,UAAU,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;CAC1D"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/organization/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,11 @@
1
+ import { type BasicContext } from '@owlmeans/context';
2
+ import type { OidcOrganizationItem } from '@owlmeans/oidc';
3
+ import type { OrganizationSwitchHelper } from './organization/types.js';
4
+ export declare const makeOrganizationSwitchHelper: (ctx: BasicContext<any>) => OrganizationSwitchHelper;
5
+ /** The organization switch of a context — one per context. */
6
+ export declare const organizationSwitchOf: (target: BasicContext<any>) => OrganizationSwitchHelper;
7
+ /** @deprecated compat:factory-refactor — use `organizationSwitchOf(ctx).listOrganizations()` */
8
+ export declare const listOrganizations: (ctx: BasicContext<any>) => Promise<OidcOrganizationItem[]>;
9
+ /** @deprecated compat:factory-refactor — use `organizationSwitchOf(ctx).switchOrganization(…)` */
10
+ export declare const switchOrganization: (ctx: BasicContext<any>, entitySlug: string) => Promise<void>;
11
+ //# sourceMappingURL=organization.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"organization.d.ts","sourceRoot":"","sources":["../src/organization.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,KAAK,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAEjE,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAA;AAE1D,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,yBAAyB,CAAA;AAEvE,eAAO,MAAM,4BAA4B,QAAS,YAAY,CAAC,GAAG,CAAC,KAAG,wBAWrE,CAAA;AAED,8DAA8D;AAC9D,eAAO,MAAM,oBAAoB,yDAAmD,CAAA;AAEpF,gGAAgG;AAChG,eAAO,MAAM,iBAAiB,QAAe,YAAY,CAAC,GAAG,CAAC,KAAG,OAAO,CAAC,oBAAoB,EAAE,CAC1C,CAAA;AAErD,kGAAkG;AAClG,eAAO,MAAM,kBAAkB,QAAe,YAAY,CAAC,GAAG,CAAC,cAAc,MAAM,KAAG,OAAO,CAAC,IAAI,CAClC,CAAA"}
@@ -0,0 +1,18 @@
1
+ import { memoHelper } from '@owlmeans/context';
2
+ import { oidcProtocols } from '@owlmeans/oidc';
3
+ import { adoptToken } from '@owlmeans/client-auth/login';
4
+ export const makeOrganizationSwitchHelper = (ctx) => {
5
+ const listOrganizations = async () => (await ctx.entrypoint(oidcProtocols.organizations).call()).items;
6
+ const switchOrganization = async (entitySlug) => {
7
+ const { token } = await ctx.entrypoint(oidcProtocols.organization).call({ body: { entitySlug } });
8
+ await adoptToken(ctx, token);
9
+ };
10
+ return { listOrganizations, switchOrganization };
11
+ };
12
+ /** The organization switch of a context — one per context. */
13
+ export const organizationSwitchOf = memoHelper.oncePer(makeOrganizationSwitchHelper);
14
+ /** @deprecated compat:factory-refactor — use `organizationSwitchOf(ctx).listOrganizations()` */
15
+ export const listOrganizations = async (ctx) => await organizationSwitchOf(ctx).listOrganizations();
16
+ /** @deprecated compat:factory-refactor — use `organizationSwitchOf(ctx).switchOrganization(…)` */
17
+ export const switchOrganization = async (ctx, entitySlug) => await organizationSwitchOf(ctx).switchOrganization(entitySlug);
18
+ //# sourceMappingURL=organization.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"organization.js","sourceRoot":"","sources":["../src/organization.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAqB,MAAM,mBAAmB,CAAA;AACjE,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAE9C,OAAO,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAA;AAGxD,MAAM,CAAC,MAAM,4BAA4B,GAAG,CAAC,GAAsB,EAA4B,EAAE;IAC/F,MAAM,iBAAiB,GAAG,KAAK,IAAqC,EAAE,CACpE,CAAC,MAAM,GAAG,CAAC,UAAU,CAAC,aAAa,CAAC,aAAa,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,KAAK,CAAA;IAElE,MAAM,kBAAkB,GAAG,KAAK,EAAE,UAAkB,EAAiB,EAAE;QACrE,MAAM,EAAE,KAAK,EAAE,GAAG,MAAM,GAAG,CAAC,UAAU,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,EAAE,UAAU,EAAE,EAAE,CAAC,CAAA;QAEjG,MAAM,UAAU,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;IAC9B,CAAC,CAAA;IAED,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,CAAA;AAClD,CAAC,CAAA;AAED,8DAA8D;AAC9D,MAAM,CAAC,MAAM,oBAAoB,GAAG,UAAU,CAAC,OAAO,CAAC,4BAA4B,CAAC,CAAA;AAEpF,gGAAgG;AAChG,MAAM,CAAC,MAAM,iBAAiB,GAAG,KAAK,EAAE,GAAsB,EAAmC,EAAE,CACjG,MAAM,oBAAoB,CAAC,GAAG,CAAC,CAAC,iBAAiB,EAAE,CAAA;AAErD,kGAAkG;AAClG,MAAM,CAAC,MAAM,kBAAkB,GAAG,KAAK,EAAE,GAAsB,EAAE,UAAkB,EAAiB,EAAE,CACpG,MAAM,oBAAoB,CAAC,GAAG,CAAC,CAAC,kBAAkB,CAAC,UAAU,CAAC,CAAA"}
@@ -0,0 +1,11 @@
1
+ export interface ConsentLoginOptions {
2
+ /** The category that must be granted. Defaults to `essential`. */
3
+ category?: string;
4
+ /** Off switch, for an application that genuinely sets no cookie at all. */
5
+ disabled?: boolean;
6
+ }
7
+ export interface AppendIamOptions {
8
+ /** Consent required before a sign-in flow may start. `{ disabled: true }` turns it off. */
9
+ consent?: ConsentLoginOptions;
10
+ }
11
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,mBAAmB;IAClC,kEAAkE;IAClE,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,2EAA2E;IAC3E,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB;AAED,MAAM,WAAW,gBAAgB;IAC/B,2FAA2F;IAC3F,OAAO,CAAC,EAAE,mBAAmB,CAAA;CAC9B"}
package/build/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,6 @@
1
+ import type { AppConfig, AppContext } from '@owlmeans/web-client';
2
+ import type { WithSharedConfig } from '@owlmeans/oidc';
3
+ export interface IamClientConfig extends AppConfig, WithSharedConfig {
4
+ }
5
+ export type IamClientContext<C extends IamClientConfig = IamClientConfig> = AppContext<C>;
6
+ //# sourceMappingURL=types.local.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.local.d.ts","sourceRoot":"","sources":["../src/types.local.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AACjE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAA;AAEtD,MAAM,WAAW,eAAgB,SAAQ,SAAS,EAAE,gBAAgB;CAAG;AAEvE,MAAM,MAAM,gBAAgB,CAAC,CAAC,SAAS,eAAe,GAAG,eAAe,IAAI,UAAU,CAAC,CAAC,CAAC,CAAA"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.local.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.local.js","sourceRoot":"","sources":["../src/types.local.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@owlmeans/client-iam",
3
- "version": "0.1.18-rc.7",
3
+ "version": "0.1.18-rc.70",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -22,15 +22,18 @@
22
22
  }
23
23
  },
24
24
  "dependencies": {
25
- "@owlmeans/context": "^0.1.18-rc.6",
26
- "@owlmeans/iam": "^0.1.18-rc.6",
27
- "@owlmeans/oidc": "^0.1.18-rc.6",
28
- "@owlmeans/web-client": "^0.1.18-rc.6",
29
- "@owlmeans/web-oidc-rp": "^0.1.18-rc.7"
25
+ "@owlmeans/client-auth": "^0.1.18-rc.65",
26
+ "@owlmeans/consent": "^0.1.18-rc.12",
27
+ "@owlmeans/context": "^0.1.18-rc.35",
28
+ "@owlmeans/entrypoint": "^0.1.18-rc.42",
29
+ "@owlmeans/iam": "^0.1.18-rc.55",
30
+ "@owlmeans/oidc": "^0.1.18-rc.60",
31
+ "@owlmeans/web-client": "^0.1.18-rc.68",
32
+ "@owlmeans/web-oidc-rp": "^0.1.18-rc.71"
30
33
  },
31
34
  "devDependencies": {
32
35
  "@owlmeans/dep-config": "workspace:*",
33
- "@types/bun": "^1.3.14",
36
+ "@types/bun": "^1.4.2",
34
37
  "nodemon": "^3.1.14",
35
38
  "typescript": "^7.0.2"
36
39
  },
package/src/append.ts CHANGED
@@ -1,9 +1,8 @@
1
- import type { AppConfig, AppContext } from '@owlmeans/web-client'
2
- import type { WithSharedConfig } from '@owlmeans/oidc'
3
1
  import { appendOidcGuard } from '@owlmeans/web-oidc-rp'
2
+ import { requireConsentForLogin } from './consent.js'
3
+ import type { IamClientConfig, IamClientContext } from './types.local.js'
4
+ import type { AppendIamOptions } from './types.js'
4
5
 
5
- type IamClientConfig = AppConfig & WithSharedConfig
6
- type IamClientContext<C extends IamClientConfig = IamClientConfig> = AppContext<C>
7
6
 
8
7
  /**
9
8
  * One-call OIDC RP setup for a web client context.
@@ -13,10 +12,16 @@ type IamClientContext<C extends IamClientConfig = IamClientConfig> = AppContext<
13
12
  *
14
13
  * The OIDC provider config (discovery URL, clientId, secret) is read from
15
14
  * context.cfg.oidc.providers[] — populate it before calling appendIam().
15
+ *
16
+ * It also installs the consent precondition, so every application gets the "confirm essential
17
+ * cookies before signing in" rule from the line it already writes. An application that sets no
18
+ * cookie at all passes `{ consent: { disabled: true } }`.
16
19
  */
17
20
  export const appendIam = <C extends IamClientConfig, T extends IamClientContext<C>>(
18
- context: T
21
+ context: T, opts?: AppendIamOptions
19
22
  ): T => {
20
23
  appendOidcGuard<C, T>(context)
24
+ requireConsentForLogin(context, opts?.consent)
25
+
21
26
  return context
22
27
  }
package/src/consent.ts ADDED
@@ -0,0 +1,53 @@
1
+ import { CONSENT_ESSENTIAL, consentStore } from '@owlmeans/consent'
2
+ import { ensureLoginService } from '@owlmeans/client-auth/login'
3
+ import type { BasicContext } from '@owlmeans/context'
4
+ import { CONSENT_LOGIN_PRECONDITION } from './consts.js'
5
+ import type { ConsentLoginOptions } from './types.js'
6
+
7
+ /**
8
+ * Refuse to start a sign-in flow until the required consent category is granted, and open the
9
+ * consent dialog in the same gesture so the user can grant it.
10
+ *
11
+ * It sits on `LoginService.begin` rather than on `useLogin`, on a screen, or in a login plugin,
12
+ * and each of those alternatives is wrong for its own reason:
13
+ *
14
+ * - `useLogin` is one of four call sites (a header control, a sign-in-required panel, the
15
+ * dispatcher, and the surrogate screen). Enforcing there means enforcing four times, and the
16
+ * fifth call site somebody adds is unguarded.
17
+ * - a `LoginPlugin` is SELECTED, not chained — `plugin(env)` returns exactly one — so a "consent
18
+ * plugin" would displace the redirect or surrogate plugin rather than run before it, and the
19
+ * flow it was guarding would stop happening at all.
20
+ * - `begin` is the single funnel every mechanic passes through, and it is the one place where the
21
+ * user's gesture is still live, which is what lets the dialog open without being blocked.
22
+ *
23
+ * The check is synchronous because `begin` is: crossing a microtask before a plugin's
24
+ * `window.open` hands the window to the popup blocker.
25
+ *
26
+ * A refusal resolves `begin` as `LoginOutcome.Gesture`, which already means "cannot proceed
27
+ * without a fresh user gesture — render a control". After accepting, the user presses Log in
28
+ * again; that second press is deliberate, not an oversight, because the redirect or popup must
29
+ * originate from a real gesture.
30
+ */
31
+ export const requireConsentForLogin = (
32
+ ctx: BasicContext<any>, opts?: ConsentLoginOptions
33
+ ): void => {
34
+ if (opts?.disabled === true) {
35
+ return
36
+ }
37
+ const category = opts?.category ?? CONSENT_ESSENTIAL
38
+
39
+ ensureLoginService(ctx).registerPrecondition({
40
+ alias: CONSENT_LOGIN_PRECONDITION,
41
+ // Above anything an application adds by default, so a user is never asked to satisfy some
42
+ // other precondition before being told about the one that is actually blocking them.
43
+ priority: 100,
44
+ check: () => {
45
+ if (consentStore.granted(category) && consentStore.get().record != null) {
46
+ return true
47
+ }
48
+ consentStore.open('login')
49
+
50
+ return false
51
+ },
52
+ })
53
+ }
package/src/consts.ts CHANGED
@@ -1 +1,3 @@
1
1
  export const CLIENT_IAM_SERVICE = 'client-iam-service'
2
+
3
+ export const CONSENT_LOGIN_PRECONDITION = 'consent-before-login'
package/src/index.ts CHANGED
@@ -1,4 +1,11 @@
1
1
  export type * from '@owlmeans/iam'
2
2
  export { hasPermission } from '@owlmeans/iam'
3
+ export { ORGANIZATION_REFUSAL, ORGANIZATION_OWNER_REFUSAL } from '@owlmeans/oidc'
4
+ export type { OidcOrganizationItem } from '@owlmeans/oidc'
3
5
  export * from './consts.js'
4
6
  export * from './append.js'
7
+ export type * from './types.js'
8
+ export type * from './organization/types.js'
9
+ export * from './consent.js'
10
+ export * from './login.js'
11
+ export * from './organization.js'
package/src/login.ts ADDED
@@ -0,0 +1,22 @@
1
+ /**
2
+ * IAM sign-in is assembled from immutable declarations: decorate the app's protocol tree with
3
+ * {@link withIamGuard}, then append the browser-local {@link iamEntrypoints} bindings.
4
+ */
5
+ export { withOidcGuard as withIamGuard } from '@owlmeans/oidc'
6
+ export { oidcEntrypoints as iamEntrypoints } from '@owlmeans/web-oidc-rp'
7
+
8
+ /**
9
+ * How a user signs in and out.
10
+ *
11
+ * Re-exported here so an app has one IAM import rather than three, and so the control it renders
12
+ * carries no knowledge of the browsing context it happens to be in — whether the flow can redirect
13
+ * from where it is, has to run one window up, or is already answered by a session this document
14
+ * holds, is decided by the login plugin the framework selected. See the `login-plugins` guidance
15
+ * for what that means and how to add a mechanic.
16
+ */
17
+ export { useLogin, useLogout } from '@owlmeans/client-auth/login'
18
+ export { LoginOutcome, LoginIntent } from '@owlmeans/client-auth/login'
19
+ export type {
20
+ LoginPlugin, LoginRequest, LogoutRequest, LoginService, LoginPrecondition,
21
+ LoginMethod, LoginMethodSource, LoginScreenProps,
22
+ } from '@owlmeans/client-auth/login'
@@ -0,0 +1,23 @@
1
+ import type { OidcOrganizationItem } from '@owlmeans/oidc'
2
+
3
+ /** The organizations of the signed-in subject, and the move between them, from the browser's side. */
4
+ export interface OrganizationSwitchHelper {
5
+ /**
6
+ * The organizations of the signed-in subject, the one the session acts in marked `acting`.
7
+ *
8
+ * Answered by the application's own server from the session it holds — refreshed against the
9
+ * provider by the guard first — so a person just removed from an organization no longer sees it.
10
+ * A session of a client without the organizations scope has none. Needs the bindings of
11
+ * `iamEntrypoints()`.
12
+ */
13
+ listOrganizations: () => Promise<OidcOrganizationItem[]>
14
+ /**
15
+ * Moves the session into another organization of its subject and adopts the re-signed token, so
16
+ * every request after it — and every screen reading `auth` — acts in that organization.
17
+ *
18
+ * There is no per-request organization selector: the session, not the request, decides where a
19
+ * request acts. An organization the subject is not in is refused (`ORGANIZATION_REFUSAL`) and the
20
+ * current token stays.
21
+ */
22
+ switchOrganization: (entitySlug: string) => Promise<void>
23
+ }
@@ -0,0 +1,29 @@
1
+ import { memoHelper, type BasicContext } from '@owlmeans/context'
2
+ import { oidcProtocols } from '@owlmeans/oidc'
3
+ import type { OidcOrganizationItem } from '@owlmeans/oidc'
4
+ import { adoptToken } from '@owlmeans/client-auth/login'
5
+ import type { OrganizationSwitchHelper } from './organization/types.js'
6
+
7
+ export const makeOrganizationSwitchHelper = (ctx: BasicContext<any>): OrganizationSwitchHelper => {
8
+ const listOrganizations = async (): Promise<OidcOrganizationItem[]> =>
9
+ (await ctx.entrypoint(oidcProtocols.organizations).call()).items
10
+
11
+ const switchOrganization = async (entitySlug: string): Promise<void> => {
12
+ const { token } = await ctx.entrypoint(oidcProtocols.organization).call({ body: { entitySlug } })
13
+
14
+ await adoptToken(ctx, token)
15
+ }
16
+
17
+ return { listOrganizations, switchOrganization }
18
+ }
19
+
20
+ /** The organization switch of a context — one per context. */
21
+ export const organizationSwitchOf = memoHelper.oncePer(makeOrganizationSwitchHelper)
22
+
23
+ /** @deprecated compat:factory-refactor — use `organizationSwitchOf(ctx).listOrganizations()` */
24
+ export const listOrganizations = async (ctx: BasicContext<any>): Promise<OidcOrganizationItem[]> =>
25
+ await organizationSwitchOf(ctx).listOrganizations()
26
+
27
+ /** @deprecated compat:factory-refactor — use `organizationSwitchOf(ctx).switchOrganization(…)` */
28
+ export const switchOrganization = async (ctx: BasicContext<any>, entitySlug: string): Promise<void> =>
29
+ await organizationSwitchOf(ctx).switchOrganization(entitySlug)
@@ -0,0 +1,6 @@
1
+ import type { AppConfig, AppContext } from '@owlmeans/web-client'
2
+ import type { WithSharedConfig } from '@owlmeans/oidc'
3
+
4
+ export interface IamClientConfig extends AppConfig, WithSharedConfig {}
5
+
6
+ export type IamClientContext<C extends IamClientConfig = IamClientConfig> = AppContext<C>
package/src/types.ts ADDED
@@ -0,0 +1,13 @@
1
+
2
+
3
+ export interface ConsentLoginOptions {
4
+ /** The category that must be granted. Defaults to `essential`. */
5
+ category?: string
6
+ /** Off switch, for an application that genuinely sets no cookie at all. */
7
+ disabled?: boolean
8
+ }
9
+
10
+ export interface AppendIamOptions {
11
+ /** Consent required before a sign-in flow may start. `{ disabled: true }` turns it off. */
12
+ consent?: ConsentLoginOptions
13
+ }
@@ -0,0 +1,85 @@
1
+ import { describe, expect, test } from 'bun:test'
2
+ import { AuthForbidden } from '@owlmeans/auth'
3
+ import { bind } from '@owlmeans/client-entrypoint'
4
+ import { makeClientContext } from '@owlmeans/client-context'
5
+ import type { ClientConfig } from '@owlmeans/client-context'
6
+ import { AppType, createService } from '@owlmeans/context'
7
+ import type { InitializedService } from '@owlmeans/context'
8
+ import { EntrypointOutcome, transportAlias } from '@owlmeans/entrypoint'
9
+ import type { AbstractRequest, AbstractResponse, EntrypointHandler, EntrypointTransport } from '@owlmeans/entrypoint'
10
+ import { ResilientError } from '@owlmeans/error'
11
+ import { DEFAULT_ALIAS as AUTH_SERVICE } from '@owlmeans/client-auth'
12
+ import { ORGANIZATION_REFUSAL, oidcProtocols } from '@owlmeans/oidc'
13
+ import type { OidcOrganizationItem } from '@owlmeans/oidc'
14
+ import { RouteProtocols } from '@owlmeans/route'
15
+ import { organizationSwitchOf } from '../src/organization.js'
16
+
17
+ /**
18
+ * The organization switch from the browser's side. The relying party's server is stood in for by
19
+ * the framework's own transport seam: a call reaches it with its alias and body, and a refusal
20
+ * crosses as the marshalled text the API client unmarshals. The auth service records what it is
21
+ * asked to adopt.
22
+ */
23
+ const ITEMS: OidcOrganizationItem[] = [
24
+ { entitySlug: 'acme', owner: true, home: true, acting: true },
25
+ { entitySlug: 'beta', owner: false, title: 'Beta', acting: false },
26
+ ]
27
+
28
+ const start = async () => {
29
+ const calls: AbstractRequest[] = []
30
+ const adopted: (string | undefined)[] = []
31
+
32
+ const context = makeClientContext({
33
+ ready: false, service: 'client-iam-tests', type: AppType.Frontend, layer: undefined, services: {}, webService: 'web',
34
+ } as unknown as ClientConfig)
35
+
36
+ context.registerService(createService<EntrypointTransport>(transportAlias(RouteProtocols.WEB), {
37
+ protocol: RouteProtocols.WEB,
38
+ handle: (async (req: AbstractRequest, res: AbstractResponse<unknown>) => {
39
+ calls.push(req)
40
+ if (req.alias === oidcProtocols.organizations.alias) {
41
+ res.resolve({ items: ITEMS }, EntrypointOutcome.Ok)
42
+ return
43
+ }
44
+ const { entitySlug } = req.body as { entitySlug: string }
45
+ if (!ITEMS.some(item => item.entitySlug === entitySlug)) {
46
+ res.reject(ResilientError.ensure(ResilientError.marshal(new AuthForbidden(ORGANIZATION_REFUSAL)).message, true))
47
+ return
48
+ }
49
+ res.resolve({ token: `wrapped-token-for-${entitySlug}` }, EntrypointOutcome.Ok)
50
+ }) as EntrypointHandler,
51
+ }))
52
+ context.registerService(createService<InitializedService & { update: (token?: string) => Promise<void> }>(AUTH_SERVICE, {
53
+ update: async token => { adopted.push(token) },
54
+ }))
55
+ context.registerEntrypoint(bind(oidcProtocols.organizations))
56
+ context.registerEntrypoint(bind(oidcProtocols.organization))
57
+ await context.configure().init()
58
+
59
+ return { context, calls, adopted }
60
+ }
61
+
62
+ describe('@owlmeans/client-iam — organizations', () => {
63
+ test('listOrganizations answers the items of the session', async () => {
64
+ const { context, calls } = await start()
65
+
66
+ expect(await organizationSwitchOf(context).listOrganizations()).toEqual(ITEMS)
67
+ expect(calls.map(call => call.alias)).toEqual([oidcProtocols.organizations.alias])
68
+ })
69
+
70
+ test('switchOrganization posts the slug and adopts the re-signed token', async () => {
71
+ const { context, calls, adopted } = await start()
72
+
73
+ await organizationSwitchOf(context).switchOrganization('beta')
74
+ expect(calls[0].alias).toBe(oidcProtocols.organization.alias)
75
+ expect(calls[0].body).toEqual({ entitySlug: 'beta' })
76
+ expect(adopted).toEqual(['wrapped-token-for-beta'])
77
+ })
78
+
79
+ test('a refused switch adopts nothing', async () => {
80
+ const { context, adopted } = await start()
81
+
82
+ await expect(organizationSwitchOf(context).switchOrganization('gamma')).rejects.toBeInstanceOf(AuthForbidden)
83
+ expect(adopted).toEqual([])
84
+ })
85
+ })
@@ -0,0 +1,12 @@
1
+ {
2
+ "extends": [
3
+ "@owlmeans/dep-config/tsconfig.base.json",
4
+ "@owlmeans/dep-config/tsconfig.node.json"
5
+ ],
6
+ "compilerOptions": {
7
+ "types": ["bun"],
8
+ "rootDir": "../",
9
+ "noEmit": true
10
+ },
11
+ "include": ["./**/*", "../src/**/*"]
12
+ }