-
Notifications
You must be signed in to change notification settings - Fork 171
feat: riskAssessments integration #1217
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
Open
josh-cain
wants to merge
10
commits into
auth0:master
Choose a base branch
from
josh-cain:addRiskAssessmentConfig
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9f6141b
feat: riskAssessments integration
josh-cain 2bf229d
feedback: nest new device settings under riskAssessment
josh-cain 659aed8
Merge branch 'master' into addRiskAssessmentConfig
josh-cain 9f11f9d
Merge branch 'master' into addRiskAssessmentConfig
josh-cain a3145bc
feedbadck: null check for status code
josh-cain 8b30faf
feedback: remove useless catch
josh-cain 98b09de
remove additional references to previous/legacy format
josh-cain c83d43f
null checkery
josh-cain 318b968
shorthand
josh-cain 6818cb8
feedback: singular-ize riskAssessments
josh-cain 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,3 @@ | ||
| { | ||
| "enabled": false | ||
| } |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -419,3 +419,6 @@ userAttributeProfiles: | |
| type: "email" | ||
| required: true | ||
|
|
||
| riskAssessments: | ||
| enabled: false | ||
|
|
||
This file contains hidden or 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 hidden or 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,52 @@ | ||
| import path from 'path'; | ||
| import fs from 'fs-extra'; | ||
| import { constants } from '../../../tools'; | ||
| import { dumpJSON, existsMustBeDir, loadJSON } from '../../../utils'; | ||
| import { DirectoryHandler } from '.'; | ||
| import DirectoryContext from '..'; | ||
| import { ParsedAsset, Asset } from '../../../types'; | ||
|
|
||
| type ParsedRiskAssessments = ParsedAsset<'riskAssessments', Asset>; | ||
|
|
||
| function parse(context: DirectoryContext): ParsedRiskAssessments { | ||
| const riskAssessmentsDirectory = path.join( | ||
| context.filePath, | ||
| constants.RISK_ASSESSMENTS_DIRECTORY | ||
| ); | ||
| const riskAssessmentsFile = path.join(riskAssessmentsDirectory, 'settings.json'); | ||
|
|
||
| if (!existsMustBeDir(riskAssessmentsDirectory)) { | ||
| return { riskAssessments: null }; | ||
| } | ||
|
|
||
| const riskAssessments = loadJSON(riskAssessmentsFile, { | ||
josh-cain marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| mappings: context.mappings, | ||
| disableKeywordReplacement: context.disableKeywordReplacement, | ||
| }); | ||
|
|
||
| return { | ||
| riskAssessments, | ||
| }; | ||
| } | ||
|
|
||
| async function dump(context: DirectoryContext): Promise<void> { | ||
| const { riskAssessments } = context.assets; | ||
|
|
||
| if (!riskAssessments) return; | ||
|
|
||
| const riskAssessmentsDirectory = path.join( | ||
| context.filePath, | ||
| constants.RISK_ASSESSMENTS_DIRECTORY | ||
| ); | ||
| const riskAssessmentsFile = path.join(riskAssessmentsDirectory, 'settings.json'); | ||
|
|
||
| fs.ensureDirSync(riskAssessmentsDirectory); | ||
| dumpJSON(riskAssessmentsFile, riskAssessments); | ||
| } | ||
|
|
||
| const riskAssessmentsHandler: DirectoryHandler<ParsedRiskAssessments> = { | ||
| parse, | ||
| dump, | ||
| }; | ||
|
|
||
| export default riskAssessmentsHandler; | ||
52 changes: 52 additions & 0 deletions
52
src/context/directory/handlers/riskAssessmentsNewDevice.ts
This file contains hidden or 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,52 @@ | ||
| import path from 'path'; | ||
| import fs from 'fs-extra'; | ||
| import { constants } from '../../../tools'; | ||
| import { dumpJSON, existsMustBeDir, loadJSON } from '../../../utils'; | ||
| import { DirectoryHandler } from '.'; | ||
| import DirectoryContext from '..'; | ||
| import { ParsedAsset, Asset } from '../../../types'; | ||
|
|
||
| type ParsedRiskAssessmentsNewDevice = ParsedAsset<'riskAssessmentsNewDevice', Asset>; | ||
|
|
||
| function parse(context: DirectoryContext): ParsedRiskAssessmentsNewDevice { | ||
| const riskAssessmentsDirectory = path.join( | ||
| context.filePath, | ||
| constants.RISK_ASSESSMENTS_DIRECTORY | ||
| ); | ||
| const newDeviceFile = path.join(riskAssessmentsDirectory, 'new-device.json'); | ||
|
|
||
| if (!existsMustBeDir(riskAssessmentsDirectory)) { | ||
| return { riskAssessmentsNewDevice: null }; | ||
| } | ||
|
|
||
| const riskAssessmentsNewDevice = loadJSON(newDeviceFile, { | ||
| mappings: context.mappings, | ||
| disableKeywordReplacement: context.disableKeywordReplacement, | ||
| }); | ||
|
|
||
| return { | ||
| riskAssessmentsNewDevice, | ||
| }; | ||
| } | ||
|
|
||
| async function dump(context: DirectoryContext): Promise<void> { | ||
| const { riskAssessmentsNewDevice } = context.assets; | ||
|
|
||
| if (!riskAssessmentsNewDevice) return; | ||
|
|
||
| const riskAssessmentsDirectory = path.join( | ||
| context.filePath, | ||
| constants.RISK_ASSESSMENTS_DIRECTORY | ||
| ); | ||
| const newDeviceFile = path.join(riskAssessmentsDirectory, 'new-device.json'); | ||
|
|
||
| fs.ensureDirSync(riskAssessmentsDirectory); | ||
| dumpJSON(newDeviceFile, riskAssessmentsNewDevice); | ||
| } | ||
|
|
||
| const riskAssessmentsNewDeviceHandler: DirectoryHandler<ParsedRiskAssessmentsNewDevice> = { | ||
| parse, | ||
| dump, | ||
| }; | ||
|
|
||
| export default riskAssessmentsNewDeviceHandler; |
This file contains hidden or 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 hidden or 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 @@ | ||
| import { YAMLHandler } from '.'; | ||
| import YAMLContext from '..'; | ||
| import { Asset, ParsedAsset } from '../../../types'; | ||
|
|
||
| type ParsedRiskAssessments = ParsedAsset<'riskAssessments', Asset>; | ||
|
|
||
| async function parse(context: YAMLContext): Promise<ParsedRiskAssessments> { | ||
| const { riskAssessments } = context.assets; | ||
|
|
||
| if (!riskAssessments) return { riskAssessments: null }; | ||
|
|
||
| return { | ||
| riskAssessments, | ||
| }; | ||
| } | ||
|
|
||
| async function dump(context: YAMLContext): Promise<ParsedRiskAssessments> { | ||
| const { riskAssessments } = context.assets; | ||
|
|
||
| if (!riskAssessments) return { riskAssessments: null }; | ||
|
|
||
| return { | ||
| riskAssessments, | ||
| }; | ||
| } | ||
|
|
||
| const riskAssessmentsHandler: YAMLHandler<ParsedRiskAssessments> = { | ||
| parse: parse, | ||
| dump: dump, | ||
| }; | ||
josh-cain marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| export default riskAssessmentsHandler; | ||
This file contains hidden or 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 @@ | ||
| import { YAMLHandler } from '.'; | ||
| import YAMLContext from '..'; | ||
| import { Asset, ParsedAsset } from '../../../types'; | ||
|
|
||
| type ParsedRiskAssessmentsNewDevice = ParsedAsset<'riskAssessmentsNewDevice', Asset>; | ||
|
|
||
| async function parse(context: YAMLContext): Promise<ParsedRiskAssessmentsNewDevice> { | ||
| const { riskAssessmentsNewDevice } = context.assets; | ||
|
|
||
| if (!riskAssessmentsNewDevice) return { riskAssessmentsNewDevice: null }; | ||
|
|
||
| return { | ||
| riskAssessmentsNewDevice, | ||
| }; | ||
| } | ||
|
|
||
| async function dump(context: YAMLContext): Promise<ParsedRiskAssessmentsNewDevice> { | ||
| const { riskAssessmentsNewDevice } = context.assets; | ||
|
|
||
| if (!riskAssessmentsNewDevice) return { riskAssessmentsNewDevice: null }; | ||
|
|
||
| return { | ||
| riskAssessmentsNewDevice, | ||
| }; | ||
| } | ||
|
|
||
| const riskAssessmentsNewDeviceHandler: YAMLHandler<ParsedRiskAssessmentsNewDevice> = { | ||
| parse: parse, | ||
| dump: dump, | ||
| }; | ||
|
|
||
| export default riskAssessmentsNewDeviceHandler; |
This file contains hidden or 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 hidden or 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,65 @@ | ||
| import DefaultAPIHandler from './default'; | ||
| import { Assets } from '../../../types'; | ||
|
|
||
| export const schema = { | ||
| type: 'object', | ||
| properties: { | ||
| enabled: { | ||
| type: 'boolean', | ||
| description: 'Whether or not risk assessment is enabled.', | ||
| }, | ||
| }, | ||
| required: ['enabled'], | ||
| }; | ||
|
|
||
| export type RiskAssessmentsSettings = { | ||
| enabled: boolean; | ||
| }; | ||
|
|
||
| export default class RiskAssessmentsHandler extends DefaultAPIHandler { | ||
| existing: RiskAssessmentsSettings | null; | ||
|
|
||
| constructor(config: DefaultAPIHandler) { | ||
| super({ | ||
| ...config, | ||
| type: 'riskAssessments', | ||
| }); | ||
| } | ||
|
|
||
| async getType(): Promise<RiskAssessmentsSettings> { | ||
| if (this.existing) { | ||
| return this.existing; | ||
| } | ||
|
|
||
| try { | ||
| const { data } = await this.client.riskAssessments.getSettings(); | ||
| this.existing = data; | ||
| return data; | ||
| } catch (err) { | ||
| if (err.statusCode === 404) return { enabled: false }; | ||
kushalshit27 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw err; | ||
| } | ||
| } | ||
|
|
||
| async processChanges(assets: Assets): Promise<void> { | ||
| const { riskAssessments } = assets; | ||
|
|
||
| // Non-existing section means it doesn't need to be processed | ||
| if (!riskAssessments) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| // Validate that enabled property exists | ||
| const settings: RiskAssessmentsSettings = { | ||
| enabled: riskAssessments.enabled as boolean, | ||
| }; | ||
|
|
||
| await this.client.riskAssessments.updateSettings(settings); | ||
| this.updated += 1; | ||
| this.didUpdate(settings); | ||
| } catch (err) { | ||
| throw err; | ||
| } | ||
josh-cain marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or 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,65 @@ | ||
| import DefaultAPIHandler from './default'; | ||
| import { Assets } from '../../../types'; | ||
|
|
||
| export const schema = { | ||
| type: 'object', | ||
| properties: { | ||
| remember_for: { | ||
| type: 'number', | ||
| description: 'Length of time to remember devices for, in days.', | ||
| }, | ||
| }, | ||
| required: ['remember_for'], | ||
| }; | ||
|
|
||
| export type RiskAssessmentsNewDeviceSettings = { | ||
| remember_for: number; | ||
| }; | ||
|
|
||
| export default class RiskAssessmentsNewDeviceHandler extends DefaultAPIHandler { | ||
| existing: RiskAssessmentsNewDeviceSettings | null; | ||
|
|
||
| constructor(config: DefaultAPIHandler) { | ||
| super({ | ||
| ...config, | ||
| type: 'riskAssessmentsNewDevice', | ||
| }); | ||
| } | ||
|
|
||
| async getType(): Promise<RiskAssessmentsNewDeviceSettings> { | ||
| if (this.existing) { | ||
| return this.existing; | ||
| } | ||
|
|
||
| try { | ||
| const { data } = await this.client.riskAssessments.getNewDeviceSettings(); | ||
| this.existing = data; | ||
| return data; | ||
| } catch (err) { | ||
| if (err.statusCode === 404) return { remember_for: 0 }; | ||
| throw err; | ||
| } | ||
| } | ||
|
|
||
| async processChanges(assets: Assets): Promise<void> { | ||
| const { riskAssessmentsNewDevice } = assets; | ||
|
|
||
| // Non-existing section means it doesn't need to be processed | ||
| if (!riskAssessmentsNewDevice) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| // Validate that remember_for property exists | ||
| const settings: RiskAssessmentsNewDeviceSettings = { | ||
| remember_for: riskAssessmentsNewDevice.remember_for as number, | ||
| }; | ||
|
|
||
| await this.client.riskAssessments.updateNewDeviceSettings(settings); | ||
| this.updated += 1; | ||
| this.didUpdate(settings); | ||
| } catch (err) { | ||
| throw err; | ||
| } | ||
| } | ||
| } |
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.