1- const { resolve, parse : parseUrl } = require ( 'url' ) ;
1+ const { URL , resolve, parse : parseUrl } = require ( 'url' ) ;
22const https = require ( 'https' ) ;
33const querystring = require ( 'querystring' ) ;
44const RelyingParty = require ( '@trust/oidc-rp' ) ;
@@ -174,8 +174,29 @@ class SolidClient {
174174 }
175175
176176 // Redirect to the authentication page, passing the session cookie
177- const authUrl = loginResponse . headers . location ;
177+ let authUrl = loginResponse . headers . location ;
178178 const cookie = loginResponse . headers [ 'set-cookie' ] [ 0 ] . replace ( / ; .* / , '' ) ;
179+
180+ // Handle the new consent page in 5.1.1
181+ if ( this . isAboveVersion511 ( loginResponse . headers [ 'x-powered-by' ] ) ) {
182+ const consentUrl = new URL ( authUrl ) ;
183+ const search = consentUrl . search . substring ( 1 ) ;
184+ let consPostData = JSON . parse ( '{"' + decodeURIComponent ( search ) . replace ( / " / g, '\\"' ) . replace ( / & / g, '","' ) . replace ( / \= / g, '":"' ) + '"}' ) ;
185+ consPostData . consent = true ;
186+ consPostData . access_mode = [ 'Read' , 'Write' , 'Append' , 'Control' ] ;
187+ consPostData = querystring . stringify ( consPostData ) ;
188+ const consOptions = parseUrl ( `${ consentUrl . origin } ${ consentUrl . pathname } ` ) ;
189+ consOptions . method = 'POST' ;
190+ consOptions . headers = {
191+ 'Content-Type' : 'application/x-www-form-urlencoded' ,
192+ 'Content-Length' : consPostData . length ,
193+ cookie,
194+ } ;
195+ const consentResponse = await this . fetch ( consOptions , consPostData ) ;
196+
197+ authUrl = consentResponse . headers . location ;
198+ }
199+
179200 const authResponse = await this . fetch ( Object . assign ( parseUrl ( authUrl ) , {
180201 headers : { cookie } ,
181202 } ) ) ;
@@ -185,6 +206,10 @@ class SolidClient {
185206 return accessUrl ;
186207 }
187208
209+ isAboveVersion511 ( version ) {
210+ return / ^ s o l i d - s e r v e r \/ 5 \. ( 1 \. [ 1 - 9 ] | [ 2 - 9 ] | 1 \d ) / . test ( version ) ;
211+ }
212+
188213 /**
189214 * Fetches the given resource over HTTP.
190215 *
0 commit comments