credentialsAreValid($credentials)) { throw new InvalidCredentialsException(sprintf('Credentials must implement either the %s or %s interfaces', PasswordCredentialsGrantInterface::class, ClientCredentialsGrantInterface::class)); } if (!$this->credentialsAreConfigured($credentials)) { throw new PluginNotConfiguredException('Authorization URL, client ID or client secret is missing'); } // Return cached initialized client if there is one. if (!empty($this->initializedClients[$credentials->getClientId()])) { return $this->initializedClients[$credentials->getClientId()]; } $this->credentials = $credentials; $this->config = $config; $this->initializedClients[$credentials->getClientId()] = new Client( [ 'handler' => $this->getStackHandler(), 'auth' => 'oauth', ] ); return $this->initializedClients[$credentials->getClientId()]; } private function credentialsAreValid(AuthCredentialsInterface $credentials): bool { return $credentials instanceof PasswordCredentialsGrantInterface || $credentials instanceof ClientCredentialsGrantInterface; } /** * @param ClientCredentialsGrantInterface|PasswordCredentialsGrantInterface|AuthCredentialsInterface $credentials */ private function credentialsAreConfigured(AuthCredentialsInterface $credentials): bool { if (empty($credentials->getAuthorizationUrl()) || empty($credentials->getClientId()) || empty($credentials->getClientSecret())) { return false; } if ($credentials instanceof PasswordCredentialsGrantInterface && (empty($credentials->getUsername()) || empty($credentials->getPassword()))) { return false; } return true; } private function getStackHandler(): HandlerStack { $reAuthConfig = $this->getReAuthConfig(); $accessTokenGrantType = $this->getGrantType($reAuthConfig); $refreshTokenGrantType = new RefreshToken($this->getReAuthClient(), $reAuthConfig); $middleware = new OAuth2Middleware($accessTokenGrantType, $refreshTokenGrantType); $this->configureMiddleware($middleware); $stack = HandlerStack::create(); $stack->push($middleware); return $stack; } private function getReAuthClient(): ClientInterface { if ($this->reAuthClient) { return $this->reAuthClient; } $this->reAuthClient = new Client( [ 'base_uri' => $this->credentials->getAuthorizationUrl(), ] ); return $this->reAuthClient; } private function getReAuthConfig(): array { $config = [ 'client_id' => $this->credentials->getClientId(), 'client_secret' => $this->credentials->getClientSecret(), ]; if ($this->credentials instanceof ScopeInterface) { $config['scope'] = $this->credentials->getScope(); } if ($this->credentials instanceof StateInterface) { $config['state'] = $this->credentials->getState(); } if ($this->credentials instanceof ClientCredentialsGrantInterface) { return $config; } $config['username'] = $this->credentials->getUsername(); $config['password'] = $this->credentials->getPassword(); return $config; } private function getGrantType(array $config): GrantTypeInterface { if ($this->credentials instanceof ClientCredentialsGrantInterface) { return new ClientCredentials($this->getReAuthClient(), $config); } return new PasswordCredentials($this->getReAuthClient(), $config); } private function configureMiddleware(OAuth2Middleware $oauth): void { if (!$this->config) { return; } if ($this->config instanceof ConfigCredentialsSignerInterface) { $oauth->setClientCredentialsSigner($this->config->getCredentialsSigner()); } if ($this->config instanceof ConfigTokenPersistenceInterface) { $oauth->setTokenPersistence($this->config->getTokenPersistence()); } if ($this->config instanceof ConfigTokenSignerInterface) { $oauth->setAccessTokenSigner($this->config->getTokenSigner()); } if ($this->config instanceof ConfigTokenFactoryInterface) { $oauth->setTokenFactory($this->config->getTokenFactory()); } } }