Skip to content

Commit cbb4fe4

Browse files
Save returnToUrl query param in session, in SelectProviderRequest.
1 parent 7bcf0b6 commit cbb4fe4

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/handlers/select-provider-request.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ class SelectProviderRequest {
1212
* @param [options.oidcManager] {OidcManager}
1313
* @param [options.response] {HttpResponse}
1414
* @param [options.serverUri] {string}
15+
* @param [options.returnToUrl] {string} Encoded url of the original resource
16+
* a client was trying to access before being redirected to select provider
1517
*/
1618
constructor (options) {
1719
this.webId = options.webId
1820
this.oidcManager = options.oidcManager
1921
this.response = options.response
2022
this.session = options.session
2123
this.serverUri = options.serverUri
24+
this.returnToUrl = options.returnToUrl
2225
}
2326

2427
/**
@@ -58,8 +61,9 @@ class SelectProviderRequest {
5861
* @return {SelectProviderRequest}
5962
*/
6063
static fromParams (req, res) {
61-
let body = req.body || {}
62-
let webId = SelectProviderRequest.normalizeUri(body.webid)
64+
const body = req.body || {}
65+
const query = req.query || {}
66+
const webId = SelectProviderRequest.normalizeUri(body.webid)
6367

6468
let oidcManager, serverUri
6569
if (req.app && req.app.locals) {
@@ -72,6 +76,7 @@ class SelectProviderRequest {
7276
webId,
7377
oidcManager,
7478
serverUri,
79+
returnToUrl: query.returnToUrl,
7580
response: res,
7681
session: req.session
7782
}
@@ -124,6 +129,7 @@ class SelectProviderRequest {
124129
static handlePost (request) {
125130
return Promise.resolve()
126131
.then(() => request.validate())
132+
.then(() => request.saveReturnToUrl())
127133
.then(() => request.selectProvider())
128134
.catch(err => request.error(err))
129135
}
@@ -163,6 +169,15 @@ class SelectProviderRequest {
163169
.then(providerAuthUrl => this.response.redirect(providerAuthUrl))
164170
}
165171

172+
/**
173+
* Saves `returnToUrl` param for later use in AuthCallbackRequest handler,
174+
* to redirect the client to the original resource they were trying to access
175+
* before entering the authn workflow.
176+
*/
177+
saveReturnToUrl () {
178+
this.session.returnToUrl = this.returnToUrl
179+
}
180+
166181
/**
167182
* @throws {Error}
168183
*

test/unit/select-provider-request.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,17 @@ describe('SelectProviderRequest', () => {
7979
let res = HttpMocks.createResponse()
8080
let serverUri = 'https://example.com'
8181

82+
// 'https%3A%2F%2Foriginal.com%2Fpath%23hash'
83+
const returnToUrl = encodeURIComponent('https://original.com/path#hash')
84+
8285
it('should initialize a SelectProviderRequest instance', () => {
8386
let aliceWebId = 'https://alice.example.com'
8487
let oidcManager = {}
8588
let session = {}
8689
let req = {
8790
session,
8891
body: { webid: aliceWebId },
92+
query: { returnToUrl },
8993
app: { locals: { oidc: oidcManager, host: { serverUri } } }
9094
}
9195

@@ -95,6 +99,7 @@ describe('SelectProviderRequest', () => {
9599
expect(request.oidcManager).to.equal(oidcManager)
96100
expect(request.session).to.equal(session)
97101
expect(request.serverUri).to.equal(serverUri)
102+
expect(request.returnToUrl).to.equal(returnToUrl)
98103
})
99104

100105
it('should attempt to normalize an invalid webid uri', () => {
@@ -126,6 +131,19 @@ describe('SelectProviderRequest', () => {
126131
})
127132
})
128133

134+
describe('saveReturnToUrl()', () => {
135+
it('should save the returnToUrl in session', () => {
136+
let response = HttpMocks.createResponse()
137+
let session = {}
138+
let returnToUrl = encodeURIComponent('https://example.com/path#hash')
139+
let request = new SelectProviderRequest({ response, session, returnToUrl })
140+
141+
request.saveReturnToUrl()
142+
143+
expect(request.session.returnToUrl).to.equal(returnToUrl)
144+
})
145+
})
146+
129147
describe('selectProvider()', () => {
130148
it('should fetch the provider uri and redirect user to its /authorize endpoint', () => {
131149
let webId = 'https://example.com/#me'
@@ -178,11 +196,13 @@ describe('SelectProviderRequest', () => {
178196

179197
request.validate = sinon.stub().resolves()
180198
request.selectProvider = sinon.stub().resolves()
199+
request.saveReturnToUrl = sinon.stub()
181200

182201
return SelectProviderRequest.handlePost(request)
183202
.then(() => {
184203
expect(request.validate).to.have.been.called()
185204
expect(request.selectProvider).to.have.been.called()
205+
expect(request.saveReturnToUrl).to.have.been.called()
186206
})
187207
})
188208

0 commit comments

Comments
 (0)