Skip to content

Commit

Permalink
Refactor: Use factory methods instead of bulky constructors
Browse files Browse the repository at this point in the history
Replaced long constructor dependency injections with dedicated factory methods to improve readability and maintainability.
Related to #1
  • Loading branch information
UnnikrishnanBhargavakurup committed Dec 7, 2024
1 parent 6e4b146 commit 98d4afd
Show file tree
Hide file tree
Showing 10 changed files with 426 additions and 189 deletions.
43 changes: 38 additions & 5 deletions src/Handler/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,61 @@

namespace HelloCoop\Handler;

use HelloCoop\HelloResponse\HelloResponseInterface;
use HelloCoop\HelloRequest\HelloRequestInterface;
use HelloCoop\Config\ConfigInterface;
use HelloCoop\Lib\OIDCManager;
use HelloCoop\Lib\Crypto;
use HelloCoop\Type\AuthUpdates;
use HelloCoop\Type\Auth as AuthType;
use HelloCoop\Lib\Auth as AuthLib;

class Auth
{
private AuthLib $authLib;
public function __construct(AuthLib $authLib)
private HelloResponseInterface $helloResponse;
private HelloRequestInterface $helloRequest;
private ConfigInterface $config;
private ?AuthLib $authLib = null;
public function __construct(
HelloRequestInterface $helloRequest,
HelloResponseInterface $helloResponse,
ConfigInterface $config
) {
$this->helloRequest = $helloRequest;
$this->helloResponse = $helloResponse;
$this->config = $config;
}

private function getAuthLib(): AuthLib
{
$this->authLib = $authLib;
if ($this->authLib instanceof AuthLib) {
return $this->authLib;
}
$crypto = new Crypto($this->config->getSecret());
return $this->authLib = new AuthLib(
$this->helloRequest,
$this->helloResponse,
$this->config,
new OIDCManager(
$this->helloRequest,
$this->helloResponse,
$this->config,
$crypto
),
$crypto
);
}

public function handleAuth(): ?AuthType
{
return $this->authLib->getAuthfromCookies();
return $this->getAuthLib()->getAuthfromCookies();
}
public function updateAuth(AuthUpdates $authUpdates): ?AuthType
{
return null;
}
public function clearAuth(): void
{
$this->authLib->clearAuthCookie();
$this->getAuthLib()->clearAuthCookie();
}
}
60 changes: 43 additions & 17 deletions src/Handler/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace HelloCoop\Handler;

use HelloCoop\HelloResponse\HelloResponseInterface;
use HelloCoop\HelloRequest\HelloRequestInterface;
use HelloCoop\Config\ConfigInterface;
use HelloCoop\Config\Constants;
use HelloCoop\Lib\Crypto;
use HelloCoop\Lib\OIDCManager;
use HelloCoop\Lib\Auth;
use HelloCoop\Type\Auth as AuthType;
Expand All @@ -13,31 +15,56 @@
use HelloCoop\Lib\TokenFetcher;
use HelloCoop\Lib\TokenParser;
use Exception;
use HelloCoop\Utils\CurlWrapper;

class Callback
{
private HelloResponseInterface $helloResponse;
private HelloRequestInterface $helloRequest;
private ConfigInterface $config;
private OIDCManager $oidcManager;
private Auth $auth;
private TokenFetcher $tokenFetcher;

private TokenParser $tokenParser;

public function __construct(
HelloRequestInterface $helloRequest,
ConfigInterface $config,
OIDCManager $oidcManager,
Auth $auth,
TokenFetcher $tokenFetcher,
TokenParser $tokenParser
HelloResponseInterface $helloResponse,
ConfigInterface $config
) {
$this->helloRequest = $helloRequest;
$this->helloResponse = $helloResponse;
$this->config = $config;
$this->oidcManager = $oidcManager;
$this->auth = $auth;
$this->tokenFetcher = $tokenFetcher;
$this->tokenParser = $tokenParser;
}

private function getOIDCManager(): OIDCManager
{
return $this->oidcManager ??= new OIDCManager(
$this->helloRequest,
$this->helloResponse,
$this->config,
new Crypto($this->config->getSecret())
);
}

private function getAuth(): Auth
{
return $this->auth ??= new Auth(
$this->helloRequest,
$this->helloResponse,
$this->config,
$this->getOIDCManager(),
new Crypto($this->config->getSecret())
);
}

private function getTokenFetcher(): TokenFetcher
{
return $this->tokenFetcher ??= new TokenFetcher(new CurlWrapper());
}
private function getTokenParser(): TokenParser
{
return $this->tokenParser ??= new TokenParser();
}

public function handleCallback(): ?string
Expand Down Expand Up @@ -65,8 +92,7 @@ public function handleCallback(): ?string
throw new SameSiteCallbackException();
}

