-
Notifications
You must be signed in to change notification settings - Fork 144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
attemptSilentLogin feature #121
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
const auth = require('./middleware/auth'); | ||
const requiresAuth = require('./middleware/requiresAuth'); | ||
const attemptSilentLogin = require('./middleware/attemptSilentLogin'); | ||
|
||
module.exports = { | ||
auth, | ||
...requiresAuth, | ||
attemptSilentLogin, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const debug = require('../lib/debug')('attemptSilentLogin'); | ||
const COOKIES = require('../lib/cookies'); | ||
const weakRef = require('../lib/weakCache'); | ||
|
||
const COOKIE_NAME = 'skipSilentLogin'; | ||
|
||
const cancelSilentLogin = (req, res) => { | ||
const { | ||
config: { | ||
session: { cookie: cookieConfig }, | ||
}, | ||
} = weakRef(req.oidc); | ||
res.cookie(COOKIE_NAME, true, { | ||
httpOnly: true, | ||
secure: | ||
typeof cookieConfig.secure === 'boolean' | ||
? cookieConfig.secure | ||
: req.secure, | ||
}); | ||
}; | ||
|
||
const resumeSilentLogin = (req, res) => { | ||
const { | ||
config: { | ||
session: { cookie: cookieConfig }, | ||
}, | ||
} = weakRef(req.oidc); | ||
res.clearCookie(COOKIE_NAME, { | ||
httpOnly: true, | ||
secure: | ||
typeof cookieConfig.secure === 'boolean' | ||
? cookieConfig.secure | ||
: req.secure, | ||
}); | ||
}; | ||
|
||
module.exports = function attemptSilentLogin() { | ||
return (req, res, next) => { | ||
if (!req.oidc) { | ||
next( | ||
new Error('req.oidc is not found, did you include the auth middleware?') | ||
); | ||
return; | ||
} | ||
|
||
const silentLoginAttempted = !!(req[COOKIES] || {})[COOKIE_NAME]; | ||
|
||
if ( | ||
!silentLoginAttempted && | ||
!req.oidc.isAuthenticated() && | ||
req.accepts('html') | ||
) { | ||
debug('Attempting silent login'); | ||
cancelSilentLogin(req, res); | ||
return res.oidc.silentLogin(); | ||
} | ||
next(); | ||
}; | ||
}; | ||
|
||
module.exports.cancelSilentLogin = cancelSilentLogin; | ||
module.exports.resumeSilentLogin = resumeSilentLogin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
const { assert } = require('chai'); | ||
const { create: createServer } = require('./fixture/server'); | ||
const { makeIdToken } = require('./fixture/cert'); | ||
const { auth, attemptSilentLogin } = require('./..'); | ||
const request = require('request-promise-native').defaults({ | ||
simple: false, | ||
resolveWithFullResponse: true, | ||
followRedirect: false, | ||
}); | ||
|
||
const baseUrl = 'http://localhost:3000'; | ||
|
||
const defaultConfig = { | ||
secret: '__test_session_secret__', | ||
clientID: '__test_client_id__', | ||
baseURL: 'https://example.org', | ||
issuerBaseURL: 'https://op.example.com', | ||
}; | ||
|
||
const login = async (claims) => { | ||
const jar = request.jar(); | ||
await request.post('/session', { | ||
baseUrl, | ||
jar, | ||
json: { | ||
id_token: makeIdToken(claims), | ||
}, | ||
}); | ||
return jar; | ||
}; | ||
|
||
describe('attemptSilentLogin', () => { | ||
let server; | ||
|
||
afterEach(async () => { | ||
if (server) { | ||
server.close(); | ||
} | ||
}); | ||
|
||
it("should attempt silent login on user's first route", async () => { | ||
server = await createServer( | ||
auth({ | ||
...defaultConfig, | ||
authRequired: false, | ||
}), | ||
attemptSilentLogin() | ||
); | ||
const jar = request.jar(); | ||
const response = await request({ baseUrl, jar, url: '/protected' }); | ||
|
||
assert.equal(response.statusCode, 302); | ||
assert.include(jar.getCookies(baseUrl)[0], { | ||
key: 'skipSilentLogin', | ||
value: 'true', | ||
httpOnly: true, | ||
}); | ||
}); | ||
|
||
it('should not attempt silent login for non html requests', async () => { | ||
server = await createServer( | ||
auth({ | ||
...defaultConfig, | ||
authRequired: false, | ||
}), | ||
attemptSilentLogin() | ||
); | ||
const jar = request.jar(); | ||
const response = await request({ | ||
baseUrl, | ||
jar, | ||
url: '/protected', | ||
json: true, | ||
}); | ||
|
||
assert.equal(response.statusCode, 200); | ||
}); | ||
|
||
it("should not attempt silent login on user's subsequent routes", async () => { | ||
server = await createServer( | ||
auth({ | ||
...defaultConfig, | ||
authRequired: false, | ||
}), | ||
attemptSilentLogin() | ||
); | ||
const jar = request.jar(); | ||
const response = await request({ baseUrl, jar, url: '/protected' }); | ||
assert.equal(response.statusCode, 302); | ||
const response2 = await request({ baseUrl, jar, url: '/protected' }); | ||
assert.equal(response2.statusCode, 200); | ||
const response3 = await request({ baseUrl, jar, url: '/protected' }); | ||
assert.equal(response3.statusCode, 200); | ||
}); | ||
|
||
it('should not attempt silent login for authenticated user', async () => { | ||
server = await createServer( | ||
auth({ | ||
...defaultConfig, | ||
authRequired: false, | ||
}), | ||
attemptSilentLogin() | ||
); | ||
const jar = await login(); | ||
const response = await request({ baseUrl, jar, url: '/protected' }); | ||
assert.equal(response.statusCode, 200); | ||
}); | ||
|
||
it('should not attempt silent login after first anonymous request after logout', async () => { | ||
server = await createServer( | ||
auth({ | ||
...defaultConfig, | ||
authRequired: false, | ||
}), | ||
attemptSilentLogin() | ||
); | ||
const jar = await login(); | ||
await request({ baseUrl, jar, url: '/protected' }); | ||
await request.get({ | ||
uri: '/logout', | ||
baseUrl, | ||
jar, | ||
followRedirect: false, | ||
}); | ||
const response = await request({ baseUrl, jar, url: '/protected' }); | ||
assert.equal(response.statusCode, 200); | ||
}); | ||
|
||
it('should not attempt silent login after first request is to logout', async () => { | ||
server = await createServer( | ||
auth({ | ||
...defaultConfig, | ||
authRequired: false, | ||
}), | ||
attemptSilentLogin() | ||
); | ||
const jar = await login(); | ||
await request.get({ | ||
uri: '/logout', | ||
baseUrl, | ||
jar, | ||
followRedirect: false, | ||
}); | ||
const response = await request({ baseUrl, jar, url: '/protected' }); | ||
assert.equal(response.statusCode, 200); | ||
}); | ||
|
||
it("should throw when there's no auth middleware", async () => { | ||
server = await createServer(attemptSilentLogin()); | ||
const { | ||
body: { err }, | ||
} = await request({ baseUrl, url: '/protected', json: true }); | ||
assert.equal( | ||
err.message, | ||
'req.oidc is not found, did you include the auth middleware?' | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: Clear cookie on login
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
6c1cb89