Skip to content

Commit

Permalink
refactor(credentials): refactor BasicAuthCredential, CallbackCredenti…
Browse files Browse the repository at this point in the history
…al, DigestAuthCredential, NtlmAuthCredential, and WsseAuthCredential classes

- Refactored BasicAuthCredential class to remove nullable password parameter
- Added CallbackCredential class to handle credentials using a callback
- Removed digest parameter from DigestAuthCredential class
- Added NtlmAuthCredential class for NTLM authentication
- Added WsseAuthCredential class for WSSE authentication
  • Loading branch information
guanguans committed Jan 25, 2024
1 parent 545d221 commit 8634605
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 6 deletions.
10 changes: 8 additions & 2 deletions src/Foundation/Credentials/BasicAuthCredential.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@

use GuzzleHttp\RequestOptions;

/**
* @see https://github.com/saloonphp/saloon/blob/v3/src/Http/Auth/BasicAuthenticator.php
* @see https://github.com/kriswallsmith/Buzz/blob/master/lib/Middleware/BasicAuthMiddleware.php
* @see https://github.com/guzzle/guzzle/blob/7.8/src/Client.php#L400
* @see https://github.com/phanxipang/fansipan/blob/main/src/Middleware/Auth/BasicAuthentication.php
*/
class BasicAuthCredential extends NullCredential
{
private string $username;
private ?string $password;
private string $password;

public function __construct(string $username, string $password = null)
public function __construct(string $username, string $password)
{
$this->username = $username;
$this->password = $password;
Expand Down
38 changes: 38 additions & 0 deletions src/Foundation/Credentials/CallbackCredential.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

/**
* This file is part of the guanguans/notify.
*
* (c) guanguans <[email protected]>
*
* This source file is subject to the MIT license that is bundled.
*/

namespace Guanguans\Notify\Foundation\Credentials;

use Psr\Http\Message\RequestInterface;

class CallbackCredential extends NullCredential
{
/**
* @var callable(array|RequestInterface):array|RequestInterface
*/
private $callback;

public function __construct(callable $callback)
{
$this->callback = $callback;
}

public function applyToOptions(array $options): array
{
return ($this->callback)($options) ?: $options;
}

public function applyToRequest(RequestInterface $request): RequestInterface
{
return ($this->callback)($request) ?: $request;
}
}
6 changes: 2 additions & 4 deletions src/Foundation/Credentials/DigestAuthCredential.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,17 @@ class DigestAuthCredential extends NullCredential
{
private string $username;
private string $password;
private string $digest;

public function __construct(string $username, string $password, string $digest)
public function __construct(string $username, string $password)
{
$this->username = $username;
$this->password = $password;
$this->digest = $digest;
}

public function applyToOptions(array $options): array
{
return [
RequestOptions::AUTH => [$this->username, $this->password, $this->digest],
RequestOptions::AUTH => [$this->username, $this->password, 'digest'],
] + $options;
}
}
34 changes: 34 additions & 0 deletions src/Foundation/Credentials/NtlmAuthCredential.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

/**
* This file is part of the guanguans/notify.
*
* (c) guanguans <[email protected]>
*
* This source file is subject to the MIT license that is bundled.
*/

namespace Guanguans\Notify\Foundation\Credentials;

use GuzzleHttp\RequestOptions;

class NtlmAuthCredential extends NullCredential
{
private string $username;
private string $password;

public function __construct(string $username, string $password)
{
$this->username = $username;
$this->password = $password;
}

public function applyToOptions(array $options): array
{
return [
RequestOptions::AUTH => [$this->username, $this->password, 'ntlm'],
] + $options;
}
}
49 changes: 49 additions & 0 deletions src/Foundation/Credentials/WsseAuthCredential.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

/**
* This file is part of the guanguans/notify.
*
* (c) guanguans <[email protected]>
*
* This source file is subject to the MIT license that is bundled.
*/

namespace Guanguans\Notify\Foundation\Credentials;

use Psr\Http\Message\RequestInterface;

/**
* @see https://github.com/kriswallsmith/Buzz/blob/master/lib/Middleware/WsseAuthMiddleware.php
*/
class WsseAuthCredential extends NullCredential
{
private string $username;
private string $password;

public function __construct(string $username, string $password)
{
$this->username = $username;
$this->password = $password;
}

public function applyToRequest(RequestInterface $request): RequestInterface
{
$nonce = substr(sha1(uniqid('', true)), 0, 16);
$created = date('c');
$digest = base64_encode(sha1(base64_decode($nonce).$created.$this->password, true));

$wsse = sprintf(
'UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"',
$this->username,
$digest,
$nonce,
$created
);

return $request
->withHeader('Authorization', 'WSSE profile="UsernameToken"')
->withHeader('X-WSSE', $wsse);
}
}

0 comments on commit 8634605

Please sign in to comment.