@@ -3,9 +3,6 @@ const fs = require('fs')
33const util = require ( '../utils' )
44const Auth = require ( '../api/authn' )
55
6- // Authentication methods that require a Provider Select page
7- const SELECT_PROVIDER_AUTH_METHODS = [ 'oidc' ]
8-
96/**
107 * Serves as a last-stop error handler for all other middleware.
118 *
@@ -28,23 +25,21 @@ function handler (err, req, res, next) {
2825 return ldp . errorHandler ( err , req , res , next )
2926 }
3027
31- let statusCode = statusCodeFor ( err , req , authMethod )
32-
33- if ( statusCode === 401 ) {
34- debug ( err , 'error:' , err . error , 'desc:' , err . error_description )
35- setAuthenticateHeader ( req , res , err )
36- }
37-
38- if ( requiresSelectProvider ( authMethod , statusCode , req ) ) {
39- return redirectToSelectProvider ( req , res )
40- }
41-
42- // If noErrorPages is set,
43- // then return the response directly
44- if ( ldp . noErrorPages ) {
45- sendErrorResponse ( statusCode , res , err )
46- } else {
47- sendErrorPage ( statusCode , res , err , ldp )
28+ const statusCode = statusCodeFor ( err , req , authMethod )
29+ switch ( statusCode ) {
30+ case 401 :
31+ setAuthenticateHeader ( req , res , err )
32+ renderLoginRequired ( req , res )
33+ break
34+ case 403 :
35+ renderNoPermission ( req , res )
36+ break
37+ default :
38+ if ( ldp . noErrorPages ) {
39+ sendErrorResponse ( statusCode , res , err )
40+ } else {
41+ sendErrorPage ( statusCode , res , err , ldp )
42+ }
4843 }
4944}
5045
@@ -67,26 +62,6 @@ function statusCodeFor (err, req, authMethod) {
6762 return statusCode
6863}
6964
70- /**
71- * Tests whether a given authentication method requires a Select Provider
72- * page redirect for 401 error responses.
73- *
74- * @param authMethod {string}
75- * @param statusCode {number}
76- * @param req {IncomingRequest}
77- *
78- * @returns {boolean }
79- */
80- function requiresSelectProvider ( authMethod , statusCode , req ) {
81- if ( statusCode !== 401 ) { return false }
82-
83- if ( ! SELECT_PROVIDER_AUTH_METHODS . includes ( authMethod ) ) { return false }
84-
85- if ( ! req . accepts ( 'text/html' ) ) { return false }
86-
87- return true
88- }
89-
9065/**
9166 * Dispatches the writing of the `WWW-Authenticate` response header (used for
9267 * 401 Unauthorized responses).
@@ -150,26 +125,30 @@ function sendErrorPage (statusCode, res, err, ldp) {
150125}
151126
152127/**
153- * Sends a 401 response with an HTML http-equiv type redirect body, to
154- * redirect any users requesting a resource directly in the browser to the
155- * Select Provider page and login workflow.
156- * Implemented as a 401 + redirect body instead of a 302 to provide a useful
157- * 401 response to REST/XHR clients.
128+ * Renders a 401 response explaining that a login is required.
158129 *
159130 * @param req {IncomingRequest}
160131 * @param res {ServerResponse}
161132 */
162- function redirectToSelectProvider ( req , res ) {
133+ function renderLoginRequired ( req , res ) {
134+ const currentUrl = util . fullUrlForReq ( req )
135+ debug ( `Display login-required for ${ currentUrl } ` )
163136 res . status ( 401 )
164- res . header ( 'Content-Type' , 'text/html' )
137+ res . render ( 'auth/login-required' , { currentUrl } )
138+ }
165139
166- const locals = req . app . locals
140+ /**
141+ * Renders a 403 response explaining that the user has no permission.
142+ *
143+ * @param req {IncomingRequest}
144+ * @param res {ServerResponse}
145+ */
146+ function renderNoPermission ( req , res ) {
167147 const currentUrl = util . fullUrlForReq ( req )
168- const loginUrl = `${ locals . host . serverUri } /api/auth/select-provider?returnToUrl=${ encodeURIComponent ( currentUrl ) } `
169- debug ( 'Redirecting to Select Provider: ' + loginUrl )
170-
171- const body = redirectBody ( loginUrl )
172- res . send ( body )
148+ const webId = req . session . userId
149+ debug ( `Display no-permission for ${ currentUrl } ` )
150+ res . status ( 403 )
151+ res . render ( 'auth/no-permission' , { currentUrl, webId } )
173152}
174153
175154/**
@@ -199,8 +178,6 @@ follow the <a href='${url}'>link to login</a>
199178module . exports = {
200179 handler,
201180 redirectBody,
202- redirectToSelectProvider,
203- requiresSelectProvider,
204181 sendErrorPage,
205182 sendErrorResponse,
206183 setAuthenticateHeader
0 commit comments