Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

III-6412 cleanup auth0 code #198

Merged
merged 8 commits into from
Nov 19, 2024
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# JWT Provider
Application that provides JSON Web Tokens from UiTID v2 (Auth0)
Application that provides JSON Web Tokens from UiTID v2 (Keycloak)

# Architecture
Code is split into Domain and Infrastructure.

Domain contains actions, domain services (interfaces), value objects, etc... Infrastructure contains technical
capabilities to support the domain - mostly domain interface concrete implementation. The intention in this
division is to decouple from Auth0, at least to some extent so further changes in Auth provider can be
division is to decouple from Keycloak, at least to some extent so further changes in Auth provider can be
easier to implement.

`web/index.php` is the **entry point** for the application. It will pass the request to the
Expand All @@ -16,5 +16,5 @@ with the rest of the Service Providers. Every route is tied to single action cla


# Authentication flow
jwt-provider service serves as a proxy between front end application and Auth0 service.
jwt-provider service serves as a proxy between front end application and Keycloak service.
![image](.doc/auth-flow.png)
52 changes: 22 additions & 30 deletions app/ActionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
use CultuurNet\UDB3\JwtProvider\Infrastructure\Service\ExtractClientInformationFromRequest;
use CultuurNet\UDB3\JwtProvider\Infrastructure\Service\ExtractLocaleFromRequest;
use CultuurNet\UDB3\JwtProvider\Infrastructure\Service\IsAllowedRefreshToken;
use CultuurNet\UDB3\JwtProvider\Infrastructure\Service\LoginAuth0Adapter;
use CultuurNet\UDB3\JwtProvider\Infrastructure\Service\LogOutAuth0Adapter;
use CultuurNet\UDB3\JwtProvider\Infrastructure\Service\RefreshAuth0Adapter;
use CultuurNet\UDB3\JwtProvider\Infrastructure\Service\LoginOAuthAdapter;
use CultuurNet\UDB3\JwtProvider\Infrastructure\Service\LogOutOAuthAdapter;
use CultuurNet\UDB3\JwtProvider\Infrastructure\Service\RefreshOAuthAdapter;
use GuzzleHttp\Client;
use Slim\Psr7\Factory\UriFactory;

Expand Down Expand Up @@ -94,20 +94,20 @@ public function register(): void

$this->addShared(
LogOutServiceInterface::class,
fn (): LogOutAuth0Adapter => new LogOutAuth0Adapter(
fn (): LogOutOAuthAdapter => new LogOutOAuthAdapter(
$this->get(Auth0::class),
new Authentication(
[
'domain' => $this->parameter($this->getIdentityProvider() . '.domain'),
'clientId' => $this->parameter($this->getIdentityProvider() . '.client_id'),
'clientSecret' => $this->parameter($this->getIdentityProvider() . '.client_secret'),
'cookieSecret' => $this->parameter($this->getIdentityProvider() . '.cookie_secret'),
'domain' => $this->parameter('keycloak.domain'),
'clientId' => $this->parameter('keycloak.client_id'),
'clientSecret' => $this->parameter('keycloak.client_secret'),
'cookieSecret' => $this->parameter('keycloak.cookie_secret'),
]
),
$this->get(ResponseFactoryInterface::class),
new UriFactory(),
$this->parameter($this->getIdentityProvider() . '.log_out_uri'),
$this->parameter($this->getIdentityProvider() . '.client_id')
$this->parameter('keycloak.log_out_uri'),
$this->parameter('keycloak.client_id')
)
);

Expand All @@ -118,34 +118,34 @@ public function register(): void

$this->addShared(
LoginServiceInterface::class,
fn (): LoginAuth0Adapter => new LoginAuth0Adapter(
fn (): LoginOAuthAdapter => new LoginOAuthAdapter(
$this->get(Auth0::class)
)
);

$this->addShared(
RefreshServiceInterface::class,
fn (): RefreshAuth0Adapter => new RefreshAuth0Adapter(
fn (): RefreshOAuthAdapter => new RefreshOAuthAdapter(
new Client(),
$this->parameter($this->getIdentityProvider() . '.client_id'),
$this->parameter($this->getIdentityProvider() . '.client_secret'),
$this->parameter($this->getIdentityProvider() . '.domain')
$this->parameter('keycloak.client_id'),
$this->parameter('keycloak.client_secret'),
$this->parameter('keycloak.domain')
)
);

$this->addShared(
Auth0::class,
fn (): Auth0 => new Auth0(
[
'domain' => $this->parameter($this->getIdentityProvider() . '.domain'),
'clientId' => $this->parameter($this->getIdentityProvider() . '.client_id'),
'clientSecret' => $this->parameter($this->getIdentityProvider() . '.client_secret'),
'redirectUri' => $this->parameter($this->getIdentityProvider() . '.redirect_uri'),
'domain' => $this->parameter('keycloak.domain'),
'clientId' => $this->parameter('keycloak.client_id'),
'clientSecret' => $this->parameter('keycloak.client_secret'),
'redirectUri' => $this->parameter('keycloak.redirect_uri'),
'scope' => ['openid','email','profile','offline_access'],
'persistIdToken' => true,
'persistRefreshToken' => true,
'tokenLeeway' => $this->parameter($this->getIdentityProvider() . '.id_token_leeway'),
'cookieSecret' => $this->parameter($this->getIdentityProvider() . '.cookie_secret'),
'tokenLeeway' => $this->parameter('keycloak.id_token_leeway'),
'cookieSecret' => $this->parameter('keycloak.cookie_secret'),
]
)
);
Expand All @@ -154,7 +154,7 @@ public function register(): void
IsAllowedRefreshToken::class,
fn (): IsAllowedRefreshToken => new IsAllowedRefreshToken(
$this->get(ConsumerReadRepositoryInterface::class),
(string)$this->parameter($this->getIdentityProvider() . '.allowed_refresh_permission')
(string)$this->parameter('keycloak.allowed_refresh_permission')
)
);

Expand All @@ -178,12 +178,4 @@ function (): SessionClientInformation {
)
);
}

