-
-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial and registration access token policies
Policies are sync/async functions that are assigned to an Initial Access
Token that run before the regular client property validations are run.
Multiple policies may be assigned to an Initial Access Token and by
default the same policies will transfer over to the Registration Access
Token. A policy may throw / reject and it may modify the properties
object.
Resolves #394
- Loading branch information
Showing
13 changed files
with
614 additions
and
21 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
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
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,14 +1,12 @@ | ||
const apply = require('./mixins/apply'); | ||
const hasFormat = require('./mixins/has_format'); | ||
const hasPolicies = require('./mixins/has_policies'); | ||
|
||
module.exports = provider => class InitialAccessToken extends hasFormat( | ||
provider, | ||
'InitialAccessToken', | ||
provider.BaseToken, | ||
) { | ||
module.exports = provider => class InitialAccessToken extends apply([ | ||
hasPolicies(provider), | ||
hasFormat(provider, 'InitialAccessToken', provider.BaseToken), | ||
]) { | ||
static get IN_PAYLOAD() { | ||
return [ | ||
'jti', | ||
'kind', | ||
]; | ||
return super.IN_PAYLOAD.filter(v => v !== 'clientId'); | ||
} | ||
}; |
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,32 @@ | ||
const assert = require('assert'); | ||
|
||
const instance = require('../../helpers/weak_cache'); | ||
|
||
function validate(provider, policies) { | ||
assert(Array.isArray(policies), 'policies must be an array'); | ||
assert(policies.length, 'policies must not be empty'); | ||
policies.forEach((policy) => { | ||
assert(typeof policy === 'string', 'policies must be strings'); | ||
assert(instance(provider).configuration(`features.registration.policies.${policy}`), `policy ${policy} not configured`); | ||
}); | ||
} | ||
|
||
module.exports = provider => superclass => class extends superclass { | ||
async save() { | ||
if (typeof this.policies !== 'undefined') validate(provider, this.policies); | ||
return super.save(); | ||
} | ||
|
||
static async find(...args) { | ||
const result = await super.find(...args); | ||
if (result && typeof result.policies !== 'undefined') validate(provider, result.policies); | ||
return result; | ||
} | ||
|
||
static get IN_PAYLOAD() { | ||
return [ | ||
...super.IN_PAYLOAD, | ||
'policies', | ||
]; | ||
} | ||
}; |
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,8 @@ | ||
const apply = require('./mixins/apply'); | ||
const hasFormat = require('./mixins/has_format'); | ||
const hasPolicies = require('./mixins/has_policies'); | ||
|
||
module.exports = provider => class RegistrationAccessToken extends hasFormat( | ||
provider, | ||
'RegistrationAccessToken', | ||
provider.BaseToken, | ||
) {}; | ||
module.exports = provider => class RegistrationAccessToken extends apply([ | ||
hasPolicies(provider), | ||
hasFormat(provider, 'RegistrationAccessToken', provider.BaseToken), | ||
]) {}; |
17 changes: 17 additions & 0 deletions
17
test/registration_policies/registration_policies.config.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,17 @@ | ||
const { clone } = require('lodash'); | ||
|
||
const config = clone(require('../default.config')); | ||
|
||
config.features = { | ||
registrationManagement: true, | ||
registration: { | ||
initialAccessToken: true, | ||
policies: { | ||
'empty-policy': () => {}, | ||
}, | ||
}, | ||
}; | ||
|
||
module.exports = { | ||
config, | ||
}; |
Oops, something went wrong.