1818
1919namespace Rhubarb \Scaffolds \TokenBasedRestApi \Model ;
2020
21+ use Rhubarb \Scaffolds \TokenBasedRestApi \ApiSettings ;
2122use Rhubarb \Scaffolds \TokenBasedRestApi \Exceptions \TokenInvalidException ;
2223use Rhubarb \Stem \Exceptions \RecordNotFoundException ;
2324use Rhubarb \Stem \Filters \AndGroup ;
3536
3637class ApiToken extends Model
3738{
39+ /** @deprecated Use ApiSettings::$tokenExpiration */
3840 const TOKEN_EXPIRATION = "+1 day " ;
3941
4042 protected function createSchema ()
@@ -76,7 +78,13 @@ public static function validateToken($tokenString)
7678 throw new TokenInvalidException ();
7779 }
7880
81+ /** @var ApiToken $token */
7982 $ token = $ tokens [0 ];
83+ $ settings = ApiSettings::singleton ();
84+ if ($ settings ->extendTokenExpirationOnUse ) {
85+ $ token ->Expires = $ settings ->tokenExpiration ;
86+ $ token ->save ();
87+ }
8088
8189 return $ token ->AuthenticatedUser ;
8290 }
@@ -110,8 +118,7 @@ public static function retrieveOrCreateToken(Model $user, $ipAddress)
110118 new Equals ("IpAddress " , $ ipAddress ),
111119 new GreaterThan ("Expires " , "now " , true )
112120 ]));
113-
114- $ token ->Expires = self ::TOKEN_EXPIRATION ;
121+ $ token ->Expires = ApiSettings::singleton ()->tokenExpiration ;
115122 $ token ->save ();
116123 } catch (RecordNotFoundException $ ex ) {
117124 $ token = self ::createToken ($ user , $ ipAddress );
@@ -131,7 +138,7 @@ protected function createConsistencyValidator()
131138 protected function beforeSave ()
132139 {
133140 if ($ this ->isNewRecord ()) {
134- $ this ->Expires = self :: TOKEN_EXPIRATION ;
141+ $ this ->Expires = ApiSettings:: singleton ()-> tokenExpiration ;
135142 }
136143
137144 parent ::beforeSave ();
0 commit comments