@@ -7,6 +7,7 @@ import rsaPemToJwk from 'rsa-pem-to-jwk'
77import URLSearchParams from 'url-search-params'
88
99import { currentSession , fetch , login , logout } from './api'
10+ import { saveHost } from './hosts'
1011import { getSession , saveSession } from './session'
1112import { memStorage } from './storage'
1213
@@ -203,8 +204,8 @@ describe('login', () => {
203204
204205describe ( 'currentSession' , ( ) => {
205206 it ( 'can find the current session if stored' , ( ) => {
206- saveSession ( window . localStorage , {
207- type : 'WebID-OIDC' ,
207+ saveSession ( window . localStorage ) ( {
208+ authType : 'WebID-OIDC' ,
208209 idp : 'https://localhost' ,
209210 webId : 'https://person.me/#me' ,
210211 accessToken : 'fake_access_token' ,
@@ -291,7 +292,8 @@ describe('currentSession', () => {
291292describe ( 'logout' , ( ) => {
292293 describe ( 'WebID-TLS' , ( ) => {
293294 it ( 'just removes the current session from the store' , ( ) => {
294- saveSession ( window . localStorage , {
295+ saveSession ( window . localStorage ) ( {
296+ authType : 'WebID-TLS' ,
295297 idp : 'https://localhost' ,
296298 webId : 'https://person.me/#me'
297299 } )
@@ -374,8 +376,8 @@ describe('logout', () => {
374376
375377describe ( 'fetch' , ( ) => {
376378 it ( 'handles 401s from WebID-OIDC resources by resending with credentials' , ( ) => {
377- saveSession ( window . localStorage , {
378- type : 'WebID-OIDC' ,
379+ saveSession ( window . localStorage ) ( {
380+ authType : 'WebID-OIDC' ,
379381 idp : 'https://localhost' ,
380382 webId : 'https://person.me/#me' ,
381383 accessToken : 'fake_access_token' ,
@@ -384,7 +386,7 @@ describe('fetch', () => {
384386
385387 nock ( 'https://third-party.com' )
386388 . get ( '/protected-resource' )
387- . reply ( 401 , '' , { 'www-authenticate' : 'Bearer scope=openid' } )
389+ . reply ( 401 , '' , { 'www-authenticate' : 'Bearer scope=" openid webid" ' } )
388390 . get ( '/protected-resource' )
389391 . matchHeader ( 'authorization' , 'Bearer abc.def.ghi' )
390392 . reply ( 200 )
@@ -396,8 +398,8 @@ describe('fetch', () => {
396398 } )
397399
398400 it ( 'merges request headers with the authorization header' , ( ) => {
399- saveSession ( window . localStorage , {
400- type : 'WebID-OIDC' ,
401+ saveSession ( window . localStorage ) ( {
402+ authType : 'WebID-OIDC' ,
401403 idp : 'https://localhost' ,
402404 webId : 'https://person.me/#me' ,
403405 accessToken : 'fake_access_token' ,
@@ -406,7 +408,7 @@ describe('fetch', () => {
406408
407409 nock ( 'https://third-party.com' )
408410 . get ( '/private-resource' )
409- . reply ( 401 , '' , { 'www-authenticate' : 'Bearer scope=openid' } )
411+ . reply ( 401 , '' , { 'www-authenticate' : 'Bearer scope=" openid webid" ' } )
410412 . get ( '/private-resource' )
411413 . matchHeader ( 'accept' , 'text/plain' )
412414 . matchHeader ( 'authorization' , 'Bearer abc.def.ghi' )
@@ -419,8 +421,8 @@ describe('fetch', () => {
419421 } )
420422
421423 it ( 'does not resend with credentials if the www-authenticate header is missing' , ( ) => {
422- saveSession ( window . localStorage , {
423- type : 'WebID-OIDC' ,
424+ saveSession ( window . localStorage ) ( {
425+ authType : 'WebID-OIDC' ,
424426 idp : 'https://localhost' ,
425427 webId : 'https://person.me/#me' ,
426428 accessToken : 'fake_access_token' ,
@@ -438,8 +440,8 @@ describe('fetch', () => {
438440 } )
439441
440442 it ( 'does not resend with credentials if the www-authenticate header suggests an unknown scheme' , ( ) => {
441- saveSession ( window . localStorage , {
442- type : 'WebID-OIDC' ,
443+ saveSession ( window . localStorage ) ( {
444+ authType : 'WebID-OIDC' ,
443445 idp : 'https://localhost' ,
444446 webId : 'https://person.me/#me' ,
445447 accessToken : 'fake_access_token' ,
@@ -459,7 +461,7 @@ describe('fetch', () => {
459461 it ( 'does not resend with credentials if there is no session' , ( ) => {
460462 nock ( 'https://third-party.com' )
461463 . get ( '/protected-resource' )
462- . reply ( 401 , '' , { 'www-authenticate' : 'Bearer scope=openid' } )
464+ . reply ( 401 , '' , { 'www-authenticate' : 'Bearer scope=" openid webid" ' } )
463465
464466 return fetch ( 'https://third-party.com/protected-resource' )
465467 . then ( resp => {
@@ -481,4 +483,86 @@ describe('fetch', () => {
481483 expect ( body ) . toEqual ( 'public content' )
482484 } )
483485 } )
486+
487+ it ( 'does not resend with credentials if the requested resources uses plain OIDC' , ( ) => {
488+ nock ( 'https://third-party.com' )
489+ . get ( '/protected-resource' )
490+ . reply ( 401 , '' , { 'www-authenticate' : 'Bearer scope="openid"' } )
491+
492+ return fetch ( 'https://third-party.com/protected-resource' )
493+ . then ( resp => {
494+ expect ( resp . status ) . toBe ( 401 )
495+ } )
496+ } )
497+
498+ describe ( 'familiar domains with WebID-OIDC' , ( ) => {
499+ it ( 'just sends one request when the RP is also the IDP' , ( ) => {
500+ saveSession ( window . localStorage ) ( {
501+ authType : 'WebID-OIDC' ,
502+ idp : 'https://localhost' ,
503+ webId : 'https://person.me/#me' ,
504+ accessToken : 'fake_access_token' ,
505+ idToken : 'abc.def.ghi'
506+ } )
507+
508+ nock ( 'https://localhost' )
509+ . get ( '/resource' )
510+ . matchHeader ( 'authorization' , 'Bearer abc.def.ghi' )
511+ . reply ( 200 )
512+
513+ return fetch ( 'https://localhost/resource' )
514+ . then ( resp => {
515+ expect ( resp . status ) . toBe ( 200 )
516+ } )
517+ } )
518+
519+ it ( 'just sends one request to domains it has already encountered' , ( ) => {
520+ saveSession ( window . localStorage ) ( {
521+ authType : 'WebID-OIDC' ,
522+ idp : 'https://localhost' ,
523+ webId : 'https://person.me/#me' ,
524+ accessToken : 'fake_access_token' ,
525+ idToken : 'abc.def.ghi'
526+ } )
527+
528+ saveHost ( window . localStorage ) ( {
529+ url : 'third-party.com' ,
530+ authType : 'WebID-OIDC'
531+ } )
532+
533+ nock ( 'https://third-party.com' )
534+ . get ( '/resource' )
535+ . matchHeader ( 'authorization' , 'Bearer abc.def.ghi' )
536+ . reply ( 200 )
537+
538+ return fetch ( 'https://third-party.com/resource' )
539+ . then ( resp => {
540+ expect ( resp . status ) . toBe ( 200 )
541+ } )
542+ } )
543+
544+ it ( 'does not send credentials to a familiar domain when that domain uses a different auth type' , ( ) => {
545+ saveSession ( window . localStorage ) ( {
546+ authType : 'WebID-OIDC' ,
547+ idp : 'https://localhost' ,
548+ webId : 'https://person.me/#me' ,
549+ accessToken : 'fake_access_token' ,
550+ idToken : 'abc.def.ghi'
551+ } )
552+
553+ saveHost ( window . localStorage ) ( {
554+ url : 'third-party.com' ,
555+ authType : 'WebID-TLS'
556+ } )
557+
558+ nock ( 'https://third-party.com' )
559+ . get ( '/resource' )
560+ . reply ( 401 )
561+
562+ return fetch ( 'https://third-party.com/resource' )
563+ . then ( resp => {
564+ expect ( resp . status ) . toBe ( 401 )
565+ } )
566+ } )
567+ } )
484568} )
0 commit comments