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

Reworked auth config to allow for different identity attributes in different configs.

This commit is contained in:
Geert Bevin 2022-12-23 14:49:57 -05:00
parent 980317245b
commit ed4b9fc5e6
7 changed files with 33 additions and 15 deletions

View file

@ -24,7 +24,7 @@ public class HelloAuthentication extends Site {
before(new Authenticated(config));
landing = get("/hello", c -> {
var t = c.template("HelloAuthenticated");
t.setValue("user", AuthConfig.identityAttribute(c).getLogin());
t.setValue("user", config.identityAttribute(c).getLogin());
c.print(t);
});
logout = get("/logout", new Logout(config, TemplateFactory.HTML.get("HelloLogout")));

View file

@ -62,11 +62,11 @@ public class AuthConfig {
return this;
}
public static String identityAttributeName() {
public String identityAttributeName() {
return DEFAULT_IDENTITY_ATTRIBUTE_NAME;
}
public static RoleUserIdentity identityAttribute(Context c) {
public RoleUserIdentity identityAttribute(Context c) {
return (RoleUserIdentity) c.attribute(identityAttributeName());
}

View file

@ -15,6 +15,19 @@ public class Identified implements Element {
authConfig_ = config;
}
public static Identified getIdentifiedElementInRequest(Context c) {
var identified = c.attribute(Identified.class.getName());
if (identified instanceof Identified result) {
return result;
}
return null;
}
public AuthConfig getAuthConfig() {
return authConfig_;
}
public void process(Context c)
throws Exception {
if (!c.hasAttribute(authConfig_.identityAttributeName())) {
@ -22,16 +35,15 @@ public class Identified implements Element {
}
}
public void setIdentityAttribute(Context c)
throws Exception {
public void setIdentityAttribute(Context c) {
var identity = getIdentity(c);
if (identity != null) {
c.setAttribute(Identified.class.getName(), this);
c.setAttribute(authConfig_.identityAttributeName(), identity);
}
}
public RoleUserIdentity getIdentity(Context c)
throws Exception {
public RoleUserIdentity getIdentity(Context c) {
if (!c.hasCookie(authConfig_.authCookieName())) {
return null;
}

View file

@ -70,7 +70,7 @@ public class Logout implements Element {
}
}
c.removeAttribute(AuthConfig.identityAttributeName());
c.removeAttribute(authConfig_.identityAttributeName());
loggedOut(template);

View file

@ -5,6 +5,7 @@
package rife.engine;
import rife.authentication.elements.AuthConfig;
import rife.authentication.elements.Identified;
import rife.engine.exceptions.EngineException;
import rife.template.Template;
import rife.template.TemplateEncoder;
@ -127,7 +128,12 @@ class EngineTemplateProcessor {
}
private void processAuthentication(final List<String> setValues) {
final var identity = AuthConfig.identityAttribute(context_);
var identified = Identified.getIdentifiedElementInRequest(context_);
if (identified == null) {
return;
}
final var identity = identified.getAuthConfig().identityAttribute(context_);
if (identity != null) {
final var auth_value_tags = template_.getFilteredValues(TemplateFactoryFilters.TAG_AUTH);
final var auth_block_tags = template_.getFilteredBlocks(TemplateFactoryFilters.TAG_AUTH);
@ -140,26 +146,26 @@ class EngineTemplateProcessor {
// handle authenticated login blocks assignment
if (!template_.isValueSet(auth_value_id)) {
for (var block_groups : auth_login_block_tags) {
for (var block_groups : auth_login_block_tags) {
var auth_block_id = block_groups[0];
if (block_groups[1].equals(auth_differentiator) &&
identity.getLogin().equals(block_groups[2])) {
template_.setBlock(auth_value_id, auth_block_id);
setValues.add(auth_value_id);
}
}
}
}
// handle authenticated role blocks assignment
if (!template_.isValueSet(auth_value_id)) {
for (var block_groups : auth_role_block_tags) {
for (var block_groups : auth_role_block_tags) {
var auth_block_id = block_groups[0];
if (block_groups[1].equals(auth_differentiator) &&
identity.getAttributes().isInRole(block_groups[2])) {
template_.setBlock(auth_value_id, auth_block_id);
setValues.add(auth_value_id);
}
}
}
}
// handle authenticated blocks assignment

View file

@ -61,7 +61,7 @@ public class DatabaseAuthenticatedSite extends Site implements AutoCloseable {
landing = get("/landing", c -> c.print("Landing"));
logout = get("/logout", new Logout(config, TemplateFactory.HTML.get("authentication.logout")));
template = get("/template", c -> c.print(c.template("filtered_tags_auth")));
get("/username", c -> c.print(AuthConfig.identityAttribute(c) != null ? AuthConfig.identityAttribute(c).getLogin() : "not logged in"));
get("/username", c -> c.print(config.identityAttribute(c) != null ? config.identityAttribute(c).getLogin() : "not logged in"));
group(new Router() {
public void setup() {
before(new Logout(config));

View file

@ -43,7 +43,7 @@ public class MemoryAuthenticatedSite extends Site {
landing = get("/landing", c -> c.print("Landing"));
logout = get("/logout", new Logout(config, TemplateFactory.HTML.get("authentication.logout")));
template = get("/template", c -> c.print(c.template("filtered_tags_auth")));
get("/username", c -> c.print(AuthConfig.identityAttribute(c) != null ? AuthConfig.identityAttribute(c).getLogin() : "not logged in"));
get("/username", c -> c.print(config.identityAttribute(c) != null ? config.identityAttribute(c).getLogin() : "not logged in"));
group(new Router() {
public void setup() {
before(new Logout(config));