aboutsummaryrefslogtreecommitdiff
path: root/web/ui/src/lib/keys.ts
blob: 5fddedc8ceeb65150dbfb6311ae55c6c5ce7f885 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { DATASETS } from './datasets';

/** Display mask: prefix + bullets. We don't store the full key, so we
 *  cannot show any trailing characters from it after creation. */
export function maskKey(prefix: string): string {
  return `${prefix}${'\u2022'.repeat(24)}`;
}

/** Validate + normalize a scope list from user input. Used server-side
 *  when persisting a new key; kept in the client-safe module so the
 *  create-key form can mirror its logic if it wants. */
export function normalizeScopes(input: string[]): string[] {
  if (!input || input.length === 0) return ['*'];
  if (input.includes('*')) return ['*'];
  const allowed = new Set<string>(DATASETS);
  const out = input.filter((s) => allowed.has(s));
  return out.length === 0 ? ['*'] : out;
}

export function scopeSummary(scopes: string[]): string {
  if (scopes.length === 1 && scopes[0] === '*') return 'all datasets';
  if (scopes.length <= 3) return scopes.join(', ');
  return `${scopes.slice(0, 2).join(', ')} +${scopes.length - 2} more`;
}