forked from WebGoat/WebGoat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move plugin messages (WebGoat#1968)
- Loading branch information
Showing
134 changed files
with
757 additions
and
693 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
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
130 changes: 130 additions & 0 deletions
130
src/main/java/org/owasp/webgoat/container/assignments/AttackResultBuilder.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,130 @@ | ||
package org.owasp.webgoat.container.assignments; | ||
|
||
import org.owasp.webgoat.container.i18n.PluginMessages; | ||
|
||
public class AttackResultBuilder { | ||
|
||
private PluginMessages messages; | ||
private boolean lessonCompleted; | ||
private Object[] feedbackArgs; | ||
private String feedbackResourceBundleKey; | ||
private String output; | ||
private Object[] outputArgs; | ||
private AssignmentEndpoint assignment; | ||
private boolean attemptWasMade = false; | ||
private boolean assignmentCompleted; | ||
|
||
public AttackResultBuilder(PluginMessages messages) { | ||
this.messages = messages; | ||
} | ||
|
||
public AttackResultBuilder() {} | ||
|
||
public AttackResultBuilder lessonCompleted(boolean lessonCompleted) { | ||
this.lessonCompleted = lessonCompleted; | ||
this.feedbackResourceBundleKey = "lesson.completed"; | ||
return this; | ||
} | ||
|
||
public AttackResultBuilder lessonCompleted(boolean lessonCompleted, String resourceBundleKey) { | ||
this.lessonCompleted = lessonCompleted; | ||
this.feedbackResourceBundleKey = resourceBundleKey; | ||
return this; | ||
} | ||
|
||
public AttackResultBuilder assignmentCompleted(boolean assignmentCompleted) { | ||
this.assignmentCompleted = assignmentCompleted; | ||
this.feedbackResourceBundleKey = "assignment.completed"; | ||
return this; | ||
} | ||
|
||
public AttackResultBuilder assignmentCompleted( | ||
boolean assignmentCompleted, String resourceBundleKey) { | ||
this.assignmentCompleted = assignmentCompleted; | ||
this.feedbackResourceBundleKey = resourceBundleKey; | ||
return this; | ||
} | ||
|
||
public AttackResultBuilder feedbackArgs(Object... args) { | ||
this.feedbackArgs = args; | ||
return this; | ||
} | ||
|
||
public AttackResultBuilder feedback(String resourceBundleKey) { | ||
this.feedbackResourceBundleKey = resourceBundleKey; | ||
return this; | ||
} | ||
|
||
public AttackResultBuilder output(String output) { | ||
this.output = output; | ||
return this; | ||
} | ||
|
||
public AttackResultBuilder outputArgs(Object... args) { | ||
this.outputArgs = args; | ||
return this; | ||
} | ||
|
||
public AttackResultBuilder attemptWasMade() { | ||
this.attemptWasMade = true; | ||
return this; | ||
} | ||
|
||
public AttackResult build() { | ||
return new AttackResult( | ||
lessonCompleted, | ||
feedbackResourceBundleKey, | ||
feedbackArgs, | ||
output, | ||
outputArgs, | ||
assignment.getClass().getSimpleName(), | ||
attemptWasMade); | ||
} | ||
|
||
public AttackResultBuilder assignment(AssignmentEndpoint assignment) { | ||
this.assignment = assignment; | ||
return this; | ||
} | ||
|
||
/** | ||
* Convenience method for create a successful result: | ||
* | ||
* <p>- Assignment is set to solved - Feedback message is set to 'assignment.solved' | ||
* | ||
* <p>Of course you can overwrite these values in a specific lesson | ||
* | ||
* @return a builder for creating a result from a lesson | ||
* @param assignment | ||
*/ | ||
public static AttackResultBuilder success(AssignmentEndpoint assignment) { | ||
return new AttackResultBuilder() | ||
.lessonCompleted(true) | ||
.assignmentCompleted(true) | ||
.attemptWasMade() | ||
.feedback("assignment.solved") | ||
.assignment(assignment); | ||
} | ||
|
||
/** | ||
* Convenience method for create a failed result: | ||
* | ||
* <p>- Assignment is set to not solved - Feedback message is set to 'assignment.not.solved' | ||
* | ||
* <p>Of course you can overwrite these values in a specific lesson | ||
* | ||
* @return a builder for creating a result from a lesson | ||
* @param assignment | ||
*/ | ||
public static AttackResultBuilder failed(AssignmentEndpoint assignment) { | ||
return new AttackResultBuilder() | ||
.lessonCompleted(false) | ||
.assignmentCompleted(true) | ||
.attemptWasMade() | ||
.feedback("assignment.not.solved") | ||
.assignment(assignment); | ||
} | ||
|
||
public static AttackResultBuilder informationMessage(AssignmentEndpoint assignment) { | ||
return new AttackResultBuilder().lessonCompleted(false).assignment(assignment); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...n/java/org/owasp/webgoat/container/assignments/AttackResultMessageResponseBodyAdvice.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,41 @@ | ||
package org.owasp.webgoat.container.assignments; | ||
|
||
import org.owasp.webgoat.container.i18n.PluginMessages; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.converter.HttpMessageConverter; | ||
import org.springframework.http.server.ServerHttpRequest; | ||
import org.springframework.http.server.ServerHttpResponse; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; | ||
|
||
/** This class intercepts the response body and applies the plugin messages to the attack result. */ | ||
@RestControllerAdvice | ||
public class AttackResultMessageResponseBodyAdvice implements ResponseBodyAdvice<Object> { | ||
|
||
private final PluginMessages pluginMessages; | ||
|
||
public AttackResultMessageResponseBodyAdvice(PluginMessages pluginMessages) { | ||
this.pluginMessages = pluginMessages; | ||
} | ||
|
||
@Override | ||
public boolean supports( | ||
MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Object beforeBodyWrite( | ||
Object body, | ||
MethodParameter returnType, | ||
MediaType selectedContentType, | ||
Class<? extends HttpMessageConverter<?>> selectedConverterType, | ||
ServerHttpRequest request, | ||
ServerHttpResponse response) { | ||
if (body instanceof AttackResult a) { | ||
return a.apply(pluginMessages); | ||
} | ||
return body; | ||
} | ||
} |
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.