-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(credentials): refactor BasicAuthCredential, CallbackCredenti…
…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
Showing
5 changed files
with
131 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |