Skip to content

Instantly share code, notes, and snippets.

@luke10x
Created June 1, 2023 01:55
Show Gist options
  • Save luke10x/4bdb486b176dadaa0c8a86deadd90597 to your computer and use it in GitHub Desktop.
Save luke10x/4bdb486b176dadaa0c8a86deadd90597 to your computer and use it in GitHub Desktop.
import org.json.JSONObject;
import javax.swing.*;
import java.awt.*;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class SwingJwtHelper {
public static void main(String[] args) {
SwingUtilities.invokeLater(SwingAppUI::createAndShowGUI);
}
private static class SwingAppUI {
private static JFrame frame;
private static JTextArea encodedTokenTextArea;
private static JTextArea headerTextArea;
private static JTextArea payloadTextArea;
private static JTextField publicKeyTextField;
private static void createAndShowGUI() {
// Create the main frame
frame = new JFrame("JWT Decoder");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.setLayout(new BorderLayout());
// Create the encoded token text area
encodedTokenTextArea = new JTextArea();
encodedTokenTextArea.setLineWrap(true);
encodedTokenTextArea.getDocument().addDocumentListener(new TokenChangeListener());
JScrollPane encodedTokenScrollPane = new JScrollPane(encodedTokenTextArea);
// Create the header text area
headerTextArea = new JTextArea();
headerTextArea.setEditable(false);
JScrollPane headerScrollPane = new JScrollPane(headerTextArea);
// Create the payload text area
payloadTextArea = new JTextArea();
payloadTextArea.setEditable(false);
JScrollPane payloadScrollPane = new JScrollPane(payloadTextArea);
// Create the public and private key text fields
publicKeyTextField = new JTextField();
// Create the Verify Signature button
JButton verifyButton = new JButton("Verify Signature");
// Create the left panel
JPanel leftPanel = new JPanel(new BorderLayout());
leftPanel.add(new JLabel("Encoded Token"), BorderLayout.NORTH);
leftPanel.add(encodedTokenScrollPane, BorderLayout.CENTER);
// Create the right panel
JPanel rightPanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(5, 5, 5, 5);
// Add Header section
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.WEST;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0.0;
rightPanel.add(new JLabel("Header"), c);
c.gridy = 1;
c.anchor = GridBagConstraints.EAST;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.5;
c.weighty = 1.0;
rightPanel.add(headerScrollPane, c);
// Add Payload section
c.gridy = 2;
c.weighty = 0.0;
rightPanel.add(new JLabel("Payload"), c);
c.gridy = 3;
c.weighty = 1.0;
rightPanel.add(payloadScrollPane, c);
// Add Verify Signature section
c.gridy = 4;
c.weighty = 0.0;
rightPanel.add(new JLabel("Verify Signature"), c);
c.gridy = 5;
c.weightx = 1.0;
c.weighty = 0.0;
c.fill = GridBagConstraints.HORIZONTAL;
rightPanel.add(new JLabel("Public Key"), c);
c.gridy = 6;
rightPanel.add(publicKeyTextField, c);
c.gridy = 7;
rightPanel.add(new JLabel("Private Key"), c);
c.gridy = 8;
rightPanel.add(verifyButton, c);
// Add components to the main frame
frame.add(leftPanel, BorderLayout.CENTER);
frame.add(rightPanel, BorderLayout.EAST);
// Display the main frame
frame.setVisible(true);
}
private static class TokenChangeListener implements javax.swing.event.DocumentListener {
@Override
public void insertUpdate(javax.swing.event.DocumentEvent e) {
processTokenChange();
}
@Override
public void removeUpdate(javax.swing.event.DocumentEvent e) {
processTokenChange();
}
@Override
public void changedUpdate(javax.swing.event.DocumentEvent e) {
processTokenChange();
}
private static void processTokenChange() {
String encodedToken = encodedTokenTextArea.getText();
// Reset header and payload text areas
headerTextArea.setText("");
payloadTextArea.setText("");
try {
String[] tokenParts = encodedToken.split("\\.");
if (tokenParts.length == 3) {
String header = decodeBase64String(tokenParts[0]);
String payload = decodeBase64String(tokenParts[1]);
JSONObject headerJson = new JSONObject(header);
JSONObject payloadJson = new JSONObject(payload);
String algorithm = headerJson.optString("alg");
if ("RS256".equals(algorithm)) {
headerTextArea.setText(headerJson.toString(2));
payloadTextArea.setText(payloadJson.toString(2));
} else {
headerTextArea.setText("Algorithm is not RS256");
}
} else {
headerTextArea.setText("Invalid token format");
}
} catch (Exception e) {
headerTextArea.setText("Error decoding token");
}
}
private static String decodeBase64String(String base64String) {
byte[] decodedBytes = Base64.getUrlDecoder().decode(base64String);
return new String(decodedBytes, StandardCharsets.UTF_8);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment