Skip to content

Commit 8634605

Browse files
committed
refactor(credentials): refactor BasicAuthCredential, CallbackCredential, 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
1 parent 545d221 commit 8634605

5 files changed

Lines changed: 131 additions & 6 deletions

File tree

src/Foundation/Credentials/BasicAuthCredential.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@
1414

1515
use GuzzleHttp\RequestOptions;
1616

17+
/**
18+
* @see https://github.com/saloonphp/saloon/blob/v3/src/Http/Auth/BasicAuthenticator.php
19+
* @see https://github.com/kriswallsmith/Buzz/blob/master/lib/Middleware/BasicAuthMiddleware.php
20+
* @see https://github.com/guzzle/guzzle/blob/7.8/src/Client.php#L400
21+
* @see https://github.com/phanxipang/fansipan/blob/main/src/Middleware/Auth/BasicAuthentication.php
22+
*/
1723
class BasicAuthCredential extends NullCredential
1824
{
1925
private string $username;
20-
private ?string $password;
26+
private string $password;
2127

22-
public function __construct(string $username, string $password = null)
28+
public function __construct(string $username, string $password)
2329
{
2430
$this->username = $username;
2531
$this->password = $password;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of the guanguans/notify.
7+
*
8+
* (c) guanguans <ityaozm@gmail.com>
9+
*
10+
* This source file is subject to the MIT license that is bundled.
11+
*/
12+
13+
namespace Guanguans\Notify\Foundation\Credentials;
14+
15+
use Psr\Http\Message\RequestInterface;
16+
17+
class CallbackCredential extends NullCredential
18+
{
19+
/**
20+
* @var callable(array|RequestInterface):array|RequestInterface
21+
*/
22+
private $callback;
23+
24+
public function __construct(callable $callback)
25+
{
26+
$this->callback = $callback;
27+
}
28+
29+
public function applyToOptions(array $options): array
30+
{
31+
return ($this->callback)($options) ?: $options;
32+
}
33+
34+
public function applyToRequest(RequestInterface $request): RequestInterface
35+
{
36+
return ($this->callback)($request) ?: $request;
37+
}
38+
}

src/Foundation/Credentials/DigestAuthCredential.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,17 @@ class DigestAuthCredential extends NullCredential
1818
{
1919
private string $username;
2020
private string $password;
21-
private string $digest;
2221

23-
public function __construct(string $username, string $password, string $digest)
22+
public function __construct(string $username, string $password)
2423
{
2524
$this->username = $username;
2625
$this->password = $password;
27-
$this->digest = $digest;
2826
}
2927

3028
public function applyToOptions(array $options): array
3129
{
3230
return [
33-
RequestOptions::AUTH => [$this->username, $this->password, $this->digest],
31+
RequestOptions::AUTH => [$this->username, $this->password, 'digest'],
3432
] + $options;
3533
}
3634
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of the guanguans/notify.
7+
*
8+
* (c) guanguans <ityaozm@gmail.com>
9+
*
10+
* This source file is subject to the MIT license that is bundled.
11+
*/
12+
13+
namespace Guanguans\Notify\Foundation\Credentials;
14+
15+
use GuzzleHttp\RequestOptions;
16+
17+
class NtlmAuthCredential extends NullCredential
18+
{
19+
private string $username;
20+
private string $password;
21+
22+
public function __construct(string $username, string $password)
23+
{
24+
$this->username = $username;
25+
$this->password = $password;
26+
}
27+
28+
public function applyToOptions(array $options): array
29+
{
30+
return [
31+
RequestOptions::AUTH => [$this->username, $this->password, 'ntlm'],
32+
] + $options;
33+
}
34+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of the guanguans/notify.
7+
*
8+
* (c) guanguans <ityaozm@gmail.com>
9+
*
10+
* This source file is subject to the MIT license that is bundled.
11+
*/
12+
13+
namespace Guanguans\Notify\Foundation\Credentials;
14+
15+
use Psr\Http\Message\RequestInterface;
16+
17+
/**
18+
* @see https://github.com/kriswallsmith/Buzz/blob/master/lib/Middleware/WsseAuthMiddleware.php
19+
*/
20+
class WsseAuthCredential extends NullCredential
21+
{
22+
private string $username;
23+
private string $password;
24+
25+
public function __construct(string $username, string $password)
26+
{
27+
$this->username = $username;
28+
$this->password = $password;
29+
}
30+
31+
public function applyToRequest(RequestInterface $request): RequestInterface
32+
{
33+
$nonce = substr(sha1(uniqid('', true)), 0, 16);
34+
$created = date('c');
35+
$digest = base64_encode(sha1(base64_decode($nonce).$created.$this->password, true));
36+
37+
$wsse = sprintf(
38+
'UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"',
39+
$this->username,
40+
$digest,
41+
$nonce,
42+
$created
43+
);
44+
45+
return $request
46+
->withHeader('Authorization', 'WSSE profile="UsernameToken"')
47+
->withHeader('X-WSSE', $wsse);
48+
}
49+
}

0 commit comments

Comments
 (0)