-
Notifications
You must be signed in to change notification settings - Fork 12
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 #83 from DolphFlynn/import
Import
- Loading branch information
Showing
9 changed files
with
487 additions
and
6 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
44 changes: 44 additions & 0 deletions
44
src/main/java/com/blackberry/jwteditor/model/keys/JWKSetParser.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,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(); | ||
} | ||
} |
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
117 changes: 117 additions & 0 deletions
117
src/main/java/com/blackberry/jwteditor/view/dialog/jwks/JWKSImportDialog.form
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,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> |
147 changes: 147 additions & 0 deletions
147
src/main/java/com/blackberry/jwteditor/view/dialog/jwks/JWKSImportDialog.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,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(); | ||
} | ||
} |
Oops, something went wrong.