3
3
namespace Laravel \Passport \Tests ;
4
4
5
5
use Mockery as m ;
6
+ use Laravel \Passport \Token ;
6
7
use Illuminate \Http \Request ;
8
+ use Laravel \Passport \Client ;
7
9
use PHPUnit \Framework \TestCase ;
10
+ use Laravel \Passport \TokenRepository ;
8
11
use League \OAuth2 \Server \ResourceServer ;
9
12
use League \OAuth2 \Server \Exception \OAuthServerException ;
10
13
use Laravel \Passport \Http \Middleware \CheckClientCredentialsForAnyScope ;
@@ -25,7 +28,17 @@ public function test_request_is_passed_along_if_token_is_valid()
25
28
$ psr ->shouldReceive ('getAttribute ' )->with ('oauth_access_token_id ' )->andReturn ('token ' );
26
29
$ psr ->shouldReceive ('getAttribute ' )->with ('oauth_scopes ' )->andReturn (['* ' ]);
27
30
28
- $ middleware = new CheckClientCredentialsForAnyScope ($ resourceServer );
31
+ $ client = m::mock (Client::class);
32
+ $ client ->shouldReceive ('firstParty ' )->andReturnFalse ();
33
+
34
+ $ token = m::mock (Token::class);
35
+ $ token ->shouldReceive ('getAttribute ' )->with ('client ' )->andReturn ($ client );
36
+ $ token ->shouldReceive ('getAttribute ' )->with ('scopes ' )->andReturn (['* ' ]);
37
+
38
+ $ tokenRepository = m::mock (TokenRepository::class);
39
+ $ tokenRepository ->shouldReceive ('find ' )->with ('token ' )->andReturn ($ token );
40
+
41
+ $ middleware = new CheckClientCredentialsForAnyScope ($ resourceServer , $ tokenRepository );
29
42
30
43
$ request = Request::create ('/ ' );
31
44
$ request ->headers ->set ('Authorization ' , 'Bearer token ' );
@@ -46,7 +59,19 @@ public function test_request_is_passed_along_if_token_has_any_required_scope()
46
59
$ psr ->shouldReceive ('getAttribute ' )->with ('oauth_access_token_id ' )->andReturn ('token ' );
47
60
$ psr ->shouldReceive ('getAttribute ' )->with ('oauth_scopes ' )->andReturn (['foo ' , 'bar ' , 'baz ' ]);
48
61
49
- $ middleware = new CheckClientCredentialsForAnyScope ($ resourceServer );
62
+ $ client = m::mock (Client::class);
63
+ $ client ->shouldReceive ('firstParty ' )->andReturnFalse ();
64
+
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 );
73
+
74
+ $ middleware = new CheckClientCredentialsForAnyScope ($ resourceServer , $ tokenRepository );
50
75
51
76
$ request = Request::create ('/ ' );
52
77
$ request ->headers ->set ('Authorization ' , 'Bearer token ' );
@@ -63,12 +88,13 @@ public function test_request_is_passed_along_if_token_has_any_required_scope()
63
88
*/
64
89
public function test_exception_is_thrown_when_oauth_throws_exception ()
65
90
{
91
+ $ tokenRepository = m::mock (TokenRepository::class);
66
92
$ resourceServer = m::mock (ResourceServer::class);
67
93
$ resourceServer ->shouldReceive ('validateAuthenticatedRequest ' )->andThrow (
68
94
new OAuthServerException ('message ' , 500 , 'error type ' )
69
95
);
70
96
71
- $ middleware = new CheckClientCredentialsForAnyScope ($ resourceServer );
97
+ $ middleware = new CheckClientCredentialsForAnyScope ($ resourceServer, $ tokenRepository );
72
98
73
99
$ request = Request::create ('/ ' );
74
100
$ request ->headers ->set ('Authorization ' , 'Bearer token ' );
@@ -90,7 +116,19 @@ public function test_exception_is_thrown_if_token_does_not_have_required_scope()
90
116
$ psr ->shouldReceive ('getAttribute ' )->with ('oauth_access_token_id ' )->andReturn ('token ' );
91
117
$ psr ->shouldReceive ('getAttribute ' )->with ('oauth_scopes ' )->andReturn (['foo ' , 'bar ' ]);
92
118
93
- $ middleware = new CheckClientCredentialsForAnyScope ($ resourceServer );
119
+ $ client = m::mock (Client::class);
120
+ $ client ->shouldReceive ('firstParty ' )->andReturnFalse ();
121
+
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 ();
127
+
128
+ $ tokenRepository = m::mock (TokenRepository::class);
129
+ $ tokenRepository ->shouldReceive ('find ' )->with ('token ' )->andReturn ($ token );
130
+
131
+ $ middleware = new CheckClientCredentialsForAnyScope ($ resourceServer , $ tokenRepository );
94
132
95
133
$ request = Request::create ('/ ' );
96
134
$ request ->headers ->set ('Authorization ' , 'Bearer token ' );
@@ -99,4 +137,35 @@ public function test_exception_is_thrown_if_token_does_not_have_required_scope()
99
137
return 'response ' ;
100
138
}, 'baz ' , 'notbar ' );
101
139
}
140
+
141
+ /**
142
+ * @expectedException \Illuminate\Auth\AuthenticationException
143
+ */
144
+ public function test_exception_is_thrown_if_token_belongs_to_first_party_client ()
145
+ {
146
+ $ resourceServer = m::mock (ResourceServer::class);
147
+ $ resourceServer ->shouldReceive ('validateAuthenticatedRequest ' )->andReturn ($ psr = m::mock ());
148
+ $ psr ->shouldReceive ('getAttribute ' )->with ('oauth_user_id ' )->andReturn (1 );
149
+ $ psr ->shouldReceive ('getAttribute ' )->with ('oauth_client_id ' )->andReturn (1 );
150
+ $ psr ->shouldReceive ('getAttribute ' )->with ('oauth_access_token_id ' )->andReturn ('token ' );
151
+ $ psr ->shouldReceive ('getAttribute ' )->with ('oauth_scopes ' )->andReturn (['* ' ]);
152
+
153
+ $ client = m::mock (Client::class);
154
+ $ client ->shouldReceive ('firstParty ' )->andReturnTrue ();
155
+
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 );
161
+
162
+ $ middleware = new CheckClientCredentialsForAnyScope ($ resourceServer , $ tokenRepository );
163
+
164
+ $ request = Request::create ('/ ' );
165
+ $ request ->headers ->set ('Authorization ' , 'Bearer token ' );
166
+
167
+ $ response = $ middleware ->handle ($ request , function () {
168
+ return 'response ' ;
169
+ });
170
+ }
102
171
}
0 commit comments