@nebulr-group/bridge-svelte 1.0.0-alpha.14 → 1.0.0-alpha.16
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 +1 -1
- package/dist/bootstrap.js +8 -7
- package/dist/components/auth/SignUpForm.svelte +19 -0
- package/dist/components/auth/SignUpForm.svelte.d.ts +11 -0
- package/dist/components/auth/VerifyEmail.svelte +57 -2
- package/dist/core/guard.d.ts +1 -1
- package/dist/server/index.d.ts +30 -5
- package/dist/server/index.js +74 -11
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -74,7 +74,7 @@ In your app's settings in Bridge, add your app's address as a browser address an
|
|
|
74
74
|
| Billing | `BridgeBillingRoutes`, `PlanPicker`, `BillingPortalButton`, `SubscriptionStatus`, `BillingNotice`, `QuotaBanner`, `QuotaGate`, `Entitled`, `CheckoutNotice`, `UpgradeDialog`, `bridgeFetch` |
|
|
75
75
|
| Team | `TeamPanel`, `MemberList`, `InviteDialog`, `InviteList`, `EditMemberDialog`, `WorkspaceForm`, `ProfileForm` |
|
|
76
76
|
| State | `session`, `user`, `workspace`, `workspaces`, `me`, `flags`, `billing`, `plans`, `usage`, `liveStatus`, `getBridge()` |
|
|
77
|
-
| Server | `@nebulr-group/bridge-svelte/server`: `bridgeHandle`, `requireActor`, `meter`, `flagsFor` |
|
|
77
|
+
| Server | `@nebulr-group/bridge-svelte/server`: `bridgeHandle`, `requireActor`, `requireFlag`, `meter`, `flagsFor` |
|
|
78
78
|
| Hand-off | `HandoffSignIn`, `readHandoff`, `handoffSignIn`: a sign-in page for a command-line tool |
|
|
79
79
|
| Hosted mode | `hosted: {}` in the config: `/auth/sign-in` sends people to the page Bridge hosts for the app, and `/auth/callback` (`HostedCallback`) finishes the sign-in when they come back. Register the page's origin as a browser address and `https://<your app>/auth/callback` as a sign-in return |
|
|
80
80
|
| Several sign-in pages | `linkAddress` in the config: which of the app's registered email-links addresses its emails link to |
|
package/dist/bootstrap.js
CHANGED
|
@@ -49,15 +49,16 @@ function followLink(config, url) {
|
|
|
49
49
|
redirect(307, `${page}${hash}`);
|
|
50
50
|
}
|
|
51
51
|
/**
|
|
52
|
-
* Whether the
|
|
53
|
-
* workspace
|
|
54
|
-
*
|
|
52
|
+
* Whether the paywall's reason holds now: the workspace has no plan. The app's active flag is off for that, and also for
|
|
53
|
+
* a workspace that is locked for payment, and a flag result can lag Bridge's billing by a moment after the lock is
|
|
54
|
+
* lifted or a plan is chosen, so the flag alone does not send the page away. Bridge's billing facts are read now,
|
|
55
|
+
* not taken from what is held; only "needs a plan" goes to the paywall. When they cannot be read, the flag decides.
|
|
55
56
|
*/
|
|
56
|
-
async function
|
|
57
|
+
async function needsPlanNow(client) {
|
|
57
58
|
if (client.isLocked())
|
|
58
|
-
return
|
|
59
|
+
return false;
|
|
59
60
|
const facts = await within(client.billing.gate(), READY_WAIT_MS);
|
|
60
|
-
return facts
|
|
61
|
+
return facts === undefined ? true : facts.access === 'needs_plan';
|
|
61
62
|
}
|
|
62
63
|
async function guard(client, config, url) {
|
|
63
64
|
await within(client.ready, READY_WAIT_MS);
|
|
@@ -74,7 +75,7 @@ async function guard(client, config, url) {
|
|
|
74
75
|
const decision = decide(config, input);
|
|
75
76
|
if (decision.type !== 'redirect' || decision.reason !== 'paywall')
|
|
76
77
|
return decision;
|
|
77
|
-
return (await
|
|
78
|
+
return (await needsPlanNow(client)) ? decision : decide(config, { ...input, locked: true });
|
|
78
79
|
}
|
|
79
80
|
/**
|
|
80
81
|
* Makes the root layout's load function. Set `export const ssr = false`
|
|
@@ -23,6 +23,13 @@
|
|
|
23
23
|
import Spinner from '../shared/Spinner.svelte';
|
|
24
24
|
import PasswordInput from '../shared/PasswordInput.svelte';
|
|
25
25
|
|
|
26
|
+
/** Where a visit came from: campaign tags as strings, and the visitor's marketing consent (auth-core's sign-up attribution). */
|
|
27
|
+
interface SignUpAttribution {
|
|
28
|
+
readonly first: Readonly<Record<string, string>>;
|
|
29
|
+
readonly last?: Readonly<Record<string, string>>;
|
|
30
|
+
readonly marketingConsent: boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
26
33
|
interface Props extends HTMLAttributes<HTMLDivElement> {
|
|
27
34
|
heading?: string | null;
|
|
28
35
|
headingSnippet?: Snippet;
|
|
@@ -42,6 +49,11 @@
|
|
|
42
49
|
onSignIn?: () => void;
|
|
43
50
|
showLoginLink?: boolean;
|
|
44
51
|
loginHref?: string;
|
|
52
|
+
/**
|
|
53
|
+
* Where the visit came from, sent with the sign-up for your sign-up reports: campaign tags and the visitor's
|
|
54
|
+
* marketing consent. A function is read when the form is sent. Leave out for none.
|
|
55
|
+
*/
|
|
56
|
+
attribution?: SignUpAttribution | (() => SignUpAttribution | undefined);
|
|
45
57
|
/** Called once the email is sent. */
|
|
46
58
|
onSent?: (email: string) => void;
|
|
47
59
|
onError?: (error: unknown) => void;
|
|
@@ -58,6 +70,7 @@
|
|
|
58
70
|
onSignIn,
|
|
59
71
|
showLoginLink = true,
|
|
60
72
|
loginHref = undefined,
|
|
73
|
+
attribution = undefined,
|
|
61
74
|
onSent,
|
|
62
75
|
onError,
|
|
63
76
|
footer,
|
|
@@ -68,6 +81,11 @@
|
|
|
68
81
|
}: Props = $props();
|
|
69
82
|
|
|
70
83
|
const t = $derived(translator(messages));
|
|
84
|
+
|
|
85
|
+
function attributionOf(): { attribution?: SignUpAttribution } {
|
|
86
|
+
const given = typeof attribution === 'function' ? attribution() : attribution;
|
|
87
|
+
return given === undefined ? {} : { attribution: given };
|
|
88
|
+
}
|
|
71
89
|
const config = peekConfig();
|
|
72
90
|
const authBase = config?.authBase ?? '/auth';
|
|
73
91
|
|
|
@@ -124,6 +142,7 @@
|
|
|
124
142
|
...secret,
|
|
125
143
|
...names,
|
|
126
144
|
...(workspace === 'ask' ? { workspace: workspaceName.trim() || true } : workspace ? { workspace: true } : {}),
|
|
145
|
+
...attributionOf(),
|
|
127
146
|
});
|
|
128
147
|
sentTo = address;
|
|
129
148
|
onSent?.(address);
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import type { Snippet } from 'svelte';
|
|
2
2
|
import type { HTMLAttributes } from 'svelte/elements';
|
|
3
3
|
import type { BridgeMessages } from '../../core/config.js';
|
|
4
|
+
/** Where a visit came from: campaign tags as strings, and the visitor's marketing consent (auth-core's sign-up attribution). */
|
|
5
|
+
interface SignUpAttribution {
|
|
6
|
+
readonly first: Readonly<Record<string, string>>;
|
|
7
|
+
readonly last?: Readonly<Record<string, string>>;
|
|
8
|
+
readonly marketingConsent: boolean;
|
|
9
|
+
}
|
|
4
10
|
interface Props extends HTMLAttributes<HTMLDivElement> {
|
|
5
11
|
heading?: string | null;
|
|
6
12
|
headingSnippet?: Snippet;
|
|
@@ -20,6 +26,11 @@ interface Props extends HTMLAttributes<HTMLDivElement> {
|
|
|
20
26
|
onSignIn?: () => void;
|
|
21
27
|
showLoginLink?: boolean;
|
|
22
28
|
loginHref?: string;
|
|
29
|
+
/**
|
|
30
|
+
* Where the visit came from, sent with the sign-up for your sign-up reports: campaign tags and the visitor's
|
|
31
|
+
* marketing consent. A function is read when the form is sent. Leave out for none.
|
|
32
|
+
*/
|
|
33
|
+
attribution?: SignUpAttribution | (() => SignUpAttribution | undefined);
|
|
23
34
|
/** Called once the email is sent. */
|
|
24
35
|
onSent?: (email: string) => void;
|
|
25
36
|
onError?: (error: unknown) => void;
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
import AuthFormWrapper from '../shared/AuthFormWrapper.svelte';
|
|
24
24
|
import Alert from '../shared/Alert.svelte';
|
|
25
25
|
import Spinner from '../shared/Spinner.svelte';
|
|
26
|
+
import PasswordInput from '../shared/PasswordInput.svelte';
|
|
26
27
|
import WorkspaceChooser from './WorkspaceChooser.svelte';
|
|
27
28
|
import { takeEmailLink } from './email-link.js';
|
|
28
29
|
import { bounded, handOff, leave } from './finish-link.js';
|
|
@@ -48,7 +49,8 @@
|
|
|
48
49
|
| { step: 'continued' }
|
|
49
50
|
| { step: 'signed-in'; to: string }
|
|
50
51
|
| { step: 'workspace'; workspaces: readonly WorkspaceChoice[]; choose: (id: string) => Promise<SignInStep> }
|
|
51
|
-
| { step: 'failed'; message: string; kind: string | null }
|
|
52
|
+
| { step: 'failed'; message: string; kind: string | null }
|
|
53
|
+
| { step: 'password'; token: string; error: string | null; sending: boolean };
|
|
52
54
|
let view = $state<State>({ step: 'working' });
|
|
53
55
|
|
|
54
56
|
async function finish() {
|
|
@@ -62,6 +64,29 @@
|
|
|
62
64
|
if (!(await leave(() => goto(to, { invalidateAll: true }), () => location.pathname !== here))) view = { step: 'signed-in', to };
|
|
63
65
|
}
|
|
64
66
|
|
|
67
|
+
/** auth-core's finish, with the password chosen at the link (from the release that takes it). */
|
|
68
|
+
const finishSignUp = (link: string, password?: string): Promise<SignInStep> =>
|
|
69
|
+
(getBridge().signUp.finish as (token: string, password?: string) => Promise<SignInStep>)(link, password);
|
|
70
|
+
|
|
71
|
+
/** A sign-up started with no password: the person chooses it here, and the link is finished with it. */
|
|
72
|
+
let chosen = $state('');
|
|
73
|
+
|
|
74
|
+
async function choosePassword(event: SubmitEvent) {
|
|
75
|
+
event.preventDefault();
|
|
76
|
+
if (view.step !== 'password') return;
|
|
77
|
+
const link = view.token;
|
|
78
|
+
view = { ...view, sending: true, error: null };
|
|
79
|
+
try {
|
|
80
|
+
await follow(await bounded(finishSignUp(link, chosen)));
|
|
81
|
+
} catch (error) {
|
|
82
|
+
if (isBridgeError(error) && (error.code === 'password_rules' || error.code === 'password_required')) {
|
|
83
|
+
view = { step: 'password', token: link, sending: false, error: errorMessage(error, t, 'password.tooShort') };
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
fail(error);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
65
90
|
function fail(error: unknown) {
|
|
66
91
|
view = { step: 'failed', message: errorMessage(error, t, 'link.error.invalid'), kind: isBridgeError(error) ? error.kind : null };
|
|
67
92
|
onError?.(error);
|
|
@@ -91,8 +116,13 @@
|
|
|
91
116
|
view = { step: 'continued' };
|
|
92
117
|
return;
|
|
93
118
|
}
|
|
94
|
-
await follow(await bounded(
|
|
119
|
+
await follow(await bounded(finishSignUp(found)));
|
|
95
120
|
} catch (error) {
|
|
121
|
+
// Started with no password: the person chooses it now; the link stays usable.
|
|
122
|
+
if (isBridgeError(error) && error.code === 'password_required') {
|
|
123
|
+
view = { step: 'password', token: found, error: null, sending: false };
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
96
126
|
fail(error);
|
|
97
127
|
}
|
|
98
128
|
}
|
|
@@ -124,6 +154,31 @@
|
|
|
124
154
|
<a href={view.to} class="bridge-link">{t('invite.continue')}</a>
|
|
125
155
|
</div>
|
|
126
156
|
</AuthFormWrapper>
|
|
157
|
+
{:else if view.step === 'password'}
|
|
158
|
+
<AuthFormWrapper heading={t('forgot.headingSet')} {headingSnippet} class={className} {style} data-bridge-verify-email="password" {...rest}>
|
|
159
|
+
{#if view.error}
|
|
160
|
+
<Alert variant="error">{view.error}</Alert>
|
|
161
|
+
{/if}
|
|
162
|
+
<form onsubmit={choosePassword}>
|
|
163
|
+
<div class="bridge-form-group">
|
|
164
|
+
<label for="verify-password">{t('field.newPassword')}</label>
|
|
165
|
+
<PasswordInput
|
|
166
|
+
id="verify-password"
|
|
167
|
+
autocomplete="new-password"
|
|
168
|
+
placeholder={t('placeholder.newPassword')}
|
|
169
|
+
bind:value={chosen}
|
|
170
|
+
showLabel={t('action.showPassword')}
|
|
171
|
+
hideLabel={t('action.hidePassword')}
|
|
172
|
+
disabled={view.sending}
|
|
173
|
+
required
|
|
174
|
+
/>
|
|
175
|
+
</div>
|
|
176
|
+
<button type="submit" class="bridge-btn-primary" disabled={view.sending || chosen === ''}>
|
|
177
|
+
{#if view.sending}<Spinner size={16} />{/if}
|
|
178
|
+
{t('forgot.setSubmit')}
|
|
179
|
+
</button>
|
|
180
|
+
</form>
|
|
181
|
+
</AuthFormWrapper>
|
|
127
182
|
{:else if view.step === 'workspace'}
|
|
128
183
|
<AuthFormWrapper heading={t('workspace.chooseHeading')} {headingSnippet} class={className} {style} data-bridge-verify-email="workspace" {...rest}>
|
|
129
184
|
<WorkspaceChooser workspaces={view.workspaces} onChoose={choose} {messages} />
|
package/dist/core/guard.d.ts
CHANGED
|
@@ -50,7 +50,7 @@ export interface GuardInput {
|
|
|
50
50
|
readonly flags: FlagReader;
|
|
51
51
|
/** The return address already on the URL, on an auth page. */
|
|
52
52
|
readonly returnTo: string | null;
|
|
53
|
-
/**
|
|
53
|
+
/** The page stays although the app's active flag is off: the workspace is locked for payment (the lock screen covers the page) or has a plan (the flag lags Bridge's billing), rather than the paywall. */
|
|
54
54
|
readonly locked?: boolean;
|
|
55
55
|
}
|
|
56
56
|
/** Decides one navigation. */
|
package/dist/server/index.d.ts
CHANGED
|
@@ -14,14 +14,18 @@
|
|
|
14
14
|
* export const handle = bridgeHandle({ issuer: pub.PUBLIC_BRIDGE_ISSUER, appKey: env.BRIDGE_APP_KEY });
|
|
15
15
|
*
|
|
16
16
|
* // src/routes/api/reports/+server.ts
|
|
17
|
-
* import {
|
|
17
|
+
* import { requireFlag, meter } from '@nebulr-group/bridge-svelte/server';
|
|
18
18
|
* export const POST = async (event) => {
|
|
19
|
-
* const actor =
|
|
19
|
+
* const actor = await requireFlag(event, 'reports'); // a person, or a key the flag's rule lets in
|
|
20
20
|
* meter(event, 'reports');
|
|
21
21
|
* ...
|
|
22
22
|
* };
|
|
23
23
|
* ```
|
|
24
24
|
*
|
|
25
|
+
* A request that comes with a Bridge key is protected by default: it reaches
|
|
26
|
+
* an endpoint only through `requireFlag` (the key must match the flag's rule),
|
|
27
|
+
* or one marked open with `requireActor(event, { openToKeys: true })`.
|
|
28
|
+
*
|
|
25
29
|
* Token checks follow the app's revocation list, so a session that ended or
|
|
26
30
|
* a member who was removed is refused within seconds. While Bridge cannot be
|
|
27
31
|
* checked, the answer is 503, never yes.
|
|
@@ -32,7 +36,11 @@ export type { Actor, BridgeServerOptions };
|
|
|
32
36
|
export { FlagSet };
|
|
33
37
|
/** What `bridgeHandle` puts on `event.locals.bridge`. */
|
|
34
38
|
export interface BridgeLocals {
|
|
35
|
-
/**
|
|
39
|
+
/**
|
|
40
|
+
* Who called: the person or key behind the request's credential, or null
|
|
41
|
+
* when it carried none. A key is null here until the endpoint lets keys in
|
|
42
|
+
* with `requireFlag` or `requireActor(event, { openToKeys: true })`.
|
|
43
|
+
*/
|
|
36
44
|
readonly actor: Actor | null;
|
|
37
45
|
/** The caller's flag results, worked out by Bridge. Fetched once per request, when first asked. */
|
|
38
46
|
flags(hints?: Readonly<Record<string, Hint>>): Promise<FlagSet>;
|
|
@@ -69,8 +77,25 @@ export interface BridgeHandleOptions extends BridgeServerOptions {
|
|
|
69
77
|
* a route with `requireActor`, and leave routes out with `skip`.
|
|
70
78
|
*/
|
|
71
79
|
export declare function bridgeHandle(options: BridgeHandleOptions): Handle;
|
|
72
|
-
/**
|
|
73
|
-
|
|
80
|
+
/**
|
|
81
|
+
* The caller, or a 401 when the request carried no credential.
|
|
82
|
+
*
|
|
83
|
+
* A request that came with a Bridge key is refused 403 unless the endpoint
|
|
84
|
+
* says keys may come in: pass `{ openToKeys: true }` for an endpoint that is
|
|
85
|
+
* open to every valid key, or gate it with `requireFlag`. People are
|
|
86
|
+
* unaffected.
|
|
87
|
+
*/
|
|
88
|
+
export declare function requireActor(event: Pick<RequestEvent, 'locals'>, options?: {
|
|
89
|
+
readonly openToKeys?: boolean;
|
|
90
|
+
}): Actor;
|
|
91
|
+
/**
|
|
92
|
+
* The caller, once the flag is on for them: a 401 when the request carried no
|
|
93
|
+
* credential, a 403 when the flag is off. Bridge works the flag out for the
|
|
94
|
+
* caller. For a key, rules on the key's privileges apply, so only a key whose
|
|
95
|
+
* privileges match the flag's rule gets in; this is what lets a key through
|
|
96
|
+
* an endpoint. Fails closed: while Bridge cannot answer, every flag is off.
|
|
97
|
+
*/
|
|
98
|
+
export declare function requireFlag(event: Pick<RequestEvent, 'locals'>, flag: string, hints?: Readonly<Record<string, Hint>>): Promise<Actor>;
|
|
74
99
|
/** Counts usage of a metric for the caller's workspace ("count this"). Needs the app key in `bridgeHandle`. */
|
|
75
100
|
export declare function meter(event: Pick<RequestEvent, 'locals'>, metric: string, quantity?: number, options?: {
|
|
76
101
|
readonly id?: string;
|
package/dist/server/index.js
CHANGED
|
@@ -14,14 +14,18 @@
|
|
|
14
14
|
* export const handle = bridgeHandle({ issuer: pub.PUBLIC_BRIDGE_ISSUER, appKey: env.BRIDGE_APP_KEY });
|
|
15
15
|
*
|
|
16
16
|
* // src/routes/api/reports/+server.ts
|
|
17
|
-
* import {
|
|
17
|
+
* import { requireFlag, meter } from '@nebulr-group/bridge-svelte/server';
|
|
18
18
|
* export const POST = async (event) => {
|
|
19
|
-
* const actor =
|
|
19
|
+
* const actor = await requireFlag(event, 'reports'); // a person, or a key the flag's rule lets in
|
|
20
20
|
* meter(event, 'reports');
|
|
21
21
|
* ...
|
|
22
22
|
* };
|
|
23
23
|
* ```
|
|
24
24
|
*
|
|
25
|
+
* A request that comes with a Bridge key is protected by default: it reaches
|
|
26
|
+
* an endpoint only through `requireFlag` (the key must match the flag's rule),
|
|
27
|
+
* or one marked open with `requireActor(event, { openToKeys: true })`.
|
|
28
|
+
*
|
|
25
29
|
* Token checks follow the app's revocation list, so a session that ended or
|
|
26
30
|
* a member who was removed is refused within seconds. While Bridge cannot be
|
|
27
31
|
* checked, the answer is 503, never yes.
|
|
@@ -47,6 +51,21 @@ function serverFor(options) {
|
|
|
47
51
|
}
|
|
48
52
|
return server;
|
|
49
53
|
}
|
|
54
|
+
/** A key's caller: anything that is neither a person nor a job. A kind added later is protected too. */
|
|
55
|
+
/** A workspace id as Bridge writes them; any other is not passed on, since auth-core's `forRequest` throws on it. */
|
|
56
|
+
const WORKSPACE_ID = /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/;
|
|
57
|
+
/**
|
|
58
|
+
* The workspace an app key names for its flag results: `Bridge-Workspace`, or `x-tenant-id` as 0.x callers send it. Only a
|
|
59
|
+
* valid id is passed on; a key that names none, or an invalid one, gets its own results. For keys only.
|
|
60
|
+
*/
|
|
61
|
+
function workspaceNamedBy(headers) {
|
|
62
|
+
const named = (headers.get('bridge-workspace') ?? headers.get('x-tenant-id'))?.trim();
|
|
63
|
+
return named !== undefined && WORKSPACE_ID.test(named) ? named : undefined;
|
|
64
|
+
}
|
|
65
|
+
function isKey(actor) {
|
|
66
|
+
return actor.kind !== 'person' && actor.kind !== 'job';
|
|
67
|
+
}
|
|
68
|
+
const states = new WeakMap();
|
|
50
69
|
const WRITES = new Set(['POST', 'PUT', 'PATCH', 'DELETE']);
|
|
51
70
|
/** The `iss` of a JWT, read without checking it, or null for anything that is not one. */
|
|
52
71
|
function issuerOf(token) {
|
|
@@ -100,19 +119,30 @@ export function bridgeHandle(options) {
|
|
|
100
119
|
actor = checked.actor;
|
|
101
120
|
}
|
|
102
121
|
let flags = null;
|
|
122
|
+
const state = { actor, admitted: actor === null || !isKey(actor) };
|
|
103
123
|
const locals = {
|
|
104
|
-
actor
|
|
124
|
+
get actor() {
|
|
125
|
+
return state.admitted ? state.actor : null;
|
|
126
|
+
},
|
|
105
127
|
server,
|
|
106
128
|
flags(hints) {
|
|
107
|
-
|
|
129
|
+
// A credential nothing checked (a `skip` route) is never forwarded: it may be a key
|
|
130
|
+
// from someone else's Bridge app, and that app's flag results would come back.
|
|
131
|
+
if (actor === null && carriesBridgeCredential(event.request, options.issuer))
|
|
132
|
+
return Promise.resolve(new FlagSet());
|
|
133
|
+
const workspace = actor !== null && isKey(actor) ? workspaceNamedBy(event.request.headers) : undefined;
|
|
134
|
+
flags ??= server.flags.forRequest(event.request, hints, workspace === undefined ? {} : { workspace }).catch(() => new FlagSet());
|
|
108
135
|
return flags;
|
|
109
136
|
},
|
|
110
137
|
meter(metric, quantity, countOptions) {
|
|
111
|
-
if (actor === null)
|
|
138
|
+
if (state.actor === null)
|
|
112
139
|
throw new Error('[bridge] meter() needs a signed-in caller: check with requireActor first.');
|
|
113
|
-
|
|
140
|
+
if (!state.admitted)
|
|
141
|
+
error(403, KEY_REFUSAL);
|
|
142
|
+
server.usage.count(state.actor, metric, quantity, countOptions);
|
|
114
143
|
},
|
|
115
144
|
};
|
|
145
|
+
states.set(locals, state);
|
|
116
146
|
event.locals.bridge = locals;
|
|
117
147
|
return resolve(event);
|
|
118
148
|
};
|
|
@@ -123,12 +153,45 @@ function localsOf(event) {
|
|
|
123
153
|
throw new Error('[bridge] No Bridge on this request. Add `export const handle = bridgeHandle({ ... })` to hooks.server.ts.');
|
|
124
154
|
return locals;
|
|
125
155
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
156
|
+
const KEY_REFUSAL = 'This endpoint is not open to keys. Gate it with requireFlag, or mark it open with requireActor(event, { openToKeys: true }).';
|
|
157
|
+
function stateOf(event) {
|
|
158
|
+
return states.get(localsOf(event));
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* The caller, or a 401 when the request carried no credential.
|
|
162
|
+
*
|
|
163
|
+
* A request that came with a Bridge key is refused 403 unless the endpoint
|
|
164
|
+
* says keys may come in: pass `{ openToKeys: true }` for an endpoint that is
|
|
165
|
+
* open to every valid key, or gate it with `requireFlag`. People are
|
|
166
|
+
* unaffected.
|
|
167
|
+
*/
|
|
168
|
+
export function requireActor(event, options = {}) {
|
|
169
|
+
const state = stateOf(event);
|
|
170
|
+
if (state.actor === null)
|
|
171
|
+
error(401, 'Sign in first.');
|
|
172
|
+
if (!state.admitted) {
|
|
173
|
+
if (options.openToKeys !== true)
|
|
174
|
+
error(403, KEY_REFUSAL);
|
|
175
|
+
state.admitted = true;
|
|
176
|
+
}
|
|
177
|
+
return state.actor;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* The caller, once the flag is on for them: a 401 when the request carried no
|
|
181
|
+
* credential, a 403 when the flag is off. Bridge works the flag out for the
|
|
182
|
+
* caller. For a key, rules on the key's privileges apply, so only a key whose
|
|
183
|
+
* privileges match the flag's rule gets in; this is what lets a key through
|
|
184
|
+
* an endpoint. Fails closed: while Bridge cannot answer, every flag is off.
|
|
185
|
+
*/
|
|
186
|
+
export async function requireFlag(event, flag, hints) {
|
|
187
|
+
const state = stateOf(event);
|
|
188
|
+
if (state.actor === null)
|
|
130
189
|
error(401, 'Sign in first.');
|
|
131
|
-
|
|
190
|
+
const flags = await localsOf(event).flags(hints);
|
|
191
|
+
if (!flags.enabled(flag))
|
|
192
|
+
error(403, 'Not available.');
|
|
193
|
+
state.admitted = true;
|
|
194
|
+
return state.actor;
|
|
132
195
|
}
|
|
133
196
|
/** Counts usage of a metric for the caller's workspace ("count this"). Needs the app key in `bridgeHandle`. */
|
|
134
197
|
export function meter(event, metric, quantity, options) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nebulr-group/bridge-svelte",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.16",
|
|
4
4
|
"description": "The Bridge for SvelteKit: sign-in in your own pages, workspaces, flag-based route guards, plans and checkout, the team panel and live updates, on one setting: the issuer.",
|
|
5
5
|
"author": "Iman Pouya",
|
|
6
6
|
"license": "MIT",
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"@sveltejs/kit": "^2.0.0"
|
|
61
61
|
},
|
|
62
62
|
"dependencies": {
|
|
63
|
-
"@nebulr-group/bridge-auth-core": "1.0.0-alpha.
|
|
63
|
+
"@nebulr-group/bridge-auth-core": "1.0.0-alpha.14"
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|
|
66
66
|
"@sveltejs/kit": "^2.16.0",
|