1212use Exception ;
1313use InvalidArgumentException ;
1414use VaasSdk \Exceptions \VaasAuthenticationException ;
15- use VaasSdk \Options \AuthenticationOptions ;
1615use function Amp \async ;
1716use function Amp \delay ;
1817
1918class TokenReceiver
2019{
21- private HttpClient $ httpClient ;
22- private AuthenticationOptions $ authenticationOptions ;
2320 private Mutex $ mutex ;
2421 private ?TokenResponse $ lastTokenResponse = null ;
2522 private int $ validTo = 0 ;
2623 private ?int $ lastRequestTime = null ;
2724
28- public function __construct (AuthenticationOptions $ options , ?HttpClient $ httpClient = null )
25+ public function __construct (
26+ public AuthenticatorInterface $ authenticator ,
27+ public ?string $ tokenUrl = null ,
28+ public ?HttpClient $ httpClient = null )
2929 {
30- $ this ->authenticationOptions = $ options ;
30+ $ this ->tokenUrl = $ tokenUrl ?? ' https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token ' ;
3131 $ this ->httpClient = $ httpClient ?? HttpClientBuilder::buildDefault ();
3232 $ this ->mutex = new LocalMutex ();
3333 }
@@ -81,7 +81,7 @@ private function requestTokenAsync(?Cancellation $cancellation = null): Future
8181 {
8282 return async (function () use ($ cancellation ) {
8383 $ form = $ this ->tokenRequestToForm ();
84- $ request = new Request ($ this ->authenticationOptions -> tokenUrl , 'POST ' );
84+ $ request = new Request ($ this ->tokenUrl , 'POST ' );
8585 $ request ->setBody ($ form );
8686 $ request ->setHeader ('Content-Type ' , 'application/x-www-form-urlencoded ' );
8787
@@ -118,19 +118,20 @@ private function requestTokenAsync(?Cancellation $cancellation = null): Future
118118 */
119119 private function tokenRequestToForm (): string
120120 {
121- if ($ this ->authenticationOptions ->grantType == GrantType::CLIENT_CREDENTIALS ) {
122- return http_build_query ([
123- 'client_id ' => $ this ->authenticationOptions ->clientId ,
124- 'client_secret ' => $ this ->authenticationOptions ->clientSecret ?? throw new InvalidArgumentException (),
121+ return match (true )
122+ {
123+ $ this ->authenticator instanceof ClientCredentialsGrantAuthenticator => http_build_query ([
124+ 'client_id ' => $ this ->authenticator ->clientId ,
125+ 'client_secret ' => $ this ->authenticator ->clientSecret ,
125126 'grant_type ' => 'client_credentials ' ,
126- ]);
127- }
128-
129- return http_build_query ([
130- 'client_id ' => $ this ->authenticationOptions ->clientId ,
131- 'username ' => $ this ->authenticationOptions ->userName ?? throw new InvalidArgumentException (),
132- 'password ' => $ this ->authenticationOptions ->password ?? throw new InvalidArgumentException (),
127+ ]),
128+ $ this ->authenticator instanceof ResourceOwnerPasswordGrantAuthenticator => http_build_query ([
129+ 'client_id ' => $ this ->authenticator ->clientId ,
130+ 'username ' => $ this ->authenticator ->userName ,
131+ 'password ' => $ this ->authenticator ->password ,
133132 'grant_type ' => 'password ' ,
134- ]);
133+ ]),
134+ default => throw new InvalidArgumentException ("Unknown authenticator type " )
135+ };
135136 }
136137}
0 commit comments