Skip to content

Commit 7850047

Browse files
Fix storing redirectTo URL in session
1 parent 145d0d4 commit 7850047

6 files changed

Lines changed: 83 additions & 24 deletions

File tree

lib/api/accounts/signin.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ function signin () {
2929
}
3030

3131
const linkHeaders = li.parse(response.headers.link)
32-
console.log(linkHeaders)
3332
if (!linkHeaders['oidc.issuer']) {
3433
res.status(400).send('The URI requested is not a valid endpoint')
3534
return

lib/create-app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ function createApp (argv = {}) {
110110

111111
app.use('/', express.static(path.join(__dirname, '../static/oidc')))
112112
app.use('/', oidcHandler.authenticate(oidcRpClient))
113-
app.use('/api/oidc', oidcHandler.api(corsSettings))
113+
app.use('/api/oidc', oidcHandler.api(corsSettings, oidcRpClient))
114114
}
115115

116116
// Adding proxy

lib/handlers/error-pages.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,16 @@ If you are not redirected automatically, follow the <a href='${url}'>link to log
6464
function redirectToLogin (req, res, next) {
6565
res.header('Content-Type', 'text/html')
6666
// var loginUrl = req.app.locals.oidc.urlForSignin(req)
67-
67+
var currentUrl = util.fullUrlForReq(req)
6868
let loginUrl = util.uriBase(req) + '/signin.html'
6969
debug('Redirecting to login: ' + loginUrl)
70-
var currentUrl = util.fullUrlForReq(req)
71-
req.session.returnToUrl = currentUrl
70+
71+
if (!req.session.returnToUrl) {
72+
req.session.returnToUrl = currentUrl
73+
debug('Saving current request as: ' + currentUrl)
74+
} else {
75+
debug('Not saving current request, already set!')
76+
}
7277

7378
var body = redirectBody(loginUrl)
7479
res.send(body)

lib/handlers/oidc.js

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module.exports.oidcIssuerHeader = oidcIssuerHeader
2525
* @param corsSettings
2626
* @returns {Router} Express router
2727
*/
28-
function api (corsSettings) {
28+
function api (corsSettings, oidcRpClient) {
2929
const router = express.Router('/')
3030

3131
if (corsSettings) {
@@ -36,7 +36,7 @@ function api (corsSettings) {
3636
// (req, res, next) => {
3737
// // const userServer = req.body.oidcServer
3838
// })
39-
router.get('/rp', rpCallback)
39+
router.get('/rp', authCallback(oidcRpClient), authSessionInit, rpCallback(oidcRpClient))
4040
// router.get('/signout', (req, res, next) => {
4141
// req.session.userId = null
4242
// req.session.identified = false
@@ -104,6 +104,9 @@ function authSessionInit (req, res, next) {
104104
}
105105
debug.oidc('authSessionInit: starting up user session, recording userId')
106106
var webId = req.userInfo.profile
107+
if (!webId) {
108+
debug.oidc('Error signing in: User\'s contains no WebId in the .profile')
109+
}
107110
req.session.userId = webId
108111
req.session.identified = true
109112
debug.oidc('WebId: ' + webId)
@@ -170,11 +173,49 @@ function loadAuthClient (oidcRpClient) {
170173
}
171174
}
172175

173-
function rpCallback (req, res, next) {
174-
console.log('In authRp handler:')
175-
if (req.session.returnToUrl) {
176-
console.log(' Redirecting to ' + req.session.returnToUrl)
177-
return res.redirect(302, req.session.returnToUrl)
176+
function authCallback (oidcRpClient) {
177+
return (req, res, next) => {
178+
debug.oidc('in authCallback():')
179+
const tokenOptions = {
180+
code: req.query.code
181+
}
182+
var accessToken
183+
debug.oidc('code: ' + req.query.code)
184+
oidcRpClient.trustedClient.client.token(tokenOptions)
185+
.then((tokenResult) => {
186+
const verifyOptions = {
187+
allowNoToken: true,
188+
loadUserInfo: true
189+
}
190+
accessToken = tokenResult.access_token
191+
debug.oidc('Verifying token')
192+
return oidcRpClient.trustedClient.verifyToken(req, accessToken, verifyOptions)
193+
})
194+
.then(() => {
195+
return oidcRpClient.trustedClient.client.userInfo({ token: accessToken })
196+
})
197+
.then(function (userInfo) {
198+
req.userInfo = userInfo
199+
next()
200+
})
201+
.catch((err) => {
202+
debug.oidc(err)
203+
next(err)
204+
})
205+
}
206+
}
207+
208+
function rpCallback (oidcRpClient) {
209+
return (req, res, next) => {
210+
debug.oidc('In authRp handler:')
211+
212+
if (req.session.returnToUrl) {
213+
let returnToUrl = req.session.returnToUrl
214+
debug.oidc(' Redirecting to ' + returnToUrl)
215+
delete req.session.returnToUrl
216+
return res.redirect(302, returnToUrl)
217+
}
218+
res.send('OK')
219+
// next()
178220
}
179-
res.send('OK')
180221
}

lib/oidc-rp-client.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ module.exports = class OidcRpClient {
6666
// client not already in store, create and register it
6767
let clientConfig = {
6868
issuer: issuer,
69-
redirect_uri: this.trustedClient.redirect_uri,
69+
redirect_uri: this.trustedClient.client.redirect_uri,
7070
scope: 'openid profile'
7171
}
7272
return this.initClient(clientConfig)
@@ -87,8 +87,8 @@ module.exports = class OidcRpClient {
8787
debug.idp('Client discovered, JWKs retrieved')
8888
if (!oidcExpress.client.client_id) {
8989
// Register if you haven't already.
90-
debug.idp('Registering client')
91-
return oidcExpress.client.register(this.registration)
90+
debug.oidc('Registering client')
91+
return oidcExpress.client.register(this.newClientConfig())
9292
}
9393
})
9494
.then(() => {
@@ -106,19 +106,31 @@ module.exports = class OidcRpClient {
106106
})
107107
}
108108

109+
newClientConfig () {
110+
return {
111+
client_name: 'Solid Server Remote Client',
112+
grant_types: ['authorization_code', 'implicit'],
113+
default_max_age: 86400, // one day in seconds
114+
response_types: ['code', 'id_token token', 'code id_token token',
115+
'refresh_token', 'client_credentials']
116+
}
117+
}
118+
109119
/**
110120
* Returns the Signin page URL for the trusted OIDC provider
111-
* @param client {OIDCExpressClient}
121+
* @param oidcExpress {OIDCExpressClient}
112122
* @returns {String}
113123
*/
114-
urlForSignin (client) {
124+
urlForSignin (oidcExpress) {
115125
// return 'https://anvil.local/authorize?stuff'
116-
var loginUrl = client.client.authorizationUri({
126+
var loginUrl = oidcExpress.client.authorizationUri({
117127
endpoint: 'signin',
118-
nonce: '123',
119-
response_mode: 'query',
120-
response_type: 'token id_token',
121-
redirect_uri: client.redirect_uri
128+
// nonce: '123',
129+
// response_mode: 'query',
130+
// response_type: 'token id_token',
131+
response_type: 'code',
132+
// redirect_uri: client.redirect_uri
133+
redirect_uri: oidcExpress.client.redirect_uri
122134
})
123135
return loginUrl
124136
}

lib/utils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ var from = require('from2')
2121
var url = require('url')
2222

2323
function fullUrlForReq (req) {
24-
return url.format({
24+
let fullUrl = url.format({
2525
protocol: req.protocol,
2626
host: req.get('host'),
2727
pathname: req.originalUrl
2828
})
29+
console.log('URL: ' + fullUrl)
30+
return fullUrl
2931
}
3032

3133
function uriToFilename (uri, base) {

0 commit comments

Comments
 (0)