@@ -2,9 +2,13 @@ const chai = require('chai');
22const chaiAsPromised = require ( 'chai-as-promised' ) ;
33const chaiArrays = require ( 'chai-arrays' ) ;
44
5+ const express = require ( 'express' ) ;
6+ const jsrsasign = require ( 'jsrsasign' ) ;
7+
58const utils = require ( './utils.js' ) ;
69const tokens = require ( './tokens.js' ) ;
710const defaultTokens = require ( './default-tokens.js' ) ;
11+ const jwks = require ( './jwks.json' ) ;
812
913const isVisible = utils . isVisible ;
1014
@@ -454,16 +458,135 @@ describe('Editor', function() {
454458 } ) ;
455459
456460 describe ( 'Should download public-keys when possible' , function ( ) {
457- before ( function ( ) {
461+ before ( async function ( ) {
462+ this . app = express ( ) ;
463+
464+ this . app . get ( '/.well-known/jwks.json' , ( req , res ) => {
465+ res . set ( 'Access-Control-Allow-Origin' , '*' ) ;
466+ res . json ( jwks ) ;
467+ } ) ;
468+
469+ this . server = this . app . listen ( 3000 ) ;
458470
471+ await this . page . select ( '#algorithm-select' , 'RS256' ) ;
472+ } ) ;
473+
474+ beforeEach ( async function ( ) {
475+ const publicKeyInput = await this . page . $ ( 'textarea[name="public-key"]' ) ;
476+ await publicKeyInput . click ( ) ;
477+ await this . page . keyboard . down ( 'ControlLeft' ) ;
478+ await this . page . keyboard . press ( 'KeyA' ) ;
479+ await this . page . keyboard . up ( 'ControlLeft' ) ;
480+ await this . page . keyboard . press ( 'Delete' ) ;
459481 } ) ;
460482
461483 after ( function ( ) {
484+ this . server . close ( ) ;
485+ } ) ;
486+
487+ it ( 'iss URL + .well-known' , async function ( ) {
488+ this . timeout ( 20000 ) ;
489+
490+ const token = jsrsasign . jws . JWS . sign ( null , JSON . stringify ( {
491+ alg : 'RS256' ,
492+ typ : 'JWT' ,
493+ kid : '1'
494+ } ) , JSON . stringify ( {
495+ sub : 'test' ,
496+ iss : 'http://localhost:3000/'
497+ } ) , defaultTokens . rs256 . privateKey ) ;
498+
499+ await this . page . click ( '.js-input' ) ;
500+ await this . page . keyboard . down ( 'ControlLeft' ) ;
501+ await this . page . keyboard . press ( 'KeyA' ) ;
502+ await this . page . keyboard . up ( 'ControlLeft' ) ;
503+ await this . page . keyboard . type ( token , {
504+ delay : 5
505+ } ) ;
462506
463- } ) ;
507+ await this . page . waitFor ( 2000 ) ;
508+
509+ const publicKey = await this . page . $eval ( 'textarea[name="public-key"]' ,
510+ publicKeyElement => publicKeyElement . value ) ;
511+
512+ expect ( publicKey ) . to . include ( jwks . keys [ 0 ] . x5c [ 0 ] ) ;
513+
514+ const valid = await this . page . $eval ( '.validation-status' , status => {
515+ return status . classList . contains ( 'valid-token' ) &&
516+ status . textContent . indexOf ( 'verified' ) !== - 1 ;
517+ } ) ;
518+
519+ expect ( valid ) . to . be . true ;
520+ } ) ;
521+
522+ it ( 'jku' , async function ( ) {
523+ this . timeout ( 20000 ) ;
524+
525+ const token = jsrsasign . jws . JWS . sign ( null , JSON . stringify ( {
526+ alg : 'RS256' ,
527+ typ : 'JWT' ,
528+ kid : '1' ,
529+ jku : 'http://localhost:3000/.well-known/jwks.json'
530+ } ) , JSON . stringify ( {
531+ sub : 'test'
532+ } ) , defaultTokens . rs256 . privateKey ) ;
533+
534+ await this . page . click ( '.js-input' ) ;
535+ await this . page . keyboard . down ( 'ControlLeft' ) ;
536+ await this . page . keyboard . press ( 'KeyA' ) ;
537+ await this . page . keyboard . up ( 'ControlLeft' ) ;
538+ await this . page . keyboard . type ( token , {
539+ delay : 5
540+ } ) ;
464541
465- it ( 'iss URL + .well-known' ) ;
466- it ( 'jku' ) ;
542+ await this . page . waitFor ( 2000 ) ;
543+
544+ const publicKey = await this . page . $eval ( 'textarea[name="public-key"]' ,
545+ publicKeyElement => publicKeyElement . value ) ;
546+
547+ expect ( publicKey ) . to . include ( jwks . keys [ 0 ] . x5c [ 0 ] ) ;
548+
549+ const valid = await this . page . $eval ( '.validation-status' , status => {
550+ return status . classList . contains ( 'valid-token' ) &&
551+ status . textContent . indexOf ( 'verified' ) !== - 1 ;
552+ } ) ;
553+
554+ expect ( valid ) . to . be . true ;
555+ } ) ;
556+
557+ it ( 'x5c' , async function ( ) {
558+ this . timeout ( 35000 ) ;
559+
560+ const token = jsrsasign . jws . JWS . sign ( null , JSON . stringify ( {
561+ alg : 'RS256' ,
562+ typ : 'JWT' ,
563+ x5c : jwks . keys [ 0 ] . x5c [ 0 ]
564+ } ) , JSON . stringify ( {
565+ sub : 'test'
566+ } ) , defaultTokens . rs256 . privateKey ) ;
567+
568+ await this . page . click ( '.js-input' ) ;
569+ await this . page . keyboard . down ( 'ControlLeft' ) ;
570+ await this . page . keyboard . press ( 'KeyA' ) ;
571+ await this . page . keyboard . up ( 'ControlLeft' ) ;
572+ await this . page . keyboard . type ( token , {
573+ delay : 5
574+ } ) ;
575+
576+ await this . page . waitFor ( 2000 ) ;
577+
578+ const publicKey = await this . page . $eval ( 'textarea[name="public-key"]' ,
579+ publicKeyElement => publicKeyElement . value ) ;
580+
581+ expect ( publicKey ) . to . include ( jwks . keys [ 0 ] . x5c [ 0 ] ) ;
582+
583+ const valid = await this . page . $eval ( '.validation-status' , status => {
584+ return status . classList . contains ( 'valid-token' ) &&
585+ status . textContent . indexOf ( 'verified' ) !== - 1 ;
586+ } ) ;
587+
588+ expect ( valid ) . to . be . true ;
589+ } ) ;
467590 } ) ;
468591
469592 it ( 'Clears the token when the header is edited and there ' +
0 commit comments