@owlmeans/client-iam 0.1.18-rc.6 → 0.1.18-rc.61
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 +15 -0
- package/agent-meta/manifest.json +16 -0
- package/agent-meta/skills/client-iam/SKILL.md +127 -0
- package/build/append.d.ts +10 -1
- package/build/append.d.ts.map +1 -1
- package/build/append.js +7 -1
- package/build/append.js.map +1 -1
- package/build/consent.d.ts +34 -0
- package/build/consent.d.ts.map +1 -0
- package/build/consent.js +47 -0
- package/build/consent.js.map +1 -0
- package/build/index.d.ts +5 -0
- package/build/index.d.ts.map +1 -1
- package/build/index.js +4 -0
- package/build/index.js.map +1 -1
- package/build/login.d.ts +19 -0
- package/build/login.d.ts.map +1 -0
- package/build/login.js +18 -0
- package/build/login.js.map +1 -0
- package/build/organization.d.ts +21 -0
- package/build/organization.d.ts.map +1 -0
- package/build/organization.js +24 -0
- package/build/organization.js.map +1 -0
- package/package.json +10 -7
- package/src/append.ts +14 -1
- package/src/consent.ts +60 -0
- package/src/index.ts +5 -0
- package/src/login.ts +22 -0
- package/src/organization.ts +29 -0
- package/tests/organization.spec.ts +85 -0
- package/tests/tsconfig.json +12 -0
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
<!-- owlmeans:agent-guidance:start -->
|
|
2
|
+
## Agent guidance
|
|
3
|
+
|
|
4
|
+
This package ships embedded agent skills under `agent-meta/`. After installing your
|
|
5
|
+
`@owlmeans/*` packages, run the OwlMeans agent-skills installer to place them into
|
|
6
|
+
your project's skill store (`.agents/skills/`):
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
npx @owlmeans/agent-skills@^0.1.18-rc.47
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
The embedded files are version-matched to this package release. Do not edit them
|
|
13
|
+
directly — they are regenerated on each publish. To contribute guidance edits,
|
|
14
|
+
open a PR against the source monorepo.
|
|
15
|
+
<!-- owlmeans:agent-guidance:end -->
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"schemaVersion": 2,
|
|
3
|
+
"package": "@owlmeans/client-iam",
|
|
4
|
+
"version": "0.1.18-rc.61",
|
|
5
|
+
"generatedAt": "2026-10-03T20:05:07.603Z",
|
|
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,127 @@
|
|
|
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 (listOrganizations) and the organization switch (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.61"` 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
|
+
| `listOrganizations(ctx)` | `OidcOrganizationItem[]` of the session — `{ entitySlug, title?, owner, groups?, home?, acting }` |
|
|
27
|
+
| `switchOrganization(ctx, entitySlug)` | Moves the session into another of its organizations and adopts the re-signed token with `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 |
|
|
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 dialog in the same gesture, and the person presses
|
|
58
|
+
"Log in" again after accepting (`login-plugins`, `web-consent`).
|
|
59
|
+
|
|
60
|
+
## Organizations of the session
|
|
61
|
+
|
|
62
|
+
A person is one account; an application sees it through its rows, one per organization the person
|
|
63
|
+
belongs to in that application. With the provider's `organizations` scope the session acts in ONE
|
|
64
|
+
of them at a time, and that is session state held by the application's server — never a parameter
|
|
65
|
+
of a request:
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
import { listOrganizations, switchOrganization, ORGANIZATION_REFUSAL } from '@owlmeans/client-iam'
|
|
69
|
+
|
|
70
|
+
const items = await listOrganizations(context) // the acting one carries `acting: true`
|
|
71
|
+
try {
|
|
72
|
+
await switchOrganization(context, 'hiking-club')
|
|
73
|
+
// every request from here on, and every screen reading `auth`, acts in hiking-club
|
|
74
|
+
} catch (error) {
|
|
75
|
+
// AuthForbidden(ORGANIZATION_REFUSAL): not one of the person's organizations — the token stays
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
- Both calls go to the application's OWN server (`GET /authenticate/oidc/organizations`,
|
|
80
|
+
`POST /authenticate/oidc/organization`), behind `OIDC_GUARD`, so they need the bindings
|
|
81
|
+
`iamEntrypoints()` spreads. The server side — the session record, re-picking, refusals — is
|
|
82
|
+
governed by `server-oidc-rp` → "Organizations".
|
|
83
|
+
- The re-signed token carries the new organization's `entitySlug`, its groups and only its
|
|
84
|
+
permission sets. Re-read `auth` after a switch; nothing else needs resetting.
|
|
85
|
+
- There is no per-request organization selector: a screen offering "act in another organization"
|
|
86
|
+
calls `switchOrganization`. A client without the `organizations` scope lists none and refuses
|
|
87
|
+
every switch.
|
|
88
|
+
|
|
89
|
+
Managing organizations — creating one, inviting members, granting in it — is the server's job
|
|
90
|
+
through `iamRuntime` (`@owlmeans/server-iam`); the browser calls the application's own endpoints
|
|
91
|
+
for it, never the provider.
|
|
92
|
+
|
|
93
|
+
## Permissions in a screen
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
import { hasPermission } from '@owlmeans/client-iam'
|
|
97
|
+
|
|
98
|
+
const canApprove = hasPermission(context.auth().user(), 'loan--approve', { resourceId: loanId })
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
The browser token holds the unbound sets plus the acting organization's sets with their binding
|
|
102
|
+
already stripped, so a screen needs no `entitySlug`. A set without `resources` covers every id. Use
|
|
103
|
+
the answer to hide or show a control only — the server gate decides.
|
|
104
|
+
|
|
105
|
+
## Rules
|
|
106
|
+
|
|
107
|
+
- One IAM import: take sign-in, organizations and permission checks from here, not from
|
|
108
|
+
`@owlmeans/web-oidc-rp`, `@owlmeans/client-auth/login` and `@owlmeans/iam` separately.
|
|
109
|
+
- Adopt a token only through `adoptToken` / `context.login().adopt(token)` — `switchOrganization`
|
|
110
|
+
already does; never write `auth` or the stored token by hand.
|
|
111
|
+
- Phrase `ORGANIZATION_REFUSAL` and `ORGANIZATION_OWNER_REFUSAL` as sentences for the person; they
|
|
112
|
+
are reasons, not messages.
|
|
113
|
+
- Never send an organization id from the browser: organizations travel by `entitySlug` only.
|
|
114
|
+
|
|
115
|
+
## Depends On
|
|
116
|
+
|
|
117
|
+
- `@owlmeans/web-oidc-rp`, `@owlmeans/oidc` — the guard, the bindings and the switch protocols
|
|
118
|
+
- `@owlmeans/client-auth` — the login host and `adoptToken`
|
|
119
|
+
- `@owlmeans/consent` — the consent store the precondition reads
|
|
120
|
+
- `@owlmeans/iam` — types and `hasPermission`
|
|
121
|
+
- `@owlmeans/context`, `@owlmeans/entrypoint`, `@owlmeans/web-client`
|
|
122
|
+
|
|
123
|
+
## Related
|
|
124
|
+
|
|
125
|
+
- `server-iam` — the gate, the session's organizations on the server, the runtime IAM client
|
|
126
|
+
- `iam` — the permission model (kinds, default classes) and the `IamService`
|
|
127
|
+
- `web-oidc-rp`, `login-plugins`, `login-methods` — the sign-in machinery underneath
|
package/build/append.d.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import type { AppConfig, AppContext } from '@owlmeans/web-client';
|
|
2
2
|
import type { WithSharedConfig } from '@owlmeans/oidc';
|
|
3
|
+
import type { ConsentLoginOptions } from './consent.js';
|
|
3
4
|
type IamClientConfig = AppConfig & WithSharedConfig;
|
|
4
5
|
type IamClientContext<C extends IamClientConfig = IamClientConfig> = AppContext<C>;
|
|
6
|
+
export interface AppendIamOptions {
|
|
7
|
+
/** Consent required before a sign-in flow may start. `{ disabled: true }` turns it off. */
|
|
8
|
+
consent?: ConsentLoginOptions;
|
|
9
|
+
}
|
|
5
10
|
/**
|
|
6
11
|
* One-call OIDC RP setup for a web client context.
|
|
7
12
|
*
|
|
@@ -10,7 +15,11 @@ type IamClientContext<C extends IamClientConfig = IamClientConfig> = AppContext<
|
|
|
10
15
|
*
|
|
11
16
|
* The OIDC provider config (discovery URL, clientId, secret) is read from
|
|
12
17
|
* context.cfg.oidc.providers[] — populate it before calling appendIam().
|
|
18
|
+
*
|
|
19
|
+
* It also installs the consent precondition, so every application gets the "confirm essential
|
|
20
|
+
* cookies before signing in" rule from the line it already writes. An application that sets no
|
|
21
|
+
* cookie at all passes `{ consent: { disabled: true } }`.
|
|
13
22
|
*/
|
|
14
|
-
export declare const appendIam: <C extends IamClientConfig, T extends IamClientContext<C>>(context: T) => T;
|
|
23
|
+
export declare const appendIam: <C extends IamClientConfig, T extends IamClientContext<C>>(context: T, opts?: AppendIamOptions) => T;
|
|
15
24
|
export {};
|
|
16
25
|
//# sourceMappingURL=append.d.ts.map
|
package/build/append.d.ts.map
CHANGED
|
@@ -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
|
|
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,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAA;AAEvD,KAAK,eAAe,GAAG,SAAS,GAAG,gBAAgB,CAAA;AACnD,KAAK,gBAAgB,CAAC,CAAC,SAAS,eAAe,GAAG,eAAe,IAAI,UAAU,CAAC,CAAC,CAAC,CAAA;AAElF,MAAM,WAAW,gBAAgB;IAC/B,2FAA2F;IAC3F,OAAO,CAAC,EAAE,mBAAmB,CAAA;CAC9B;AAED;;;;;;;;;;;;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
|
package/build/append.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"append.js","sourceRoot":"","sources":["../src/append.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;
|
|
1
|
+
{"version":3,"file":"append.js","sourceRoot":"","sources":["../src/append.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AACvD,OAAO,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAA;AAWrD;;;;;;;;;;;;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,34 @@
|
|
|
1
|
+
import type { BasicContext } from '@owlmeans/context';
|
|
2
|
+
export declare const CONSENT_LOGIN_PRECONDITION = "consent-before-login";
|
|
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
|
+
* Refuse to start a sign-in flow until the required consent category is granted, and open the
|
|
11
|
+
* consent dialog in the same gesture so the user can grant it.
|
|
12
|
+
*
|
|
13
|
+
* It sits on `LoginService.begin` rather than on `useLogin`, on a screen, or in a login plugin,
|
|
14
|
+
* and each of those alternatives is wrong for its own reason:
|
|
15
|
+
*
|
|
16
|
+
* - `useLogin` is one of four call sites (a header control, a sign-in-required panel, the
|
|
17
|
+
* dispatcher, and the surrogate screen). Enforcing there means enforcing four times, and the
|
|
18
|
+
* fifth call site somebody adds is unguarded.
|
|
19
|
+
* - a `LoginPlugin` is SELECTED, not chained — `plugin(env)` returns exactly one — so a "consent
|
|
20
|
+
* plugin" would displace the redirect or surrogate plugin rather than run before it, and the
|
|
21
|
+
* flow it was guarding would stop happening at all.
|
|
22
|
+
* - `begin` is the single funnel every mechanic passes through, and it is the one place where the
|
|
23
|
+
* user's gesture is still live, which is what lets the dialog open without being blocked.
|
|
24
|
+
*
|
|
25
|
+
* The check is synchronous because `begin` is: crossing a microtask before a plugin's
|
|
26
|
+
* `window.open` hands the window to the popup blocker.
|
|
27
|
+
*
|
|
28
|
+
* A refusal resolves `begin` as `LoginOutcome.Gesture`, which already means "cannot proceed
|
|
29
|
+
* without a fresh user gesture — render a control". After accepting, the user presses Log in
|
|
30
|
+
* again; that second press is deliberate, not an oversight, because the redirect or popup must
|
|
31
|
+
* originate from a real gesture.
|
|
32
|
+
*/
|
|
33
|
+
export declare const requireConsentForLogin: (ctx: BasicContext<any>, opts?: ConsentLoginOptions) => void;
|
|
34
|
+
//# 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,eAAO,MAAM,0BAA0B,yBAAyB,CAAA;AAEhE,MAAM,WAAW,mBAAmB;IAClC,kEAAkE;IAClE,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,2EAA2E;IAC3E,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,eAAO,MAAM,sBAAsB,QAC5B,YAAY,CAAC,GAAG,CAAC,SAAS,mBAAmB,KACjD,IAoBF,CAAA"}
|
package/build/consent.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { CONSENT_ESSENTIAL, consentStore } from '@owlmeans/consent';
|
|
2
|
+
import { ensureLoginService } from '@owlmeans/client-auth/login';
|
|
3
|
+
export const CONSENT_LOGIN_PRECONDITION = 'consent-before-login';
|
|
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;AAGhE,MAAM,CAAC,MAAM,0BAA0B,GAAG,sBAAsB,CAAA;AAShE;;;;;;;;;;;;;;;;;;;;;;;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/index.d.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
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 * from './consent.js';
|
|
8
|
+
export * from './login.js';
|
|
9
|
+
export * from './organization.js';
|
|
5
10
|
//# sourceMappingURL=index.d.ts.map
|
package/build/index.d.ts.map
CHANGED
|
@@ -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,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
|
package/build/index.js.map
CHANGED
|
@@ -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;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA;AAC1B,cAAc,mBAAmB,CAAA"}
|
package/build/login.d.ts
ADDED
|
@@ -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,21 @@
|
|
|
1
|
+
import type { BasicContext } from '@owlmeans/context';
|
|
2
|
+
import type { OidcOrganizationItem } from '@owlmeans/oidc';
|
|
3
|
+
/**
|
|
4
|
+
* The organizations of the signed-in subject, the one the session acts in marked `acting`.
|
|
5
|
+
*
|
|
6
|
+
* Answered by the application's own server from the session it holds — refreshed against the
|
|
7
|
+
* provider by the guard first — so a person just removed from an organization no longer sees it.
|
|
8
|
+
* A session of a client without the organizations scope has none. Needs the bindings of
|
|
9
|
+
* `iamEntrypoints()`.
|
|
10
|
+
*/
|
|
11
|
+
export declare const listOrganizations: (ctx: BasicContext<any>) => Promise<OidcOrganizationItem[]>;
|
|
12
|
+
/**
|
|
13
|
+
* Moves the session into another organization of its subject and adopts the re-signed token, so
|
|
14
|
+
* every request after it — and every screen reading `auth` — acts in that organization.
|
|
15
|
+
*
|
|
16
|
+
* There is no per-request organization selector: the session, not the request, decides where a
|
|
17
|
+
* request acts. An organization the subject is not in is refused (`ORGANIZATION_REFUSAL`) and the
|
|
18
|
+
* current token stays.
|
|
19
|
+
*/
|
|
20
|
+
export declare const switchOrganization: (ctx: BasicContext<any>, entitySlug: string) => Promise<void>;
|
|
21
|
+
//# sourceMappingURL=organization.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"organization.d.ts","sourceRoot":"","sources":["../src/organization.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAErD,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAA;AAG1D;;;;;;;GAOG;AACH,eAAO,MAAM,iBAAiB,QAAe,YAAY,CAAC,GAAG,CAAC,KAAG,OAAO,CAAC,oBAAoB,EAAE,CAC7B,CAAA;AAElE;;;;;;;GAOG;AACH,eAAO,MAAM,kBAAkB,QAAe,YAAY,CAAC,GAAG,CAAC,cAAc,MAAM,KAAG,OAAO,CAAC,IAAI,CAIjG,CAAA"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { oidcProtocols } from '@owlmeans/oidc';
|
|
2
|
+
import { adoptToken } from '@owlmeans/client-auth/login';
|
|
3
|
+
/**
|
|
4
|
+
* The organizations of the signed-in subject, the one the session acts in marked `acting`.
|
|
5
|
+
*
|
|
6
|
+
* Answered by the application's own server from the session it holds — refreshed against the
|
|
7
|
+
* provider by the guard first — so a person just removed from an organization no longer sees it.
|
|
8
|
+
* A session of a client without the organizations scope has none. Needs the bindings of
|
|
9
|
+
* `iamEntrypoints()`.
|
|
10
|
+
*/
|
|
11
|
+
export const listOrganizations = async (ctx) => (await ctx.entrypoint(oidcProtocols.organizations).call()).items;
|
|
12
|
+
/**
|
|
13
|
+
* Moves the session into another organization of its subject and adopts the re-signed token, so
|
|
14
|
+
* every request after it — and every screen reading `auth` — acts in that organization.
|
|
15
|
+
*
|
|
16
|
+
* There is no per-request organization selector: the session, not the request, decides where a
|
|
17
|
+
* request acts. An organization the subject is not in is refused (`ORGANIZATION_REFUSAL`) and the
|
|
18
|
+
* current token stays.
|
|
19
|
+
*/
|
|
20
|
+
export const switchOrganization = async (ctx, entitySlug) => {
|
|
21
|
+
const { token } = await ctx.entrypoint(oidcProtocols.organization).call({ body: { entitySlug } });
|
|
22
|
+
await adoptToken(ctx, token);
|
|
23
|
+
};
|
|
24
|
+
//# sourceMappingURL=organization.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"organization.js","sourceRoot":"","sources":["../src/organization.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAE9C,OAAO,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAA;AAExD;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,KAAK,EAAE,GAAsB,EAAmC,EAAE,CACjG,CAAC,MAAM,GAAG,CAAC,UAAU,CAAC,aAAa,CAAC,aAAa,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,KAAK,CAAA;AAElE;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,KAAK,EAAE,GAAsB,EAAE,UAAkB,EAAiB,EAAE;IACpG,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;IAEjG,MAAM,UAAU,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;AAC9B,CAAC,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@owlmeans/client-iam",
|
|
3
|
-
"version": "0.1.18-rc.
|
|
3
|
+
"version": "0.1.18-rc.61",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -22,15 +22,18 @@
|
|
|
22
22
|
}
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@owlmeans/
|
|
26
|
-
"@owlmeans/
|
|
27
|
-
"@owlmeans/
|
|
28
|
-
"@owlmeans/
|
|
29
|
-
"@owlmeans/
|
|
25
|
+
"@owlmeans/client-auth": "^0.1.18-rc.56",
|
|
26
|
+
"@owlmeans/consent": "^0.1.18-rc.9",
|
|
27
|
+
"@owlmeans/context": "^0.1.18-rc.32",
|
|
28
|
+
"@owlmeans/entrypoint": "^0.1.18-rc.39",
|
|
29
|
+
"@owlmeans/iam": "^0.1.18-rc.48",
|
|
30
|
+
"@owlmeans/oidc": "^0.1.18-rc.53",
|
|
31
|
+
"@owlmeans/web-client": "^0.1.18-rc.59",
|
|
32
|
+
"@owlmeans/web-oidc-rp": "^0.1.18-rc.62"
|
|
30
33
|
},
|
|
31
34
|
"devDependencies": {
|
|
32
35
|
"@owlmeans/dep-config": "workspace:*",
|
|
33
|
-
"@types/bun": "^1.
|
|
36
|
+
"@types/bun": "^1.4.0",
|
|
34
37
|
"nodemon": "^3.1.14",
|
|
35
38
|
"typescript": "^7.0.2"
|
|
36
39
|
},
|
package/src/append.ts
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
import type { AppConfig, AppContext } from '@owlmeans/web-client'
|
|
2
2
|
import type { WithSharedConfig } from '@owlmeans/oidc'
|
|
3
3
|
import { appendOidcGuard } from '@owlmeans/web-oidc-rp'
|
|
4
|
+
import { requireConsentForLogin } from './consent.js'
|
|
5
|
+
import type { ConsentLoginOptions } from './consent.js'
|
|
4
6
|
|
|
5
7
|
type IamClientConfig = AppConfig & WithSharedConfig
|
|
6
8
|
type IamClientContext<C extends IamClientConfig = IamClientConfig> = AppContext<C>
|
|
7
9
|
|
|
10
|
+
export interface AppendIamOptions {
|
|
11
|
+
/** Consent required before a sign-in flow may start. `{ disabled: true }` turns it off. */
|
|
12
|
+
consent?: ConsentLoginOptions
|
|
13
|
+
}
|
|
14
|
+
|
|
8
15
|
/**
|
|
9
16
|
* One-call OIDC RP setup for a web client context.
|
|
10
17
|
*
|
|
@@ -13,10 +20,16 @@ type IamClientContext<C extends IamClientConfig = IamClientConfig> = AppContext<
|
|
|
13
20
|
*
|
|
14
21
|
* The OIDC provider config (discovery URL, clientId, secret) is read from
|
|
15
22
|
* context.cfg.oidc.providers[] — populate it before calling appendIam().
|
|
23
|
+
*
|
|
24
|
+
* It also installs the consent precondition, so every application gets the "confirm essential
|
|
25
|
+
* cookies before signing in" rule from the line it already writes. An application that sets no
|
|
26
|
+
* cookie at all passes `{ consent: { disabled: true } }`.
|
|
16
27
|
*/
|
|
17
28
|
export const appendIam = <C extends IamClientConfig, T extends IamClientContext<C>>(
|
|
18
|
-
context: T
|
|
29
|
+
context: T, opts?: AppendIamOptions
|
|
19
30
|
): T => {
|
|
20
31
|
appendOidcGuard<C, T>(context)
|
|
32
|
+
requireConsentForLogin(context, opts?.consent)
|
|
33
|
+
|
|
21
34
|
return context
|
|
22
35
|
}
|
package/src/consent.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { CONSENT_ESSENTIAL, consentStore } from '@owlmeans/consent'
|
|
2
|
+
import { ensureLoginService } from '@owlmeans/client-auth/login'
|
|
3
|
+
import type { BasicContext } from '@owlmeans/context'
|
|
4
|
+
|
|
5
|
+
export const CONSENT_LOGIN_PRECONDITION = 'consent-before-login'
|
|
6
|
+
|
|
7
|
+
export interface ConsentLoginOptions {
|
|
8
|
+
/** The category that must be granted. Defaults to `essential`. */
|
|
9
|
+
category?: string
|
|
10
|
+
/** Off switch, for an application that genuinely sets no cookie at all. */
|
|
11
|
+
disabled?: boolean
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Refuse to start a sign-in flow until the required consent category is granted, and open the
|
|
16
|
+
* consent dialog in the same gesture so the user can grant it.
|
|
17
|
+
*
|
|
18
|
+
* It sits on `LoginService.begin` rather than on `useLogin`, on a screen, or in a login plugin,
|
|
19
|
+
* and each of those alternatives is wrong for its own reason:
|
|
20
|
+
*
|
|
21
|
+
* - `useLogin` is one of four call sites (a header control, a sign-in-required panel, the
|
|
22
|
+
* dispatcher, and the surrogate screen). Enforcing there means enforcing four times, and the
|
|
23
|
+
* fifth call site somebody adds is unguarded.
|
|
24
|
+
* - a `LoginPlugin` is SELECTED, not chained — `plugin(env)` returns exactly one — so a "consent
|
|
25
|
+
* plugin" would displace the redirect or surrogate plugin rather than run before it, and the
|
|
26
|
+
* flow it was guarding would stop happening at all.
|
|
27
|
+
* - `begin` is the single funnel every mechanic passes through, and it is the one place where the
|
|
28
|
+
* user's gesture is still live, which is what lets the dialog open without being blocked.
|
|
29
|
+
*
|
|
30
|
+
* The check is synchronous because `begin` is: crossing a microtask before a plugin's
|
|
31
|
+
* `window.open` hands the window to the popup blocker.
|
|
32
|
+
*
|
|
33
|
+
* A refusal resolves `begin` as `LoginOutcome.Gesture`, which already means "cannot proceed
|
|
34
|
+
* without a fresh user gesture — render a control". After accepting, the user presses Log in
|
|
35
|
+
* again; that second press is deliberate, not an oversight, because the redirect or popup must
|
|
36
|
+
* originate from a real gesture.
|
|
37
|
+
*/
|
|
38
|
+
export const requireConsentForLogin = (
|
|
39
|
+
ctx: BasicContext<any>, opts?: ConsentLoginOptions
|
|
40
|
+
): void => {
|
|
41
|
+
if (opts?.disabled === true) {
|
|
42
|
+
return
|
|
43
|
+
}
|
|
44
|
+
const category = opts?.category ?? CONSENT_ESSENTIAL
|
|
45
|
+
|
|
46
|
+
ensureLoginService(ctx).registerPrecondition({
|
|
47
|
+
alias: CONSENT_LOGIN_PRECONDITION,
|
|
48
|
+
// Above anything an application adds by default, so a user is never asked to satisfy some
|
|
49
|
+
// other precondition before being told about the one that is actually blocking them.
|
|
50
|
+
priority: 100,
|
|
51
|
+
check: () => {
|
|
52
|
+
if (consentStore.granted(category) && consentStore.get().record != null) {
|
|
53
|
+
return true
|
|
54
|
+
}
|
|
55
|
+
consentStore.open('login')
|
|
56
|
+
|
|
57
|
+
return false
|
|
58
|
+
},
|
|
59
|
+
})
|
|
60
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
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 * from './consent.js'
|
|
8
|
+
export * from './login.js'
|
|
9
|
+
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,29 @@
|
|
|
1
|
+
import 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
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The organizations of the signed-in subject, the one the session acts in marked `acting`.
|
|
8
|
+
*
|
|
9
|
+
* Answered by the application's own server from the session it holds — refreshed against the
|
|
10
|
+
* provider by the guard first — so a person just removed from an organization no longer sees it.
|
|
11
|
+
* A session of a client without the organizations scope has none. Needs the bindings of
|
|
12
|
+
* `iamEntrypoints()`.
|
|
13
|
+
*/
|
|
14
|
+
export const listOrganizations = async (ctx: BasicContext<any>): Promise<OidcOrganizationItem[]> =>
|
|
15
|
+
(await ctx.entrypoint(oidcProtocols.organizations).call()).items
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Moves the session into another organization of its subject and adopts the re-signed token, so
|
|
19
|
+
* every request after it — and every screen reading `auth` — acts in that organization.
|
|
20
|
+
*
|
|
21
|
+
* There is no per-request organization selector: the session, not the request, decides where a
|
|
22
|
+
* request acts. An organization the subject is not in is refused (`ORGANIZATION_REFUSAL`) and the
|
|
23
|
+
* current token stays.
|
|
24
|
+
*/
|
|
25
|
+
export const switchOrganization = async (ctx: BasicContext<any>, entitySlug: string): Promise<void> => {
|
|
26
|
+
const { token } = await ctx.entrypoint(oidcProtocols.organization).call({ body: { entitySlug } })
|
|
27
|
+
|
|
28
|
+
await adoptToken(ctx, token)
|
|
29
|
+
}
|
|
@@ -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 { listOrganizations, switchOrganization } 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 listOrganizations(context)).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 switchOrganization(context, '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(switchOrganization(context, 'gamma')).rejects.toBeInstanceOf(AuthForbidden)
|
|
83
|
+
expect(adopted).toEqual([])
|
|
84
|
+
})
|
|
85
|
+
})
|