1616namespace FloatPHP \Kernel ;
1717
1818use FloatPHP \Classes \Http \Response ;
19- use FloatPHP \Helpers \Connection \Transient ;
2019
2120class BaseController extends View
2221{
@@ -109,60 +108,69 @@ protected function addCSS(string $css, string $hook = 'add-css')
109108
110109 /**
111110 * Verify token against request data.
112- *
111+ *
113112 * @access protected
114113 * @param string $token
115114 * @param string $action
116115 * @param bool
117116 */
118- protected function verifyToken (?string $ token = null , ?string $ source = null )
117+ protected function verifyToken (?string $ token = null , ?string $ action = null ) : bool
119118 {
120- if ( $ token ) {
121-
122- $ transient = new Transient ();
123- $ data = (string )$ transient ->getTemp ($ token );
124- $ data = $ this ->unserialize ($ data );
125-
126- // Override
127- $ this ->doAction ('verify-token ' , $ data );
128-
129- // Verify token data
130- if ( $ data ) {
131- if ( $ this ->isValidSession () ) {
132- if ( isset ($ data ['user ' ]) ) {
133- if ( $ this ->getSession ($ this ->getSessionId ()) !== $ data ['user ' ] ) {
134- return false ;
135- }
136- }
137- }
138- if ( $ source !== $ data ['source ' ] ) {
139- return false ;
140- }
141- if ( $ this ->getServerIp () !== $ data ['ip ' ] ) {
142- return false ;
143- }
144- if ( $ this ->getServer ('http-referer ' ) !== $ data ['url ' ] ) {
145- return false ;
146- }
147- return true ;
119+ // Get token session
120+ $ session = $ this ->getSession ('--token ' ) ?: [];
121+
122+ // Get token data
123+ $ data = $ session [$ token ] ?? [];
124+
125+ // Apply default data
126+ $ data = $ this ->mergeArray ([
127+ 'action ' => '' ,
128+ 'url ' => false ,
129+ 'ip ' => false ,
130+ 'user ' => false
131+ ], $ data );
132+
133+ // Override verification
134+ $ this ->doAction ('verify-token ' , $ data );
135+
136+ // Verify authenticated user
137+ if ( $ this ->isAuthenticated () ) {
138+ $ user = $ this ->getSession ($ this ->getSessionId ());
139+ if ( $ user !== $ data ['user ' ] ) {
140+ return false ;
148141 }
149142 }
150143
151- return false ;
144+ // Verify action
145+ if ( $ action !== $ data ['action ' ] ) {
146+ return false ;
147+ }
148+
149+ // Verify IP
150+ if ( $ this ->getServerIp () !== $ data ['ip ' ] ) {
151+ return false ;
152+ }
153+
154+ // Verify URL
155+ if ( $ this ->getServer ('http-referer ' ) !== $ data ['url ' ] ) {
156+ return false ;
157+ }
158+
159+ return $ this ->verifyHash ($ token , $ data );
152160 }
153161
154162 /**
155163 * Verify current request.
156- *
164+ *
157165 * @access protected
158166 * @param bool $force, Token validation
159167 * @return void
160168 */
161169 protected function verifyRequest (bool $ force = false )
162170 {
163- $ token = $ this ->applyFilter ('verify-request-token ' , '--token ' );
164- $ source = $ this ->applyFilter ('verify-request-source ' , '--source ' );
165- $ ignore = $ this ->applyFilter ('verify-request-ignore ' , '--ignore ' );
171+ $ token = ( string ) $ this ->applyFilter ('verify-request-token ' , '--token ' );
172+ $ action = ( string ) $ this ->applyFilter ('verify-request-action ' , '--action ' );
173+ $ ignore = ( string ) $ this ->applyFilter ('verify-request-ignore ' , '--ignore ' );
166174
167175 if ( $ force ) {
168176 if ( !$ this ->hasRequest ($ token ) ) {
@@ -173,8 +181,8 @@ protected function verifyRequest(bool $force = false)
173181 }
174182
175183 if ( $ this ->hasRequest ($ token ) ) {
176- $ source = $ this ->hasRequest ($ source ) ? $ this ->getRequest ($ source ) : '' ;
177- if ( !$ this ->verifyToken ($ this ->getRequest ($ token ), $ source ) ) {
184+ $ action = $ this ->hasRequest ($ action ) ? $ this ->getRequest ($ action ) : '' ;
185+ if ( !$ this ->verifyToken ($ this ->getRequest ($ token ), $ action ) ) {
178186 $ msg = $ this ->applyFilter ('invalid-request-token ' , 'Invalid request token ' );
179187 $ msg = $ this ->translate ($ msg );
180188 $ this ->setResponse ($ msg , [], 'error ' , 401 );
@@ -205,7 +213,7 @@ protected function sanitizeRequest(bool $verify = true, bool $force = false) : a
205213
206214 if ( !$ force ) {
207215 $ excepts = $ this ->mergeArray ([
208- 'submit ' , '--token ' , '--source ' , '--ignore '
216+ 'submit ' , '--token ' , '--action ' , '--ignore '
209217 ], $ excepts );
210218 }
211219
0 commit comments