44
55namespace Tempest \Auth \OAuth \Testing ;
66
7+ use Closure ;
78use League \OAuth2 \Client \Token \AccessToken ;
89use PHPUnit \Framework \Assert ;
10+ use Tempest \Auth \Authentication \Authenticatable ;
11+ use Tempest \Auth \Authentication \Authenticator ;
912use Tempest \Auth \OAuth \OAuthClient ;
1013use Tempest \Auth \OAuth \OAuthConfig ;
1114use Tempest \Auth \OAuth \OAuthUser ;
15+ use Tempest \Http \Request ;
16+ use Tempest \Http \Responses \Redirect ;
1217use Tempest \Router \UriGenerator ;
1318use Tempest \Support \Arr ;
1419use Tempest \Support \Random ;
@@ -38,19 +43,23 @@ final class TestingOAuthClient implements OAuthClient
3843 get => Arr \last ($ this ->authorizationUrls )['url ' ] ?? null ;
3944 }
4045
46+ /** @var array{url: string, scopes: array, options: array, state: string}[] */
4147 private array $ authorizationUrls = [];
4248
49+ /** @var array{access_token: string, token_type: 'Bearer', expires_in: int, code: string}[] */
4350 private array $ accessTokens = [];
4451
45- private array $ callbacks = [];
52+ /** @var array{token: AccessToken, code: string, user: OAuthUser}[] */
53+ private array $ users = [];
4654
4755 public function __construct (
4856 private(set) OAuthUser $ user ,
49- private(set) OAuthConfig $ config ,
50- private UriGenerator $ uri ,
57+ private(set) readonly OAuthConfig $ config ,
58+ private readonly Authenticator $ authenticator ,
59+ private readonly UriGenerator $ uri ,
5160 ) {}
5261
53- public function getAuthorizationUrl (array $ scopes = [], array $ options = []): string
62+ public function buildAuthorizationUrl (array $ scopes = [], array $ options = []): string
5463 {
5564 $ this ->state = Random \secure_string (16 );
5665
@@ -80,12 +89,13 @@ public function getState(): ?string
8089 return $ this ->state ;
8190 }
8291
83- public function getAccessToken (string $ code ): AccessToken
92+ public function requestAccessToken (string $ code ): AccessToken
8493 {
8594 $ token = new AccessToken ([
86- 'access_token ' => 'fat - ' . $ code ,
95+ 'access_token ' => 'tok - ' . $ code ,
8796 'token_type ' => 'Bearer ' ,
8897 'expires_in ' => 3600 ,
98+ 'code ' => $ code ,
8999 ]);
90100
91101 $ this ->accessTokens [] = [
@@ -96,23 +106,31 @@ public function getAccessToken(string $code): AccessToken
96106 return $ token ;
97107 }
98108
99- public function getUser (AccessToken $ token ): OAuthUser
109+ public function fetchUser (AccessToken $ token ): OAuthUser
100110 {
111+ $ this ->users [] = [
112+ 'token ' => $ token ,
113+ 'code ' => Arr \get_by_key ($ token ->getValues (), 'code ' ),
114+ 'user ' => $ this ->user ,
115+ ];
116+
101117 return $ this ->user ;
102118 }
103119
104- public function fetchUser ( string $ code ): OAuthUser
120+ public function createRedirect ( array $ scopes = [], array $ options = [] ): Redirect
105121 {
106- $ token = $ this ->getAccessToken ( $ code );
107- $ user = $ this -> getUser ( $ token );
122+ return new Redirect ( $ this ->buildAuthorizationUrl ( $ scopes , $ options ) );
123+ }
108124
109- $ this ->callbacks [] = [
110- 'code ' => $ code ,
111- 'token ' => $ token ,
112- 'user ' => $ user ,
113- ];
125+ public function authenticate (Request $ request , Closure $ map ): Authenticatable
126+ {
127+ $ user = $ this ->fetchUser ($ this ->requestAccessToken ($ request ->get ('code ' )));
128+
129+ $ authenticatable = $ map ($ user );
130+
131+ $ this ->authenticator ->authenticate ($ authenticatable );
114132
115- return $ user ;
133+ return $ authenticatable ;
116134 }
117135
118136 /**
@@ -189,8 +207,8 @@ public function assertAuthorizationUrlGenerated(?array $scopes = null, ?array $o
189207 public function assertUserFetched (string $ code ): void
190208 {
191209 Assert::assertNotEmpty (
192- actual: array_filter ($ this ->callbacks , fn (array $ callback ) => $ callback ['code ' ] === $ code ),
193- message: sprintf ('Callback with code "%s" was not handled. ' , $ code ),
210+ actual: array_filter ($ this ->users , fn (array $ user ) => $ user ['code ' ] === $ code ),
211+ message: sprintf ('User with code "%s" was not handled. ' , $ code ),
194212 );
195213 }
196214
@@ -238,6 +256,6 @@ public function getAccessTokenCount(): int
238256 */
239257 public function getCallbackCount (): int
240258 {
241- return count ($ this ->callbacks );
259+ return count ($ this ->users );
242260 }
243261}
0 commit comments