@rebasepro/client 0.30.0 → 0.30.1

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/dist/auth.d.ts CHANGED
@@ -17,6 +17,55 @@ export interface PublicUserProfile {
17
17
  * every backend does send.
18
18
  */
19
19
  export type AuthConfig = AuthConfigDocument;
20
+ /**
21
+ * A device sign-in waiting to be approved in a browser. See
22
+ * `startDeviceLogin`.
23
+ */
24
+ export interface DeviceLoginStart {
25
+ /** The secret this device polls with. Never shown. */
26
+ deviceCode: string;
27
+ /** The code the person checks on the approval page, like `BCDF-GHJK`. */
28
+ userCode: string;
29
+ /** The approval page. */
30
+ verificationUri: string;
31
+ /** The approval page with the code filled in. */
32
+ verificationUriComplete: string;
33
+ /** Seconds until the sign-in expires. */
34
+ expiresIn: number;
35
+ /** Seconds to wait between polls. */
36
+ interval: number;
37
+ }
38
+ /** What one poll of a device sign-in found. See `pollDeviceLogin`. */
39
+ export type DeviceLoginPoll = {
40
+ status: "pending";
41
+ interval: number;
42
+ }
43
+ /** Polled too soon: wait `interval` seconds — the new, longer interval — before the next. */
44
+ | {
45
+ status: "slow_down";
46
+ interval: number;
47
+ } | {
48
+ status: "denied";
49
+ } | {
50
+ status: "expired";
51
+ } | {
52
+ status: "approved";
53
+ user: User;
54
+ accessToken: string;
55
+ refreshToken: string;
56
+ };
57
+ /** A device sign-in as the approval page shows it. See `describeDeviceLogin`. */
58
+ export interface DeviceLoginRequest {
59
+ userCode: string;
60
+ /** What the device calls itself, e.g. `Rebase CLI on francesco-mbp`. */
61
+ clientName: string;
62
+ /** The address the device asked from. */
63
+ requestIp: string;
64
+ requestUserAgent: string;
65
+ status: "pending" | "approved" | "denied" | "consumed" | "expired";
66
+ createdAt: string;
67
+ expiresAt: string;
68
+ }
20
69
  /** The body of an Apple sign-in. See `signInWithApple`. */
21
70
  export interface AppleSignInPayload {
22
71
  /** The authorization code Apple returned. */
@@ -360,6 +409,13 @@ export declare function createAuth(transport: Transport, options?: CreateAuthOpt
360
409
  accessToken: string;
361
410
  refreshToken: string;
362
411
  }>;
412
+ startDeviceLogin: (options?: {
413
+ clientName?: string;
414
+ }) => Promise<DeviceLoginStart>;
415
+ pollDeviceLogin: (deviceCode: string) => Promise<DeviceLoginPoll>;
416
+ describeDeviceLogin: (userCode: string) => Promise<DeviceLoginRequest>;
417
+ approveDeviceLogin: (userCode: string) => Promise<void>;
418
+ denyDeviceLogin: (userCode: string) => Promise<void>;
363
419
  getSessions: () => Promise<DeviceSession[]>;
364
420
  revokeSession: (sessionId: string) => Promise<{
365
421
  success: boolean;
package/dist/index.d.ts CHANGED
@@ -23,7 +23,7 @@ export { or, and, not, cond } from "@rebasepro/common";
23
23
  export { resolveStorageReferences, storageReference, parseStorageReference, isStorageReference, STORAGE_REFERENCE_SCHEME, storageTargetOfDownloadUrl, storageReferencesForDownloadUrls, storageTargetsInText } from "@rebasepro/types";
24
24
  export type { StorageReferenceTarget, StorageReferenceResolver } from "@rebasepro/types";
25
25
  export { createCookieStorage, createMemoryStorage } from "./auth.js";
26
- export type { AppleSignInPayload, AuthConfig, AuthStorage, CookieStorageOptions, CreateAuthOptions, SessionRestoreOutcome } from "./auth.js";
26
+ export type { AppleSignInPayload, AuthConfig, AuthStorage, CookieStorageOptions, CreateAuthOptions, DeviceLoginPoll, DeviceLoginRequest, DeviceLoginStart, SessionRestoreOutcome } from "./auth.js";
27
27
  export { requestAppleAuthorization, preloadAppleSignIn, APPLE_JS_SDK_URL } from "./apple-sign-in.js";
28
28
  export type { AppleAuthorization, AppleAuthorizationRequest } from "./apple-sign-in.js";
29
29
  export type { User, RebaseSession, AuthTokens, AuthChangeEvent, DeviceSession } from "@rebasepro/types";
package/dist/index.es.js CHANGED
@@ -1257,6 +1257,95 @@ function createAuth(transport, options) {
1257
1257
  };
1258
1258
  }
1259
1259
  /**
1260
+ * Start a device sign-in: the way something without a browser of its own —
1261
+ * a terminal — signs in through one (RFC 8628's device authorization
1262
+ * grant). Show the person `verificationUriComplete` and `userCode`, then
1263
+ * call {@link pollDeviceLogin} every `interval` seconds until it is
1264
+ * decided. Refused unless the backend has `auth.deviceLogin` on; its
1265
+ * `/auth/config` says `deviceLogin: true` when it does.
1266
+ *
1267
+ * @param options.clientName What the approval page calls this device.
1268
+ */
1269
+ async function startDeviceLogin(options) {
1270
+ const res = await getFetch()(authUrl("/device"), {
1271
+ method: "POST",
1272
+ headers: { "Content-Type": "application/json" },
1273
+ body: JSON.stringify({ clientName: options?.clientName })
1274
+ });
1275
+ const body = await res.json().catch(() => ({}));
1276
+ if (!res.ok) throwApiError(res.status, body, res.statusText);
1277
+ return body;
1278
+ }
1279
+ /**
1280
+ * Ask once whether a device sign-in has been decided. When it has been
1281
+ * approved, this client is signed in — the session is stored like any other
1282
+ * sign-in's is — and the answer carries it. Any other answer is what to do
1283
+ * next: wait `interval` seconds and ask again (`pending`, `slow_down`),
1284
+ * or stop (`denied`, `expired`).
1285
+ *
1286
+ * Rejects for anything else: a network failure, a rate limit, a device
1287
+ * code that has already been used.
1288
+ */
1289
+ async function pollDeviceLogin(deviceCode) {
1290
+ const res = await getFetch()(authUrl("/device/token"), {
1291
+ method: "POST",
1292
+ headers: { "Content-Type": "application/json" },
1293
+ body: JSON.stringify({ deviceCode })
1294
+ });
1295
+ const body = await res.json().catch(() => ({}));
1296
+ if (!res.ok) {
1297
+ const error = apiError(res.status, body, res.statusText);
1298
+ const details = error.details;
1299
+ const interval = typeof details === "object" && details !== null && "interval" in details && typeof details.interval === "number" ? details.interval : void 0;
1300
+ if (error.code === "authorization_pending") return {
1301
+ status: "pending",
1302
+ interval: interval ?? 5
1303
+ };
1304
+ if (error.code === "slow_down") return {
1305
+ status: "slow_down",
1306
+ interval: interval ?? 10
1307
+ };
1308
+ if (error.code === "access_denied") return { status: "denied" };
1309
+ if (error.code === "expired_token") return { status: "expired" };
1310
+ throw error;
1311
+ }
1312
+ const session = handleAuthResponse(body, "SIGNED_IN");
1313
+ return {
1314
+ status: "approved",
1315
+ user: session.user,
1316
+ accessToken: session.accessToken,
1317
+ refreshToken: session.refreshToken
1318
+ };
1319
+ }
1320
+ /**
1321
+ * What a device sign-in is, for the page that approves it: the code, what
1322
+ * the device calls itself and where it asked from. Needs this client to
1323
+ * be signed in. Rejects with `DEVICE_LOGIN_NOT_FOUND` (404) for a code no
1324
+ * sign-in has.
1325
+ */
1326
+ async function describeDeviceLogin(userCode) {
1327
+ return transport.request(`${authPath}/device/${encodeURIComponent(userCode)}`, { method: "GET" });
1328
+ }
1329
+ /**
1330
+ * Approve a device sign-in as the signed-in account: the device's next
1331
+ * poll gets a session for it. Rejects with `DEVICE_LOGIN_EXPIRED` (410),
1332
+ * `DEVICE_LOGIN_DECIDED` (409), or `AAL2_REQUIRED` (403) when the account
1333
+ * has two-factor authentication on and this session did not use it.
1334
+ */
1335
+ async function approveDeviceLogin(userCode) {
1336
+ await transport.request(`${authPath}/device/approve`, {
1337
+ method: "POST",
1338
+ body: JSON.stringify({ userCode })
1339
+ });
1340
+ }
1341
+ /** Deny a device sign-in: its next poll is told so, and it gets nothing. */
1342
+ async function denyDeviceLogin(userCode) {
1343
+ await transport.request(`${authPath}/device/deny`, {
1344
+ method: "POST",
1345
+ body: JSON.stringify({ userCode })
1346
+ });
1347
+ }
1348
+ /**
1260
1349
  * Sign in without credentials, as a throwaway account.
1261
1350
  *
1262
1351
  * The account is real — it has an id, roles and a session, so row-level
@@ -1582,6 +1671,11 @@ function createAuth(transport, options) {
1582
1671
  verifyMagicLink,
1583
1672
  sendEmailOtp,
1584
1673
  verifyEmailOtp,
1674
+ startDeviceLogin,
1675
+ pollDeviceLogin,
1676
+ describeDeviceLogin,
1677
+ approveDeviceLogin,
1678
+ denyDeviceLogin,
1585
1679
  getSessions,
1586
1680
  revokeSession,
1587
1681
  revokeAllSessions,