Unverified Commit 9d9c9817 authored by Hong Minhee's avatar Hong Minhee
Browse files

Authenticated document loader

parent 62f98c9b
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -55,6 +55,7 @@
    "nodeinfo",
    "phensley",
    "Pixelfed",
    "redirections",
    "rels",
    "setext",
    "subproperty",
+7 −1
Original line number Diff line number Diff line
@@ -21,9 +21,15 @@ To be released.

 -  Added `getActorHandle()` function.

 -  Now `loookupWebFinger()` follows redirections.
 -  Fedify now has authenticated document loader.  [[#12]]

     -  Added `Context.getDocumentLoader()` method.
     -  Added `getAuthenticatedDocumentLoader()` function.

 -  Now `lookupWebFinger()` follows redirections.

[Fresh]: https://fresh.deno.dev/
[#12]: https://github.com/dahlia/fedify/issues/12


Version 0.3.0
+19 −0
Original line number Diff line number Diff line
@@ -153,3 +153,22 @@ So you can just pass a `Context` object to those methods:
const object = await Object.fromJsonLd(jsonLd, ctx);
const json = await object.toJsonLd(ctx);
~~~~


Getting an authenticated `DocumentLoader`
-----------------------------------------

Sometimes you need to load a remote document which requires authentication,
such as an actor's following collection that is configured as private.
In such cases, you can use the `Context.getDocumentLoader()` method to get
an authenticated `DocumentLoader` object.  The following shows an example:

~~~~ typescript
const documentLoader = await ctx.getDocumentLoader({ handle: "john" });
const following = await actor.getFollowing({ documentLoader });
~~~~

In the above example, the `getFollowing()` method takes the `documentLoader`
which is authenticated as the actor with a handle of `john`.
If the `actor` allows `john` to see the following collection,
the `getFollowing()` method returns the following collection.
+16 −0
Original line number Diff line number Diff line
@@ -217,6 +217,22 @@ example of looking up a `Note` object from the URI:
const note = await lookupObject("https://todon.eu/@hongminhee/112060633798771581");
~~~~

> [!NOTE]
> Some objects require authentication to look up, such as a `Note` object with
> a visibility of followers-only.  In such cases, you need to use
> the `Context.getDocumentLoader()` method to get an authenticated
> `DocumentLoader` object.  The `lookupObject()` function takes the
> `documentLoader` option to specify the method to fetch the remote object:
>
> ~~~~ typescript
> const documentLoader = await ctx.getDocumentLoader({ handle: "john" });
> const note = await lookupObject("...", { documentLoader });
> ~~~~
>
> See the [*Getting an authenticated
> `DocumentLoader`*](./context.md#getting-an-authenticated-documentloader)
> section for details.


JSON-LD
-------
+27 −1
Original line number Diff line number Diff line
@@ -84,12 +84,38 @@ export interface Context<TContextData> {
   */
  getActorKey(handle: string): Promise<CryptographicKey | null>;

  /**
   * Gets an authenticated {@link DocumentLoader} for the given identity.
   * Note that an authenticated document loader intentionally does not cache
   * the fetched documents.
   * @param identity The identity to get the document loader for.
   *                 The actor's handle.
   * @returns The authenticated document loader.
   * @throws {Error} If the identity is not valid.
   * @throws {TypeError} If the key is invalid or unsupported.
   */
  getDocumentLoader(identity: { handle: string }): Promise<DocumentLoader>;

  /**
   * Gets an authenticated {@link DocumentLoader} for the given identity.
   * Note that an authenticated document loader intentionally does not cache
   * the fetched documents.
   * @param identity The identity to get the document loader for.
   *                 The actor's key pair.
   * @returns The authenticated document loader.
   * @throws {TypeError} If the key is invalid or unsupported.
   */
  getDocumentLoader(
    identity: { keyId: URL; privateKey: CryptoKey },
  ): DocumentLoader;

  /**
   * Sends an activity to recipients' inboxes.
   * @param sender The sender's handle or sender's key pair.
   * @param sender The sender's handle or the sender's key pair.
   * @param recipients The recipients of the activity.
   * @param activity The activity to send.
   * @param options Options for sending the activity.
   * @throws {Error} If the sender is not valid.
   */
  sendActivity(
    sender: { keyId: URL; privateKey: CryptoKey } | { handle: string },
Loading