22namespace DesignMyNight \Laravel \OAuth2 ;
33
44use Closure ;
5- use DesignMyNight \Laravel \OAuth2 \Exceptions \InvalidAccessTokenException ;
6- use DesignMyNight \Laravel \OAuth2 \Exceptions \InvalidEndpointException ;
7- use DesignMyNight \Laravel \OAuth2 \Exceptions \InvalidInputException ;
85use GuzzleHttp \Client ;
96use GuzzleHttp \Exception \RequestException ;
7+ use Illuminate \Auth \AuthenticationException ;
108use Illuminate \Support \Facades \Cache ;
9+ use Laravel \Passport \Exceptions \MissingScopeException ;
1110
1211class VerifyAccessToken
1312{
1413 protected $ accessTokenCacheKey = 'access_token ' ;
1514
1615 private $ client = null ;
1716
18- private function getClient (): Client
17+ protected function checkScopes ( $ scopesForToken , $ requiredScopes )
1918 {
20- if ($ this -> client === null ) {
21- $ this -> setClient ( new Client ()) ;
19+ if (! is_array ( $ requiredScopes ) ) {
20+ $ requiredScopes = [ $ requiredScopes ] ;
2221 }
2322
24- return $ this ->client ;
23+ $ misingScopes = array_diff ($ scopesForToken , $ scopesForToken );
24+
25+ if (count ($ misingScopes ) > 0 ) {
26+ throw new MissingScopeException ($ misingScopes );
27+ }
2528 }
2629
27- public function setClient ( Client $ client ): self
30+ protected function getAccessToken ( ): string
2831 {
29- $ this -> client = $ client ;
32+ $ accessToken = Cache:: get ( $ this -> accessTokenCacheKey ) ;
3033
31- return $ this ;
34+ return $ accessToken ?: $ this ->getNewAccessToken ();
35+ }
36+
37+ private function getClient (): Client
38+ {
39+ if ($ this ->client === null ) {
40+ $ this ->setClient (new Client ());
41+ }
42+
43+ return $ this ->client ;
3244 }
3345
3446 protected function getIntrospect ($ accessToken )
@@ -46,37 +58,6 @@ protected function getIntrospect($accessToken)
4658 return json_decode ((string ) $ response ->getBody (), true );
4759 }
4860
49- protected function getAccessToken (): string
50- {
51- $ accessToken = Cache::get ($ this ->accessTokenCacheKey );
52-
53- return $ accessToken ?: $ this ->getNewAccessToken ();
54- }
55-
56- protected function getNewAccessToken (): string
57- {
58- $ response = $ this ->getClient ()->post (config ('authorizationserver.token_url ' ), [
59- 'form_params ' => [
60- 'grant_type ' => 'client_credentials ' ,
61- 'client_id ' => config ('authorizationserver.client_id ' ),
62- 'client_secret ' => config ('authorizationserver.client_secret ' ),
63- 'scope ' => '' ,
64- ],
65- ]);
66-
67- $ result = json_decode ((string ) $ response ->getBody (), true );
68-
69- if (isset ($ result ['access_token ' ])) {
70- $ accessToken = $ result ['access_token ' ];
71-
72- Cache::add ($ this ->accessTokenCacheKey , $ accessToken , intVal ($ result ['expires_in ' ]) / 60 );
73-
74- return $ accessToken ;
75- }
76-
77- throw new InvalidEndpointException ('Did not receive an access token ' );
78- }
79-
8061 /**
8162 * Handle an incoming request.
8263 *
@@ -86,50 +67,65 @@ protected function getNewAccessToken(): string
8667 */
8768 public function handle ($ request , Closure $ next , ...$ scopes )
8869 {
89- $ authorization = $ request ->header ('Authorization ' );
90-
91- if (!$ authorization ) {
92- throw new InvalidInputException ('No Authorization header present ' );
93- }
94-
9570 $ bearerToken = $ request ->bearerToken ();
9671
9772 if (!$ bearerToken ) {
98- throw new InvalidInputException ('No Bearer token in the Authorization header present ' );
73+ throw new AuthenticationException ('No Bearer token present ' );
9974 }
10075
10176 try {
10277 $ result = $ this ->getIntrospect ($ bearerToken );
10378
10479 if (!$ result ['active ' ]) {
105- throw new InvalidAccessTokenException ('Invalid token! ' );
80+ throw new AuthenticationException ('Invalid token! ' );
10681 }
10782
108- if ($ scopes != null ) {
109- if (!\is_array ($ scopes )) {
110- $ scopes = [$ scopes ];
111- }
112-
113- $ scopesForToken = \explode (' ' , $ result ['scope ' ]);
114-
115- if (count ($ misingScopes = array_diff ($ scopes , $ scopesForToken )) > 0 ) {
116- throw new InvalidAccessTokenException ('Missing the following required scopes: ' . implode (' , ' , $ misingScopes ));
117- }
83+ if ($ scopes !== null ) {
84+ $ this ->checkScopes (explode (' ' , $ result ['scope ' ]), $ scopes );
11885 }
11986 } catch (RequestException $ e ) {
12087 if ($ e ->hasResponse ()) {
12188 $ result = json_decode ((string ) $ e ->getResponse ()->getBody (), true );
12289
12390 if (isset ($ result ['error ' ])) {
124- throw new InvalidAccessTokenException ($ result ['error ' ]['title ' ] ?? 'Invalid token! ' );
91+ throw new AuthenticationException ($ result ['error ' ]['title ' ] ?? '' );
12592 }
126-
127- throw new InvalidAccessTokenException ('Invalid token! ' );
12893 }
12994
130- throw new InvalidAccessTokenException ($ e );
95+ throw new AuthenticationException ($ e-> getMessage () );
13196 }
13297
13398 return $ next ($ request );
13499 }
100+
101+ protected function getNewAccessToken (): string
102+ {
103+ $ response = $ this ->getClient ()->post (config ('authorizationserver.token_url ' ), [
104+ 'form_params ' => [
105+ 'grant_type ' => 'client_credentials ' ,
106+ 'client_id ' => config ('authorizationserver.client_id ' ),
107+ 'client_secret ' => config ('authorizationserver.client_secret ' ),
108+ 'scope ' => '' ,
109+ ],
110+ ]);
111+
112+ $ result = json_decode ((string ) $ response ->getBody (), true );
113+
114+ if (isset ($ result ['access_token ' ])) {
115+ $ accessToken = $ result ['access_token ' ];
116+
117+ Cache::add ($ this ->accessTokenCacheKey , $ accessToken , intVal ($ result ['expires_in ' ]) / 60 );
118+
119+ return $ accessToken ;
120+ }
121+
122+ throw new AuthenticationException ('Did not receive an access token ' );
123+ }
124+
125+ public function setClient (Client $ client ): self
126+ {
127+ $ this ->client = $ client ;
128+
129+ return $ this ;
130+ }
135131}
0 commit comments