private function getIdentityProvider(): string
{
if ($this->parameter('keycloak.enabled')) {
return 'keycloak';
}
return 'auth0';
}
}
2 changes: 1 addition & 1 deletion config.dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jwt:
keys:
public:
file: public.pem
auth0:
keycloak:
domain:
client_id:
client_secret:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Psr\Http\Message\UriFactoryInterface;
use Psr\Http\Message\UriInterface;

final class LogOutAuth0Adapter implements LogOutServiceInterface
final class LogOutOAuthAdapter implements LogOutServiceInterface
{
private ResponseFactoryInterface $responseFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Psr\Http\Message\ResponseInterface;
use Slim\Psr7\Response;

final class LoginAuth0Adapter implements LoginServiceInterface
final class LoginOAuthAdapter implements LoginServiceInterface
{
private Auth0Interface $auth0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;

final class RefreshAuth0Adapter implements RefreshServiceInterface
final class RefreshOAuthAdapter implements RefreshServiceInterface
{
private Client $httpClient;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Prophecy\PhpUnit\ProphecyTrait;
use Slim\Psr7\Factory\UriFactory;

final class LogOutAuth0AdapterTest extends TestCase
final class LogOutOAuthAdapterTest extends TestCase
{
use ProphecyTrait;

Expand All @@ -28,7 +28,7 @@ public function it_logs_out_user(): void

$authentication->getLogoutLink('http://foo-bar.com', ['clientId' => 'client-id'])->willReturn($auth0LogOutUri);

$auth0adapter = new LogOutAuth0Adapter(
$logOutOAuthAdapter = new LogOutOAuthAdapter(
$auth0->reveal(),
$authentication->reveal(),
new SlimResponseFactory(),
Expand All @@ -37,7 +37,7 @@ public function it_logs_out_user(): void
'client-id'
);

$response = $auth0adapter->logout();
$response = $logOutOAuthAdapter->logout();

$this->assertEquals(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode());
$this->assertEquals($auth0LogOutUri, $response->getHeaderLine('Location'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

final class LoginAuth0AdapterTest extends TestCase
final class LoginOAuthAdapterTest extends TestCase
{
use ProphecyTrait;

Expand All @@ -25,7 +25,7 @@ public function it_redirects_to_login_page(): void
{
$auth0 = $this->prophesize(Auth0Interface::class);

$auth0adapter = new LoginAuth0Adapter(
$loginOAuthAdapter = new LoginOAuthAdapter(
$auth0->reveal()
);

Expand All @@ -39,7 +39,7 @@ public function it_redirects_to_login_page(): void
]
)->shouldBeCalled();

$auth0adapter->redirectToLogin();
$loginOAuthAdapter->redirectToLogin();
}

/**
Expand All @@ -49,14 +49,14 @@ public function it_returns_token(): void
{
$auth0 = $this->prophesize(Auth0Interface::class);

$auth0adapter = new LoginAuth0Adapter(
$loginOAuthAdapter = new LoginOAuthAdapter(
$auth0->reveal()
);

$auth0->getIdToken()->willReturn('token');
$auth0->exchange()->willReturn(true);

$this->assertEquals('token', $auth0adapter->token());
$this->assertEquals('token', $loginOAuthAdapter->token());
}

/**
Expand All @@ -66,13 +66,13 @@ public function it_returns_refresh_token(): void
{
$auth0 = $this->prophesize(Auth0Interface::class);

$auth0adapter = new LoginAuth0Adapter(
$loginOAuthAdapter = new LoginOAuthAdapter(
$auth0->reveal()
);

$auth0->getRefreshToken()->willReturn('refresh-token');

$this->assertEquals('refresh-token', $auth0adapter->refreshToken());
$this->assertEquals('refresh-token', $loginOAuthAdapter->refreshToken());
}

/**
Expand All @@ -84,7 +84,7 @@ public function it_wraps_auth0_exceptions_to_unsuccessful_auth_exception(Excepti
{
$auth0 = $this->prophesize(Auth0Interface::class);

$auth0adapter = new LoginAuth0Adapter(
$loginOAuthAdapter = new LoginOAuthAdapter(
$auth0->reveal()
);

Expand All @@ -93,7 +93,7 @@ public function it_wraps_auth0_exceptions_to_unsuccessful_auth_exception(Excepti

$this->expectException(UnSuccessfulAuthException::class);

$auth0adapter->token();
$loginOAuthAdapter->token();
}

/**
Expand Down
Loading