Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Config/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public function getHost(): string;
public function getRedirectURI(): ?string;
public function getHelloDomain(): string;
public function getHelloWallet(): ?string;
public function getSecret(): ?string;
public function getSecret(): string;
public function getLogDebug(): ?bool;
}
8 changes: 4 additions & 4 deletions src/Config/HelloConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class HelloConfig implements ConfigInterface
private string $helloDomain;
private ?string $helloWallet = null;
private string $host;
private ?string $secret = null;
private string $secret;
private ?bool $logDebug = null;
/** @var array<string, int|string>|null */
private ?array $error = null;
Expand All @@ -42,7 +42,7 @@ class HelloConfig implements ConfigInterface
* @param string|null $clientId
* @param string|null $redirectURI
* @param string $host
* @param string|null $secret
* @param string $secret
* @param callable|null $loginSync
* @param callable|null $logoutSync
* @param array<string, string> $cookies
Expand All @@ -65,7 +65,7 @@ public function __construct(
?string $clientId = null,
?string $redirectURI = null,
string $host = '',
?string $secret = null,
string $secret = '',
?callable $loginSync = null,
?callable $logoutSync = null,
array $cookies = [
Expand Down Expand Up @@ -219,7 +219,7 @@ public function getHelloWallet(): ?string
return $this->helloWallet;
}

public function getSecret(): ?string
public function getSecret(): string
{
return $this->secret;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Config/HelloConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class HelloConfigBuilder
private ?string $clientId = null;
private ?string $redirectURI = null;
private string $host = '';
private ?string $secret = null;
private string $secret = '';
/** @var array<string, string> */
private array $cookies = [
'authName' => 'hellocoop_auth',
Expand Down Expand Up @@ -88,7 +88,7 @@ public function setHost(string $host): self
return $this;
}

public function setSecret(?string $secret): self
public function setSecret(string $secret): self
{
$this->secret = $secret;
return $this;
Expand Down
6 changes: 3 additions & 3 deletions src/Exception/CallbackException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

class CallbackException extends Exception
{
/** @var array<string, int|string> */
/** @var array<string, string> */
private array $errorDetails;

/**
* @param array<string, int|string> $errorDetails
* @param array<string, string> $errorDetails
* @param string $message
* @param int $code
* @param Throwable|null $previous
Expand All @@ -27,7 +27,7 @@ public function __construct(
}

/**
* @return array<string, int|string>
* @return array<string, string>
*/
public function getErrorDetails(): array
{
Expand Down
4 changes: 2 additions & 2 deletions src/Handler/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
);
}

public function handleAuth(): ?AuthType
public function handleAuth(): AuthType
{
return $this->getAuthLib()->getAuthfromCookies();
}

public function updateAuth(AuthUpdates $authUpdates): ?AuthType
public function updateAuth(AuthUpdates $authUpdates): AuthType

Check warning on line 43 in src/Handler/Auth.php

View check run for this annotation

Codecov / codecov/patch

src/Handler/Auth.php#L43

Added line #L43 was not covered by tests
{
$auth = $this->getAuthLib()->getAuthfromCookies();
if ($auth->isLoggedIn === false) {
Expand Down
25 changes: 11 additions & 14 deletions src/Handler/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private function getTokenParser(): TokenParser
return $this->tokenParser ??= new TokenParser();
}

public function handleCallback(): ?string
public function handleCallback(): string
{
try {
$params = $this->helloRequest->fetchMultiple([
Expand All @@ -82,10 +82,13 @@ public function handleCallback(): ?string
'app_name'
]);

/** @var string $code */
$code = $params['code'] ?? null;
$error = $params['error'] ?? null;
$sameSite = $params['same_site'] ?? null;
/** @var string $code */
$wildcardDomain = $params['wildcard_domain'] ?? null;
/** @var string $code */
$appName = $params['app_name'] ?? null;

if ($this->config->getSameSiteStrict() && !$sameSite) {
Expand All @@ -103,8 +106,10 @@ public function handleCallback(): ?string
}

$codeVerifier = $oidcState['code_verifier'] ?? null;
$targetUri = $oidcState['target_uri'] ?? null;
$redirectUri = $oidcState['redirect_uri'] ?? null;
/** @var string $targetUri */
$targetUri = $oidcState['target_uri'] ?? '';
/** @var string $redirectUri */
$redirectUri = $oidcState['redirect_uri'] ?? '';
$nonce = $oidcState['nonce'] ?? null;

if ($error) {
Expand All @@ -119,14 +124,6 @@ public function handleCallback(): ?string
], 'Missing code parameter in callback request.');
}

if (is_array($code)) {
return $this->sendErrorPage([
'error' => 'invalid_request',
'error_description' => 'Received more than one code',
'target_uri' => $targetUri,
], 'Received multiple codes in callback request.');
}

if (!$codeVerifier) {
return $this->sendErrorPage([
'error' => 'invalid_request',
Expand All @@ -145,6 +142,7 @@ public function handleCallback(): ?string
'client_id' => $this->config->getClientId()
]);

/** @var array<string, string> $payload */
$payload = $this->getTokenParser()->parseToken($token)['payload'];

if ($payload['aud'] != $this->config->getClientId()) {
Expand Down Expand Up @@ -229,11 +227,10 @@ public function handleCallback(): ?string

if ($wildcardDomain) {
// the redirect_uri is not registered at Hellō - prompt to add
$appName = is_array($appName) ? $appName[0] : $appName;
$appName = $appName ?: 'Your App'; // Default to 'Your App' if $appName is empty

$queryParams = [
'uri' => is_array($wildcardDomain) ? $wildcardDomain[0] : $wildcardDomain,
'uri' => $wildcardDomain,
'appName' => $appName,
'redirectURI' => $redirectUri,
'targetURI' => $targetUri,
Expand Down Expand Up @@ -265,7 +262,7 @@ public function handleCallback(): ?string
* Uses the target URI from error details or a fallback error route. Updates the query
* string with error information. Throws an exception if no error URI is available.
*
* @param array<string, int|string|null> $error Error details including 'target_uri', 'error', and 'error_description'.
* @param array<string, string> $error Error details including 'target_uri', 'error', and 'error_description'.
* @param string $errorMessage A message describing the error.
* @param Throwable|null $previous Previous exception for chaining (optional).
*
Expand Down
4 changes: 2 additions & 2 deletions src/Handler/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private function getAuthHelper(): AuthHelper
/**
* @throws CryptoFailedException|InvalidSecretException
*/
public function generateLoginUrl(): ?string
public function generateLoginUrl(): string
{
$params = $this->helloRequest->fetchMultiple([
'provider_hint',
Expand Down Expand Up @@ -127,6 +127,6 @@ public function generateLoginUrl(): ?string
'target_uri' => $params['target_uri'],
]));

return is_string($authResponse['url']) ? $authResponse['url'] : null;
return $authResponse['url'];
}
}
17 changes: 14 additions & 3 deletions src/HelloClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,28 @@
*/
public function getAuth(): array
{
return $this->getAuthHandler()->handleAuth() ? $this->getAuthHandler()->handleAuth()->toArray() : [];
return$this->getAuthHandler()->handleAuth()->toArray();

Check warning on line 98 in src/HelloClient.php

View check run for this annotation

Codecov / codecov/patch

src/HelloClient.php#L98

Added line #L98 was not covered by tests
}

/**
* @throws InvalidSecretException
* @throws CryptoFailedException
* @return mixed|null
* @throws CryptoFailedException | InvalidSecretException
*/
private function handleLogin()
{
return $this->helloResponse->redirect($this->getLoginHandler()->generateLoginUrl());
}

/**
* @return mixed|null
*/
private function handleLogout()
{
return $this->helloResponse->redirect($this->getLogoutHandler()->generateLogoutUrl());
}

/**
* @return mixed|null
* @throws Exception
*/
private function handleInvite()
Expand All @@ -128,6 +132,9 @@
return $this->helloResponse->json($this->getAuthHandler()->handleAuth()->toArray());
}

/**
* @return mixed|string|null
*/
private function handleCallback()
{
try {
Expand All @@ -144,6 +151,10 @@
}
}

/**
* @return mixed|string|void|null
* @throws CryptoFailedException | InvalidSecretException
*/
public function route()
{
if (in_array($this->helloRequest->getMethod(), ["POST", "GET"]) === false) {
Expand Down
4 changes: 2 additions & 2 deletions src/HelloRequest/HelloRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class HelloRequest implements HelloRequestInterface
* @param string|null $default Default value if the key is not found.
* @return string|null The value of the parameter or default.
*/
public function fetch(string $key, string $default = null): ?string
public function fetch(string $key, ?string $default = null): ?string
{
// First check GET, then POST if not found.
return $_GET[$key] ?? $_POST[$key] ?? $default;
Expand Down Expand Up @@ -41,7 +41,7 @@ public function fetchMultiple(array $keys): array
* @param string|null $default Default value if the key is not found.
* @return string|null The value of the header or default.
*/
public function fetchHeader(string $key, string $default = null): ?string
public function fetchHeader(string $key, ?string $default = null): ?string
{
$headers = $this->getAllHeaders();
$normalizedKey = strtolower($key);
Expand Down
4 changes: 2 additions & 2 deletions src/HelloRequest/HelloRequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface HelloRequestInterface
* @param string|null $default Default value if the key is not found.
* @return string|null The value of the parameter or default.
*/
public function fetch(string $key, string $default = null): ?string;
public function fetch(string $key, ?string $default = null): ?string;

/**
* Fetch multiple parameters by keys from either GET or POST data.
Expand All @@ -28,7 +28,7 @@ public function fetchMultiple(array $keys): array;
* @param string|null $default Default value if the key is not found.
* @return string|null The value of the header or default.
*/
public function fetchHeader(string $key, string $default = null): ?string;
public function fetchHeader(string $key, ?string $default = null): ?string;

/**
* Fetch a cookie value by name.
Expand Down
3 changes: 2 additions & 1 deletion src/Lib/AuthHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct(PKCE $pkce)

/**
* @param array<string, mixed> $config
* @return array<string, mixed>
* @return array<string, string>
*/
public function createAuthRequest(array $config): array
{
Expand All @@ -43,6 +43,7 @@ public function createAuthRequest(array $config): array
$scopes = implode(' ', array_unique(array_merge($scopes, ['openid'])));
}

/** @var string $nonce */
$nonce = $config['nonce'] ?? $this->generateUuid();
// Prepare parameters
$params = [
Expand Down
4 changes: 2 additions & 2 deletions src/Lib/Crypto.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function encrypt(array $data): string
}

/**
* @return array<string, mixed>|null
* @return array<string, string>|null
* @throws DecryptionFailedException
*/
public function decrypt(string $encryptedStr): ?array
Expand All @@ -65,7 +65,7 @@ public function decrypt(string $encryptedStr): ?array
throw new DecryptionFailedException();
}

/** @var array<string, mixed>|null $jsonData */
/** @var array<string, string>|null $jsonData */
$jsonData = json_decode($decryptedData, true);
return $jsonData;
} catch (Exception $e) {
Expand Down
8 changes: 7 additions & 1 deletion src/Lib/TokenFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,20 @@

try {
$ch = $this->curl->init($tokenEndpoint);
if (!$ch) {
throw new \Exception('Curl error: initializing ' . $tokenEndpoint);

Check warning on line 46 in src/Lib/TokenFetcher.php

View check run for this annotation

Codecov / codecov/patch

src/Lib/TokenFetcher.php#L46

Added line #L46 was not covered by tests
}

$this->curl->setOpt($ch, CURLOPT_POST, true);
$this->curl->setOpt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);
$this->curl->setOpt($ch, CURLOPT_POSTFIELDS, $body);
$this->curl->setOpt($ch, CURLOPT_RETURNTRANSFER, true);

/** @var string $response */
$response = $this->curl->exec($ch);
$httpCode = $this->curl->getInfo($ch, CURLINFO_HTTP_CODE);

/** @var int $httpCode */
$httpCode = $this->curl->getInfo($ch, CURLINFO_HTTP_CODE);
if ($this->curl->error($ch)) {
throw new \Exception('Curl error: ' . $this->curl->error($ch));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Type/OIDC.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(string $codeVerifier, string $nonce, string $redirec
}

/**
* @param array<string, mixed> $data
* @param array<string, string> $data
* @return self
*/
public static function fromArray(array $data): self
Expand Down
1 change: 1 addition & 0 deletions tests/Lib/TokenFetcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function testFetchTokenSuccess(): void
public function testFetchTokenErrorResponse(): void
{
$curlMock = $this->createMock(CurlWrapper::class);
$curlMock->method('init')->willReturn(json_encode(['error' => 'mock_error']));
$curlMock->method('exec')->willReturn(json_encode(['error' => 'mock_error']));
$curlMock->method('getInfo')->willReturn(400);

Expand Down
Loading