22
33namespace Laravel \Passport ;
44
5+ use Illuminate \Contracts \Support \Arrayable ;
6+ use Illuminate \Contracts \Support \Jsonable ;
57use Illuminate \Support \Traits \ForwardsCalls ;
8+ use JsonSerializable ;
69use Psr \Http \Message \ServerRequestInterface ;
710
811/**
9- * @property string oauth_access_token_id
10- * @property string oauth_client_id
11- * @property string oauth_user_id
12- * @property string[] oauth_scopes
12+ * @template TKey of string
13+ * @template TValue
14+ *
15+ * @implements \Illuminate\Contracts\Support\Arrayable<TKey, TValue>
16+ *
17+ * @property string $oauth_access_token_id
18+ * @property string $oauth_client_id
19+ * @property string $oauth_user_id
20+ * @property string[] $oauth_scopes
1321 */
14- class AccessToken
22+ class AccessToken implements Arrayable, Jsonable, JsonSerializable
1523{
1624 use ResolvesInheritedScopes, ForwardsCalls;
1725
@@ -23,7 +31,7 @@ class AccessToken
2331 /**
2432 * All the attributes set on the access token instance.
2533 *
26- * @var array<string, mixed >
34+ * @var array<TKey, TValue >
2735 */
2836 protected array $ attributes = [];
2937
@@ -52,21 +60,7 @@ public static function fromPsrRequest(ServerRequestInterface $request): static
5260 */
5361 public function can (string $ scope ): bool
5462 {
55- if (in_array ('* ' , $ this ->oauth_scopes )) {
56- return true ;
57- }
58-
59- $ scopes = Passport::$ withInheritedScopes
60- ? $ this ->resolveInheritedScopes ($ scope )
61- : [$ scope ];
62-
63- foreach ($ scopes as $ scope ) {
64- if (array_key_exists ($ scope , array_flip ($ this ->oauth_scopes ))) {
65- return true ;
66- }
67- }
68-
69- return false ;
63+ return in_array ('* ' , $ this ->oauth_scopes ) || $ this ->scopeExists ($ scope , $ this ->oauth_scopes );
7064 }
7165
7266 /**
@@ -90,15 +84,45 @@ public function transient(): bool
9084 */
9185 public function revoke (): bool
9286 {
93- return Passport::token ()->whereKey ($ this ->oauth_access_token_id )->forceFill (['revoked ' => true ])-> save ( );
87+ return Passport::token ()->newQuery ()-> whereKey ($ this ->oauth_access_token_id )->update (['revoked ' => true ]);
9488 }
9589
9690 /**
9791 * Get the token instance.
9892 */
9993 protected function getToken (): ?Token
10094 {
101- return $ this ->token ??= Passport::token ()->find ($ this ->oauth_access_token_id );
95+ return $ this ->token ??= Passport::token ()->newQuery ()->find ($ this ->oauth_access_token_id );
96+ }
97+
98+ /**
99+ * Convert the access token instance to an array.
100+ *
101+ * @return array<TKey, TValue>
102+ */
103+ public function toArray (): array
104+ {
105+ return $ this ->attributes ;
106+ }
107+
108+ /**
109+ * Convert the object into something JSON serializable.
110+ *
111+ * @return array<TKey, TValue>
112+ */
113+ public function jsonSerialize (): array
114+ {
115+ return $ this ->toArray ();
116+ }
117+
118+ /**
119+ * Convert the access token instance to JSON.
120+ *
121+ * @param int $options
122+ */
123+ public function toJson ($ options = 0 ): string
124+ {
125+ return json_encode ($ this ->jsonSerialize (), $ options );
102126 }
103127
104128 /**
@@ -112,7 +136,7 @@ public function __isset(string $key): bool
112136 /**
113137 * Dynamically retrieve the value of an attribute.
114138 */
115- public function __get (string $ key )
139+ public function __get (string $ key ): mixed
116140 {
117141 if (array_key_exists ($ key , $ this ->attributes )) {
118142 return $ this ->attributes [$ key ];
0 commit comments