@calimero-network/mero-js 19.9.0 → 19.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -240,6 +240,22 @@ export interface AccountLinkProof {
240
240
  /** `signAccountLink`'s output — standard base64. */
241
241
  signature: string;
242
242
  }
243
+ /** Where to send someone so a cloud login can consent to linking an account. */
244
+ export interface AccountLinkHandoff {
245
+ /** The cloud portal URL to open — a new tab, a popup, or the system browser. */
246
+ url: string;
247
+ /** The account the consent will be for, echoed so a caller can show it. */
248
+ accountId: string;
249
+ }
250
+ /** What comes back on the callback: a grant, a refusal, or neither. */
251
+ export interface AccountLinkCallback {
252
+ /** The grant to spend with {@link CloudClient.linkAccountWithGrant}. */
253
+ grant?: string;
254
+ /** The account the grant is for, as the portal echoed it. */
255
+ accountId?: string;
256
+ /** `'denied'` when the person cancelled. Absent on success. */
257
+ error?: string;
258
+ }
243
259
  /** A single-use challenge for an account root to sign itself in with. */
244
260
  export interface AccountLoginChallenge {
245
261
  /** Sign this verbatim. Opaque and sealed by the cloud; nonces live ~2 minutes. */
@@ -544,6 +560,64 @@ export declare class CloudClient {
544
560
  * key rather than letting a user type either.
545
561
  */
546
562
  submitAccountLink(proof: AccountLinkProof): Promise<CloudAccountLink>;
563
+ /**
564
+ * Where to send someone to authorise linking this account, and what to expect
565
+ * back. Pairs with {@link readAccountLinkCallback} and
566
+ * {@link linkAccountWithGrant}.
567
+ *
568
+ * This exists because a browser app is in a bind: it holds an account root and
569
+ * can never hold a *first* cloud session, since only Google issues one. So the
570
+ * person goes to the cloud, where they are signed in, and consents there. What
571
+ * comes back is a grant — consent to link this one account, spendable only by
572
+ * whoever can sign with its root.
573
+ *
574
+ * @param callbackUrl where the portal sends the person back. Must be an origin
575
+ * the cloud allows; unlisted ones are refused when the grant is minted, not
576
+ * here. The grant arrives in the URL **fragment**, which browsers do not
577
+ * send to servers.
578
+ *
579
+ * Static, so it needs no client and no session: there is nothing to
580
+ * authenticate as yet.
581
+ */
582
+ static accountLinkHandoff(options: {
583
+ /** The cloud portal's origin, e.g. `https://cloud.calimero.network`. */
584
+ portalUrl: string;
585
+ /** The account to be linked, 64 hex — derive it, never let a user type it. */
586
+ accountId: string;
587
+ /** Where to come back to. */
588
+ callbackUrl: string;
589
+ }): AccountLinkHandoff;
590
+ /**
591
+ * Read the portal's answer out of a callback URL's fragment.
592
+ *
593
+ * Returns `{}` when there is nothing to read, so it is safe to call on every
594
+ * load — which is how a redirected app finds out it has been sent back.
595
+ *
596
+ * @param href defaults to the current location, when there is one.
597
+ */
598
+ static readAccountLinkCallback(href?: string): AccountLinkCallback;
599
+ /**
600
+ * Spend a link grant: sign it with the account root and post the pair.
601
+ *
602
+ * Anonymous, and it has to be — the app doing this holds no cloud session,
603
+ * which is the whole reason the grant exists. The grant carries the consenting
604
+ * login sealed inside it, so nothing here names one; a caller-supplied email
605
+ * would be a field to lie in.
606
+ *
607
+ * The signature is an ordinary account-link signature with the grant as the
608
+ * nonce, so the same {@link signAccountLink} every other link path uses signs
609
+ * this one. Both halves are required: a grant without the key links nothing,
610
+ * which is what makes it safe to carry through a browser redirect.
611
+ *
612
+ * A `403` means the grant was bad, expired, already spent, or signed by a key
613
+ * that names a different account; `409` that the account belongs to another
614
+ * login; `402` that the plan is full.
615
+ */
616
+ linkAccountWithGrant(options: {
617
+ grant: string;
618
+ /** The root of the account the grant names, 64 hex. */
619
+ rootSecret: string;
620
+ }): Promise<CloudAccountLink>;
547
621
  /** The accounts linked to this login, and how many the plan allows. */
548
622
  getMyAccounts(): Promise<CloudLinkedAccounts>;
549
623
  private adoptSession;
@@ -489,6 +489,101 @@ export class CloudClient {
489
489
  alreadyLinked: body.already_linked === true,
490
490
  };
491
491
  }
492
+ /**
493
+ * Where to send someone to authorise linking this account, and what to expect
494
+ * back. Pairs with {@link readAccountLinkCallback} and
495
+ * {@link linkAccountWithGrant}.
496
+ *
497
+ * This exists because a browser app is in a bind: it holds an account root and
498
+ * can never hold a *first* cloud session, since only Google issues one. So the
499
+ * person goes to the cloud, where they are signed in, and consents there. What
500
+ * comes back is a grant — consent to link this one account, spendable only by
501
+ * whoever can sign with its root.
502
+ *
503
+ * @param callbackUrl where the portal sends the person back. Must be an origin
504
+ * the cloud allows; unlisted ones are refused when the grant is minted, not
505
+ * here. The grant arrives in the URL **fragment**, which browsers do not
506
+ * send to servers.
507
+ *
508
+ * Static, so it needs no client and no session: there is nothing to
509
+ * authenticate as yet.
510
+ */
511
+ static accountLinkHandoff(options) {
512
+ const portal = options.portalUrl.replace(/\/+$/, '');
513
+ const query = new URLSearchParams({
514
+ 'link-account': options.accountId,
515
+ 'callback-url': options.callbackUrl,
516
+ });
517
+ return { url: `${portal}/?${query.toString()}`, accountId: options.accountId };
518
+ }
519
+ /**
520
+ * Read the portal's answer out of a callback URL's fragment.
521
+ *
522
+ * Returns `{}` when there is nothing to read, so it is safe to call on every
523
+ * load — which is how a redirected app finds out it has been sent back.
524
+ *
525
+ * @param href defaults to the current location, when there is one.
526
+ */
527
+ static readAccountLinkCallback(href) {
528
+ const source = href ?? (typeof window === 'undefined' ? '' : window.location.href);
529
+ if (!source)
530
+ return {};
531
+ let hash;
532
+ try {
533
+ hash = new URL(source).hash;
534
+ }
535
+ catch {
536
+ return {};
537
+ }
538
+ const params = new URLSearchParams(hash.replace(/^#/, ''));
539
+ const grant = params.get('grant');
540
+ const accountId = params.get('account');
541
+ const error = params.get('error');
542
+ const out = {};
543
+ if (grant)
544
+ out.grant = grant;
545
+ if (accountId)
546
+ out.accountId = accountId;
547
+ if (error)
548
+ out.error = error;
549
+ return out;
550
+ }
551
+ /**
552
+ * Spend a link grant: sign it with the account root and post the pair.
553
+ *
554
+ * Anonymous, and it has to be — the app doing this holds no cloud session,
555
+ * which is the whole reason the grant exists. The grant carries the consenting
556
+ * login sealed inside it, so nothing here names one; a caller-supplied email
557
+ * would be a field to lie in.
558
+ *
559
+ * The signature is an ordinary account-link signature with the grant as the
560
+ * nonce, so the same {@link signAccountLink} every other link path uses signs
561
+ * this one. Both halves are required: a grant without the key links nothing,
562
+ * which is what makes it safe to carry through a browser redirect.
563
+ *
564
+ * A `403` means the grant was bad, expired, already spent, or signed by a key
565
+ * that names a different account; `409` that the account belongs to another
566
+ * login; `402` that the plan is full.
567
+ */
568
+ async linkAccountWithGrant(options) {
569
+ const root = await accountRootFromSecret(options.rootSecret);
570
+ const body = await this.request('POST', '/api/cloud/accounts/link', {
571
+ body: {
572
+ grant: options.grant,
573
+ root_public_key: root.publicKey,
574
+ signature: await signAccountLink({
575
+ rootSecret: options.rootSecret,
576
+ nonce: options.grant,
577
+ }),
578
+ },
579
+ anonymous: true,
580
+ });
581
+ return {
582
+ accountId: String(body.account_id ?? root.accountId),
583
+ linkedAt: body.linked_at ?? null,
584
+ alreadyLinked: body.already_linked === true,
585
+ };
586
+ }
492
587
  /** The accounts linked to this login, and how many the plan allows. */
493
588
  async getMyAccounts() {
494
589
  const body = await this.request('GET', '/api/cloud/me/accounts');
@@ -1,5 +1,5 @@
1
1
  export { CloudClient } from './cloud-client.js';
2
- export type { CloudClientConfig, CloudSession, CloudNamespace, CloudRelay, CloudNamespaceNode, CloudNamespaceRouting, CloudMachine, CloudMachineNamespace, CloudLinkedAccount, CloudLinkedAccounts, CloudAccountLink, AccountLinkChallenge, AccountLinkProof, AccountLoginChallenge, AccountLoginProof, EnableHAOptions, DisableHAOptions, } from './cloud-client.js';
2
+ export type { CloudClientConfig, CloudSession, CloudNamespace, CloudRelay, CloudNamespaceNode, CloudNamespaceRouting, CloudMachine, CloudMachineNamespace, CloudLinkedAccount, CloudLinkedAccounts, CloudAccountLink, AccountLinkChallenge, AccountLinkProof, AccountLinkHandoff, AccountLinkCallback, AccountLoginChallenge, AccountLoginProof, EnableHAOptions, DisableHAOptions, } from './cloud-client.js';
3
3
  export { connectCloud } from './connect.js';
4
4
  export type { ConnectCloudOptions, CloudConnection } from './connect.js';
5
5
  export { signRoutingChallenge, routingProofHeaders } from './routing-proof.js';