Skip to content

Add NTLM authentication support to Http Client #56475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 30, 2025

Conversation

cesargb
Copy link
Contributor

@cesargb cesargb commented Jul 29, 2025

This pull request introduces NTLM authentication support for the Laravel HTTP Client, extending its capabilities for integrations with services that require this authentication method (e.g., certain Microsoft services or legacy APIs).

The implementation leverages Guzzle’s built‑in NTLM option: https://docs.guzzlephp.org/en/stable/request-options.html?highlight=ntlm

Example usage:

$response = HTTP::withNtlmAuth($username, $password)->post(/* ... */);

Comment on lines 474 to 486
/**
* Specify the digest authentication username and password for the request.
*
* @param string $username
* @param string $password
* @return $this
*/
public function withNtlmAuth($username, $password)
{
return tap($this, function () use ($username, $password) {
$this->options['auth'] = [$username, $password, 'ntlm'];
});
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* Specify the digest authentication username and password for the request.
*
* @param string $username
* @param string $password
* @return $this
*/
public function withNtlmAuth($username, $password)
{
return tap($this, function () use ($username, $password) {
$this->options['auth'] = [$username, $password, 'ntlm'];
});
}
/**
* Specify the authentication username and password for the request.
*
* @param string $username
* @param string $password
* @param string|null $type
* @return $this
*/
public function withAuth($username, $password, $type)
{
return tap($this, function () use ($username, $password) {
$this->options['auth'] = $type !== null
? [$username, $password, $type]
: [$username, $password];
});
}
/**
* Specify the digest authentication username and password for the request.
*
* @param string $username
* @param string $password
* @return $this
*/
public function withNtlmAuth($username, $password)
{
return $this->withAuth($username, $password, 'ntlm');
});
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could even go a step further and validate the type:

    /**
     * @var string[]
     */
    const SUPPORTED_AUTH_TYPES = ['ntlm', 'digest'];

    /**
     * Specify the authentication username and password for the request.
     *
     * @param  string  $username
     * @param  string  $password
     * @param  value-of<SUPPORTED_AUTH_TYPES>|null  $type `null` means 'basic'
     * @return $this
     */
    public function withAuth($username, $password, $type)
    {
		if ($type !== null && !in_array($type, self::SUPPORTED_AUTH_TYPES) {
			throw new Exception(sprintf(
				'Auth type "%s" not supported. Use one of: %s',
				$type,
				implode(', ', self::SUPPORTED_AUTH_TYPES),
			));
		}

        return tap($this, function () use ($username, $password) {
            $this->options['auth'] = $type !== null
            	? [$username, $password, $type]
            	: [$username, $password];
        });
    }

Co-authored-by: Sebastian Hädrich <[email protected]>
@taylorotwell taylorotwell merged commit 2476f3c into laravel:12.x Jul 30, 2025
60 checks passed
@GrahamCampbell
Copy link
Member

Note that this is broken if ext-curl is not installed.

@cesargb cesargb deleted the hclient_ntlm_auth branch July 30, 2025 20:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants