Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Auth\Permission;
/** * Parses current granular repo and rpc OAuth permission scopes. * * @api */final class PermissionScopeParser {
/** * Parses one granular permission scope. */ public static function parse(string $scope): PermissionInterface { if (\preg_match('/^[\x21\x23-\x5B\x5D-\x7E]+$/D', $scope) !== 1) { throw new \InvalidArgumentException('Permission scopes must use printable non-whitespace ASCII.'); } $scopeParts = \explode('?', $scope, 2); $head = $scopeParts[0]; $query = $scopeParts[1] ?? NULL; $headParts = \explode(':', $head, 2); $resource = $headParts[0]; $positional = $headParts[1] ?? NULL; if (\preg_match('/^[a-z][a-z0-9-]*$/D', $resource) !== 1 || $positional === '') { throw new \InvalidArgumentException('Permission scope resource syntax is invalid.'); } $parameters = self::parseParameters($query); return match ($resource) { 'repo' => self::parseRepo($positional, $parameters), 'rpc' => self::parseRpc($positional, $parameters), default => throw new \InvalidArgumentException(\sprintf('Unsupported permission resource "%s".', $resource)), }; }
/** * Parses the permissions in an OAuth scope string. * * @return list<PermissionInterface> */ public static function parseScopeString(string $scopes): array { if (\trim($scopes) === '') { return []; } $tokens = \preg_split('/ +/', \trim($scopes)); return \array_map(self::parse(...), $tokens !== FALSE ? $tokens : []); }
/** * Serializes the scope string. * * @param list<PermissionInterface> $permissions */ public static function serializeScopeString(array $permissions): string { return \implode(' ', \array_map( static fn (PermissionInterface $permission): string => $permission->toScope(), $permissions, )); }
/** * @param array<string, list<string>> $parameters */ private static function parseRepo(?string $positional, array $parameters): RepoPermission { $collections = self::positionalOrParameter($positional, $parameters, 'collection'); self::assertOnlyParameters($parameters, ['collection', 'action']); return new RepoPermission($collections, $parameters['action'] ?? []); }
/** * @param array<string, list<string>> $parameters */ private static function parseRpc(?string $positional, array $parameters): RpcPermission { $methods = self::positionalOrParameter($positional, $parameters, 'lxm'); self::assertOnlyParameters($parameters, ['lxm', 'aud']); $audiences = $parameters['aud'] ?? []; if (\count($audiences) !== 1) { throw new \InvalidArgumentException('Granular RPC scopes require exactly one audience.'); } return new RpcPermission($methods, $audiences[0]); }
/** * @param array<string, list<string>> $parameters * @return list<string> */ private static function positionalOrParameter(?string $positional, array $parameters, string $name): array { if ($positional !== NULL && \array_key_exists($name, $parameters)) { throw new \InvalidArgumentException(\sprintf('Permission parameter "%s" is specified twice.', $name)); } if ($positional !== NULL) { return [self::decode($positional)]; } return $parameters[$name] ?? []; }
/** * @return array<string, list<string>> */ private static function parseParameters(?string $query): array { if ($query === NULL || $query === '') { return []; } $parameters = []; foreach (\explode('&', $query) as $pair) { if ($pair === '' || !\str_contains($pair, '=')) { throw new \InvalidArgumentException('Permission parameters must have name and value parts.'); } [$name, $value] = \explode('=', $pair, 2); $name = self::decode($name); $value = self::decode($value); if ($name === '' || $value === '') { throw new \InvalidArgumentException('Permission parameter names and values must not be empty.'); } $parameters[$name][] = $value; } return $parameters; }
private static function decode(string $value): string { if (\preg_match('/%(?![0-9A-Fa-f]{2})/', $value) === 1) { throw new \InvalidArgumentException('Permission scope contains invalid percent encoding.'); } return \rawurldecode($value); }
/** * @param array<string, list<string>> $parameters * @param list<string> $allowed */ private static function assertOnlyParameters(array $parameters, array $allowed): void { foreach (\array_keys($parameters) as $name) { if (!\in_array($name, $allowed, TRUE)) { throw new \InvalidArgumentException(\sprintf('Unsupported permission parameter "%s".', $name)); } } }
}