UNPKG

@atproto/oauth-scopes

Version:

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

55 lines • 1.79 kB
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 const ACCOUNT_ATTRIBUTES = Object.freeze([ 'email', 'repo', 'status', ]); export const ACCOUNT_ACTIONS = Object.freeze(['read', 'manage']); export class AccountPermission { constructor(attr, action) { this.attr = attr; this.action = action; } matches(options) { return (this.attr === options.attr && (this.action.includes('manage') || this.action.includes(options.action))); } toString() { return AccountPermission.parser.format(this); } static { this.parser = new Parser('account', { attr: { multiple: false, required: true, validate: knownValuesValidator(ACCOUNT_ATTRIBUTES), }, action: { multiple: true, required: false, validate: knownValuesValidator(ACCOUNT_ACTIONS), default: ['read'], }, }, 'attr'); } static fromString(scope) { if (!isScopeStringFor(scope, 'account')) return null; const syntax = ScopeStringSyntax.fromString(scope); return AccountPermission.fromSyntax(syntax); } static fromSyntax(syntax) { const result = AccountPermission.parser.parse(syntax); if (!result) return null; return new AccountPermission(result.attr, result.action); } static scopeNeededFor(options) { return AccountPermission.parser.format({ attr: options.attr, action: [options.action], }); } } //# sourceMappingURL=account-permission.js.map