2
0
Fork 0
mirror of https://github.com/ethauvin/rife2.git synced 2025-04-30 18:48:13 -07:00

Added logout element.

Improved support for path-based route resolving, now also taking super classes into account.
This commit is contained in:
Geert Bevin 2022-11-02 23:25:24 -04:00
parent 9ae3bb7148
commit ddfea1d1d0
7 changed files with 153 additions and 19 deletions

View file

@ -14,8 +14,11 @@ public class HelloAuthentication extends Site {
final MemorySessionValidator validator = new MemorySessionValidator();
final AuthenticationConfig config = new AuthenticationConfig(validator);
static class AuthenticatedSection extends Router {
final Route hello = get("/hello", c -> c.print("Hello World"));
class AuthenticatedSection extends Router {
Route hello = get("/hello", c -> {
c.print(c.template("HelloAuthenticated"));
});
Route logout = get("/logout", new Logout(config, TemplateFactory.HTML.get("HelloLogout")));
}
public void setup() {

View file

@ -0,0 +1,6 @@
<html lang="en">
<body>
<p>Hello authenticated!</p>
<p><a href="{{v route:logout/}}">Log out</a></p>
</body>
</html>

View file

@ -0,0 +1,5 @@
<html lang="en">
<body>
<p>Logged out</p>
</body>
</html>

View file

@ -4,9 +4,7 @@
*/
package rife.authentication.elements;
import rife.authentication.credentialsmanagers.IdentifiableUsersManager;
import rife.authentication.credentialsmanagers.RoleUserAttributes;
import rife.authentication.credentialsmanagers.RoleUserIdentity;
import rife.authentication.credentialsmanagers.*;
import rife.engine.Context;
import rife.engine.Element;

View file

@ -0,0 +1,88 @@
/*
* Copyright 2001-2022 Geert Bevin (gbevin[remove] at uwyn dot com)
* Licensed under the Apache License, Version 2.0 (the "License")
*/
package rife.authentication.elements;
import jakarta.servlet.http.Cookie;
import rife.authentication.elements.exceptions.UndefinedLogoutRememberManagerException;
import rife.engine.Context;
import rife.engine.Element;
import rife.template.Template;
public class Logout implements Element {
protected final AuthenticationConfig authConfig_;
protected final Template template_;
public Logout(AuthenticationConfig config, Template template) {
authConfig_ = config;
template_ = template;
}
protected void initializeLogout() {
}
protected void entrance(Template template) {
}
protected void loggedOut(Template template) {
}
public void process(Context c)
throws Exception {
initializeLogout();
final Template template;
if (template_ == null) {
template = null;
} else {
template = (Template) template_.clone();
}
entrance(template);
String authid = null;
var auth_cookie_name = authConfig_.authCookieName();
if (c.hasCookie(authConfig_.authCookieName())) {
var auth_cookie = c.cookie(auth_cookie_name);
authid = auth_cookie.getValue();
}
if (authid != null) {
authConfig_.sessionValidator().getSessionManager().eraseSession(authid);
// clear remember cookie for the user
if (c.hasCookie(authConfig_.rememberCookieName())) {
if (null == authConfig_.sessionValidator().getRememberManager()) {
throw new UndefinedLogoutRememberManagerException();
}
var remember_cookie_name = authConfig_.rememberCookieName();
authConfig_.sessionValidator().getRememberManager().eraseRememberId(c.cookieValue(remember_cookie_name));
var remember_cookie = c.cookie(remember_cookie_name);
remember_cookie.setMaxAge(-1);
remember_cookie.setPath("/");
remember_cookie.setValue("");
c.cookie(remember_cookie);
}
// clear the authentication cookie
if (c.hasCookie(auth_cookie_name)) {
var cookie = c.cookie(auth_cookie_name);
cookie.setMaxAge(-1);
cookie.setPath("/");
cookie.setValue("");
c.cookie(cookie);
}
}
c.removeAttribute(authConfig_.identityAttributeName());
loggedOut(template);
if (template != null) {
c.print(template);
}
}
}

View file

@ -0,0 +1,17 @@
/*
* Copyright 2001-2022 Geert Bevin (gbevin[remove] at uwyn dot com)
* Licensed under the Apache License, Version 2.0 (the "License")
*/
package rife.authentication.elements.exceptions;
import rife.engine.exceptions.EngineException;
import java.io.Serial;
public class UndefinedLogoutRememberManagerException extends EngineException {
@Serial private static final long serialVersionUID = 2230068458914504593L;
public UndefinedLogoutRememberManagerException() {
super("The RememberManager is null, maybe this type of logout element doesn't support remember Remember Me functionalities.");
}
}

View file

@ -4,6 +4,7 @@
*/
package rife.engine;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;
@ -214,7 +215,7 @@ public class Router {
// do nothing
} else if (token.equals("^")) {
if (route != null) {
route = null;
route = null;
} else {
if (router.parent_ == null) {
return null;
@ -223,22 +224,38 @@ public class Router {
router = router.parent_;
}
} else {
try {
var field = router.getClass().getDeclaredField(token);
field.setAccessible(true);
Class klass = router.getClass();
Field field = null;
while (field == null && klass != Site.class && klass != Router.class) {
try {
field = klass.getDeclaredField(token);
field.setAccessible(true);
if (!Modifier.isStatic(field.getModifiers()) &&
!Modifier.isTransient(field.getModifiers())) {
if (Route.class.isAssignableFrom(field.getType())) {
if (route != null) {
return null;
}
route = (Route) field.get(router);
} else if (Router.class.isAssignableFrom(field.getType())) {
router = (Router) field.get(router);
if (Modifier.isStatic(field.getModifiers()) ||
Modifier.isTransient(field.getModifiers()) ||
(!Route.class.isAssignableFrom(field.getType()) && !Router.class.isAssignableFrom(field.getType()))) {
field = null;
}
} catch (NoSuchFieldException ignored) {
}
} catch (IllegalAccessException | NoSuchFieldException e) {
klass = klass.getSuperclass();
}
if (field == null) {
return null;
}
try {
if (Route.class.isAssignableFrom(field.getType())) {
if (route != null) {
return null;
}
route = (Route) field.get(router);
} else if (Router.class.isAssignableFrom(field.getType())) {
router = (Router) field.get(router);
}
} catch (IllegalAccessException e) {
return null;
}
}