@@ -508,6 +508,78 @@ describe('oauth/', function () {
508508 expect ( err . get ( 'www-authenticate' ) ) . toEqual ( expect . stringContaining ( 'insufficient_scope' ) ) ;
509509 } ) ;
510510
511+ it ( 'rejects collection writes for a scoped thing token' , async ( ) => {
512+ setupOAuth ( ) ;
513+ await addDevice ( ) ;
514+ await addDevice ( {
515+ ...TEST_THING ,
516+ id : 'test-2' ,
517+ title : 'test-2' ,
518+ } ) ;
519+
520+ const res = await chai
521+ . request ( server )
522+ . get (
523+ `${ Constants . OAUTH_PATH } /allow?${ qs . stringify ( {
524+ response_type : 'code' ,
525+ client_id : CLIENT_ID ,
526+ scope : '/things/test-1:readwrite' ,
527+ state : REQUEST_STATE ,
528+ } ) } `
529+ )
530+ . set ( 'Accept' , 'application/json' )
531+ . set ( ...headerAuth ( userJWT ) ) ;
532+
533+ const jwt = res . body . token . access_token ;
534+
535+ const listRes = await chai
536+ . request ( server )
537+ . get ( Constants . THINGS_PATH )
538+ . set ( 'Accept' , 'application/json' )
539+ . set ( ...headerAuth ( jwt ) ) ;
540+ expect ( listRes . status ) . toEqual ( 200 ) ;
541+ expect ( listRes . body . length ) . toEqual ( 1 ) ;
542+ expect ( listRes . body [ 0 ] . href ) . toEqual ( '/things/test-1' ) ;
543+
544+ const pinErr = await chai
545+ . request ( server )
546+ . patch ( Constants . THINGS_PATH )
547+ . set ( 'Accept' , 'application/json' )
548+ . set ( ...headerAuth ( jwt ) )
549+ . send ( { thingId : 'test-2' , pin : '1234' } ) ;
550+ expect ( pinErr . status ) . toEqual ( 403 ) ;
551+ expect ( pinErr . header ) . toHaveProperty ( 'www-authenticate' ) ;
552+ expect ( pinErr . get ( 'www-authenticate' ) ) . toEqual ( expect . stringContaining ( 'insufficient_scope' ) ) ;
553+
554+ const credentialsErr = await chai
555+ . request ( server )
556+ . patch ( Constants . THINGS_PATH )
557+ . set ( 'Accept' , 'application/json' )
558+ . set ( ...headerAuth ( jwt ) )
559+ . send ( { thingId : 'test-2' , username : 'user' , password : 'pass' } ) ;
560+ expect ( credentialsErr . status ) . toEqual ( 403 ) ;
561+
562+ const createErr = await chai
563+ . request ( server )
564+ . post ( Constants . THINGS_PATH )
565+ . set ( 'Accept' , 'application/json' )
566+ . set ( ...headerAuth ( jwt ) )
567+ . send ( {
568+ id : 'test-3' ,
569+ title : 'test-3' ,
570+ '@context' : [ 'https://www.w3.org/2022/wot/td/v1.1' , 'https://webthings.io/schemas' ] ,
571+ '@type' : [ 'OnOffSwitch' ] ,
572+ properties : {
573+ power : {
574+ '@type' : 'OnOffProperty' ,
575+ type : 'boolean' ,
576+ value : false ,
577+ } ,
578+ } ,
579+ } ) ;
580+ expect ( createErr . status ) . toEqual ( 403 ) ;
581+ } ) ;
582+
511583 it ( 'rejects use of authorization code as access token' , async ( ) => {
512584 setupOAuth ( { } , function customCallbackHandler ( req : express . Request , res : express . Response ) {
513585 const code = req . query . code ;
0 commit comments