diff --git a/README.md b/README.md index 13df1a93f..f2a16a16a 100644 --- a/README.md +++ b/README.md @@ -218,6 +218,8 @@ Other parts of the session: - When the application comes back to the foreground without a session, the session loads the authority again, because the user can log in from a different tab. - `session.useToken(token)` and `session.useApiKey(key)` use a credential from a different source. +- `session.useRefreshToken(refresh_token)` keeps a refresh token from a different source. When + there is no valid access token, the next `authorize` exchanges it. - `session.invalidateToken()` removes the access token, so the next request gets a new one. - `session.logout()` ends the session on the server, removes the credentials and goes to the logout page. diff --git a/src/auth/session.ts b/src/auth/session.ts index 48c4884c0..c593e974c 100644 --- a/src/auth/session.ts +++ b/src/auth/session.ts @@ -264,6 +264,11 @@ export interface AuthSession { handleRedirect(url: string): Promise>; /** Uses a token from a different source. */ useToken(token: string, expires_at_ms?: number): void; + /** + * Keeps a refresh token from a different source. When there is no valid access token, the next + * `authorize` exchanges it. It is in memory only if the application is not trusted. + */ + useRefreshToken(refresh_token: string): void; /** Uses an API key, for a fixed device. */ useApiKey(key: string, options?: { readonly trusted?: boolean }): void; /** Removes the access token, so the next request gets a new one. */ @@ -311,7 +316,7 @@ export function createAuthSession(config: AuthSessionConfig): AuthSession { /** * A login that the user or the server refused: a consent denial or a refused code. Then * `authorize` does not send the user to the server again, so a request cannot start a loop. - * `start`, `handleRedirect`, `useToken` and `useApiKey` clear it. + * `start`, `handleRedirect`, `useToken`, `useRefreshToken` and `useApiKey` clear it. */ let halted: AuthError | null = null; /** `logout` adds one. A token request from an earlier epoch drops its tokens. */ @@ -340,6 +345,7 @@ export function createAuthSession(config: AuthSessionConfig): AuthSession { reloadAuthority: loadAuthority, handleRedirect, useToken, + useRefreshToken, useApiKey, invalidateToken, logout, @@ -956,6 +962,13 @@ export function createAuthSession(config: AuthSessionConfig): AuthSession { setStatus({ kind: 'signed_in', credential: 'bearer' }); } + function useRefreshToken(refresh_token: string): void { + assert(refresh_token.length > 0, 'the refresh token is not empty'); + writeTokens({ ...readTokens(), refresh_token }); + halted = null; + log.info('using a refresh token of the application'); + } + function useApiKey(key: string, options: { readonly trusted?: boolean } = {}): void { assert(key.length > 0, 'the API key is not empty'); storage.set(storageKey('x-api-key'), key); diff --git a/src/generated/operations/playlist-media.ts b/src/generated/operations/playlist-media.ts index 837c0ded7..bab19e0db 100644 --- a/src/generated/operations/playlist-media.ts +++ b/src/generated/operations/playlist-media.ts @@ -95,8 +95,9 @@ const DESTROY = /* @__PURE__ */ operation({ * list media items for the current authority. * * Non-admin callers see only items linked to groups they're a member of (direct or transitive). - * `group_id=...` scopes to a single group; `q=...` is a case-insensitive substring search across - * `name` and `description`; `tags=...` returns items carrying any of the supplied tags. + * `group_id=...` scopes to a single group by default. `q=...` is a case-insensitive substring + * search across `name` and `description`; `tags=...` returns items carrying any of the supplied + * tags. * * `GET /api/engine/v2/signage/media` */ @@ -114,6 +115,8 @@ const QUERY = /* @__PURE__ */ operation({ params: /* @__PURE__ */ parameters({ /** filter to items linked to this group (caller must have Read on the group) */ group_id: /* @__PURE__ */ nullable(/* @__PURE__ */ string()), + /** with group_id, also include items linked to readable descendant groups */ + include_child_data: /* @__PURE__ */ boolean(), /** case-insensitive substring search on name and description (SQL ILIKE) */ q: /* @__PURE__ */ nullable(/* @__PURE__ */ string()), /** return items carrying any of these tags */ @@ -134,10 +137,10 @@ const QUERY = /* @__PURE__ */ operation({ * Remove a tag from every media item carrying it, or with. * * remove a tag from every media item carrying it, or with `remove_media=true` remove the tagged - * media itself. Scoping and permissions per `tag_operation_scope`: authority-wide is admin/support - * only, `group_id` requires Read and Update (or Manage) on that group — Read and Delete when - * removing media. Removing media with `group_id` behaves like `DELETE /:id?group_id=`: the items - * are unlinked from that group and only deleted once no group links remain. + * media itself. Authority-wide is admin/support only. A single-group operation requires Read and + * Update (or Manage) on that group, or Read and Delete when removing media. An expanded operation + * requires Update on each matched item, or Delete on every matched group link when removing media. + * Group-scoped media removal keeps items that are still linked outside the scope. * * `DELETE /api/engine/v2/signage/media/tags` */ @@ -159,11 +162,17 @@ const REMOVE_TAG = /* @__PURE__ */ operation({ /** if true, removes the tag by removing all the media with this tag */ remove_media: /* @__PURE__ */ boolean(), /** - * limit to media linked to this group (caller must have Read and Update on the group, - * Read and - * Delete when removing media) + * select the media group. A single-group operation requires Read and Update, or Read + * and Delete + * when removing media. */ group_id: /* @__PURE__ */ nullable(/* @__PURE__ */ string()), + /** + * with group_id, also include media linked to readable descendant groups. Requires + * Update on + * every matched item, or Delete on every matched group link when removing media. + */ + include_child_data: /* @__PURE__ */ boolean(), }, ['tag'], ), @@ -176,8 +185,8 @@ const REMOVE_TAG = /* @__PURE__ */ operation({ * Rename a tag on every media item carrying it. Items that already carry. * * rename a tag on every media item carrying it. Items that already carry `new_tag` end up with it - * once. Scoping and permissions per `tag_operation_scope`: authority-wide is admin/support only, - * `group_id` requires Read and Update (or Manage) on that group. + * once. Authority-wide is admin/support only. A single-group operation requires Read and Update (or + * Manage) on that group. An expanded operation requires Update access to every matched item. * * `PATCH /api/engine/v2/signage/media/tags` */ @@ -199,9 +208,15 @@ const RENAME_TAG = /* @__PURE__ */ operation({ /** the replacement tag name */ new_tag: /* @__PURE__ */ string(), /** - * limit to media linked to this group (caller must have Read and Update on the group) + * select the media group. A single-group rename requires Read and Update on that group. */ group_id: /* @__PURE__ */ nullable(/* @__PURE__ */ string()), + /** + * with group_id, also include media linked to readable descendant groups. Every matched + * item + * must allow Update. + */ + include_child_data: /* @__PURE__ */ boolean(), }, ['current_tag', 'new_tag'], ), @@ -290,9 +305,9 @@ const SHOW = /* @__PURE__ */ operation({ * Return the number of media items carrying each tag. Scopes the same. * * return the number of media items carrying each tag. Scopes the same way as `index`/`tags`: - * `group_id=...` limits to media linked to that group (caller must have Read on it); without - * `group_id`, admin/support callers see counts across the authority while regular users see counts - * from media in groups they can read. An item carrying a tag twice still counts once. + * `group_id=...` limits to media linked to that group by default (caller must have Read on it). + * Without `group_id`, admin/support callers see counts across the authority while regular users see + * counts from media in groups they can read. An item carrying a tag twice still counts once. * * `GET /api/engine/v2/signage/media/tag_counts` */ @@ -310,6 +325,8 @@ const TAG_COUNTS = /* @__PURE__ */ operation({ params: /* @__PURE__ */ parameters({ /** limit to media linked to this group (caller must have Read on the group) */ group_id: /* @__PURE__ */ nullable(/* @__PURE__ */ string()), + /** with group_id, also count media linked to readable descendant groups */ + include_child_data: /* @__PURE__ */ boolean(), }), body: null, response: /* @__PURE__ */ record(/* @__PURE__ */ integer('Int32')), @@ -320,9 +337,9 @@ const TAG_COUNTS = /* @__PURE__ */ operation({ * Return the distinct tags in use by media. Scopes the same way as. * * return the distinct tags in use by media. Scopes the same way as `index`: `group_id=...` limits - * to media linked to that group (caller must have Read on it); without `group_id`, admin/support - * callers see every tag in the authority while regular users see tags from media in groups they can - * read. + * to media linked to that group by default (caller must have Read on it). Without `group_id`, + * admin/support callers see every tag in the authority while regular users see tags from media in + * groups they can read. * * `GET /api/engine/v2/signage/media/tags` */ @@ -340,6 +357,8 @@ const TAGS = /* @__PURE__ */ operation({ params: /* @__PURE__ */ parameters({ /** limit to media linked to this group (caller must have Read on the group) */ group_id: /* @__PURE__ */ nullable(/* @__PURE__ */ string()), + /** with group_id, also include tags on media linked to readable descendant groups */ + include_child_data: /* @__PURE__ */ boolean(), }), body: null, response: /* @__PURE__ */ array(/* @__PURE__ */ string()), diff --git a/src/generated/operations/playlist.ts b/src/generated/operations/playlist.ts index f931e7a22..9cbe198c9 100644 --- a/src/generated/operations/playlist.ts +++ b/src/generated/operations/playlist.ts @@ -270,8 +270,8 @@ const MEDIA_REVISIONS = /* @__PURE__ */ operation({ * list media playlists in the current authority. * * Non-admin callers see only playlists linked to groups they're a member of (direct or transitive). - * Pass `group_id=...` to scope to a specific group (caller must have Read on that group). Pass - * `q=...` for a case-insensitive substring search over `name` and `description`. + * Pass `group_id=...` to scope to a specific group by default (caller must have Read on that + * group). Pass `q=...` for a case-insensitive substring search over `name` and `description`. * * `GET /api/engine/v2/signage/playlists` */ @@ -289,6 +289,8 @@ const QUERY = /* @__PURE__ */ operation({ params: /* @__PURE__ */ parameters({ /** filter to playlists linked to this group (caller must have Read on the group) */ group_id: /* @__PURE__ */ nullable(/* @__PURE__ */ string()), + /** with group_id, also include playlists linked to readable descendant groups */ + include_child_data: /* @__PURE__ */ boolean(), /** case-insensitive substring search on name and description (SQL ILIKE) */ q: /* @__PURE__ */ nullable(/* @__PURE__ */ string()), limit: /* @__PURE__ */ integer('Int32'), diff --git a/test/auth.test.ts b/test/auth.test.ts index 330453927..38ed6195f 100644 --- a/test/auth.test.ts +++ b/test/auth.test.ts @@ -569,6 +569,18 @@ describe('frames, deep links and fixed devices', () => { expect(result).toEqual({ ok: true, value: { kind: 'bearer', token: 'token-1' } }); }); + test('useRefreshToken gives the refresh grant to an application that is not trusted', async () => { + const world = fake(); + const auth = session(world); + auth.useRefreshToken('pasted'); + const result = await auth.start(); + expect(result).toEqual({ ok: true, value: { kind: 'bearer', token: 'token-1' } }); + expect(world.tokenRequests()[0]?.form.get('refresh_token')).toBe('pasted'); + expect(world.navigations).toEqual([]); + // Not trusted: memory only. + expect(world.storage.get(`${CLIENT_ID}_refresh_token`)).toBeNull(); + }); + test('an API key in the URL is the credential of a fixed device', async () => { const world = fake({ url: 'http://localhost:4200/?x-api-key=key-1&fixed_device=true' }); const auth = session(world);