1- import { copyTextToClipboard , deferToNextLoop , safeLocalStorageSetItem } from '../utils.js' ;
1+ import {
2+ copyTextToClipboard ,
3+ deferToNextLoop ,
4+ safeLocalStorageSetItem ,
5+ copyTokenLink
6+ } from '../utils.js' ;
27import { downloadPublicKeyIfPossible } from './public-key-download.js' ;
38import { tooltipHandler } from './tooltip.js' ;
49import { tokenEditor , headerEditor , payloadEditor } from './instances.js' ;
510import {
6- copyTokenLink ,
711 getTrimmedValue ,
812 stringify ,
913 fixEditorHeight
@@ -38,6 +42,14 @@ import {
3842// passed to the event manager.
3943const eventManager = new EventManager ( ) ;
4044
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+
4153function markAsInvalid ( ) {
4254 signatureStatusElement . classList . remove ( 'valid-token' ) ;
4355 signatureStatusElement . classList . add ( 'invalid-token' ) ;
@@ -115,7 +127,7 @@ export function useDefaultToken(algorithm) {
115127 headerEditor . setValue ( stringify ( decoded . header ) ) ;
116128 payloadEditor . setValue ( stringify ( decoded . payload ) ) ;
117129
118- if ( algorithm . indexOf ( 'HS' ) === 0 ) {
130+ if ( isSharedSecretAlgorithm ( algorithm ) ) {
119131 secretInput . value = defaults . secret ;
120132 } else {
121133 publicKeyTextArea . value = defaults . publicKey ;
@@ -233,7 +245,7 @@ function encodeToken() {
233245
234246 try {
235247 const encoded = sign ( header , payload ,
236- header . alg . indexOf ( 'HS' ) === 0 ?
248+ isSharedSecretAlgorithm ( header . alg ) ?
237249 secretInput . value :
238250 privateKeyTextArea . value ,
239251 secretBase64Checkbox . checked ) ;
@@ -260,7 +272,7 @@ function decodeToken() {
260272 const decoded = decode ( jwt ) ;
261273
262274 selectAlgorithm ( decoded . header . alg ) ;
263- if ( decoded . header . alg && decoded . header . alg . indexOf ( 'HS' ) === - 1 ) {
275+ if ( isPublicKeyAlgorithm ( decoded . header . alg ) ) {
264276 downloadPublicKeyIfPossible ( decoded ) . then ( publicKey => {
265277 eventManager . withDisabledEvents ( ( ) => {
266278 publicKeyTextArea . value = publicKey ;
@@ -294,7 +306,7 @@ function verifyToken() {
294306 }
295307
296308 const publicKeyOrSecret =
297- decoded . header . alg . indexOf ( 'HS' ) === 0 ?
309+ isSharedSecretAlgorithm ( decoded . header . alg ) ?
298310 secretInput . value :
299311 publicKeyTextArea . value ;
300312
@@ -327,6 +339,17 @@ function setupTabEvents() {
327339 } ) ;
328340}
329341
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+
330353function setupEvents ( ) {
331354 // The event manager lets us enable/disable events as needed without
332355 // manually tracking them. Events that need to be disabled should be
@@ -356,7 +379,7 @@ function setupEvents() {
356379 // Human readable timestamp tooltips
357380 payloadElement . addEventListener ( 'mousemove' , tooltipHandler ) ;
358381 // Temporary (share button not ready yet)
359- signatureStatusElement . addEventListener ( 'click' , copyTokenLink ) ;
382+ signatureStatusElement . addEventListener ( 'click' , copyTokenHandler ) ;
360383
361384 setupTabEvents ( ) ;
362385}
@@ -365,6 +388,15 @@ export function setTokenEditorValue(value) {
365388 tokenEditor . setValue ( value ) ;
366389}
367390
391+ export function getTokenEditorValue ( ) {
392+ return {
393+ token : getTrimmedValue ( tokenEditor ) ,
394+ publicKey : isPublicKeyAlgorithm ( getSelectedAlgorithm ( ) ) ?
395+ publicKeyTextArea . value :
396+ undefined
397+ } ;
398+ }
399+
368400export function setupTokenEditor ( ) {
369401 setupEvents ( ) ;
370402 selectAlgorithm ( 'HS256' ) ;
0 commit comments