-
-
Notifications
You must be signed in to change notification settings - Fork 372
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1125 from OWASP/nbaars/lazy-load-challenges
refactor: simplify challenges when answer is fixed
- Loading branch information
Showing
36 changed files
with
214 additions
and
468 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/org/owasp/wrongsecrets/challenges/FixedAnswerChallenge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.owasp.wrongsecrets.challenges; | ||
|
||
import com.google.common.base.Supplier; | ||
import com.google.common.base.Suppliers; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Use this class when a challenge where the answer is fixed, meaning it will not change and does | ||
* not depend on the given answer. For example: a hardcoded key or Spring environment variable. | ||
* | ||
* <p>Why do we make this distinction? Because in the case of the fixed answer we can cache the | ||
* value. It is important to <b>NOT</b> do any reading / calculation in the constructor when using | ||
* this interface. | ||
* | ||
* <p>NOTE: If the challenge depends on a calculation you can implement {@link Challenge} | ||
*/ | ||
public abstract class FixedAnswerChallenge implements Challenge { | ||
|
||
private Supplier<String> cachedAnswer = Suppliers.memoize(() -> getAnswer()); | ||
|
||
@Override | ||
public final Spoiler spoiler() { | ||
return new Spoiler(cachedAnswer.get()); | ||
} | ||
|
||
@Override | ||
public final boolean answerCorrect(String answer) { | ||
return Objects.equals(cachedAnswer.get(), answer); | ||
} | ||
|
||
public abstract String getAnswer(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.