1
- import { copyTextToClipboard , deferToNextLoop , safeLocalStorageSetItem } from '../utils.js' ;
1
+ import {
2
+ copyTextToClipboard ,
3
+ deferToNextLoop ,
4
+ safeLocalStorageSetItem ,
5
+ copyTokenLink
6
+ } from '../utils.js' ;
2
7
import { downloadPublicKeyIfPossible } from './public-key-download.js' ;
3
8
import { tooltipHandler } from './tooltip.js' ;
4
9
import { tokenEditor , headerEditor , payloadEditor } from './instances.js' ;
5
10
import {
6
- copyTokenLink ,
7
11
getTrimmedValue ,
8
12
stringify ,
9
13
fixEditorHeight
@@ -38,6 +42,14 @@ import {
38
42
// passed to the event manager.
39
43
const eventManager = new EventManager ( ) ;
40
44
45
+ function isSharedSecretAlgorithm ( algorithm ) {
46
+ return algorithm && algorithm . indexOf ( 'HS' ) === 0 ;
47
+ }
48
+
49
+ function isPublicKeyAlgorithm ( algorithm ) {
50
+ return algorithm && algorithm . indexOf ( 'HS' ) === - 1 ;
51
+ }
52
+
41
53
function markAsInvalid ( ) {
42
54
signatureStatusElement . classList . remove ( 'valid-token' ) ;
43
55
signatureStatusElement . classList . add ( 'invalid-token' ) ;
@@ -115,7 +127,7 @@ export function useDefaultToken(algorithm) {
115
127
headerEditor . setValue ( stringify ( decoded . header ) ) ;
116
128
payloadEditor . setValue ( stringify ( decoded . payload ) ) ;
117
129
118
- if ( algorithm . indexOf ( 'HS' ) === 0 ) {
130
+ if ( isSharedSecretAlgorithm ( algorithm ) ) {
119
131
secretInput . value = defaults . secret ;
120
132
} else {
121
133
publicKeyTextArea . value = defaults . publicKey ;
@@ -233,7 +245,7 @@ function encodeToken() {
233
245
234
246
try {
235
247
const encoded = sign ( header , payload ,
236
- header . alg . indexOf ( 'HS' ) === 0 ?
248
+ isSharedSecretAlgorithm ( header . alg ) ?
237
249
secretInput . value :
238
250
privateKeyTextArea . value ,
239
251
secretBase64Checkbox . checked ) ;
@@ -260,7 +272,7 @@ function decodeToken() {
260
272
const decoded = decode ( jwt ) ;
261
273
262
274
selectAlgorithm ( decoded . header . alg ) ;
263
- if ( decoded . header . alg && decoded . header . alg . indexOf ( 'HS' ) === - 1 ) {
275
+ if ( isPublicKeyAlgorithm ( decoded . header . alg ) ) {
264
276
downloadPublicKeyIfPossible ( decoded ) . then ( publicKey => {
265
277
eventManager . withDisabledEvents ( ( ) => {
266
278
publicKeyTextArea . value = publicKey ;
@@ -294,7 +306,7 @@ function verifyToken() {
294
306
}
295
307
296
308
const publicKeyOrSecret =
297
- decoded . header . alg . indexOf ( 'HS' ) === 0 ?
309
+ isSharedSecretAlgorithm ( decoded . header . alg ) ?
298
310
secretInput . value :
299
311
publicKeyTextArea . value ;
300
312
@@ -327,6 +339,17 @@ function setupTabEvents() {
327
339
} ) ;
328
340
}
329
341
342
+ function copyTokenHandler ( event ) {
343
+ event . preventDefault ( ) ;
344
+
345
+ const token = getTrimmedValue ( tokenEditor ) ;
346
+ const publicKey = isPublicKeyAlgorithm ( getSelectedAlgorithm ( ) ) ?
347
+ publicKeyTextArea . value :
348
+ null ;
349
+
350
+ copyTokenLink ( token , publicKey ) ;
351
+ }
352
+
330
353
function setupEvents ( ) {
331
354
// The event manager lets us enable/disable events as needed without
332
355
// manually tracking them. Events that need to be disabled should be
@@ -356,7 +379,7 @@ function setupEvents() {
356
379
// Human readable timestamp tooltips
357
380
payloadElement . addEventListener ( 'mousemove' , tooltipHandler ) ;
358
381
// Temporary (share button not ready yet)
359
- signatureStatusElement . addEventListener ( 'click' , copyTokenLink ) ;
382
+ signatureStatusElement . addEventListener ( 'click' , copyTokenHandler ) ;
360
383
361
384
setupTabEvents ( ) ;
362
385
}
@@ -365,6 +388,15 @@ export function setTokenEditorValue(value) {
365
388
tokenEditor . setValue ( value ) ;
366
389
}
367
390
391
+ export function getTokenEditorValue ( ) {
392
+ return {
393
+ token : getTrimmedValue ( tokenEditor ) ,
394
+ publicKey : isPublicKeyAlgorithm ( getSelectedAlgorithm ( ) ) ?
395
+ publicKeyTextArea . value :
396
+ undefined
397
+ } ;
398
+ }
399
+
368
400
export function setupTokenEditor ( ) {
369
401
setupEvents ( ) ;
370
402
selectAlgorithm ( 'HS256' ) ;
0 commit comments