22
33namespace Kubernetes \Client \Adapter \Http ;
44
5+ require_once 'functions.php ' ;
6+
57class AuthenticationMiddleware implements HttpClient
68{
9+ const USERNAME_PASSWORD = 'username:password ' ;
10+ const TOKEN = 'token ' ;
11+ const CERTIFICATE = 'certificate ' ;
12+
713 /**
814 * @var HttpClient
915 */
@@ -12,73 +18,79 @@ class AuthenticationMiddleware implements HttpClient
1218 /**
1319 * @var string
1420 */
15- private $ usernameOrToken ;
21+ private $ authenticationType ;
1622
1723 /**
1824 * @var string
1925 */
20- private $ password ;
26+ private $ credentials ;
2127
2228 /**
2329 * @param HttpClient $httpClient
24- * @param string $usernameOrToken
25- * @param string $password
30+ * @param string $authenticationType
31+ * @param string $credentials
2632 */
27- public function __construct (HttpClient $ httpClient , $ usernameOrToken , $ password = null )
33+ public function __construct (HttpClient $ httpClient , string $ authenticationType , string $ credentials )
2834 {
2935 $ this ->httpClient = $ httpClient ;
30- $ this ->usernameOrToken = $ usernameOrToken ;
31- $ this ->password = $ password ;
36+ $ this ->authenticationType = $ authenticationType ;
37+ $ this ->credentials = $ credentials ;
3238 }
3339
3440 /**
3541 * {@inheritdoc}
3642 */
3743 public function request ($ method , $ path , $ body = null , array $ options = [])
3844 {
39- return $ this ->httpClient ->request ($ method , $ path , $ body , $ this ->addAuthenticationHeader ($ options ));
45+ return $ this ->httpClient ->request ($ method , $ path , $ body , $ this ->addAuthenticationOptions ($ options ));
4046 }
4147
4248 /**
4349 * {@inheritdoc}
4450 */
4551 public function asyncRequest ($ method , $ path , $ body = null , array $ options = [])
4652 {
47- return $ this ->httpClient ->asyncRequest ($ method , $ path , $ body , $ this ->addAuthenticationHeader ($ options ));
53+ return $ this ->httpClient ->asyncRequest ($ method , $ path , $ body , $ this ->addAuthenticationOptions ($ options ));
4854 }
4955
5056 /**
5157 * @return string
5258 */
5359 private function getBasicAuthorizationString ()
5460 {
55- return 'Basic ' .base64_encode (sprintf ( ' %s:%s ' , $ this ->usernameOrToken , $ this -> password ) );
61+ return 'Basic ' .base64_encode ($ this ->credentials );
5662 }
5763
5864 /**
5965 * @return string
6066 */
6167 private function getTokenAuthorizationString ()
6268 {
63- return 'Bearer ' .$ this ->usernameOrToken ;
69+ return 'Bearer ' .$ this ->credentials ;
6470 }
6571
6672 /**
6773 * @return bool
6874 */
6975 private function isTokenAuthentication ()
7076 {
71- return null === $ this ->password ;
77+ return self :: TOKEN == $ this ->authenticationType ;
7278 }
7379
74- private function addAuthenticationHeader (array $ options ): array
80+ private function addAuthenticationOptions (array $ options ): array
7581 {
76- $ authorizationHeader = $ this ->isTokenAuthentication () ? $ this ->getTokenAuthorizationString () : $ this ->getBasicAuthorizationString ();
82+ if (self ::CERTIFICATE == $ this ->authenticationType ) {
83+ $ authorizationOptions = [
84+ 'cert ' => certificate_file_path_from_contents ($ this ->credentials ),
85+ ];
86+ } else {
87+ $ authorizationOptions = [
88+ 'headers ' => [
89+ 'Authorization ' => $ this ->isTokenAuthentication () ? $ this ->getTokenAuthorizationString () : $ this ->getBasicAuthorizationString (),
90+ ],
91+ ];
92+ }
7793
78- return array_merge_recursive ([
79- 'headers ' => [
80- 'Authorization ' => $ authorizationHeader ,
81- ],
82- ], $ options );
94+ return array_merge_recursive ($ authorizationOptions , $ options );
8395 }
8496}
0 commit comments