Skip to content

Commit

Permalink
URI internal codebase improve user information handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jul 5, 2024
1 parent 8cfbd03 commit 8b74978
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 49 deletions.
10 changes: 2 additions & 8 deletions components/Components/UserInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,12 @@ public function getPass(): ?string

public function getUsername(): ?string
{
return match ($this->username) {
null => null,
default => Encoder::encodeUser($this->username)
};
return Encoder::encodeUser($this->username);
}

public function getPassword(): ?string
{
return match ($this->password) {
null => null,
default => Encoder::encodePassword($this->password)
};
return Encoder::encodePassword($this->password);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion interfaces/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Closure;
use League\Uri\Contracts\UriComponentInterface;
use League\Uri\Exceptions\SyntaxError;
use SensitiveParameter;
use Stringable;

use function preg_match;
Expand Down Expand Up @@ -57,7 +58,7 @@ public static function encodeUser(Stringable|string|null $component): ?string
*
* Generic delimiters ":" MUST NOT be encoded
*/
public static function encodePassword(Stringable|string|null $component): ?string
public static function encodePassword(#[SensitiveParameter] Stringable|string|null $component): ?string
{
static $pattern = '/[^'.self::REGEXP_PART_UNRESERVED.self::REGEXP_PART_SUBDELIM.':]+|'.self::REGEXP_PART_ENCODED.'/';

Expand Down
66 changes: 26 additions & 40 deletions uri/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,18 @@ private function __construct(
?string $fragment
) {
$this->scheme = $this->formatScheme($scheme);
$this->user = Encoder::encodeUser($user);
$this->pass = Encoder::encodePassword($pass);
$this->userInfo = $this->formatUserInfo($user, $pass);
[$this->user, $this->pass] = $this->setUserAndPass();
$this->host = $this->formatHost($host);
$this->port = $this->formatPort($port);
$this->authority = $this->setAuthority();
$this->path = $this->formatPath($path);
$this->query = Encoder::encodeQueryOrFragment($query);
$this->fragment = Encoder::encodeQueryOrFragment($fragment);

$this->assertValidState();

$this->uri = $this->getUriString();
}

Expand Down Expand Up @@ -272,8 +275,8 @@ private function formatUserInfo(
#[SensitiveParameter] ?string $password
): ?string {
return match (null) {
$password => Encoder::encodeUser($user),
default => Encoder::encodeUser($user).':'.Encoder::encodePassword($password),
$password => $user,
default => $user.':'.$password,
};
}

Expand Down Expand Up @@ -734,14 +737,6 @@ private function setAuthority(): ?string
return $authority;
}

/**
* @return non-empty-array<int, ?string>
*/
private function setUserAndPass(): array
{
return null !== $this->userInfo ? explode(':', $this->userInfo, 2) + [1 => null] : [null, null];
}

/**
* Format the Path component.
*/
Expand Down Expand Up @@ -939,27 +934,24 @@ private function isNonEmptyHostUriWithoutFragmentAndQuery(): bool
* @link https://tools.ietf.org/html/rfc3986#section-5.3
*/
private function getUriString(): string {
$scheme = match ($this->scheme) {
null => '',
default => $this->scheme.':',
};
$uri = '';
if (null !== $this->scheme) {
$uri .= $this->scheme.':';
}

$authority = match ($this->authority) {
null => '',
default => '//'.$this->authority,
};
if (null !== $this->authority) {
$uri .= '//'.$this->authority;
}

$query = match ($this->query) {
null => '',
default => '?'.$this->query,
};
$uri .= $this->path;
if (null !== $this->query) {
$uri .= '?'.$this->query;
}

$fragment = match ($this->fragment) {
null => '',
default => '#'.$this->fragment,
return match ($this->fragment) {
null => $uri,
default => $uri.'#'.$this->fragment,
};

return $scheme.$authority.$this->path.$query.$fragment;
}

public function toString(): string
Expand Down Expand Up @@ -1110,7 +1102,7 @@ public function withScheme(Stringable|string|null $scheme): UriInterface
*
* @throws SyntaxError if the submitted data cannot be converted to string
*/
private function filterString(Stringable|string|null $str): ?string
private function filterString(#[SensitiveParameter] Stringable|string|null $str): ?string
{
$str = match (true) {
$str instanceof UriComponentInterface => $str->value(),
Expand All @@ -1129,18 +1121,12 @@ public function withUserInfo(
Stringable|string|null $user,
#[SensitiveParameter] Stringable|string|null $password = null
): UriInterface {
$user_info = null;
$user = $this->filterString($user);
if (null !== $password) {
$password = $this->filterString($password);
}

if ('' !== $user) {
$user_info = $this->formatUserInfo($user, $password);
}
$user = Encoder::encodeUser($this->filterString($user));
$password = Encoder::encodePassword($this->filterString($password));
$userInfo = ('' !== $user) ? $this->formatUserInfo($user, $password) : null;

return match ($user_info) {
$this->userInfo=> $this,
return match ($userInfo) {
$this->userInfo => $this,
default => new self(
$this->scheme,
$user,
Expand Down

0 comments on commit 8b74978

Please sign in to comment.