customHeader($this->configuration['header'], $this->configuration['secret']); break; case AuthenticationEnum::Bearer: $this->bearer($this->configuration['token']); break; case AuthenticationEnum::Key: $this->key($this->configuration['token']); break; case AuthenticationEnum::Token: $this->token($this->configuration['token']); break; case AuthenticationEnum::Basic: $this->basic($this->configuration['username'], $this->configuration['password']); break; case AuthenticationEnum::Ntlm: $this->ntlm($this->configuration['username'], $this->configuration['password']); break; case AuthenticationEnum::Negotiate: $this->negotiate($this->configuration['username'], $this->configuration['password']); break; case AuthenticationEnum::OAuth2: $this->oauth2($this->configuration['grant'], $this->configuration['oauth_token_url'], $this->configuration['redirect_uri'], $this->configuration['client_id'], $this->configuration['client_secret'], $this->configuration['scope']); break; } return $this; } public function bearer(#[\SensitiveParameter] string $token) : void { $this->options[CURLOPT_HTTPAUTH] = CURLAUTH_BEARER; $this->options[CURLOPT_XOAUTH2_BEARER] = $token; } public function apikey(#[\SensitiveParameter] string $token) : void { $this->customToken('apikey', $token); } public function key(#[\SensitiveParameter] string $token) : void { $this->customToken('key', $token); } public function token(#[\SensitiveParameter] string $token) : void { $this->customToken('token', $token); } public function customToken(string $key, #[\SensitiveParameter] string $token) : void { $this->headers['Authorization'] = "$key {$token}"; } public function basic(string $username, #[\SensitiveParameter] string $password) : void { $this->userpass(CURLAUTH_BASIC, $username, $password); } public function digest(string $username, #[\SensitiveParameter] string $password) : void { $this->userpass(CURLAUTH_DIGEST, $username, $password); } public function ntlm(string $username, #[\SensitiveParameter] string $password) : void { $this->userpass(CURLAUTH_NTLM, $username, $password); } public function negotiate(string $username, #[\SensitiveParameter] string $password) : void { $this->userpass(CURLAUTH_NEGOTIATE, $username, $password); } public function customHeader(string $header, #[\SensitiveParameter] string $token) : void { $this->headers[$header] = $token; } public function oauth2(Oauth2GrantTypeEnum $grant, string $oauthTokenUrl, string $redirectUri, string $clientId, string $clientSecret, string $scope) : void { $unid = sprintf("oauth_token_%s", md5(serialize($this->configuration))); $token = apcu_fetch($unid, $exists); if ( ! $exists ) { $provider = new GenericProvider([ 'clientId' => $clientId, 'clientSecret' => $clientSecret, 'redirectUri' => $redirectUri, 'urlAccessToken' => $oauthTokenUrl, 'urlAuthorize' => 'https://service.example.com/authorize', 'urlResourceOwnerDetails' => 'https://service.example.com/resource', 'scopes' => $scope, ]); try { $accessToken = $provider->getAccessToken($grant->value); $token = json_encode($accessToken); apcu_store($unid, $token, $accessToken->getExpires() - time() - 10); } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) { throw $e; } } try { $decodedToken = json_decode($token, true, 512, JSON_THROW_ON_ERROR); } catch(\Exception $ex) { throw new \InvalidArgumentException("Given OAuth2 token is not a parsable JSON stream."); } if ( empty($decodedToken) ) { throw new \InvalidArgumentException("Given OAuth Token is empty"); } $this->bearer($decodedToken['access_token']); } protected function userpass(int $authType, string $username, #[\SensitiveParameter] $password) : void { $this->options[CURLOPT_HTTPAUTH] = $authType; $this->options[CURLOPT_USERNAME] = $username; $this->options[CURLOPT_PASSWORD] = $password; } }