We assume that the cookie should no longer allow re-login once wp user session destroy –all has been executed. Is this assumption wrong?
The cookie has nothing to do with re-logging in, the command will destroy their current active sessions, and has nothing to do with future sessions.
A user can create new sessions by logging in, but you can prevent that via your SSID implementation. At that point it is no longer a WordPress question but an OpenID Connect question. If it allows them to log in again a new session will be created, if it does not then no session is created since a successful login is needed to create one.
Is there a way to invalidate this cookie via WP CLI without adding custom code?
Four alternative methods:
- Edit the user in WP Admin and press the log out everywhere button
- Fetch the users session manager object via
$manager = WP_Session_Tokens::get_instance( get_current_user_id() );
then calldestroy_all()
on the resulting object. - Manually delete the sessions in SQL ( Note that this carries risk as it won’t run hooks, clear caches and object caches, etc )
- Using CLI you could erase the user meta that stores the sessions, while this is safer than using a raw SQL query, and clears the user meta cache as needed, it won’t run the relevant hooks and still carries some risk that the CLI command you mentioned will handle.
If not, what is the best practice to fully log out users in this scenario?
The deletion of the session via WP CLI or via the code snippet is enough to invalidate the cookie and force the user to log in and create a new session. The existing session and any cookies it supported are ineffective and invalidated as a result of the sessions deletion.
My recommendations:
- For WP CLI use the
wp user session destroy
command you’re already using. - For PHP code, use the session token manager snippet.
A General Note on Sessions
In user meta is a list of current active/valid sessions, and the cookie refers to this. Erasing these sessions means that the cookies are no longer usable and new ones need to be created. This is how the log out everywhere button on the user profile page works, and no browser visits or cookie erasure is necessary.
https://developer.wordpress.org/reference/functions/wp_destroy_all_sessions/
wp_destroy_all_sessions
will erase all sessions for the currently logged in user.
I would also note that this list is of active sessions, once a session is no longer active it is removed, so it can’t be used to find out when a user last logged in. Any system that does can be thwarted by logging out to remove the session from the list.
Also, these are not PHP sessions, those are a separate thing that is not used by WordPress and carry their own consequences and compatibility issues.
We run WordPress with the OpenID Connect Generic Client plug-in to provide SSO using Keycloak.
Note that you should also look into this plugin specifically, only answers for general WordPress can be given here, and this plugin might interfere or change how this system works, although this is unlikely.