Something went wrong. Try again.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Auth\Permission;
/** * Shared encoding and validation helpers for permission scopes. * * @internal */final class PermissionCodec {
/** * @param array<string, list<string>> $parameters */ public static function encode(string $resource, ?string $positional, array $parameters): string { $scope = $resource . ($positional !== NULL ? ':' . self::encodeValue($positional) : ''); $pairs = []; foreach ($parameters as $name => $values) { foreach ($values as $value) { $pairs[] = \rawurlencode($name) . '=' . self::encodeValue($value); } } return $pairs === [] ? $scope : $scope . '?' . \implode('&', $pairs); }
public static function isDidServiceReference(string $value): bool { if (\substr_count($value, '#') !== 1) { return FALSE; } [$did, $fragment] = \explode('#', $value, 2); return ( \Wotnak\Atproto\Identifier\DidParser::isValid($did) && \preg_match('/^[A-Za-z][A-Za-z0-9._:-]*$/D', $fragment) === 1 ); }
/** * @param list<string> $values */ public static function assertNsidOrWildcardList(array $values, string $description): void { if ($values === []) { throw new \InvalidArgumentException($description . ' must not be empty.'); } $parser = new \Wotnak\Atproto\Identifier\NsidParser(); $seen = []; foreach ($values as $value) { if ($value !== '*' && !$parser->isValid($value) || isset($seen[$value])) { throw new \InvalidArgumentException($description . ' must contain unique NSIDs or a wildcard.'); } $seen[$value] = TRUE; } if (isset($seen['*']) && \count($seen) !== 1) { throw new \InvalidArgumentException('A permission wildcard cannot be combined with named values.'); } }
private static function encodeValue(string $value): string { return \str_replace(['%3A', '%2F', '%2A'], [':', '/', '*'], \rawurlencode($value)); }
}