<?php
namespace App\OAuth2\Repository; use App\OAuth2\Entity\ClientEntity; use League\OAuth2\Server\Entities\ClientEntityInterface; use League\OAuth2\Server\Repositories\ClientRepositoryInterface; class ClientRepository implements ClientRepositoryInterface { public function getClientEntity(string $clientIdentifier): ?ClientEntityInterface { $row = $this->fetchClientRow($clientIdentifier); if (!$row) { return null; } $redirectUris = json_decode((string)$row['redirectUris'], true); if (!is_array($redirectUris)) { $redirectUris = []; } $client = new ClientEntity(); $client->setIdentifier($row['clientId']); $client->setName((string)$row['name']); $client->setRedirectUri($redirectUris); $client->setConfidential((bool)$row['isConfidential']); $client->setAllowedScopes(array_values(array_filter(explode(' ', (string)$row['allowedScopes'])))); return $client; } public function validateClient(string $clientIdentifier, ?string $clientSecret, ?string $grantType): bool { $row = $this->fetchClientRow($clientIdentifier); if (!$row) { return false; } if (!(bool)$row['isConfidential'] || $row['secret'] === null || $row['secret'] === '') { return true; } if ($clientSecret === null || $clientSecret === '') { return false; } return password_verify($clientSecret, (string)$row['secret']); } private function fetchClientRow(string $clientIdentifier): ?array { global $DBTOauth2Clients; $db = \DB::getInstance(); $sql = "SELECT `clientId`, `name`, `secret`, `redirectUris`, `allowedScopes`, `isConfidential`
				FROM `{$DBTOauth2Clients}`
				WHERE `clientId`=? AND `isEnabled`=1"; $ex = $db->Execute($sql, [$clientIdentifier]); if (!$ex) { throw new \DBException("OAuth2: could not query client `{$clientIdentifier}`."); } $row = $ex->FetchRow(); return $row ?: null; } }