Skip to content

Commit

Permalink
Show time claim information within Information panel.
Browse files Browse the repository at this point in the history
  • Loading branch information
DolphFlynn committed Aug 4, 2024
1 parent 4e26aff commit 29d4e7d
Show file tree
Hide file tree
Showing 13 changed files with 299 additions and 28 deletions.
25 changes: 12 additions & 13 deletions src/main/java/com/blackberry/jwteditor/model/jose/TimeClaim.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,29 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Optional;

import static java.time.ZoneOffset.UTC;
import static java.util.Arrays.stream;
import static java.util.Collections.emptyList;

public record TimeClaim(TimeClaimType type, String data, Long value) {
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("EEE MMM dd yyyy HH:mm:ss").withZone(ZoneId.from(UTC));

public boolean isValid() {
return type.isValid(value);
public String date() {
return value == null ? null : FORMATTER.format(Instant.ofEpochSecond(value).atOffset(UTC));
}

public String warning() {
if (isValid()) {
return "";
}

if (value == null || value < 0) {
return "'%s' value is invalid".formatted(type.name);
}

String futurePast = type.dateInThePastRequired() ? "future" : "past";
public boolean hasDate() {
return value != null;
}

return "'%s' date is in the %s".formatted(type.name, futurePast);
public boolean isValid() {
return type.isValid(value);
}

static List<TimeClaim> from(String payloadJson) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@
import static java.time.Instant.now;

public enum TimeClaimType {
EXPIRATION_TIME("exp"),
NOT_BEFORE_TIME("nbf"),
ISSUED_AT_TIME("iat");
EXPIRATION_TIME("exp", "Expiration Time"),
NOT_BEFORE_TIME("nbf", "Not Before"),
ISSUED_AT_TIME("iat", "Issued At");

final String name;
private final String displayName;

TimeClaimType(String name) {
TimeClaimType(String name, String displayName) {
this.name = name;
this.displayName = displayName;
}

public boolean isValid(Long value) {
Expand All @@ -43,10 +45,15 @@ public boolean isValid(Long value) {
return dateInThePastRequired() ? valueTime.isBefore(now()) : valueTime.isAfter(now());
}

public boolean dateInThePastRequired() {
private boolean dateInThePastRequired() {
return switch (this) {
case EXPIRATION_TIME -> false;
case NOT_BEFORE_TIME, ISSUED_AT_TIME -> true;
};
}

@Override
public String toString() {
return displayName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
package com.blackberry.jwteditor.presenter;

import burp.api.montoya.collaborator.CollaboratorPayloadGenerator;
import com.blackberry.jwteditor.model.jose.*;
import com.blackberry.jwteditor.model.jose.JOSEObject;
import com.blackberry.jwteditor.model.jose.JWE;
import com.blackberry.jwteditor.model.jose.JWS;
import com.blackberry.jwteditor.model.jose.MutableJOSEObject;
import com.blackberry.jwteditor.model.keys.Key;
import com.blackberry.jwteditor.model.keys.KeyRing;
import com.blackberry.jwteditor.utils.Utils;
Expand Down Expand Up @@ -446,8 +449,11 @@ public void componentChanged() {
//Highlight the serialized text as changed if it differs from the original, and the change wasn't triggered by onSelectionChanging
view.setSerialized(joseObject.serialize(), mutableJoseObject.changed() && !selectionChanging);

List<TimeClaim> timeClaims = mutableJoseObject.timeClaims();
view.setInformation(""); // TODO
List<Information> information = mutableJoseObject.timeClaims().stream()
.map(Information::from)
.toList();

view.setInformation(information);
}

/**
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/com/blackberry/jwteditor/presenter/Information.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Author : Dolph Flynn
Copyright 2024 Dolph Flynn
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.blackberry.jwteditor.presenter;

import com.blackberry.jwteditor.model.jose.TimeClaim;

public record Information(String text, boolean isWarning) {

static Information from(TimeClaim timeClaim) {
StringBuilder sb = new StringBuilder(timeClaim.type().toString()).append(" - ");

if (timeClaim.hasDate()) {
sb.append(timeClaim.date());
} else {
sb.append("invalid value: ").append(timeClaim.data());
}

return new Information(sb.toString(), !timeClaim.isValid());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,14 @@
<color color="-4473925"/>
</border>
<children>
<component id="7618f" class="javax.swing.JTextPane" binding="informationTextPane">
<scrollpane id="78adf" binding="informationScrollPane">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="6" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="150" height="50"/>
</grid>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<border type="none"/>
<children/>
</scrollpane>
</children>
</grid>
</children>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import burp.api.montoya.collaborator.CollaboratorPayloadGenerator;
import burp.api.montoya.ui.Selection;
import com.blackberry.jwteditor.presenter.EditorPresenter;
import com.blackberry.jwteditor.presenter.Information;
import com.blackberry.jwteditor.presenter.PresenterStore;
import com.blackberry.jwteditor.utils.Utils;
import com.blackberry.jwteditor.view.hexcodearea.HexCodeAreaFactory;
Expand Down Expand Up @@ -59,6 +60,7 @@ public abstract class EditorView {
private final RstaFactory rstaFactory;
private final boolean editable;
private final HexCodeAreaFactory hexCodeAreaFactory;
private final InformationPanel informationPanel;
private final boolean isProVersion;

private EditorMode mode;
Expand Down Expand Up @@ -89,7 +91,7 @@ public abstract class EditorView {
private JSplitPane upperSplitPane;
private JSplitPane midSplitPane;
private JSplitPane lowerSplitPane;
private JTextPane informationTextPane;
private JScrollPane informationScrollPane;

private CodeArea codeAreaSignature;
private CodeArea codeAreaEncryptedKey;
Expand All @@ -103,13 +105,17 @@ public abstract class EditorView {
HexCodeAreaFactory hexAreaCodeFactory,
CollaboratorPayloadGenerator collaboratorPayloadGenerator,
ErrorLoggingActionListenerFactory actionListenerFactory,
InformationPanelFactory informationPanelFactory,
boolean editable,
boolean isProVersion) {
this.rstaFactory = rstaFactory;
this.editable = editable;
this.hexCodeAreaFactory = hexAreaCodeFactory;
this.isProVersion = isProVersion;
this.presenter = new EditorPresenter(this, collaboratorPayloadGenerator, actionListenerFactory, presenters);
this.informationPanel = informationPanelFactory.build();

informationScrollPane.setViewportView(informationPanel);

panel.addHierarchyListener(new RunEDTActionOnFirstRenderHierarchyListener(
panel,
Expand Down Expand Up @@ -550,6 +556,7 @@ private void createUIComponents() {
textAreaPayload = rstaFactory.buildDefaultTextArea();
}

public void setInformation(String text) {
public void setInformation(List<Information> information) {
informationPanel.updateInformation(information);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ abstract class HttpEditorView extends EditorView implements ExtensionProvidedEdi
HexCodeAreaFactory hexAreaCodeFactory,
CollaboratorPayloadGenerator collaboratorPayloadGenerator,
ErrorLoggingActionListenerFactory actionListenerFactory,
InformationPanelFactory informationPanelFactory,
boolean editable,
boolean isProVersion) {
super(
Expand All @@ -41,6 +42,7 @@ abstract class HttpEditorView extends EditorView implements ExtensionProvidedEdi
hexAreaCodeFactory,
collaboratorPayloadGenerator,
actionListenerFactory,
informationPanelFactory,
editable,
isProVersion
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public HttpRequestEditorView(
new HexCodeAreaFactory(logging, userInterface),
collaboratorPayloadGenerator,
new ErrorLoggingActionListenerFactory(logging),
new InformationPanelFactory(userInterface, logging),
editable,
isProVersion
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public HttpResponseEditorView(
new HexCodeAreaFactory(logging, userInterface),
collaboratorPayloadGenerator,
new ErrorLoggingActionListenerFactory(logging),
new InformationPanelFactory(userInterface, logging),
editable,
isProVersion
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
Author : Dolph Flynn
Copyright 2024 Dolph Flynn
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.blackberry.jwteditor.view.editor;

import burp.api.montoya.logging.Logging;
import com.blackberry.jwteditor.presenter.Information;

import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

import static java.awt.EventQueue.invokeLater;

class InformationPanel extends JTextPane {
private final Supplier<Font> fontSupplier;
private final Logging logging;
private final List<Information> informationList;
private final Style informationStyle;
private final Style warningStyle;
private final Style lineSpacingStyle;

InformationPanel(Supplier<Font> fontSupplier, Logging logging) {
this.fontSupplier = fontSupplier;
this.logging = logging;
this.informationList = new ArrayList<>();
this.informationStyle = addStyle("informationStyleName", null);
this.warningStyle = addStyle("warningStyleName", null);
this.lineSpacingStyle = addStyle("lineSpacingStyleName", null);

setBorder(BorderFactory.createEmptyBorder(10, 10, 5, 5));

configureStyles();
}

void updateInformation(List<Information> infoList) {
invokeLater(() -> {
informationList.clear();
informationList.addAll(infoList);
updateDocument();
});
}

private void configureStyles() {
Font font = fontSupplier.get();

StyleConstants.setFontFamily(informationStyle, font.getFamily());
StyleConstants.setFontSize(informationStyle, font.getSize());

StyleConstants.setFontFamily(warningStyle, font.getFamily());
StyleConstants.setFontSize(warningStyle, font.getSize());
StyleConstants.setForeground(warningStyle, Color.RED);

StyleConstants.setFontFamily(lineSpacingStyle, font.getFamily());
StyleConstants.setFontSize(lineSpacingStyle, Math.round(font.getSize() * 0.3F));
}

private void updateDocument() {
setText("");

try {
for (Information information : informationList) {
addInformation(information);
}
} catch (BadLocationException e) {
logging.logToError(e);
}
}

private void addInformation(Information information) throws BadLocationException {
StyledDocument document = getStyledDocument();

document.insertString(document.getLength(), "• ", informationStyle);

Style style = information.isWarning() ? warningStyle : informationStyle;
document.insertString(document.getLength(), information.text() + "\n", style);

document.insertString(document.getLength(), "\n", lineSpacingStyle);
}

@Override
public void updateUI() {
super.updateUI();

if (fontSupplier != null) {
configureStyles();
updateDocument();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Author : Dolph Flynn
Copyright 2024 Dolph Flynn
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.blackberry.jwteditor.view.editor;

import burp.api.montoya.logging.Logging;
import burp.api.montoya.ui.UserInterface;

public class InformationPanelFactory {
private final UserInterface userInterface;
private final Logging logging;

public InformationPanelFactory(UserInterface userInterface, Logging logging) {
this.userInterface = userInterface;
this.logging = logging;
}

InformationPanel build() {
return new InformationPanel(userInterface::currentDisplayFont, logging);
}
}
Loading

0 comments on commit 29d4e7d

Please sign in to comment.