Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update DbAuthMiddleware to refresh session data via /me endpoint #996

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,7 @@ You can tune the middleware behavior using middleware specific configuration par
- "dbAuth.loginAfterRegistration": 1 or zero if registered users should be logged in after registration ("")
- "dbAuth.passwordLength": Minimum length that the password must have ("12")
- "dbAuth.sessionName": The name of the PHP session that is started ("")
- "dbAuth.updateSessionData": Number of minutes after which the session data is updated when calling /me end-point (0)
- "wpAuth.mode": Set to "optional" if you want to allow anonymous access ("required")
- "wpAuth.wpDirectory": The folder/path where the Wordpress install can be found (".")
- "wpAuth.usernameFormField": The name of the form field that holds the username ("username")
Expand Down
20 changes: 20 additions & 0 deletions src/Tqdev/PhpCrudApi/Middleware/DbAuthMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
session_regenerate_id(true);
}
unset($user[$passwordColumnName]);
$user['updatedAt'] = time();
$_SESSION['user'] = $user;
return $this->responder->success($user);
} else {
Expand All @@ -128,6 +129,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
session_regenerate_id(true);
}
unset($user[$passwordColumnName]);
$user['updatedAt'] = time();
$_SESSION['user'] = $user;
return $this->responder->success($user);
}
Expand Down Expand Up @@ -176,6 +178,24 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
}
if ($method == 'GET' && $path == 'me') {
if (isset($_SESSION['user'])) {
$updateAfter = $this->getProperty('updateSessionData',0) * 60;
$passwordColumnName = $this->getProperty('passwordColumn','password');
if($updateAfter > 0 && (time() > ($_SESSION['user']['updatedAt'] + $updateAfter))){
$tableName = $this->getProperty('loginTable','users');
$table = $this->reflection->getTable($tableName);
$pkName = $table->getPk()->getName();
$returnedColumns = $this->getProperty('returnedColumns','');
if(!$returnedColumns){
$columnNames = $table->getColumnNames();
}else{
$columnNames = array_map('trim',explode(',',$returnedColumns));
$columnNames = array_values(array_unique($columnNames));
}
$user = $this->db->selectSingle($table,$columnNames,$_SESSION['user'][$pkName]);
unset($user[$passwordColumnName]);
$user['updatedAt'] = time();
$_SESSION['user'] = $user;
}
return $this->responder->success($_SESSION['user']);
}
return $this->responder->error(ErrorCode::AUTHENTICATION_REQUIRED, '');
Expand Down