11'use strict'
22
3+ const AuthResponseSent = require ( '../errors/auth-response-sent' )
4+ const url = require ( 'url' )
5+
36class LoginConsentRequest {
47 constructor ( options ) {
58 this . opAuthRequest = options . opAuthRequest
@@ -63,7 +66,7 @@ class LoginConsentRequest {
6366 let { opAuthRequest, clientId } = consentRequest
6467
6568 // Consent for the local RP client (the home pod) is implied
66- if ( consentRequest . isLocalRpClient ( clientId ) ) {
69+ if ( consentRequest . isLocalRpClient ( consentRequest ) ) {
6770 return Promise . resolve ( )
6871 . then ( ( ) => { consentRequest . markConsentSuccess ( opAuthRequest ) } )
6972 . then ( ( ) => opAuthRequest )
@@ -82,7 +85,7 @@ class LoginConsentRequest {
8285 if ( priorConsent ) {
8386 consentRequest . markConsentSuccess ( opAuthRequest )
8487 } else {
85- consentRequest . renderConsentPage ( )
88+ consentRequest . redirectToConsent ( )
8689 }
8790 } )
8891 . then ( ( ) => opAuthRequest )
@@ -95,10 +98,10 @@ class LoginConsentRequest {
9598 return this . params [ 'client_id' ]
9699 }
97100
98- isLocalRpClient ( clientId ) {
99- let host = this . opAuthRequest . host || { }
100-
101- return ! ! clientId && clientId === host . localClientId
101+ isLocalRpClient ( request ) {
102+ const parsedAppOrigin = url . parse ( request . opAuthRequest . params . redirect_uri )
103+ const appOrigin = ` ${ parsedAppOrigin . protocol } // ${ parsedAppOrigin . host } `
104+ return request . opAuthRequest . req . app . locals . ldp . serverUri === appOrigin
102105 }
103106
104107 checkSavedConsentFor ( opAuthRequest ) {
@@ -114,11 +117,21 @@ class LoginConsentRequest {
114117 return Promise . resolve ( clientId )
115118 }
116119
117- renderConsentPage ( ) {
118- let { response, params, opAuthRequest } = this
119-
120- response . render ( 'auth/consent' , params )
121- opAuthRequest . headersSent = true
120+ redirectToConsent ( authRequest ) {
121+ let { opAuthRequest } = this
122+ let consentUrl = url . parse ( '/consent' )
123+ consentUrl . query = opAuthRequest . req . query
124+
125+ consentUrl = url . format ( consentUrl )
126+ opAuthRequest . subject = null
127+
128+ opAuthRequest . res . redirect ( consentUrl )
129+
130+ this . signalResponseSent ( )
131+ }
132+
133+ signalResponseSent ( ) {
134+ throw new AuthResponseSent ( 'User redirected to login' )
122135 }
123136}
124137
0 commit comments