-
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.
Merge pull request #231 from Adam-Auth0/master
Add rule templates for multifactor stepup authenticaiton & adding email address to access token.
- Loading branch information
Showing
7 changed files
with
155 additions
and
0 deletions.
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
Binary file not shown.
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,19 @@ | ||
/** | ||
* @title Add email to access token | ||
* @overview Add the authenticated user's email address to the access token. | ||
* @gallery true | ||
* @category access control | ||
* | ||
* This rule will add the authenticated user's `email` attribute value to the access token. | ||
* | ||
* | ||
*/ | ||
|
||
function (user, context, callback) { | ||
// This rule adds the authenticated user's email address to the access token. | ||
|
||
var namespace = 'https://example.com/'; | ||
|
||
context.accessToken[namespace + 'email'] = user.email; | ||
return 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,34 @@ | ||
/** | ||
* @title Multifactor Stepup Authentication | ||
* @overview Used to challenge for a second factor when requested by sending acr_values. | ||
* @gallery true | ||
* @category multifactor | ||
* | ||
* This rule will challenge for a second authentication factor on request (step up) when | ||
* acr_values = 'http://schemas.openid.net/pape/policies/2007/06/multi-factor' is sent in | ||
* the request. Before the challenge is made, 'context.authentication.methods' is checked | ||
* to determine when the user has already successfully completed a challenge in the | ||
* current session. | ||
* | ||
*/ | ||
|
||
function (user, context, callback) { | ||
|
||
// This rule initiates multi-factor authenticaiton as a second factor | ||
// whenever the request contains the following value: | ||
// | ||
// acr_values = 'http://schemas.openid.net/pape/policies/2007/06/multi-factor' | ||
// | ||
// and multi-factor authentication has not already been completed in the | ||
// current session/ | ||
|
||
if (context.request.query.acr_values === 'http://schemas.openid.net/pape/policies/2007/06/multi-factor' && !context.authentication.methods.some(method => method.name === 'mfa')) { | ||
|
||
context.multifactor = { | ||
provider: 'any', | ||
allowRememberBrowser: false, | ||
}; | ||
} | ||
|
||
callback(null, user, context); | ||
} |
Binary file not shown.
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,29 @@ | ||
'use strict'; | ||
|
||
const loadRule = require('../utils/load-rule'); | ||
const ContextBuilder = require('../utils/contextBuilder'); | ||
const UserBuilder = require('../utils/userBuilder') | ||
|
||
const ruleName = 'add-email-to-access-token'; | ||
|
||
describe(ruleName, () => { | ||
let user; | ||
let context; | ||
let rule; | ||
|
||
beforeEach(() => { | ||
rule = loadRule(ruleName); | ||
|
||
user = new UserBuilder().build(); | ||
context = new ContextBuilder().build(); | ||
}); | ||
|
||
it('should add email to outgoing accessToken', (done) => { | ||
const namespace = 'https://example.com/'; | ||
rule(user, context, (err, u, c) => { | ||
expect(c.accessToken[namespace + 'email']).toBe(user.email); | ||
|
||
done(); | ||
}); | ||
}); | ||
}); |
53 changes: 53 additions & 0 deletions
53
test/rules/guardian-multifactor-stepup-authentication.test.js
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,53 @@ | ||
'use strict'; | ||
|
||
const loadRule = require('../utils/load-rule'); | ||
const ContextBuilder = require('../utils/contextBuilder'); | ||
const RequestBuilder = require('../utils/requestBuilder'); | ||
|
||
const ruleName = 'guardian-multifactor-ip-range'; | ||
|
||
describe(ruleName, () => { | ||
let user; | ||
let context; | ||
let rule; | ||
|
||
beforeEach(() => { | ||
rule = loadRule(ruleName); | ||
}); | ||
|
||
describe('should set multifactor provider', () => { | ||
beforeEach(() => { | ||
const request = new RequestBuilder().build(); | ||
context = new ContextBuilder() | ||
.withRequest(request) | ||
.build(); | ||
}); | ||
|
||
it('if request contains acr_vales="http://schemas.openid.net/pape/policies/2007/06/multi-factor" and the context.authentication.methods array contains an element for name="mfa"', (done) => { | ||
rule(user, context, (err, u, c) => { | ||
expect(c.multifactor.provider).toBe('guardian'); | ||
expect(c.multifactor.allowRememberBrowser).toBe(false); | ||
|
||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('should do nothing', () => { | ||
beforeEach(() => { | ||
const request = new RequestBuilder().build(); | ||
request.ip = '192.168.1.135'; | ||
context = new ContextBuilder() | ||
.withRequest(request) | ||
.build(); | ||
}); | ||
|
||
it('if acr_values is not in request or if it is present, but not set to "http://schemas.openid.net/pape/policies/2007/06/multi-factor" or the context.authentication.methods array does not contain an element for name="mfa"', (done) => { | ||
rule(user, context, (err, u, c) => { | ||
expect(c.multifactor).toBeFalsy(); | ||
|
||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |