Skip to content

Commit

Permalink
Merge pull request #83 from DolphFlynn/import
Browse files Browse the repository at this point in the history
Import
  • Loading branch information
DolphFlynn authored Jan 3, 2025
2 parents 6be33cd + 1eb22f3 commit 8aeb8a0
Show file tree
Hide file tree
Showing 9 changed files with 487 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Additionally it facilitates several well-known attacks against JWT implementatio

**Unreleased**
- Add ability to test for HMAC signatures using [weak secrets](https://github.com/wallarm/jwt-secrets).
- Add import capability for JWK data.
- Remember last used key within Signing dialog.

**2.4 2024-12-24**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Author : Dolph Flynn
Copyright 2025 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.model.keys;

import com.blackberry.jwteditor.exceptions.UnsupportedKeyException;
import com.nimbusds.jose.jwk.JWKSet;

import java.text.ParseException;
import java.util.List;
import java.util.Objects;

public class JWKSetParser {
public List<Key> parse(String json) throws ParseException {
return JWKSet.parse(json)
.getKeys()
.stream()
.map(jwk -> {
try {
return JWKKeyFactory.from(jwk);
} catch (UnsupportedKeyException e) {
return null;
}
})
.filter(Objects::nonNull)
.map(key -> (Key) key)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
import com.blackberry.jwteditor.model.persistence.KeysModelPersistence;
import com.blackberry.jwteditor.utils.PEMUtils;
import com.blackberry.jwteditor.utils.Utils;
import com.blackberry.jwteditor.view.dialog.jwks.JWKSImportDialog;
import com.blackberry.jwteditor.view.dialog.keys.KeyDialog;
import com.blackberry.jwteditor.view.dialog.keys.KeysDialogFactory;
import com.blackberry.jwteditor.view.keys.KeysView;
import com.blackberry.jwteditor.view.rsta.RstaFactory;
import com.nimbusds.jose.jwk.JWK;

import javax.swing.*;
Expand All @@ -44,14 +46,17 @@ public class KeysPresenter {
private final KeysModel model;
private final KeysView view;
private final KeysDialogFactory keysDialogFactory;
private final RstaFactory rstaFactory;

public KeysPresenter(KeysView view,
KeysModelPersistence keysModelPersistence,
KeysModel keysModel,
KeysDialogFactory keysDialogFactory) {
KeysDialogFactory keysDialogFactory,
RstaFactory rstaFactory) {
this.view = view;
this.model = keysModel;
this.keysDialogFactory = keysDialogFactory;
this.rstaFactory = rstaFactory;

model.addKeyModelListener(new KeysModelListener() {
@Override
Expand Down Expand Up @@ -142,6 +147,13 @@ public void onButtonNewPasswordClick() {
onButtonNewClicked(keysDialogFactory.passwordDialog());
}

public void onButtonImportJWKSet() {
JWKSImportDialog dialog = new JWKSImportDialog(view.getParent(), model, rstaFactory);
dialog.display();

dialog.getKeys().forEach(model::addKey);
}

/**
* Can the key at a position in the model be copied as a JWK with private key
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.blackberry.jwteditor.view.dialog.jwks.JWKSImportDialog">
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<xy x="48" y="54" width="624" height="629"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="5d53" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="2" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="600" height="450"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="94766" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<hspacer id="98af6">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<grid id="9538f" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="true" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="e7465" class="javax.swing.JButton" binding="buttonImport">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<enabled value="false"/>
<text resource-bundle="strings" key="import"/>
</properties>
</component>
<component id="5723f" class="javax.swing.JButton" binding="buttonCancel">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="strings" key="cancel"/>
</properties>
</component>
</children>
</grid>
<component id="8bc1f" class="javax.swing.JLabel" binding="labelError">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<foreground color="-65536"/>
<text value=" "/>
</properties>
</component>
</children>
</grid>
<grid id="e3588" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="10">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="500" height="260"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="d4bef" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="line" title-resource-bundle="strings" title-key="keys">
<color color="-4473925"/>
</border>
<children>
<scrollpane id="f7d11">
<constraints>
<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/>
<border type="none"/>
<children>
<component id="99b6d" class="org.fife.ui.rsyntaxtextarea.RSyntaxTextArea" binding="textAreaKeysJson" custom-create="true">
<constraints/>
<properties>
<currentLineHighlightColor color="-1"/>
<showMatchedBracketPopup value="false"/>
<syntaxEditingStyle value="text/json"/>
</properties>
</component>
</children>
</scrollpane>
</children>
</grid>
</children>
</grid>
</children>
</grid>
</children>
</grid>
</form>
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
Author : Dolph Flynn
Copyright 2025 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.dialog.jwks;

import com.blackberry.jwteditor.model.keys.JWKSetParser;
import com.blackberry.jwteditor.model.keys.Key;
import com.blackberry.jwteditor.model.keys.KeysModel;
import com.blackberry.jwteditor.utils.Utils;
import com.blackberry.jwteditor.view.rsta.RstaFactory;
import com.blackberry.jwteditor.view.utils.DebouncingDocumentAdapter;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;

import javax.swing.*;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.ParseException;
import java.util.LinkedList;
import java.util.List;

import static java.awt.Color.PINK;
import static java.awt.Dialog.ModalityType.APPLICATION_MODAL;
import static java.awt.event.KeyEvent.VK_ESCAPE;
import static javax.swing.JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT;
import static javax.swing.JOptionPane.*;

public class JWKSImportDialog extends JDialog {
private final KeysModel keysModel;
private final RstaFactory rstaFactory;
private final List<Key> keys;
private final Color textAreaKeyInitialBackgroundColor;
private final Color textAreaKeyInitialCurrentLineHighlightColor;

private JPanel contentPane;
private JButton buttonImport;
private JButton buttonCancel;
private RSyntaxTextArea textAreaKeysJson;
private JLabel labelError;

public JWKSImportDialog(Window parent, KeysModel keysModel, RstaFactory rstaFactory) {
super(parent, "Import JWKs", APPLICATION_MODAL);

this.keysModel = keysModel;
this.rstaFactory = rstaFactory;
this.keys = new LinkedList<>();

setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
onCancel();
}
});

setContentPane(contentPane);
getRootPane().setDefaultButton(buttonImport);

buttonImport.addActionListener(e -> onImport());
buttonCancel.addActionListener(e -> onCancel());

contentPane.registerKeyboardAction(
e -> onCancel(),
KeyStroke.getKeyStroke(VK_ESCAPE, 0),
WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
);

DocumentListener documentListener = new DebouncingDocumentAdapter(e -> parseJson());
textAreaKeysJson.getDocument().addDocumentListener(documentListener);

textAreaKeyInitialBackgroundColor = textAreaKeysJson.getBackground();
textAreaKeyInitialCurrentLineHighlightColor = textAreaKeysJson.getCurrentLineHighlightColor();
}

private void parseJson() {
textAreaKeysJson.setBackground(textAreaKeyInitialBackgroundColor);
textAreaKeysJson.setCurrentLineHighlightColor(textAreaKeyInitialCurrentLineHighlightColor);
buttonImport.setEnabled(false);
labelError.setText(" ");
keys.clear();

if (!textAreaKeysJson.getText().isEmpty()) {
try {
List<Key> parsedKeys = new JWKSetParser().parse(textAreaKeysJson.getText());
keys.addAll(parsedKeys);
buttonImport.setEnabled(true);
} catch (ParseException e) {
textAreaKeysJson.setBackground(PINK);
textAreaKeysJson.setCurrentLineHighlightColor(PINK);
labelError.setText(Utils.getResourceString("error_invalid_keys"));
}
}
}

public List<Key> getKeys() {
return keys;
}

public void display() {
pack();
setLocationRelativeTo(getOwner());
setVisible(true);
}

void onImport() {
boolean keyIdClash = keys.stream()
.filter(key -> key.getID() != null)
.anyMatch(key -> keysModel.keyExists(key.getID()));

// Handle overwrites if a key already exists with the same kid
if (keyIdClash) {
if (showConfirmDialog(
this,
Utils.getResourceString("keys_confirm_overwrite"),
Utils.getResourceString("keys_confirm_overwrite_title"),
OK_CANCEL_OPTION) != OK_OPTION) {
keys.clear();
}
}

dispose();
}

private void onCancel() {
keys.clear();
dispose();
}

private void createUIComponents() {
textAreaKeysJson = rstaFactory.buildDefaultTextArea();
}
}
Loading

0 comments on commit 8aeb8a0

Please sign in to comment.