11import {
2+ APPLICATION_JSON ,
23 BadRequestHttpError ,
4+ CONTENT_TYPE ,
35 createErrorMessage ,
46 getLoggerFor ,
7+ guardedStreamFrom ,
58 KeyValueStorage ,
69 MethodNotAllowedHttpError ,
10+ OperationHttpHandler ,
11+ OperationHttpHandlerInput ,
12+ readableToString ,
13+ RepresentationMetadata ,
14+ ResponseDescription ,
715 UnauthorizedHttpError ,
816 UnsupportedMediaTypeHttpError
917} from '@solid/community-server' ;
10- import { HttpHandler } from '../util/http/models/HttpHandler' ;
11- import { HttpHandlerResponse } from '../util/http/models/HttpHandlerResponse' ;
1218import { v4 } from 'uuid' ;
13- import { HttpHandlerRequest } from '../util/http/models/HttpHandlerRequest ' ;
19+ import { CUSTOM_UMA } from '../util/Vocabularies ' ;
1420import { ResourceDescription } from '../views/ResourceDescription' ;
1521import { reType } from '../util/ReType' ;
1622import { extractRequestSigner , verifyRequest } from '../util/HttpMessageSignatures' ;
@@ -23,12 +29,9 @@ type ErrorConstructor = { new(msg: string): Error };
2329 *
2430 * It provides an endpoint to a Resource Server for registering its resources.
2531 */
26- export class ResourceRegistrationRequestHandler extends HttpHandler {
32+ export class ResourceRegistrationRequestHandler extends OperationHttpHandler {
2733 protected readonly logger = getLoggerFor ( this ) ;
2834
29- /**
30- * @param {RequestingPartyRegistration[] } resourceServers - Pod Servers to be registered with the UMA AS
31- */
3235 constructor (
3336 private readonly resourceStore : KeyValueStorage < string , ResourceDescription > ,
3437 ) {
@@ -37,32 +40,30 @@ export class ResourceRegistrationRequestHandler extends HttpHandler {
3740
3841 /**
3942 * Handle incoming requests for resource registration
40- * @param {HttpHandlerRequest } request
41- * @return {HttpHandlerResponse }
4243 */
43- async handle ( request : HttpHandlerRequest ) : Promise < HttpHandlerResponse > {
44- const signer = await extractRequestSigner ( request ) ;
44+ async handle ( input : OperationHttpHandlerInput ) : Promise < ResponseDescription > {
45+ const signer = await extractRequestSigner ( input . request ) ;
4546
4647 // TODO: check if signer is actually the correct one
4748
48- if ( ! await verifyRequest ( request , signer ) ) {
49+ if ( ! await verifyRequest ( input . request , input . operation , signer ) ) {
4950 throw new UnauthorizedHttpError ( `Failed to verify signature of <${ signer } >` ) ;
5051 }
5152
52- switch ( request . method ) {
53- case 'POST' : return this . handlePost ( request ) ;
54- case 'DELETE' : return this . handleDelete ( request ) ;
53+ switch ( input . operation . method ) {
54+ case 'POST' : return this . handlePost ( input ) ;
55+ case 'DELETE' : return this . handleDelete ( input ) ;
5556 default : throw new MethodNotAllowedHttpError ( ) ;
5657 }
5758 }
5859
59- private async handlePost ( request : HttpHandlerRequest ) : Promise < HttpHandlerResponse < any > > {
60- const { headers, body } = request ;
61-
62- if ( headers [ 'content-type' ] !== 'application/json' ) {
60+ private async handlePost ( input : OperationHttpHandlerInput ) : Promise < ResponseDescription > {
61+ if ( input . request . headers [ 'content-type' ] !== 'application/json' ) {
6362 throw new UnsupportedMediaTypeHttpError ( 'Only Media Type "application/json" is supported for this route.' ) ;
6463 }
6564
65+ const body = JSON . parse ( await readableToString ( input . operation . body . data ) ) ;
66+
6667 try {
6768 reType ( body , ResourceDescription ) ;
6869 } catch ( e ) {
@@ -71,35 +72,31 @@ export class ResourceRegistrationRequestHandler extends HttpHandler {
7172 }
7273
7374 const resource = v4 ( ) ;
74- this . resourceStore . set ( resource , body ) ;
75+ await this . resourceStore . set ( resource , body ) ;
7576
7677 this . logger . info ( `Registered resource ${ resource } .` ) ;
7778
78- return ( {
79- status : 201 ,
80- headers : {
81- 'content-type' : 'application/json'
82- } ,
83- body : JSON . stringify ( {
79+ return new ResponseDescription (
80+ 201 ,
81+ new RepresentationMetadata ( { [ CONTENT_TYPE ] : APPLICATION_JSON } ) ,
82+ guardedStreamFrom ( JSON . stringify ( {
8483 _id : resource ,
8584 user_access_policy_uri : 'TODO: implement policy UI' ,
86- } ) ,
87- } )
85+ } ) )
86+ ) ;
8887 }
8988
90- private async handleDelete ( { parameters } : HttpHandlerRequest ) : Promise < HttpHandlerResponse < any > > {
91- if ( typeof parameters ?. id !== 'string' ) throw new Error ( 'URI for DELETE operation should include an id.' ) ;
89+ private async handleDelete ( input : OperationHttpHandlerInput ) : Promise < ResponseDescription > {
90+ const id = input . operation . body . metadata . get ( CUSTOM_UMA . terms . id ) ?. value ;
91+ if ( ! id ) throw new Error ( 'URI for DELETE operation should include an id.' ) ;
9292
93- if ( ! await this . resourceStore . has ( parameters . id ) ) {
93+ if ( ! await this . resourceStore . has ( id ) ) {
9494 throw new Error ( 'Registration to be deleted does not exist (id unknown).' ) ;
9595 }
9696
97- this . logger . info ( `Deleted resource ${ parameters . id } .` ) ;
97+ this . logger . info ( `Deleted resource ${ id } .` ) ;
9898
99- return ( {
100- status : 204 ,
101- headers : { } ,
102- } ) ;
99+ return new ResponseDescription ( 204 ) ;
103100 }
104101
105102 /**
0 commit comments