1818use Cake \Routing \Router ;
1919use Cake \Utility \Hash ;
2020use CakeDC \Api \Service \Auth \TwoFactorAuthentication \OneTimePasswordAuthenticationCheckerFactory ;
21- use CakeDC \Api \Service \Auth \TwoFactorAuthentication \Webauthn2fAuthenticationCheckerFactory ;
21+ use CakeDC \Api \Service \Auth \TwoFactorAuthentication \Webauthn2FAuthenticationCheckerFactory ;
2222use DateInterval ;
2323use DateTimeImmutable ;
2424use Lcobucci \JWT \Configuration ;
2525use Lcobucci \JWT \Signer \Hmac \Sha512 ;
2626use Lcobucci \JWT \Signer \Key \InMemory ;
2727
28+ /**
29+ * JwtTokenTrait
30+ */
2831trait JwtTokenTrait
2932{
30-
3133 /**
3234 * Generates token response.
3335 *
3436 * @param \Cake\Datasource\EntityInterface|array $user User info.
37+ * @param string|null $type The type of token being generated.
3538 * @return array
3639 */
3740 public function generateTokenResponse ($ user , $ type )
@@ -49,6 +52,13 @@ public function generateTokenResponse($user, $type)
4952 ]);
5053 }
5154
55+ /**
56+ * Generates refresh token response.
57+ *
58+ * @param \Cake\Datasource\EntityInterface|array $user User info.
59+ * @param array $payload Additional payload data.
60+ * @return array
61+ */
5262 public function generateRefreshTokenResponse ($ user , $ payload )
5363 {
5464 $ timestamp = new DateTimeImmutable ();
@@ -65,6 +75,8 @@ public function generateRefreshTokenResponse($user, $payload)
6575 *
6676 * @param \Cake\Datasource\EntityInterface|array $user User info.
6777 * @param \DateTimeImmutable $timestamp Timestamp.
78+ * @param string|null $type The type of token being generated.
79+ * @param array|null $payload Additional payload data.
6880 * @return bool|string
6981 */
7082 public function generateAccessToken ($ user , $ timestamp , $ type , $ payload = null )
@@ -92,6 +104,14 @@ public function generateAccessToken($user, $timestamp, $type, $payload = null)
92104 return $ token ->toString ();
93105 }
94106
107+ /**
108+ * Get the audience for the token.
109+ *
110+ * @param \Cake\Datasource\EntityInterface|array $user User info.
111+ * @param string|null $type The type of token being generated.
112+ * @param array|null $payload Additional payload data.
113+ * @return string
114+ */
95115 public function getAudience ($ user , $ type , $ payload )
96116 {
97117 if ($ type === null && is_array ($ payload ) && isset ($ payload ['aud ' ])) {
@@ -106,11 +126,23 @@ public function getAudience($user, $type, $payload)
106126 return $ audience ;
107127 }
108128
129+ /**
130+ * Check if 2FA is enabled for the user.
131+ *
132+ * @param \Cake\Datasource\EntityInterface|array $user User info.
133+ * @return bool
134+ */
109135 protected function is2FAEnabled ($ user )
110136 {
111137 return $ this ->isEnabledWebauthn2faAuthentication ($ user ) || $ this ->isEnabledOneTimePasswordAuthentication ($ user );
112138 }
113139
140+ /**
141+ * Check if Webauthn 2FA authentication is enabled for the user.
142+ *
143+ * @param \Cake\Datasource\EntityInterface|array $user User info.
144+ * @return bool
145+ */
114146 public function isEnabledWebauthn2faAuthentication ($ user )
115147 {
116148 $ enabledTwoFactorVerify = Configure::read ('Api.2fa.enabled ' );
@@ -122,6 +154,12 @@ public function isEnabledWebauthn2faAuthentication($user)
122154 return false ;
123155 }
124156
157+ /**
158+ * Check if One-Time Password authentication is enabled for the user.
159+ *
160+ * @param \Cake\Datasource\EntityInterface|array $user User info.
161+ * @return bool
162+ */
125163 public function isEnabledOneTimePasswordAuthentication ($ user )
126164 {
127165 $ enabledTwoFactorVerify = Configure::read ('Api.2fa.enabled ' );
@@ -133,6 +171,11 @@ public function isEnabledOneTimePasswordAuthentication($user)
133171 return false ;
134172 }
135173
174+ /**
175+ * Get the One-Time Password Authentication Checker.
176+ *
177+ * @return \CakeDC\Auth\Authentication\OneTimePasswordAuthenticationCheckerInterface
178+ */
136179 protected function getOneTimePasswordAuthenticationChecker ()
137180 {
138181 return (new OneTimePasswordAuthenticationCheckerFactory ())->build ();
@@ -153,6 +196,8 @@ protected function getWebauthn2fAuthenticationChecker()
153196 *
154197 * @param \Cake\Datasource\EntityInterface|array $user User info.
155198 * @param \DateTimeImmutable $timestamp Timestamp.
199+ * @param string|null $type The type of token being generated.
200+ * @param array|null $payload Additional payload data.
156201 * @return bool|string
157202 */
158203 public function generateRefreshToken ($ user , $ timestamp , $ type , $ payload = null )
@@ -184,6 +229,7 @@ public function generateRefreshToken($user, $timestamp, $type, $payload = null)
184229 $ model = $ UsersTable ->getAlias ();
185230
186231 $ table = TableRegistry::getTableLocator ()->get ('CakeDC/Api.JwtRefreshTokens ' );
232+ /** @var \CakeDC\Api\Model\Entity\JwtRefreshToken $entity */
187233 $ entity = $ table ->find ()->where ([
188234 'model ' => $ model ,
189235 'foreign_key ' => $ subject ,
0 commit comments