|
1 | 1 | import { copyTextToClipboard } from '../utils.js'; |
| 2 | +import { deferToNextLoop } from '../../utils.js'; |
2 | 3 | import { downloadPublicKeyIfPossible } from '../../public-key-download.js'; |
3 | 4 | import { tooltipHandler } from './tooltip.js'; |
4 | 5 | import { tokenEditor, headerEditor, payloadEditor } from './instances.js'; |
@@ -73,7 +74,7 @@ function displaySecretOrKeys(algorithm) { |
73 | 74 | keyEditorContainer.style.display = ''; |
74 | 75 | } |
75 | 76 |
|
76 | | - fixEditorHeight(); |
| 77 | + deferToNextLoop(fixEditorHeight); |
77 | 78 | } |
78 | 79 |
|
79 | 80 | function selectAlgorithm(algorithm) { |
@@ -175,6 +176,8 @@ function markAsInvalidWithElement(element, clearTokenEditor = true) { |
175 | 176 | } |
176 | 177 |
|
177 | 178 | function encodeToken() { |
| 179 | + deferToNextLoop(fixEditorHeight); |
| 180 | + |
178 | 181 | eventManager.withDisabledEvents(() => { |
179 | 182 | let header; |
180 | 183 | try { |
@@ -218,23 +221,31 @@ function encodeToken() { |
218 | 221 | } |
219 | 222 |
|
220 | 223 | function decodeToken() { |
| 224 | + deferToNextLoop(fixEditorHeight); |
| 225 | + |
221 | 226 | eventManager.withDisabledEvents(() => { |
222 | 227 | try { |
223 | 228 | const jwt = getTrimmedValue(tokenEditor); |
224 | 229 | const decoded = decode(jwt); |
225 | 230 |
|
226 | 231 | selectAlgorithm(decoded.header.alg); |
227 | | - downloadPublicKeyIfPossible(decoded).then(publicKey => { |
228 | | - eventManager.withDisabledEvents(() => { |
229 | | - publicKeyTextArea.value = publicKey; |
230 | | - verifyToken(); |
| 232 | + if(decoded.header.alg && decoded.header.alg.indexOf('HS') === -1) { |
| 233 | + downloadPublicKeyIfPossible(decoded).then(publicKey => { |
| 234 | + eventManager.withDisabledEvents(() => { |
| 235 | + publicKeyTextArea.value = publicKey; |
| 236 | + verifyToken(); |
| 237 | + }); |
231 | 238 | }); |
232 | | - }); |
| 239 | + } |
233 | 240 |
|
234 | 241 | headerEditor.setValue(stringify(decoded.header)); |
235 | 242 | payloadEditor.setValue(stringify(decoded.payload)); |
236 | 243 |
|
237 | | - verifyToken(); |
| 244 | + if(decoded.errors) { |
| 245 | + markAsInvalidWithElement(editorElement, false); |
| 246 | + } else { |
| 247 | + verifyToken(); |
| 248 | + } |
238 | 249 | } catch(e) { |
239 | 250 | console.error('Failed to decode token: ', e); |
240 | 251 | } |
|
0 commit comments