/** * Given a string list of scopes, return an array of valid Scope entities keyed by name. * * @param string $scopes * @param string $redirectUri * @param null|string $clientId * @param null|string $grantTypeIdentifier * @return null|\Atrauzzi\Oauth2Server\Domain\Entity\Scope[] * @throws \Atrauzzi\Oauth2Server\Exception\InvalidRequest * @throws \Atrauzzi\Oauth2Server\Exception\InvalidScope */ public function findValid($scopes, $grantTypeIdentifier = null, $clientId = null, $redirectUri = null) { if (!$scopes && !$this->config->scopeParamRequired()) { return null; } $requestedScopes = []; foreach (explode($this->config->getScopeDelimiter(), $scopes) as $scope) { if ($scope = trim($scope)) { $requestedScopes[] = $scope; } } $requestedScopes = empty($requestedScopes) ? $this->config->getDefaultScopes() : $requestedScopes; if ($this->config->scopeParamRequired() && empty($requestedScopes)) { throw new InvalidRequest('scope'); } $validScopes = $this->scopeRepository->findByNames($requestedScopes, $clientId, $grantTypeIdentifier); $invalidScopes = array_diff($requestedScopes ?: [], array_keys($validScopes)); if (!empty($invalidScopes)) { throw new InvalidScope($invalidScopes, $redirectUri); } return $validScopes; }