-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IPS-169] Add authentication object to Rule's context (#199)
* [IPS-169] Add authentication object to Rule's context * Rename rule * 0.8.0 * Build and bump version
- Loading branch information
1 parent
858004e
commit 5009fb1
Showing
7 changed files
with
138 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,28 @@ | ||
/** | ||
* @title Require MFA once per session | ||
* @overview Require multifactor authentication only once per session | ||
* @gallery true | ||
* @category multifactor | ||
* | ||
* This rule can be used to avoid prompting a user for multifactor authentication if they have successfully completed MFA in their current session. | ||
* | ||
* This is particularly useful when performing silent authentication (`prompt=none`) to renew short-lived access tokens in a SPA (Single Page Application) during the duration of a user's session without having to rely on setting `allowRememberBrowser` to `true`. | ||
* | ||
*/ | ||
|
||
function (user, context, callback) { | ||
const completedMfa = !!context.authentication.methods.find( | ||
(method) => method.name === 'mfa' | ||
); | ||
|
||
if (completedMfa) { | ||
return callback(null, user, context); | ||
} | ||
|
||
context.multifactor = { | ||
provider: 'any', | ||
allowRememberBrowser: false | ||
}; | ||
|
||
callback(null, user, context); | ||
} |
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,68 @@ | ||
'use strict'; | ||
|
||
const loadRule = require('../utils/load-rule'); | ||
const ContextBuilder = require('../utils/contextBuilder'); | ||
const RequestBuilder = require('../utils/requestBuilder'); | ||
const AuthenticationBuilder = require('../utils/authenticationBuilder'); | ||
|
||
const ruleName = 'require-mfa-once-per-session'; | ||
|
||
describe(ruleName, () => { | ||
let context; | ||
let rule; | ||
let user; | ||
|
||
describe('With only a login prompt completed', () => { | ||
beforeEach(() => { | ||
rule = loadRule(ruleName); | ||
|
||
const request = new RequestBuilder().build(); | ||
const authentication = new AuthenticationBuilder().build(); | ||
context = new ContextBuilder() | ||
.withRequest(request) | ||
.withAuthentication(authentication) | ||
.build(); | ||
}); | ||
|
||
it('should set a multifactor provider', (done) => { | ||
rule(user, context, (err, u, c) => { | ||
expect(c.multifactor.provider).toBe('any'); | ||
expect(c.multifactor.allowRememberBrowser).toBe(false); | ||
|
||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('With a login and MFA prompt completed', () => { | ||
beforeEach(() => { | ||
rule = loadRule(ruleName); | ||
|
||
const request = new RequestBuilder().build(); | ||
const authentication = new AuthenticationBuilder() | ||
.withMethods([ | ||
{ | ||
name: 'pwd', | ||
timestamp: 1434454643024 | ||
}, | ||
{ | ||
name: 'mfa', | ||
timestamp: 1534454643881 | ||
} | ||
]) | ||
.build(); | ||
context = new ContextBuilder() | ||
.withRequest(request) | ||
.withAuthentication(authentication) | ||
.build(); | ||
}); | ||
|
||
it('should not set a multifactor provider', (done) => { | ||
rule(user, context, (err, u, c) => { | ||
expect(c.multifactor).toBe(undefined); | ||
|
||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
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,23 @@ | ||
'use strict'; | ||
|
||
class AuthenticationBuilder { | ||
constructor() { | ||
this.authentication = { | ||
methods: [ | ||
{ | ||
name: 'pwd', | ||
timestamp: 1434454643024 | ||
} | ||
] | ||
} | ||
} | ||
withMethods(methods) { | ||
this.authentication.methods = methods; | ||
return this; | ||
} | ||
build() { | ||
return this.authentication; | ||
} | ||
} | ||
|
||
module.exports = AuthenticationBuilder; |
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