From d284712b8e993df28fcb92b08a52f7921ffe1832 Mon Sep 17 00:00:00 2001 From: Beu Date: Thu, 16 Feb 2023 11:00:25 +0100 Subject: [PATCH] Add doc --- README.md | 40 ++++++++++++ examples/TrackMania.php | 72 ++++++++++++++++++++++ examples/TrackManiaAuthenticator.php | 91 ++++++++++++++++++++++++++++ 3 files changed, 203 insertions(+) create mode 100644 README.md create mode 100644 examples/TrackMania.php create mode 100644 examples/TrackManiaAuthenticator.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..194a21f --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +# TrackMania (2020) OAuth2 authentication module + +## How to install it + +Add this in your composer.json file: + +```json +"repositories": [ + { + "url": "https://git.virtit.fr/beu/oauth2-trackmania.git", + "type": "git" + } +], +``` + +change the value of `minimum-stability` to dev in it. + +then launch the command: + +```bash +composer require beu/oauth2-trackmania +``` + +## How to configure it + +Install the security bundle: + +```bash +composer require symfony/security-bundle +``` + +Then create the User Entity + +```bash +php bin/console make:user +``` + +Note that you have to answer **no** to the question `Does this app need to hash/check user passwords?` + +Then, you could copy the files `TrackMania.php` in `src/Controller/OAuth2/` and `TrackManiaAuthenticator.php` in `src/Security/` \ No newline at end of file diff --git a/examples/TrackMania.php b/examples/TrackMania.php new file mode 100644 index 0000000..9792eb1 --- /dev/null +++ b/examples/TrackMania.php @@ -0,0 +1,72 @@ +getClient('TrackMania') // key used in config/packages/knpu_oauth2_client.yaml + ->redirect([],[]); + } + + /** + * After going to Ubisoft Connect OAuth2, you're redirected back here + * because this is the "redirect_route" you configured + * in config/packages/knpu_oauth2_client.yaml + */ + #[Route( + '/connect/trackmania/check', + name: 'connect_trackmania_check' + )] + public function connectCheckAction(Request $request, ClientRegistry $clientRegistry) + { + // ** if you want to *authenticate* the user, then + // leave this method blank and create a Guard authenticator + } + + #[Route( + '/login', + name: 'app_login', + methods: ['GET', 'HEAD'] + )] + public function login(Request $request, RequestStack $requestStack): RedirectResponse + { + $referer = $request->headers->get('referer'); + if ($referer !== null) { + $session = $requestStack->getSession(); + $session->set("PostLoginRedirect", $referer); + } + + return $this->redirectToRoute('connect_trackmania_start'); + } + + /** + * @Route("/logout", name="app_logout", methods={"GET"}) + */ + #[Route( + '/logout', + name: 'app_logout', + methods: ['GET', 'HEAD'] + )] + public function logout(): void + { + // controller can be blank: it will never be called! + throw new \Exception('Don\'t forget to activate logout in security.yaml'); + } +} \ No newline at end of file diff --git a/examples/TrackManiaAuthenticator.php b/examples/TrackManiaAuthenticator.php new file mode 100644 index 0000000..0f091e2 --- /dev/null +++ b/examples/TrackManiaAuthenticator.php @@ -0,0 +1,91 @@ +clientRegistry = $clientRegistry; + $this->entityManager = $entityManager; + $this->router = $router; + $this->requestStack = $requestStack; + } + + public function supports(Request $request): ?bool + { + // continue ONLY if the current ROUTE matches the check ROUTE + return $request->attributes->get('_route') === 'connect_trackmania_check'; + } + + public function authenticate(Request $request): Passport + { + $client = $this->clientRegistry->getClient('TrackMania'); + $accessToken = $this->fetchAccessToken($client); + + $selfvalidating = new SelfValidatingPassport( + new UserBadge($accessToken->getToken(), function() use ($accessToken, $client) { + /** @var Beu\TrackMania\OAuth2\Client\Provider\TrackManiaProviderOwner $user */ + $user = $client->fetchUserFromToken($accessToken); + + $existingUser = $this->entityManager->getRepository(User::class)->findOneBy(['AccountId' => $user->getId()]); + + if ($existingUser) { + /** @var User $existingUser */ + if ($existingUser->getDisplayName() !== $user->getDisplayName()) { + $existingUser->setDisplayName($user->getDisplayName()); + $this->entityManager->flush(); + } + return $existingUser; + } + + $newuser = new User(); + $newuser->setAccountId($user->getId()); + $newuser->setDisplayName($user->getDisplayName()); + + $this->entityManager->persist($newuser); + $this->entityManager->flush(); + return $newuser; + }) + ); + + return $selfvalidating; + } + + public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response + { + $session = $this->requestStack->getSession(); + if ($session->has("PostLoginRedirect")) { + $targetUrl = $session->get("PostLoginRedirect"); + } else { + $targetUrl = $this->router->generate('homepage'); + } + return new RedirectResponse($targetUrl); + } + + public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response + { + $message = strtr($exception->getMessageKey(), $exception->getMessageData()); + + return new Response($message, Response::HTTP_FORBIDDEN); + } +} \ No newline at end of file