UNPKG

@atproto/oauth-scopes

Version:

A library for manipulating and validating ATproto OAuth scopes in TypeScript.

81 lines • 2.79 kB
import { isNsid } from '../lib/nsid.js'; import { Parser } from '../lib/parser.js'; import { ScopeStringSyntax } from '../lib/syntax-string.js'; import { isScopeStringFor, } from '../lib/syntax.js'; import { knownValuesValidator } from '../lib/util.js'; export { isNsid }; export const REPO_ACTIONS = Object.freeze([ 'create', 'update', 'delete', ]); export const isRepoAction = knownValuesValidator(REPO_ACTIONS); export const isCollectionParam = (value) => value === '*' || isNsid(value); export class RepoPermission { constructor(collection, action) { this.collection = collection; this.action = action; } matches({ action, collection }) { return (this.action.includes(action) && (this.collection.includes('*') || this.collection.includes(collection))); } toString() { return RepoPermission.parser.format(this); } static { this.parser = new Parser('repo', { collection: { multiple: true, required: true, validate: isCollectionParam, normalize: (value) => { if (value.length > 1) { if (value.includes('*')) return ['*']; return [...new Set(value)].sort(); } return value; }, }, action: { multiple: true, required: false, validate: isRepoAction, default: REPO_ACTIONS, normalize: (value) => { return value === REPO_ACTIONS ? REPO_ACTIONS // No need to filter if the default was used : REPO_ACTIONS.filter(includedIn, value); }, }, }, 'collection'); } static fromString(scope) { if (!isScopeStringFor(scope, 'repo')) return null; const syntax = ScopeStringSyntax.fromString(scope); return RepoPermission.fromSyntax(syntax); } static fromSyntax(syntax) { const result = RepoPermission.parser.parse(syntax); if (!result) return null; return new RepoPermission(result.collection, result.action); } static scopeNeededFor(options) { return RepoPermission.parser.format({ collection: [options.collection], action: [options.action], }); } } /** * Special utility function to be used as predicate for array methods like * `Array.prototype.includes`, etc. When used as predicate, it expects that * the array method is called with a `thisArg` that is a readonly array of * the same type as the `value` parameter. */ function includedIn(value) { return this.includes(value); } //# sourceMappingURL=repo-permission.js.map