4
4
5
5
use Illuminate \Http \Request ;
6
6
use Laravel \Passport \Client ;
7
+ use Laravel \Passport \ClientRepository ;
7
8
use Laravel \Passport \Http \Middleware \CheckClientCredentialsForAnyScope ;
8
- use Laravel \Passport \Token ;
9
- use Laravel \Passport \TokenRepository ;
10
9
use League \OAuth2 \Server \Exception \OAuthServerException ;
11
10
use League \OAuth2 \Server \ResourceServer ;
12
11
use Mockery as m ;
@@ -24,21 +23,17 @@ public function test_request_is_passed_along_if_token_is_valid()
24
23
$ resourceServer = m::mock (ResourceServer::class);
25
24
$ resourceServer ->shouldReceive ('validateAuthenticatedRequest ' )->andReturn ($ psr = m::mock ());
26
25
$ psr ->shouldReceive ('getAttribute ' )->with ('oauth_user_id ' )->andReturn (1 );
27
- $ psr ->shouldReceive ('getAttribute ' )->with ('oauth_client_id ' )->andReturn (1 );
26
+ $ psr ->shouldReceive ('getAttribute ' )->with ('oauth_client_id ' )->andReturn (2 );
28
27
$ psr ->shouldReceive ('getAttribute ' )->with ('oauth_access_token_id ' )->andReturn ('token ' );
29
28
$ psr ->shouldReceive ('getAttribute ' )->with ('oauth_scopes ' )->andReturn (['* ' ]);
30
29
31
30
$ client = m::mock (Client::class);
32
31
$ client ->shouldReceive ('firstParty ' )->andReturnFalse ();
33
32
34
- $ token = m::mock (Token::class);
35
- $ token ->shouldReceive ('getAttribute ' )->with ('client ' )->andReturn ($ client );
36
- $ token ->shouldReceive ('getAttribute ' )->with ('scopes ' )->andReturn (['* ' ]);
33
+ $ clientRepository = m::mock (ClientRepository::class);
34
+ $ clientRepository ->shouldReceive ('find ' )->with (2 )->andReturn ($ client );
37
35
38
- $ tokenRepository = m::mock (TokenRepository::class);
39
- $ tokenRepository ->shouldReceive ('find ' )->with ('token ' )->andReturn ($ token );
40
-
41
- $ middleware = new CheckClientCredentialsForAnyScope ($ resourceServer , $ tokenRepository );
36
+ $ middleware = new CheckClientCredentialsForAnyScope ($ resourceServer , $ clientRepository );
42
37
43
38
$ request = Request::create ('/ ' );
44
39
$ request ->headers ->set ('Authorization ' , 'Bearer token ' );
@@ -55,23 +50,17 @@ public function test_request_is_passed_along_if_token_has_any_required_scope()
55
50
$ resourceServer = m::mock (ResourceServer::class);
56
51
$ resourceServer ->shouldReceive ('validateAuthenticatedRequest ' )->andReturn ($ psr = m::mock ());
57
52
$ psr ->shouldReceive ('getAttribute ' )->with ('oauth_user_id ' )->andReturn (1 );
58
- $ psr ->shouldReceive ('getAttribute ' )->with ('oauth_client_id ' )->andReturn (1 );
53
+ $ psr ->shouldReceive ('getAttribute ' )->with ('oauth_client_id ' )->andReturn (2 );
59
54
$ psr ->shouldReceive ('getAttribute ' )->with ('oauth_access_token_id ' )->andReturn ('token ' );
60
55
$ psr ->shouldReceive ('getAttribute ' )->with ('oauth_scopes ' )->andReturn (['foo ' , 'bar ' , 'baz ' ]);
61
56
62
57
$ client = m::mock (Client::class);
63
58
$ client ->shouldReceive ('firstParty ' )->andReturnFalse ();
64
59
65
- $ token = m::mock (Token::class);
66
- $ token ->shouldReceive ('getAttribute ' )->with ('client ' )->andReturn ($ client );
67
- $ token ->shouldReceive ('getAttribute ' )->with ('scopes ' )->andReturn (['foo ' , 'bar ' , 'baz ' ]);
68
- $ token ->shouldReceive ('can ' )->with ('notfoo ' )->andReturnFalse ();
69
- $ token ->shouldReceive ('can ' )->with ('bar ' )->andReturnTrue ();
70
-
71
- $ tokenRepository = m::mock (TokenRepository::class);
72
- $ tokenRepository ->shouldReceive ('find ' )->with ('token ' )->andReturn ($ token );
60
+ $ clientRepository = m::mock (ClientRepository::class);
61
+ $ clientRepository ->shouldReceive ('find ' )->with (2 )->andReturn ($ client );
73
62
74
- $ middleware = new CheckClientCredentialsForAnyScope ($ resourceServer , $ tokenRepository );
63
+ $ middleware = new CheckClientCredentialsForAnyScope ($ resourceServer , $ clientRepository );
75
64
76
65
$ request = Request::create ('/ ' );
77
66
$ request ->headers ->set ('Authorization ' , 'Bearer token ' );
@@ -88,13 +77,13 @@ public function test_request_is_passed_along_if_token_has_any_required_scope()
88
77
*/
89
78
public function test_exception_is_thrown_when_oauth_throws_exception ()
90
79
{
91
- $ tokenRepository = m::mock (TokenRepository ::class);
80
+ $ clientRepository = m::mock (ClientRepository ::class);
92
81
$ resourceServer = m::mock (ResourceServer::class);
93
82
$ resourceServer ->shouldReceive ('validateAuthenticatedRequest ' )->andThrow (
94
83
new OAuthServerException ('message ' , 500 , 'error type ' )
95
84
);
96
85
97
- $ middleware = new CheckClientCredentialsForAnyScope ($ resourceServer , $ tokenRepository );
86
+ $ middleware = new CheckClientCredentialsForAnyScope ($ resourceServer , $ clientRepository );
98
87
99
88
$ request = Request::create ('/ ' );
100
89
$ request ->headers ->set ('Authorization ' , 'Bearer token ' );
@@ -112,23 +101,17 @@ public function test_exception_is_thrown_if_token_does_not_have_required_scope()
112
101
$ resourceServer = m::mock (ResourceServer::class);
113
102
$ resourceServer ->shouldReceive ('validateAuthenticatedRequest ' )->andReturn ($ psr = m::mock ());
114
103
$ psr ->shouldReceive ('getAttribute ' )->with ('oauth_user_id ' )->andReturn (1 );
115
- $ psr ->shouldReceive ('getAttribute ' )->with ('oauth_client_id ' )->andReturn (1 );
104
+ $ psr ->shouldReceive ('getAttribute ' )->with ('oauth_client_id ' )->andReturn (2 );
116
105
$ psr ->shouldReceive ('getAttribute ' )->with ('oauth_access_token_id ' )->andReturn ('token ' );
117
106
$ psr ->shouldReceive ('getAttribute ' )->with ('oauth_scopes ' )->andReturn (['foo ' , 'bar ' ]);
118
107
119
108
$ client = m::mock (Client::class);
120
109
$ client ->shouldReceive ('firstParty ' )->andReturnFalse ();
121
110
122
- $ token = m::mock (Token::class);
123
- $ token ->shouldReceive ('getAttribute ' )->with ('client ' )->andReturn ($ client );
124
- $ token ->shouldReceive ('getAttribute ' )->with ('scopes ' )->andReturn (['foo ' , 'bar ' ]);
125
- $ token ->shouldReceive ('can ' )->with ('baz ' )->andReturnFalse ();
126
- $ token ->shouldReceive ('can ' )->with ('notbar ' )->andReturnFalse ();
111
+ $ clientRepository = m::mock (ClientRepository::class);
112
+ $ clientRepository ->shouldReceive ('find ' )->with (2 )->andReturn ($ client );
127
113
128
- $ tokenRepository = m::mock (TokenRepository::class);
129
- $ tokenRepository ->shouldReceive ('find ' )->with ('token ' )->andReturn ($ token );
130
-
131
- $ middleware = new CheckClientCredentialsForAnyScope ($ resourceServer , $ tokenRepository );
114
+ $ middleware = new CheckClientCredentialsForAnyScope ($ resourceServer , $ clientRepository );
132
115
133
116
$ request = Request::create ('/ ' );
134
117
$ request ->headers ->set ('Authorization ' , 'Bearer token ' );
@@ -146,20 +129,17 @@ public function test_exception_is_thrown_if_token_belongs_to_first_party_client(
146
129
$ resourceServer = m::mock (ResourceServer::class);
147
130
$ resourceServer ->shouldReceive ('validateAuthenticatedRequest ' )->andReturn ($ psr = m::mock ());
148
131
$ psr ->shouldReceive ('getAttribute ' )->with ('oauth_user_id ' )->andReturn (1 );
149
- $ psr ->shouldReceive ('getAttribute ' )->with ('oauth_client_id ' )->andReturn (1 );
132
+ $ psr ->shouldReceive ('getAttribute ' )->with ('oauth_client_id ' )->andReturn (2 );
150
133
$ psr ->shouldReceive ('getAttribute ' )->with ('oauth_access_token_id ' )->andReturn ('token ' );
151
134
$ psr ->shouldReceive ('getAttribute ' )->with ('oauth_scopes ' )->andReturn (['* ' ]);
152
135
153
136
$ client = m::mock (Client::class);
154
137
$ client ->shouldReceive ('firstParty ' )->andReturnTrue ();
155
138
156
- $ token = m::mock (Token::class);
157
- $ token ->shouldReceive ('getAttribute ' )->with ('client ' )->andReturn ($ client );
158
-
159
- $ tokenRepository = m::mock (TokenRepository::class);
160
- $ tokenRepository ->shouldReceive ('find ' )->with ('token ' )->andReturn ($ token );
139
+ $ clientRepository = m::mock (ClientRepository::class);
140
+ $ clientRepository ->shouldReceive ('find ' )->with (2 )->andReturn ($ client );
161
141
162
- $ middleware = new CheckClientCredentialsForAnyScope ($ resourceServer , $ tokenRepository );
142
+ $ middleware = new CheckClientCredentialsForAnyScope ($ resourceServer , $ clientRepository );
163
143
164
144
$ request = Request::create ('/ ' );
165
145
$ request ->headers ->set ('Authorization ' , 'Bearer token ' );
0 commit comments