$oidcState = $this->oidcManager->getOidc()->toArray();

$oidcState = $this->getOIDCManager()->getOidc()->toArray();
if (!$oidcState) {
return $this->sendErrorPage([
'error' => 'invalid_request',
Expand Down Expand Up @@ -106,16 +132,16 @@ public function handleCallback(): ?string
], 'Missing code_verifier in callback request.');
}

$this->oidcManager->clearOidcCookie();
$token = $this->tokenFetcher->fetchToken([
$this->getOIDCManager()->clearOidcCookie();
$token = $this->getTokenFetcher()->fetchToken([
'code' => (string) $code,
'wallet' => $this->config->getHelloWallet(),
'code_verifier' => $codeVerifier,
'redirect_uri' => $redirectUri,
'client_id' => $this->config->getClientId()
]);

$payload = $this->tokenParser->parseToken($token)['payload'];
$payload = $this->getTokenParser()->parseToken($token)['payload'];
if ($payload['aud'] != $this->config->getClientId()) {
return $this->sendErrorPage([
'error' => 'invalid_client',
Expand Down Expand Up @@ -214,11 +240,11 @@ public function handleCallback(): ?string
}

$targetUri = $targetUri ?: $this->config->getRoutes()['loggedIn'] ?: '/';
$this->auth->saveAuthCookie(AuthType::fromArray($auth));
$this->getAuth()->saveAuthCookie(AuthType::fromArray($auth));
return $targetUri;
} catch (Exception $e) {
if (!($e instanceof SameSiteCallbackException) && !($e instanceof CallbackException)) {
$this->oidcManager->clearOidcCookie();
$this->getOIDCManager()->clearOidcCookie();
}
// Let it handled in HelloClient
throw $e;
Expand Down
44 changes: 35 additions & 9 deletions src/Handler/Invite.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,47 @@

namespace HelloCoop\Handler;

use HelloCoop\Config\ConfigInterface;
use HelloCoop\HelloResponse\HelloResponseInterface;
use HelloCoop\HelloRequest\HelloRequestInterface;
use HelloCoop\Lib\Auth;
use HelloCoop\Config\ConfigInterface;
use HelloCoop\Lib\Auth as AuthLib;
use HelloCoop\Lib\OIDCManager;
use HelloCoop\Lib\Crypto;

class Invite
{
private HelloResponseInterface $helloResponse;
private HelloRequestInterface $helloRequest;
private ConfigInterface $config;
private AuthLib $authLib;
public function __construct(
HelloRequestInterface $helloRequest,
HelloResponseInterface $helloResponse,
ConfigInterface $config
) {
$this->helloRequest = $helloRequest;
$this->helloResponse = $helloResponse;
$this->config = $config;
}

private Auth $auth;
private HelloRequestInterface $helloRequest;
public function __construct(ConfigInterface $config, Auth $auth, HelloRequestInterface $helloRequest)
private function getAuthLib(): AuthLib
{
$this->config = $config;
$this->auth = $auth;
$this->helloRequest = $helloRequest;
if ($this->authLib instanceof AuthLib) {
return $this->authLib;
}
$crypto = new Crypto($this->config->getSecret());
return $this->authLib = new AuthLib(
$this->helloRequest,
$this->helloResponse,
$this->config,
new OIDCManager(
$this->helloRequest,
$this->helloResponse,
$this->config,
$crypto
),
$crypto
);
}

public function generateInviteUrl(): string
Expand All @@ -31,7 +57,7 @@ public function generateInviteUrl(): string
'redirect_uri'
]);

$auth = $this->auth->getAuthfromCookies();
$auth = $this->getAuthLib()->getAuthfromCookies();
$request = [
'app_name' => $params['app_name'],
'prompt' => $params['prompt'],
Expand Down
47 changes: 30 additions & 17 deletions src/Handler/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,53 @@

namespace HelloCoop\Handler;

use HelloCoop\Config\ConfigInterface;
use HelloCoop\HelloResponse\HelloResponseInterface;
use HelloCoop\HelloRequest\HelloRequestInterface;
use HelloCoop\Lib\Auth;
use HelloCoop\Config\ConfigInterface;
use HelloCoop\Lib\OIDCManager;
use HelloCoop\Type\OIDC;
use HelloCoop\Lib\AuthHelper;
use HelloCoop\Lib\Crypto;
use HelloCoop\Lib\PKCE;
use RuntimeException;
use HelloCoop\Type\OIDC;
use HelloCoop\Lib\OIDCManager;

class Login
{
private ConfigInterface $config;
private Auth $auth;
private HelloResponseInterface $helloResponse;
private HelloRequestInterface $helloRequest;
private ConfigInterface $config;
private OIDCManager $oidcManager;
private AuthHelper $authHelper;

private array $redirectURIs;

public function __construct(
ConfigInterface $config,
Auth $auth,
HelloRequestInterface $helloRequest,
OIDCManager $oidcManager,
AuthHelper $authHelper,
HelloResponseInterface $helloResponse,
ConfigInterface $config,
array $redirectURIs = []
) {
$this->config = $config;
$this->auth = $auth;
$this->helloRequest = $helloRequest;
$this->oidcManager = $oidcManager;
$this->authHelper = $authHelper;

$this->helloResponse = $helloResponse;
$this->config = $config;
$this->redirectURIs = $redirectURIs;
}

private function getOIDCManager(): OIDCManager
{
return $this->oidcManager ??= new OIDCManager(
$this->helloRequest,
$this->helloResponse,
$this->config,
new Crypto($this->config->getSecret())
);
}

private function getAuthHelper(): AuthHelper
{
return $this->authHelper ??= new AuthHelper(new PKCE());
}

public function generateLoginUrl(): ?string
{
$params = $this->helloRequest->fetchMultiple([
Expand Down Expand Up @@ -90,9 +103,9 @@ public function generateLoginUrl(): ?string
$request['nonce'] = $params['nonce'];
}

$authResponse = $this->authHelper->createAuthRequest($request);
$authResponse = $this->getAuthHelper()->createAuthRequest($request);

$this->oidcManager->saveOidc(OIDC::fromArray([
$this->getOIDCManager()->saveOidc(OIDC::fromArray([
'nonce' => $authResponse['nonce'],
'code_verifier' => $authResponse['code_verifier'],
'redirect_uri' => $redirectURI,
Expand Down
43 changes: 34 additions & 9 deletions src/Handler/Logout.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,53 @@

namespace HelloCoop\Handler;

use HelloCoop\Config\ConfigInterface;
use HelloCoop\HelloResponse\HelloResponseInterface;
use HelloCoop\HelloRequest\HelloRequestInterface;
use HelloCoop\Lib\Auth;
use HelloCoop\Config\ConfigInterface;
use HelloCoop\Lib\Auth as AuthLib;
use HelloCoop\Lib\OIDCManager;
use HelloCoop\Lib\Crypto;

class Logout
{
private ConfigInterface $config;
private HelloResponseInterface $helloResponse;
private HelloRequestInterface $helloRequest;
private Auth $auth;
private ConfigInterface $config;
private AuthLib $authLib;
public function __construct(
ConfigInterface $config,
HelloRequestInterface $helloRequest,
Auth $auth
HelloResponseInterface $helloResponse,
ConfigInterface $config
) {
$this->config = $config;
$this->helloRequest = $helloRequest;
$this->auth = $auth;
$this->helloResponse = $helloResponse;
$this->config = $config;
}

private function getAuthLib(): AuthLib
{
if ($this->authLib instanceof AuthLib) {
return $this->authLib;
}
$crypto = new Crypto($this->config->getSecret());
return $this->authLib = new AuthLib(
$this->helloRequest,
$this->helloResponse,
$this->config,
new OIDCManager(
$this->helloRequest,
$this->helloResponse,
$this->config,
$crypto
),
$crypto
);
}

public function generateLogoutUrl(): string
{
$targetUri = $this->helloRequest->fetch('target_uri');
$this->auth->clearAuthCookie();
$this->getAuthLib()->clearAuthCookie();
if ($this->config->getLoginSync()) {
// Call the logoutSync callback
call_user_func($this->config->getLoginSync());
Expand Down
Loading

0 comments on commit 98d4afd

Please sign in to comment.