@@ -265,7 +265,7 @@ const ONLINE_ONLY_ENDPOINTS = [
265
265
266
266
// block offline users from accessing some unaudited CouchDB endpoints
267
267
ONLINE_ONLY_ENDPOINTS . forEach ( url =>
268
- app . all ( routePrefix + url , authorization . offlineUserFirewall )
268
+ app . all ( routePrefix + url , authorization . handleAuthErrors , authorization . offlineUserFirewall )
269
269
) ;
270
270
271
271
// allow anyone to access their session
@@ -431,12 +431,36 @@ app.postJson('/api/v1/people', function(req, res) {
431
431
app . postJson ( '/api/v1/bulk-delete' , bulkDocs . bulkDelete ) ;
432
432
433
433
// offline users are not allowed to hydrate documents via the hydrate API
434
- app . get ( '/api/v1/hydrate' , authorization . offlineUserFirewall , jsonQueryParser , hydration . hydrate ) ;
435
- app . post ( '/api/v1/hydrate' , authorization . offlineUserFirewall , jsonParser , jsonQueryParser , hydration . hydrate ) ;
434
+ app . get (
435
+ '/api/v1/hydrate' ,
436
+ authorization . handleAuthErrors ,
437
+ authorization . offlineUserFirewall ,
438
+ jsonQueryParser ,
439
+ hydration . hydrate
440
+ ) ;
441
+ app . post (
442
+ '/api/v1/hydrate' ,
443
+ authorization . handleAuthErrors ,
444
+ authorization . offlineUserFirewall ,
445
+ jsonParser ,
446
+ jsonQueryParser ,
447
+ hydration . hydrate
448
+ ) ;
436
449
437
450
// offline users are not allowed to get contacts by phone
438
- app . get ( '/api/v1/contacts-by-phone' , authorization . offlineUserFirewall , contactsByPhone . request ) ;
439
- app . post ( '/api/v1/contacts-by-phone' , authorization . offlineUserFirewall , jsonParser , contactsByPhone . request ) ;
451
+ app . get (
452
+ '/api/v1/contacts-by-phone' ,
453
+ authorization . handleAuthErrors ,
454
+ authorization . offlineUserFirewall ,
455
+ contactsByPhone . request
456
+ ) ;
457
+ app . post (
458
+ '/api/v1/contacts-by-phone' ,
459
+ authorization . handleAuthErrors ,
460
+ authorization . offlineUserFirewall ,
461
+ jsonParser ,
462
+ contactsByPhone . request
463
+ ) ;
440
464
441
465
app . get ( `${ appPrefix } app_settings/${ environment . ddoc } /:path?` , settings . getV0 ) ; // deprecated
442
466
app . get ( '/api/v1/settings' , settings . get ) ;
@@ -447,9 +471,24 @@ app.putJson('/api/v1/settings', settings.put);
447
471
448
472
app . get ( '/api/couch-config-attachments' , couchConfigController . getAttachments ) ;
449
473
450
- app . get ( '/purging' , authorization . onlineUserPassThrough , purgedDocsController . info ) ;
451
- app . get ( '/purging/changes' , authorization . onlineUserPassThrough , purgedDocsController . getPurgedDocs ) ;
452
- app . get ( '/purging/checkpoint' , authorization . onlineUserPassThrough , purgedDocsController . checkpoint ) ;
474
+ app . get (
475
+ '/purging' ,
476
+ authorization . handleAuthErrors ,
477
+ authorization . onlineUserPassThrough ,
478
+ purgedDocsController . info
479
+ ) ;
480
+ app . get (
481
+ '/purging/changes' ,
482
+ authorization . handleAuthErrors ,
483
+ authorization . onlineUserPassThrough ,
484
+ purgedDocsController . getPurgedDocs
485
+ ) ;
486
+ app . get (
487
+ '/purging/checkpoint' ,
488
+ authorization . handleAuthErrors ,
489
+ authorization . onlineUserPassThrough ,
490
+ purgedDocsController . checkpoint
491
+ ) ;
453
492
454
493
app . get ( '/api/v1/users-doc-count' , replicationLimitLogController . get ) ;
455
494
@@ -467,11 +506,13 @@ const changesPath = routePrefix + '_changes(/*)?';
467
506
468
507
app . get (
469
508
changesPath ,
509
+ authorization . handleAuthErrors ,
470
510
onlineUserChangesProxy ,
471
511
changesHandler
472
512
) ;
473
513
app . post (
474
514
changesPath ,
515
+ authorization . handleAuthErrors ,
475
516
onlineUserChangesProxy ,
476
517
jsonParser ,
477
518
changesHandler
@@ -481,9 +522,16 @@ app.post(
481
522
const allDocsHandler = require ( './controllers/all-docs' ) . request ;
482
523
const allDocsPath = routePrefix + '_all_docs(/*)?' ;
483
524
484
- app . get ( allDocsPath , onlineUserProxy , jsonQueryParser , allDocsHandler ) ;
525
+ app . get (
526
+ allDocsPath ,
527
+ authorization . handleAuthErrors ,
528
+ onlineUserProxy ,
529
+ jsonQueryParser ,
530
+ allDocsHandler
531
+ ) ;
485
532
app . post (
486
533
allDocsPath ,
534
+ authorization . handleAuthErrors ,
487
535
onlineUserProxy ,
488
536
jsonParser ,
489
537
jsonQueryParser ,
@@ -494,6 +542,7 @@ app.post(
494
542
const bulkGetHandler = require ( './controllers/bulk-get' ) . request ;
495
543
app . post (
496
544
routePrefix + '_bulk_get(/*)?' ,
545
+ authorization . handleAuthErrors ,
497
546
onlineUserProxy ,
498
547
jsonParser ,
499
548
jsonQueryParser ,
@@ -506,6 +555,7 @@ app.post(
506
555
routePrefix + '_bulk_docs(/*)?' ,
507
556
jsonParser ,
508
557
infodoc . mark ,
558
+ authorization . handleAuthErrors ,
509
559
authorization . onlineUserPassThrough , // online user requests pass through to the next route
510
560
jsonQueryParser ,
511
561
bulkDocs . request ,
@@ -521,6 +571,7 @@ const ddocPath = routePrefix + '_design/+:ddocId*';
521
571
522
572
app . get (
523
573
ddocPath ,
574
+ authorization . handleAuthErrors ,
524
575
onlineUserProxy ,
525
576
jsonQueryParser ,
526
577
_ . partial ( dbDocHandler . requestDdoc , environment . ddoc ) ,
@@ -529,6 +580,7 @@ app.get(
529
580
530
581
app . get (
531
582
docPath ,
583
+ authorization . handleAuthErrors ,
532
584
onlineUserProxy , // online user GET requests are proxied directly to CouchDB
533
585
jsonQueryParser ,
534
586
dbDocHandler . request
@@ -537,6 +589,7 @@ app.post(
537
589
`/+${ environment . db } /?` ,
538
590
jsonParser ,
539
591
infodoc . mark ,
592
+ authorization . handleAuthErrors ,
540
593
authorization . onlineUserPassThrough , // online user requests pass through to the next route
541
594
jsonQueryParser ,
542
595
dbDocHandler . request ,
@@ -546,20 +599,23 @@ app.put(
546
599
docPath ,
547
600
jsonParser ,
548
601
infodoc . mark ,
602
+ authorization . handleAuthErrors ,
549
603
authorization . onlineUserPassThrough , // online user requests pass through to the next route,
550
604
jsonQueryParser ,
551
605
dbDocHandler . request ,
552
606
authorization . setAuthorized // adds the `authorized` flag to the `req` object, so it passes the firewall
553
607
) ;
554
608
app . delete (
555
609
docPath ,
610
+ authorization . handleAuthErrors ,
556
611
authorization . onlineUserPassThrough , // online user requests pass through to the next route,
557
612
jsonQueryParser ,
558
613
dbDocHandler . request ,
559
614
authorization . setAuthorized // adds the `authorized` flag to the `req` object, so it passes the firewall
560
615
) ;
561
616
app . all (
562
617
attachmentPath ,
618
+ authorization . handleAuthErrors ,
563
619
authorization . onlineUserPassThrough , // online user requests pass through to the next route
564
620
jsonQueryParser ,
565
621
dbDocHandler . request ,
@@ -677,6 +733,10 @@ proxyForChanges.on('proxyReq', (proxyReq, req) => {
677
733
678
734
// because these are longpolls, we need to manually flush the CouchDB heartbeats through compression
679
735
proxyForChanges . on ( 'proxyRes' , ( proxyRes , req , res ) => {
736
+ if ( proxyRes . statusCode === 401 ) {
737
+ return serverUtils . notLoggedIn ( req , res ) ;
738
+ }
739
+
680
740
copyProxyHeaders ( proxyRes , res ) ;
681
741
682
742
proxyRes . pipe ( res ) ;
@@ -700,6 +760,7 @@ app.all(appPrefix + '*', authorization.setAuthorized);
700
760
// block offline users requests from accessing CouchDB directly, via Proxy
701
761
// requests which are authorized (fe: by BulkDocsHandler or DbDocHandler) can pass through
702
762
// unauthenticated requests will be redirected to login or given a meaningful error
763
+ app . use ( authorization . handleAuthErrorsAllowingAuthorized ) ;
703
764
app . use ( authorization . offlineUserFirewall ) ;
704
765
705
766
const canEdit = function ( req , res ) {
@@ -742,8 +803,18 @@ proxyForAuth.on('proxyReq', function(proxyReq, req) {
742
803
writeParsedBody ( proxyReq , req ) ;
743
804
} ) ;
744
805
806
+ proxy . on ( 'proxyRes' , ( proxyRes , req , res ) => {
807
+ if ( proxyRes . statusCode === 401 ) {
808
+ serverUtils . notLoggedIn ( req , res ) ;
809
+ }
810
+ } ) ;
811
+
745
812
// intercept responses from filtered offline endpoints to fill in with forbidden docs stubs
746
813
proxyForAuth . on ( 'proxyRes' , ( proxyRes , req , res ) => {
814
+ if ( proxyRes . statusCode === 401 ) {
815
+ return serverUtils . notLoggedIn ( req , res ) ;
816
+ }
817
+
747
818
copyProxyHeaders ( proxyRes , res ) ;
748
819
749
820
if ( res . interceptResponse ) {
0 commit comments