ltiProvider = $ltiProvider; return $this; } public function withRole(string $role): AccessTokenRequestValidatorInterface { $this->role = $role; return $this; } /** * @throws InvalidLtiProviderException * @throws MissingScopeException * @throws tao_models_classes_UserException */ public function validate(ServerRequestInterface $request): void { $result = $this->getAccessTokenRequestValidator()->validate($request); if ($this->role !== null && !in_array($this->role, $result->getScopes(), true)) { throw new MissingScopeException(sprintf('Scope %s is not allowed', $this->role)); } if ($result->hasError() || $result->getRegistration() === null) { throw new tao_models_classes_UserException( sprintf('Access Token Validation failed. %s', $result->getError()) ); } if ($this->ltiProvider !== null) { $requestClientId = $result->getRegistration()->getClientId(); $ltiProvider = $this->getLtiProviderService()->searchByToolClientId( $requestClientId ); if ($ltiProvider === null) { throw new InvalidLtiProviderException(sprintf('Lti provider with client id %s does not exist', $requestClientId)); } if (!$this->isSameLtiProvider($ltiProvider)) { throw new InvalidLtiProviderException('Lti provider from registration is not matching delivery'); } } } public function withValidator(Lti1p3AccessTokenRequestValidator $validator): void { $this->validator = $validator; } private function getAccessTokenRequestValidator(): Lti1p3AccessTokenRequestValidator { if (!$this->validator) { $this->validator = new Lti1p3AccessTokenRequestValidator($this->getRegistrationRepository()); } return $this->validator; } private function getRegistrationRepository(): RegistrationRepositoryInterface { return $this->getServiceLocator()->get(Lti1p3RegistrationRepository::class); } private function getLtiProviderService(): LtiProviderService { return $this->getServiceLocator()->get(LtiProviderService::SERVICE_ID); } private function isSameLtiProvider(LtiProvider $ltiProvider): bool { return $this->ltiProvider->getId() === $ltiProvider->getId(); } }