Skip to content

Commit

Permalink
Switch logging on/off, better overriding capabilities of auth framework
Browse files Browse the repository at this point in the history
  • Loading branch information
SoltauFintel committed Jan 8, 2018
1 parent 22d47f1 commit 9d2b969
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
allprojects {
version = "0.2.1"
version = "0.2.2"
group = 'de.mwvb.maja'
}

Expand Down
44 changes: 32 additions & 12 deletions maja-auth/src/main/java/de/mwvb/maja/auth/AuthPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import de.mwvb.maja.auth.rememberme.NoOpRememberMeFeature;
import de.mwvb.maja.auth.rememberme.RememberMeFeature;
import de.mwvb.maja.web.Action;
import de.mwvb.maja.web.ActionBase;
import de.mwvb.maja.web.AppConfig;
import de.mwvb.maja.web.Template;
import spark.Filter;
Expand All @@ -32,14 +33,15 @@
*/
public class AuthPlugin implements de.mwvb.maja.web.AuthPlugin, Filter {
public static final String USER_ATTR = "user";
private static final String LOGGED_IN = "logged_in";
private static final String LOGGED_IN_YES = "yes";
private static final String USERID_ATTR = "user_id";
private final Set<String> notProtected = new HashSet<>();
private final Authorization authorization;
private final AuthFeature feature;
private final RememberMeFeature rememberMe;

public static final String LOGGED_IN = "logged_in";
public static final String LOGGED_IN_YES = "yes";
public static final String USERID_ATTR = "user_id";
protected final Set<String> notProtected = new HashSet<>();
protected final Authorization authorization;
protected final AuthFeature feature;
protected final RememberMeFeature rememberMe;
private boolean debugLogging = true;

/** No remember-me constructor */
public AuthPlugin() {
this(new NoOpRememberMeFeature());
Expand Down Expand Up @@ -78,11 +80,11 @@ protected Authorization getAuthorization() {
}
}

private boolean hasGoogle() {
protected boolean hasGoogle() {
return new AppConfig().hasFilledKey("google.key");
}

private boolean hasFacebook() {
protected boolean hasFacebook() {
return new AppConfig().hasFilledKey("facebook.key");
}

Expand All @@ -106,11 +108,15 @@ public void routes() {
before(this);

addNotProtected("/logout");
Action.get("/logout", new LogoutAction(rememberMe));
Action.get("/logout", getLogoutAction(rememberMe));

feature.routes();
}
}

protected ActionBase getLogoutAction(RememberMeFeature rememberMe) {
return new LogoutAction(rememberMe, isDebugLogging());
}

public static String getUser(Session session) {
return session.attribute(USER_ATTR);
Expand Down Expand Up @@ -166,7 +172,7 @@ public String login(Request req, Response res, String name, String foreignId, St
String longId = service + "#" + foreignId;
setLoginData(true, name, longId, req.session());
rememberMe.rememberMe(rememberMeWanted, res, name, longId);
Logger.debug("Login: " + name + " (" + longId + ")");
logLogin(name, longId);

// Redirect zur ursprünglich angewählten Seite
String uri = req.session().attribute("uri");
Expand All @@ -177,4 +183,18 @@ public String login(Request req, Response res, String name, String foreignId, St
res.redirect(uri);
return "";
}

protected void logLogin(String name, String longId) {
if (isDebugLogging()) {
Logger.debug("Login: " + name + " (" + longId + ")");
}
}

public boolean isDebugLogging() {
return debugLogging;
}

public void setDebugLogging(boolean debugLogging) {
this.debugLogging = debugLogging;
}
}
8 changes: 5 additions & 3 deletions maja-auth/src/main/java/de/mwvb/maja/auth/LogoutAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@

public class LogoutAction extends ActionBase {
private final RememberMeFeature rememberMe;

public LogoutAction(RememberMeFeature rememberMe) {
private final boolean isDebugLogging;

public LogoutAction(RememberMeFeature rememberMe, boolean isDebugLogging) {
this.rememberMe = rememberMe;
this.isDebugLogging = isDebugLogging;
}

@Override
public String run() {
Session session = req.session();
String userId = AuthPlugin.getUserId(session);
if (userId != null) {
if (userId != null && isDebugLogging) {
Logger.debug("Logout: " + AuthPlugin.getUser(session) + " (" + userId + ")");
}
rememberMe.forget(res, userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import spark.Response;

public class OneUserAuthorization implements Authorization {
public static boolean errorLogging = true;
private final String userId;
private final String service;

Expand All @@ -29,7 +30,9 @@ public String check(Request req, Response res, String name, String foreignId, St
}

public static String notAuthorized(Response res, String name, String foreignId, String service) {
Logger.error("Not authorized: " + name + " (" + foreignId + "@" + service + ")");
if (errorLogging) {
Logger.error("Not authorized: " + name + " (" + foreignId + "@" + service + ")");
}
res.status(HttpStatus.FORBIDDEN_403);
throw new RuntimeException("You are not authorized to use this web application.");
}
Expand Down

0 comments on commit 9d2b969

Please sign in to comment.