2525use Swoft \Core \RequestContext ;
2626use Swoft \Exception \RuntimeException ;
2727
28- /**
29- * Class AuthManager
30- * @package Swoft\Auth
31- */
3228class AuthManager implements AuthManagerInterface
3329{
3430 /**
@@ -66,7 +62,7 @@ class AuthManager implements AuthManagerInterface
6662 */
6763 private $ tokenParser ;
6864
69- public function getSessionDuration ()
65+ public function getSessionDuration (): int
7066 {
7167 return $ this ->sessionDuration ;
7268 }
@@ -77,54 +73,41 @@ public function setSessionDuration($time)
7773 }
7874
7975 /**
80- * @return AuthSession;
76+ * @return AuthSession|mixed
8177 */
8278 public function getSession ()
8379 {
8480 return RequestContext::getContextDataByKey (AuthConstants::AUTH_SESSION );
8581 }
8682
87- /**
88- * @param AuthSession $session
89- */
9083 public function setSession (AuthSession $ session )
9184 {
9285 RequestContext::setContextData ([AuthConstants::AUTH_SESSION => $ session ]);
9386 }
9487
9588 /**
96- * @return bool
97- *
9889 * Check if a user is currently logged in
9990 */
100- public function isLoggedIn ()
91+ public function isLoggedIn (): bool
10192 {
10293 return $ this ->getSession () instanceof AuthSession;
10394 }
10495
105- /**
106- * @param $accountTypeName
107- * @param array $data
108- * @return AuthSession
109- */
11096 public function login (string $ accountTypeName , array $ data ): AuthSession
11197 {
112- if (!$ account = $ this ->getAccountType ($ accountTypeName )) {
98+ if (! $ account = $ this ->getAccountType ($ accountTypeName )) {
11399 throw new AuthException (ErrorCode::AUTH_INVALID_ACCOUNT_TYPE );
114100 }
115101 $ result = $ account ->login ($ data );
116- if (!$ result instanceof AuthResult || $ result ->getIdentity () == '' ) {
102+ if (! $ result instanceof AuthResult || $ result ->getIdentity () = == '' ) {
117103 throw new AuthException (ErrorCode::AUTH_LOGIN_FAILED );
118104 }
119105 $ session = $ this ->generateSession ($ accountTypeName , $ result ->getIdentity (), $ result ->getExtendedData ());
120106 $ this ->setSession ($ session );
121107 if ($ this ->cacheEnable === true ) {
122108 try {
123- $ this ->getCacheClient ()->set (
124- $ this ->getCacheKey ($ session ->getIdentity (), $ session ->getExtendedData ()),
125- $ session ->getToken (),
126- $ session ->getExpirationTime ()
127- );
109+ $ this ->getCacheClient ()
110+ ->set ($ this ->getCacheKey ($ session ->getIdentity (), $ session ->getExtendedData ()), $ session ->getToken (), $ session ->getExpirationTime ());
128111 } catch (InvalidArgumentException $ e ) {
129112 $ err = sprintf ('%s Invalid Argument : %s ' , $ session ->getIdentity (), $ e ->getMessage ());
130113 throw new AuthException (ErrorCode::POST_DATA_NOT_PROVIDED , $ err );
@@ -133,7 +116,7 @@ public function login(string $accountTypeName, array $data): AuthSession
133116 return $ session ;
134117 }
135118
136- protected function getCacheKey (string $ identity , array $ extendedData )
119+ protected function getCacheKey (string $ identity , array $ extendedData ): string
137120 {
138121 if (empty ($ extendedData )) {
139122 return $ this ->prefix . $ identity ;
@@ -142,19 +125,12 @@ protected function getCacheKey(string $identity, array $extendedData)
142125 return $ this ->prefix . $ identity . '. ' . md5 ($ str );
143126 }
144127
145- /**
146- * @param string $accountTypeName
147- * @param string $identity
148- * @param array $data
149- * @return AuthSession
150- */
151- public function generateSession (string $ accountTypeName , string $ identity , array $ data = [])
128+ public function generateSession (string $ accountTypeName , string $ identity , array $ data = []): AuthSession
152129 {
153130 $ startTime = time ();
154131 $ exp = $ startTime + (int )$ this ->sessionDuration ;
155132 $ session = new AuthSession ();
156- $ session
157- ->setExtendedData ($ data )
133+ $ session ->setExtendedData ($ data )
158134 ->setExpirationTime ($ exp )
159135 ->setCreateTime ($ startTime )
160136 ->setIdentity ($ identity )
@@ -171,45 +147,42 @@ public function generateSession(string $accountTypeName, string $identity, array
171147 */
172148 public function getAccountType ($ name )
173149 {
174- if (!App::hasBean ($ name )) {
150+ if (! App::hasBean ($ name )) {
175151 return null ;
176152 }
177153 $ account = App::getBean ($ name );
178- if (!$ account instanceof AccountTypeInterface) {
154+ if (! $ account instanceof AccountTypeInterface) {
179155 return null ;
180156 }
181157 return $ account ;
182158 }
183159
184160 /**
185- * @return TokenParserInterface
161+ * @throws RuntimeException When TokenParser missing or error.
186162 */
187163 public function getTokenParser (): TokenParserInterface
188164 {
189- if (!$ this ->tokenParser instanceof TokenParserInterface) {
190- if (!App::hasBean ($ this ->tokenParserClass )) {
165+ if (! $ this ->tokenParser instanceof TokenParserInterface) {
166+ if (! App::hasBean ($ this ->tokenParserClass )) {
191167 throw new RuntimeException ('Can`t find tokenParserClass ' );
192168 }
193169 $ tokenParser = App::getBean ($ this ->tokenParserClass );
194- if (!$ tokenParser instanceof TokenParserInterface) {
170+ if (! $ tokenParser instanceof TokenParserInterface) {
195171 throw new RuntimeException ("TokenParser need implements Swoft\Auth\Mapping\TokenParserInterface " );
196172 }
197173 $ this ->tokenParser = $ tokenParser ;
198174 }
199175 return $ this ->tokenParser ;
200176 }
201177
202- /**
203- * @return CacheInterface
204- */
205- public function getCacheClient ()
178+ public function getCacheClient (): CacheInterface
206179 {
207- if (!$ this ->cache instanceof CacheInterface) {
208- if (!App::hasBean ($ this ->cacheClass )) {
180+ if (! $ this ->cache instanceof CacheInterface) {
181+ if (! App::hasBean ($ this ->cacheClass )) {
209182 throw new RuntimeException ('Can`t find cacheClass ' );
210183 }
211184 $ cache = App::getBean ($ this ->cacheClass );
212- if (!$ cache instanceof CacheInterface) {
185+ if (! $ cache instanceof CacheInterface) {
213186 throw new RuntimeException ('CacheClient need implements Psr\SimpleCache\CacheInterface ' );
214187 }
215188 $ this ->cache = $ cache ;
@@ -218,8 +191,6 @@ public function getCacheClient()
218191 }
219192
220193 /**
221- * @param $token
222- * @return bool
223194 * @throws AuthException
224195 */
225196 public function authenticateToken (string $ token ): bool
@@ -231,26 +202,27 @@ public function authenticateToken(string $token): bool
231202 throw new AuthException (ErrorCode::AUTH_TOKEN_INVALID );
232203 }
233204
234- if (!$ session ) {
205+ if (! $ session ) {
235206 return false ;
236207 }
237208
238209 if ($ session ->getExpirationTime () < time ()) {
239210 throw new AuthException (ErrorCode::AUTH_SESSION_EXPIRED );
240211 }
241212
242- if (!$ account = $ this ->getAccountType ($ session ->getAccountTypeName ())) {
213+ if (! $ account = $ this ->getAccountType ($ session ->getAccountTypeName ())) {
243214 throw new AuthException (ErrorCode::AUTH_SESSION_INVALID );
244215 }
245216
246- if (!$ account ->authenticate ($ session ->getIdentity ())) {
217+ if (! $ account ->authenticate ($ session ->getIdentity ())) {
247218 throw new AuthException (ErrorCode::AUTH_TOKEN_INVALID );
248219 }
249220
250221 if ($ this ->cacheEnable === true ) {
251222 try {
252- $ cache = $ this ->getCacheClient ()->get ($ this ->getCacheKey ($ session ->getIdentity (), $ session ->getExtendedData ()));
253- if (!$ cache || $ cache !== $ token ) {
223+ $ cache = $ this ->getCacheClient ()
224+ ->get ($ this ->getCacheKey ($ session ->getIdentity (), $ session ->getExtendedData ()));
225+ if (! $ cache || $ cache !== $ token ) {
254226 throw new AuthException (ErrorCode::AUTH_TOKEN_INVALID );
255227 }
256228 } catch (InvalidArgumentException $ e ) {
0 commit comments