2
0
Fork 0
mirror of https://github.com/ethauvin/rife2.git synced 2025-05-02 11:28:12 -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)); before(new Authenticated(config));
landing = get("/hello", c -> { landing = get("/hello", c -> {
var t = c.template("HelloAuthenticated"); var t = c.template("HelloAuthenticated");
t.setValue("user", AuthConfig.identityAttribute(c).getLogin()); t.setValue("user", config.identityAttribute(c).getLogin());
c.print(t); c.print(t);
}); });
logout = get("/logout", new Logout(config, TemplateFactory.HTML.get("HelloLogout"))); logout = get("/logout", new Logout(config, TemplateFactory.HTML.get("HelloLogout")));

View file

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

View file

@ -15,6 +15,19 @@ public class Identified implements Element {
authConfig_ = config; 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) public void process(Context c)
throws Exception { throws Exception {
if (!c.hasAttribute(authConfig_.identityAttributeName())) { if (!c.hasAttribute(authConfig_.identityAttributeName())) {
@ -22,16 +35,15 @@ public class Identified implements Element {
} }
} }
public void setIdentityAttribute(Context c) public void setIdentityAttribute(Context c) {
throws Exception {
var identity = getIdentity(c); var identity = getIdentity(c);
if (identity != null) { if (identity != null) {
c.setAttribute(Identified.class.getName(), this);
c.setAttribute(authConfig_.identityAttributeName(), identity); c.setAttribute(authConfig_.identityAttributeName(), identity);
} }
} }
public RoleUserIdentity getIdentity(Context c) public RoleUserIdentity getIdentity(Context c) {
throws Exception {
if (!c.hasCookie(authConfig_.authCookieName())) { if (!c.hasCookie(authConfig_.authCookieName())) {
return null; return null;
} }

View file

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

View file

@ -5,6 +5,7 @@
package rife.engine; package rife.engine;
import rife.authentication.elements.AuthConfig; import rife.authentication.elements.AuthConfig;
import rife.authentication.elements.Identified;
import rife.engine.exceptions.EngineException; import rife.engine.exceptions.EngineException;
import rife.template.Template; import rife.template.Template;
import rife.template.TemplateEncoder; import rife.template.TemplateEncoder;
@ -127,7 +128,12 @@ class EngineTemplateProcessor {
} }
private void processAuthentication(final List<String> setValues) { 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) { if (identity != null) {
final var auth_value_tags = template_.getFilteredValues(TemplateFactoryFilters.TAG_AUTH); final var auth_value_tags = template_.getFilteredValues(TemplateFactoryFilters.TAG_AUTH);
final var auth_block_tags = template_.getFilteredBlocks(TemplateFactoryFilters.TAG_AUTH); final var auth_block_tags = template_.getFilteredBlocks(TemplateFactoryFilters.TAG_AUTH);

View file

@ -61,7 +61,7 @@ public class DatabaseAuthenticatedSite extends Site implements AutoCloseable {
landing = get("/landing", c -> c.print("Landing")); landing = get("/landing", c -> c.print("Landing"));
logout = get("/logout", new Logout(config, TemplateFactory.HTML.get("authentication.logout"))); logout = get("/logout", new Logout(config, TemplateFactory.HTML.get("authentication.logout")));
template = get("/template", c -> c.print(c.template("filtered_tags_auth"))); 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() { group(new Router() {
public void setup() { public void setup() {
before(new Logout(config)); before(new Logout(config));

View file

@ -43,7 +43,7 @@ public class MemoryAuthenticatedSite extends Site {
landing = get("/landing", c -> c.print("Landing")); landing = get("/landing", c -> c.print("Landing"));
logout = get("/logout", new Logout(config, TemplateFactory.HTML.get("authentication.logout"))); logout = get("/logout", new Logout(config, TemplateFactory.HTML.get("authentication.logout")));
template = get("/template", c -> c.print(c.template("filtered_tags_auth"))); 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() { group(new Router() {
public void setup() { public void setup() {
before(new Logout(config)); before(new Logout(config));