@@ -227,12 +227,12 @@ class Provider {
227
227
let isApiRequest = false ;
228
228
let cookies = req . signedCookies ; // Validate issuer_code to see if its a route or normal request
229
229
230
- if ( issuer . search ( 'plat' ) === - 1 ) isApiRequest = true ;
230
+ if ( issuer . indexOf ( 'plat' ) === - 1 ) isApiRequest = true ;
231
231
232
232
if ( ! isApiRequest ) {
233
233
try {
234
234
let decode = Buffer . from ( decodeURIComponent ( issuer . split ( 'plat' ) [ 1 ] ) , 'base64' ) . toString ( 'ascii' ) ;
235
- if ( ! validator . isURL ( decode ) && decode . search ( 'localhost' ) === - 1 ) isApiRequest = true ;
235
+ if ( ! validator . isURL ( decode ) && decode . indexOf ( 'localhost' ) === - 1 ) isApiRequest = true ;
236
236
} catch ( err ) {
237
237
provMainDebug ( err ) ;
238
238
isApiRequest = true ;
@@ -327,7 +327,7 @@ class Provider {
327
327
for ( let key of Object . keys ( cookies ) . sort ( ( a , b ) => b . length - a . length ) ) {
328
328
if ( key === issuer ) continue ;
329
329
330
- if ( path . search ( key ) !== - 1 ) {
330
+ if ( path . indexOf ( key ) !== - 1 ) {
331
331
isPath = cookies [ key ] ;
332
332
break ;
333
333
}
@@ -391,6 +391,7 @@ class Provider {
391
391
/**
392
392
* @description Starts listening to a given port for LTI requests and opens connection to the database.
393
393
* @param {number } port - The port the Provider should listen to.
394
+ * @returns {Promise<true| false> }
394
395
*/
395
396
396
397
@@ -425,8 +426,8 @@ class Provider {
425
426
} ) ;
426
427
if ( this . db . readyState === 0 ) await mongoose . connect ( ( 0 , _classPrivateFieldGet2 . default ) ( this , _dbConnection ) . url , ( 0 , _classPrivateFieldGet2 . default ) ( this , _dbConnection ) . options ) ;
427
428
} catch ( err ) {
428
- provMainDebug ( 'Error in MongoDb connection: ' + err ) ;
429
- throw new Error ( 'Unable to connect to database' ) ;
429
+ provMainDebug ( err ) ;
430
+ return false ;
430
431
}
431
432
/* In case no port is provided uses 3000 */
432
433
@@ -436,6 +437,22 @@ class Provider {
436
437
( 0 , _classPrivateFieldGet2 . default ) ( this , _server ) . listen ( port , 'Lti Provider tool is listening on port ' + port + '!\n\nLTI provider config: \n>Initiate login URL: ' + ( 0 , _classPrivateFieldGet2 . default ) ( this , _loginUrl ) + '\n>App Url: ' + ( 0 , _classPrivateFieldGet2 . default ) ( this , _appUrl ) + '\n>Session Timeout Url: ' + ( 0 , _classPrivateFieldGet2 . default ) ( this , _sessionTimeoutUrl ) + '\n>Invalid Token Url: ' + ( 0 , _classPrivateFieldGet2 . default ) ( this , _invalidTokenUrl ) ) ;
437
438
return true ;
438
439
}
440
+ /**
441
+ * @description Closes connection to database and stops server.
442
+ * @returns {Promise<true | false> }
443
+ */
444
+
445
+
446
+ async close ( ) {
447
+ try {
448
+ await ( 0 , _classPrivateFieldGet2 . default ) ( this , _server ) . close ( ) ;
449
+ this . db . removeAllListeners ( ) ;
450
+ await this . db . close ( ) ;
451
+ return true ;
452
+ } catch ( err ) {
453
+ return false ;
454
+ }
455
+ }
439
456
/**
440
457
* @description Sets the callback function called whenever theres a sucessfull connection, exposing a Conection object containing the id_token decoded parameters.
441
458
* @param {Function } _connectCallback - Function that is going to be called everytime a platform sucessfully connects to the provider.
@@ -444,6 +461,7 @@ class Provider {
444
461
* @param {Function } [options.sessionTimeout] - Route function executed everytime the session expires. It must in the end return a 401 status, even if redirects ((req, res, next) => {res.sendStatus(401)}).
445
462
* @param {Function } [options.invalidToken] - Route function executed everytime the system receives an invalid token or cookie. It must in the end return a 401 status, even if redirects ((req, res, next) => {res.sendStatus(401)}).
446
463
* @example .onConnect((conection, response)=>{response.send(connection)}, {secure: true})
464
+ * @returns {true }
447
465
*/
448
466
449
467
@@ -454,51 +472,64 @@ class Provider {
454
472
if ( options . invalidToken ) ( 0 , _classPrivateFieldSet2 . default ) ( this , _invalidToken , options . invalidToken ) ;
455
473
}
456
474
457
- ( 0 , _classPrivateFieldSet2 . default ) ( this , _connectCallback2 , _connectCallback ) ;
475
+ if ( _connectCallback ) {
476
+ ( 0 , _classPrivateFieldSet2 . default ) ( this , _connectCallback2 , _connectCallback ) ;
477
+ return true ;
478
+ }
479
+
480
+ throw new Error ( 'Missing callback' ) ;
458
481
}
459
482
/**
460
483
* @description Gets/Sets login Url responsible for dealing with the OIDC login flow. If no value is set "/login" is used.
461
484
* @param {string } url - Login url.
462
485
* @example provider.loginUrl('/login')
486
+ * @returns {String }
463
487
*/
464
488
465
489
466
490
loginUrl ( url ) {
467
491
if ( ! url ) return ( 0 , _classPrivateFieldGet2 . default ) ( this , _loginUrl ) ;
468
492
( 0 , _classPrivateFieldSet2 . default ) ( this , _loginUrl , url ) ;
493
+ return ( 0 , _classPrivateFieldGet2 . default ) ( this , _loginUrl ) ;
469
494
}
470
495
/**
471
496
* @description Gets/Sets main application Url that will receive the final decoded Idtoken. If no value is set "/" (root) is used.
472
497
* @param {string } url - App url.
473
498
* @example provider.appUrl('/app')
499
+ * @returns {String }
474
500
*/
475
501
476
502
477
503
appUrl ( url ) {
478
504
if ( ! url ) return ( 0 , _classPrivateFieldGet2 . default ) ( this , _appUrl ) ;
479
505
( 0 , _classPrivateFieldSet2 . default ) ( this , _appUrl , url ) ;
506
+ return ( 0 , _classPrivateFieldGet2 . default ) ( this , _appUrl ) ;
480
507
}
481
508
/**
482
509
* @description Gets/Sets session timeout Url that will be called whenever the system encounters a session timeout. If no value is set "/sessionTimeout" is used.
483
510
* @param {string } url - Session timeout url.
484
511
* @example provider.sessionTimeoutUrl('/sesstimeout')
512
+ * @returns {String }
485
513
*/
486
514
487
515
488
516
sessionTimeoutUrl ( url ) {
489
517
if ( ! url ) return ( 0 , _classPrivateFieldGet2 . default ) ( this , _sessionTimeoutUrl ) ;
490
518
( 0 , _classPrivateFieldSet2 . default ) ( this , _sessionTimeoutUrl , url ) ;
519
+ return ( 0 , _classPrivateFieldGet2 . default ) ( this , _sessionTimeoutUrl ) ;
491
520
}
492
521
/**
493
522
* @description Gets/Sets invalid token Url that will be called whenever the system encounters a invalid token or cookie. If no value is set "/invalidToken" is used.
494
523
* @param {string } url - Invalid token url.
495
524
* @example provider.invalidTokenUrl('/invtoken')
525
+ * @returns {String }
496
526
*/
497
527
498
528
499
529
invalidTokenUrl ( url ) {
500
530
if ( ! url ) return ( 0 , _classPrivateFieldGet2 . default ) ( this , _invalidTokenUrl ) ;
501
531
( 0 , _classPrivateFieldSet2 . default ) ( this , _invalidTokenUrl , url ) ;
532
+ return ( 0 , _classPrivateFieldGet2 . default ) ( this , _invalidTokenUrl ) ;
502
533
}
503
534
/**
504
535
* @description Registers a platform.
@@ -509,6 +540,7 @@ class Provider {
509
540
* @param {object } authConfig - Authentication method and key for verifying messages from the platform. {method: "RSA_KEY", key:"PUBLIC KEY..."}
510
541
* @param {String } authConfig.method - Method of authorization "RSA_KEY" or "JWK_KEY" or "JWK_SET".
511
542
* @param {String } authConfig.key - Either the RSA public key provided by the platform, or the JWK key, or the JWK keyset address.
543
+ * @returns {Promise<Platform|false> }
512
544
*/
513
545
514
546
@@ -552,6 +584,7 @@ class Provider {
552
584
* @description Gets a platform.
553
585
* @param {String } url - Platform url.
554
586
* @param {String } [ENCRYPTIONKEY] - Encryption key. THIS PARAMETER IS ONLY IN A FEW SPECIFIC CALLS, DO NOT USE IN YOUR APPLICATION.
587
+ * @returns {Promise<Platform | false> }
555
588
*/
556
589
557
590
@@ -582,6 +615,7 @@ class Provider {
582
615
/**
583
616
* @description Deletes a platform.
584
617
* @param {string } url - Platform url.
618
+ * @returns {Promise<true | false> }
585
619
*/
586
620
587
621
@@ -593,6 +627,7 @@ class Provider {
593
627
}
594
628
/**
595
629
* @description Gets all platforms.
630
+ * @returns {Promise<Array<Platform>| false> }
596
631
*/
597
632
598
633
@@ -639,14 +674,17 @@ class Provider {
639
674
let lineitemRes = await got . get ( lineitemsEndpoint , {
640
675
headers : {
641
676
Authorization : tokenRes . token_type + ' ' + tokenRes . access_token
642
- }
677
+ } ,
678
+ body : JSON . stringify ( {
679
+ request : 'lineitems'
680
+ } )
643
681
} ) ;
644
682
let resourceId = idtoken . platformContext . resource ;
645
683
let lineitem = find ( JSON . parse ( lineitemRes . body ) , [ 'resourceLinkId' , resourceId . id ] ) ;
646
684
let lineitemUrl = lineitem . id ;
647
685
let scoreUrl = lineitemUrl + '/scores' ;
648
686
649
- if ( lineitemUrl . split ( '\ ?') !== - 1 ) {
687
+ if ( lineitemUrl . indexOf ( ' ?') !== - 1 ) {
650
688
let query = lineitemUrl . split ( '\?' ) [ 1 ] ;
651
689
let url = lineitemUrl . split ( '\?' ) [ 0 ] ;
652
690
scoreUrl = url + '/scores?' + query ;
@@ -657,15 +695,20 @@ class Provider {
657
695
message . timestamp = new Date ( Date . now ( ) ) . toISOString ( ) ;
658
696
message . scoreMaximum = lineitem . scoreMaximum ;
659
697
provMainDebug ( message ) ;
660
- await got . post ( scoreUrl , {
698
+ let finalRes = await got . post ( scoreUrl , {
661
699
headers : {
662
700
Authorization : tokenRes . token_type + ' ' + tokenRes . access_token ,
663
701
'Content-Type' : 'application/vnd.ims.lis.v1.score+json'
664
702
} ,
665
703
body : JSON . stringify ( message )
666
704
} ) ;
667
- provMainDebug ( 'Message successfully sent' ) ;
668
- return true ;
705
+
706
+ if ( finalRes . statusCode === 200 ) {
707
+ provMainDebug ( 'Message successfully sent' ) ;
708
+ return true ;
709
+ }
710
+
711
+ return false ;
669
712
} catch ( err ) {
670
713
provMainDebug ( err ) ;
671
714
return false ;
@@ -675,7 +718,7 @@ class Provider {
675
718
* @description Redirect to a new location and sets it's cookie if the location represents a separate resource
676
719
* @param {Object } res - Express response object
677
720
* @param {String } path - Redirect path
678
- * @param {Boolea } [isNewResource = false] - If true creates new resource and its cookie
721
+ * @param {Boolean } [isNewResource = false] - If true creates new resource and its cookie
679
722
* @example lti.generatePathCookie(response, '/path', true)
680
723
*/
681
724
0 commit comments