99 stringify ,
1010 fixEditorHeight ,
1111 getSelectedAlgorithm ,
12- disableUnsupportedAlgorithms
12+ disableUnsupportedAlgorithms ,
13+ getSafeTokenInfo
1314} from './utils.js' ;
1415import { sign , verify , decode } from './jwt.js' ;
1516import EventManager from './event-manager.js' ;
@@ -19,6 +20,7 @@ import {
1920 minSecretLengthCheck ,
2021 setupSecretLengthTooltip
2122} from './secret-length-tooltip.js' ;
23+ import * as metrics from '../metrics.js' ;
2224import {
2325 algorithmSelect ,
2426 signatureStatusElement ,
@@ -46,6 +48,17 @@ import log from 'loglevel';
4648// passed to the event manager.
4749const eventManager = new EventManager ( ) ;
4850
51+ function trackToken ( jwt , operation ) {
52+ const tokenInfo = getSafeTokenInfo ( jwt ) ;
53+
54+ metric . track ( 'editor-jwt-tracked' , {
55+ operation : operation ,
56+ tokenInfo : tokenInfo
57+ } ) ;
58+
59+ return tokenInfo . hash ;
60+ }
61+
4962function isSharedSecretAlgorithm ( algorithm ) {
5063 return algorithm && algorithm . indexOf ( 'HS' ) === 0 ;
5164}
@@ -169,6 +182,8 @@ function setAlgorithmInHeader(algorithm) {
169182function algorithmChangeHandler ( ) {
170183 const algorithm = getSelectedAlgorithm ( ) ;
171184
185+ metrics . track ( 'editor-algorithm-selected' , { algorithm : algorithm } ) ;
186+
172187 displaySecretOrKeys ( algorithm ) ;
173188
174189 if ( isDefaultToken ( getTrimmedValue ( tokenEditor ) ) ) {
@@ -223,13 +238,15 @@ function encodeToken() {
223238 sign ( header , payload , key , secretBase64Checkbox . checked ) . then ( encoded => {
224239 eventManager . withDisabledEvents ( ( ) => {
225240 tokenEditor . setValue ( encoded ) ;
241+ trackToken ( encoded , 'encode' ) ;
226242 } ) ;
227243 } ) . catch ( e => {
228244 eventManager . withDisabledEvents ( ( ) => {
229245 log . warn ( 'Failed to sign/encode token: ' , e ) ;
230246 markAsInvalid ( ) ;
231247 tokenEditor . setValue ( '' ) ;
232- } )
248+ } ) ;
249+ metrics . track ( 'editor-encoding-error' ) ;
233250 } ) . finally ( ( ) => {
234251 verifyToken ( ) ;
235252 } ) ;
@@ -244,9 +261,15 @@ function decodeToken() {
244261 const jwt = getTrimmedValue ( tokenEditor ) ;
245262 const decoded = decode ( jwt ) ;
246263
264+ const tokenHash = trackToken ( jwt , 'decode' ) ;
265+
247266 selectAlgorithm ( decoded . header . alg ) ;
248267 if ( isPublicKeyAlgorithm ( decoded . header . alg ) ) {
249268 downloadPublicKeyIfPossible ( decoded ) . then ( publicKey => {
269+ metrics . track ( 'editor-jwt-public-key-downloaded' , {
270+ tokenHash : tokenHash
271+ } ) ;
272+
250273 eventManager . withDisabledEvents ( ( ) => {
251274 publicKeyTextArea . value = publicKey ;
252275 verifyToken ( ) ;
@@ -259,11 +282,20 @@ function decodeToken() {
259282
260283 if ( decoded . errors ) {
261284 markAsInvalidWithElement ( editorElement , false ) ;
285+ metrics . track ( 'editor-jwt-invalid' , {
286+ reason : `partial decode` ,
287+ tokenHash : tokenHash
288+ } ) ;
262289 } else {
263290 verifyToken ( ) ;
264291 }
265292 } catch ( e ) {
266293 log . warn ( 'Failed to decode token: ' , e ) ;
294+
295+ metrics . track ( 'editor-jwt-invalid' , {
296+ reason : `failed to decode token` ,
297+ tokenHash : trackToken ( jwt )
298+ } ) ;
267299 }
268300 } ) ;
269301}
@@ -272,8 +304,14 @@ function verifyToken() {
272304 const jwt = getTrimmedValue ( tokenEditor ) ;
273305 const decoded = decode ( jwt ) ;
274306
307+ const tokenHash = trackToken ( jwt , 'verify' ) ;
308+
275309 if ( ! decoded . header . alg || decoded . header . alg === 'none' ) {
276310 markAsInvalid ( ) ;
311+ metrics . track ( 'editor-jwt-invalid' , {
312+ reason : `header.alg value is ${ decoded . header . alg } ` ,
313+ tokenHash : tokenHash
314+ } ) ;
277315 return ;
278316 }
279317
@@ -285,8 +323,15 @@ function verifyToken() {
285323 verify ( jwt , publicKeyOrSecret , secretBase64Checkbox . checked ) . then ( valid => {
286324 if ( valid ) {
287325 markAsValid ( ) ;
326+ metrics . track ( 'editor-jwt-verified' , {
327+ tokenHash : tokenHash
328+ } ) ;
288329 } else {
289330 markAsInvalid ( ) ;
331+ metrics . track ( 'editor-jwt-invalid' , {
332+ reason : 'invalid signature' ,
333+ tokenHash : tokenHash
334+ } ) ;
290335 }
291336 } ) ;
292337}
0 commit comments