Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 14 additions & 1 deletion src/auth/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ export interface AuthSession {
handleRedirect(url: string): Promise<Result<Credential, AuthError>>;
/** 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. */
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -340,6 +345,7 @@ export function createAuthSession(config: AuthSessionConfig): AuthSession {
reloadAuthority: loadAuthority,
handleRedirect,
useToken,
useRefreshToken,
useApiKey,
invalidateToken,
logout,
Expand Down Expand Up @@ -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);
Expand Down
55 changes: 37 additions & 18 deletions src/generated/operations/playlist-media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
*/
Expand All @@ -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 */
Expand All @@ -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`
*/
Expand All @@ -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'],
),
Expand All @@ -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`
*/
Expand All @@ -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'],
),
Expand Down Expand Up @@ -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`
*/
Expand All @@ -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')),
Expand All @@ -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`
*/
Expand All @@ -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()),
Expand Down
6 changes: 4 additions & 2 deletions src/generated/operations/playlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
*/
Expand All @@ -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'),
Expand Down
12 changes: 12 additions & 0 deletions test/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading