snoopdave commented on code in PR #321:
URL: https://github.com/apache/roller/pull/321#discussion_r4636769881


##########
app/src/main/java/org/apache/roller/weblogger/ui/core/RollerSession.java:
##########
@@ -131,6 +161,149 @@ public static RollerSession 
getRollerSession(HttpServletRequest request) {
         return rollerSession;
     }
 
+    /**
+     * The enabled user a login belongs to, or null if there is none.
+     */
+    private static User findUser(UserManager umgr, Principal principal) throws 
WebloggerException {
+        User user = umgr.getUserByUserName(principal.getName());
+
+        // For OIDC-authenticated users, look up by OIDC subject
+        if (user == null && principal instanceof OAuth2AuthenticationToken 
oauthToken) {
+            Object oauthPrincipal = oauthToken.getPrincipal();
+            if (oauthPrincipal instanceof OidcUser oidcUser) {
+                String oidcSubject = 
RollerOidcUserService.toOidcSubject(oidcUser);
+                user = umgr.getUserByOpenIdUrl(oidcSubject);
+            }
+        }
+        return user;
+    }
+
+    /**
+     * Records a login in its session as soon as Spring Security accepts it:
+     * the user's password as it is now, so that a later password change ends
+     * the session even if Roller's pages are not opened in between. A login in
+     * a session that already had one replaces what that one left behind.
+     */
+    public static void loggedIn(HttpServletRequest request, Principal login) {
+        try {
+            HttpSession session = request.getSession(true);
+            session.removeAttribute(ROLLER_SESSION);
+            session.removeAttribute(LOGIN_PASSWORD);
+            if (WebloggerFactory.isBootstrapped()) {
+                UserManager umgr = 
WebloggerFactory.getWeblogger().getUserManager();
+                User user = findUser(umgr, login);
+                if (user != null) {
+                    session.setAttribute(LOGIN_PASSWORD, user.getUserName() + 
"\n" + digest(user));
+                }
+            }
+        } catch (WebloggerException e) {
+            log.error("ERROR: getting user object", e);
+        }
+    }
+
+    /**
+     * Keeps the current session logged in after its own user changed their
+     * password, or an administrator changed their own. Call it after the new
+     * password is saved; the user's other sessions still end.
+     */
+    public static void refreshPassword(HttpServletRequest request, User user) {
+        HttpSession session = request != null ? request.getSession(false) : 
null;
+        if (session == null || user == null) {
+            return;
+        }
+        try {
+            Object attribute = session.getAttribute(ROLLER_SESSION);
+            if (attribute instanceof RollerSession rollerSession
+                    && user.getUserName().equals(rollerSession.userName)) {
+                rollerSession.setAuthenticatedUser(user);
+                rememberLoginPassword(session, rollerSession);
+            }
+        } catch (IllegalStateException e) {
+            if (!isInvalidated(session)) {
+                throw e;
+            }
+            // another request ended the session, there is no login to keep
+        }
+    }
+
+    /**
+     * Whether the login in this session first saw the user's current password,
+     * or the session has not recorded one for this user yet.
+     */
+    private static boolean sameLoginPassword(HttpSession session, User user) {
+        Object recorded = session.getAttribute(LOGIN_PASSWORD);
+        String prefix = user.getUserName() + "\n";
+        if (!(recorded instanceof String login) || !login.startsWith(prefix)) {
+            return true;
+        }
+        return login.equals(prefix + digest(user));
+    }
+
+    private static void rememberLoginPassword(HttpSession session, 
RollerSession rollerSession) {
+        session.setAttribute(LOGIN_PASSWORD, rollerSession.userName + "\n" + 
rollerSession.passwordDigest);
+    }
+
+    /**
+     * Whether the user this session logged in as is still enabled and still
+     * has the password the session logged in with. A failed lookup keeps the
+     * session, so a database error does not log everyone out.
+     */
+    private boolean isCurrent() {
+        User user;
+        try {
+            UserManager mgr = WebloggerFactory.getWeblogger().getUserManager();
+            // only finds enabled users
+            user = mgr.getUserByUserName(userName);
+        } catch (WebloggerException ex) {
+            log.warn("Error looking up authenticated user " + userName, ex);
+            return true;
+        }
+        return user != null && digest(user).equals(passwordDigest);

Review Comment:
   **Important:** `RollerUserDetailsService.updatePassword()` re-encodes older 
stored hashes after a successful login. This comparison ends the user's other 
sessions even though their password did not change. Track actual password 
resets separately from automatic re-encoding, and test that an encoding upgrade 
keeps existing sessions.



##########
app/src/main/java/org/apache/roller/weblogger/ui/core/RollerSession.java:
##########
@@ -119,7 +143,13 @@ public static RollerSession 
getRollerSession(HttpServletRequest request) {
                     }
                     // only set authenticated user if user is enabled
                     if (user != null && user.getEnabled()) {
+                        // a login whose password changed since it was first

Review Comment:
   **Important:** When a login has a recorded password but no RollerSession 
yet, `findUser()` can return null for a disabled or removed account. This 
branch then returns an empty RollerSession without invalidating the HTTP 
session or clearing the Spring login. End the session when the recorded account 
no longer resolves to an enabled user, and cover both a passivated session and 
a login that has not opened the UI.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to