From 2464df72d922402f2dd0a31cd177c55ad9f53293 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Gu=C3=A9rin?= Date: Sun, 10 Nov 2024 00:13:56 +0000 Subject: [PATCH] feat(wip): adds handling of token on login --- Api/AuthenticatorInterface.php | 14 ++++++++ Model/Authenticator.php | 50 ++++++++++++++++++++++++++++ Observer/AdminLoginCookie.php | 61 ++++++++++++++++++++++++++++++++++ etc/config.xml | 3 ++ etc/di.xml | 2 +- etc/events.xml | 8 +++++ etc/webapi.xml | 6 ++++ 7 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 Api/AuthenticatorInterface.php create mode 100644 Model/Authenticator.php create mode 100644 Observer/AdminLoginCookie.php create mode 100644 etc/events.xml diff --git a/Api/AuthenticatorInterface.php b/Api/AuthenticatorInterface.php new file mode 100644 index 0000000..a58c74a --- /dev/null +++ b/Api/AuthenticatorInterface.php @@ -0,0 +1,14 @@ +validator->validate($this->request)) { + throw new AuthorizationException( + __( + "The consumer isn't authorized to access %resources.", + ['resources' => ''] + ) + ); + } + + return $this->getAppToken(); + } + + private function getAppToken(): string + { + return $this->scopeConfig->getValue('better_bo/integration/token'); + } +} diff --git a/Observer/AdminLoginCookie.php b/Observer/AdminLoginCookie.php new file mode 100644 index 0000000..788722f --- /dev/null +++ b/Observer/AdminLoginCookie.php @@ -0,0 +1,61 @@ +getEvent(); + $user = $event->getUser(); + + $context = new CustomUserContext( + (int) $user->getId(), + CustomUserContext::USER_TYPE_ADMIN + ); + $params = $this->tokenParametersFactory->create(); + + $token = $this->tokenIssuer->create($context, $params); + $this->createAdminCookie($token); + } + + protected function createAdminCookie(string $token): void + { + $ttl = $this->scopeConfig->getValue('admin/security/session_lifetime'); + $customCookieMetadata = $this->customCookieMetadataFactory->createPublicCookieMetadata(); + $customCookieMetadata->setDuration($ttl); + $customCookieMetadata->setPath('/admin'); + $customCookieMetadata->setHttpOnly(false); + + $this->customCookieManager->setPublicCookie( + 'betterbo_token', + $token, + $customCookieMetadata + ); + } +} diff --git a/etc/config.xml b/etc/config.xml index 73b18db..5ed5deb 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -5,6 +5,9 @@ text,select + + 12345 + diff --git a/etc/di.xml b/etc/di.xml index 0847f65..fb4f5b0 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -19,5 +19,5 @@ - + diff --git a/etc/events.xml b/etc/events.xml new file mode 100644 index 0000000..c17af3c --- /dev/null +++ b/etc/events.xml @@ -0,0 +1,8 @@ + + + + + + diff --git a/etc/webapi.xml b/etc/webapi.xml index 8831c62..c6943bf 100644 --- a/etc/webapi.xml +++ b/etc/webapi.xml @@ -7,6 +7,12 @@ --> + + + + + +