From c260574ddad899474e3de6d866c14786808559b1 Mon Sep 17 00:00:00 2001 From: Richard Ma Date: Tue, 12 Oct 2021 18:30:12 -0700 Subject: [PATCH 1/6] disable duplicate device to be enrolled --- static/app/components/u2f/u2finterface.tsx | 44 ++++++++++++++++------ static/app/types/index.tsx | 1 + 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/static/app/components/u2f/u2finterface.tsx b/static/app/components/u2f/u2finterface.tsx index 6e219e91a88129..0d545143e282b9 100644 --- a/static/app/components/u2f/u2finterface.tsx +++ b/static/app/components/u2f/u2finterface.tsx @@ -49,18 +49,7 @@ class U2fInterface extends React.Component { } } - invokeU2fFlow() { - let promise: Promise; - - if (this.props.flowMode === 'sign') { - promise = u2f.sign(this.props.challengeData.authenticateRequests); - } else if (this.props.flowMode === 'enroll') { - const {registerRequests, authenticateRequests} = this.props.challengeData; - promise = u2f.register(registerRequests, authenticateRequests); - } else { - throw new Error(`Unsupported flow mode '${this.props.flowMode}'`); - } - + invokeU2fSetupFlow(promise) { promise .then(data => { this.setState( @@ -122,6 +111,37 @@ class U2fInterface extends React.Component { }); } + invokeU2fFlow() { + let promise: Promise; + + if (this.props.flowMode === 'sign') { + promise = u2f.sign(this.props.challengeData.authenticateRequests); + } else if (this.props.flowMode === 'enroll') { + const {registerRequests, authenticateRequests, registeredKeys} = + this.props.challengeData; + if (registeredKeys.length !== 0) { + u2f + .register(registerRequests, registeredKeys) + .then(() => { + this.invokeU2fSetupFlow(promise); + }) + .catch(err => { + const failure = 'DUPLICATE_DEVICE'; + Sentry.captureException(err); + this.setState({ + deviceFailure: failure, + hasBeenTapped: false, + }); + }); + } else { + promise = u2f.register(registerRequests, authenticateRequests); + this.invokeU2fSetupFlow(promise); + } + } else { + throw new Error(`Unsupported flow mode '${this.props.flowMode}'`); + } + } + onTryAgain = () => { this.setState( {hasBeenTapped: false, deviceFailure: null}, diff --git a/static/app/types/index.tsx b/static/app/types/index.tsx index ef71dd58a7c232..f108a8673f8d4a 100644 --- a/static/app/types/index.tsx +++ b/static/app/types/index.tsx @@ -728,6 +728,7 @@ export type Authenticator = { export type ChallengeData = { authenticateRequests: u2f.SignRequest; registerRequests: u2f.RegisterRequest; + registeredKeys: u2f.RegisteredKey; }; export type EnrolledAuthenticator = { From 463e49ec3317a22b865e3bfbd041b93d6a2085c7 Mon Sep 17 00:00:00 2001 From: Richard Ma Date: Wed, 13 Oct 2021 10:59:55 -0700 Subject: [PATCH 2/6] fixing type errors --- static/app/components/u2f/u2finterface.tsx | 4 ++-- static/app/types/index.tsx | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/static/app/components/u2f/u2finterface.tsx b/static/app/components/u2f/u2finterface.tsx index 0d545143e282b9..a443c05466c645 100644 --- a/static/app/components/u2f/u2finterface.tsx +++ b/static/app/components/u2f/u2finterface.tsx @@ -121,7 +121,7 @@ class U2fInterface extends React.Component { this.props.challengeData; if (registeredKeys.length !== 0) { u2f - .register(registerRequests, registeredKeys) + .register(registerRequests as any, registeredKeys as any) .then(() => { this.invokeU2fSetupFlow(promise); }) @@ -200,7 +200,7 @@ class U2fInterface extends React.Component { { UNKNOWN_ERROR: t('There was an unknown problem, please try again'), DEVICE_ERROR: t('Your U2F device reported an error.'), - DUPLICATE_DEVICE: t('This device is already in use.'), + DUPLICATE_DEVICE: t('This device is already registered with Sentry.'), UNKNOWN_DEVICE: t('The device you used for sign-in is unknown.'), BAD_APPID: tct( '[p1:The Sentry server administrator modified the ' + diff --git a/static/app/types/index.tsx b/static/app/types/index.tsx index f108a8673f8d4a..5f9444aa8c3a98 100644 --- a/static/app/types/index.tsx +++ b/static/app/types/index.tsx @@ -726,9 +726,10 @@ export type Authenticator = { ); export type ChallengeData = { - authenticateRequests: u2f.SignRequest; - registerRequests: u2f.RegisterRequest; - registeredKeys: u2f.RegisteredKey; + // will have only authenticateRequest or registerRequest + authenticateRequests?: u2f.SignRequest[]; + registerRequests?: u2f.RegisterRequest[]; + registeredKeys: u2f.RegisteredKey[]; }; export type EnrolledAuthenticator = { From a8feb9de4430bdd8a7dc0c8dc71fa8edc872f989 Mon Sep 17 00:00:00 2001 From: Richard Ma Date: Wed, 13 Oct 2021 11:14:46 -0700 Subject: [PATCH 3/6] typing fix --- static/app/types/index.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/static/app/types/index.tsx b/static/app/types/index.tsx index 5f9444aa8c3a98..8210f5fa696c1f 100644 --- a/static/app/types/index.tsx +++ b/static/app/types/index.tsx @@ -727,8 +727,8 @@ export type Authenticator = { export type ChallengeData = { // will have only authenticateRequest or registerRequest - authenticateRequests?: u2f.SignRequest[]; - registerRequests?: u2f.RegisterRequest[]; + authenticateRequests: u2f.SignRequest; + registerRequests: u2f.RegisterRequest; registeredKeys: u2f.RegisteredKey[]; }; From 334e772ec5878d1ef31be49b6185fe9d04f787f7 Mon Sep 17 00:00:00 2001 From: Richard Ma Date: Wed, 13 Oct 2021 15:00:22 -0700 Subject: [PATCH 4/6] fix functionality --- static/app/components/u2f/u2finterface.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/static/app/components/u2f/u2finterface.tsx b/static/app/components/u2f/u2finterface.tsx index a443c05466c645..9ddd5baf821929 100644 --- a/static/app/components/u2f/u2finterface.tsx +++ b/static/app/components/u2f/u2finterface.tsx @@ -123,6 +123,7 @@ class U2fInterface extends React.Component { u2f .register(registerRequests as any, registeredKeys as any) .then(() => { + promise = u2f.register(registerRequests, authenticateRequests); this.invokeU2fSetupFlow(promise); }) .catch(err => { From 22bfd6fc7b86008205e5bd942f79062ac58d9590 Mon Sep 17 00:00:00 2001 From: Richard Ma Date: Fri, 15 Oct 2021 11:56:26 -0700 Subject: [PATCH 5/6] fixed code functionality --- static/app/components/u2f/u2finterface.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/static/app/components/u2f/u2finterface.tsx b/static/app/components/u2f/u2finterface.tsx index 9ddd5baf821929..2cfc781b639250 100644 --- a/static/app/components/u2f/u2finterface.tsx +++ b/static/app/components/u2f/u2finterface.tsx @@ -116,6 +116,7 @@ class U2fInterface extends React.Component { if (this.props.flowMode === 'sign') { promise = u2f.sign(this.props.challengeData.authenticateRequests); + this.invokeU2fSetupFlow(promise); } else if (this.props.flowMode === 'enroll') { const {registerRequests, authenticateRequests, registeredKeys} = this.props.challengeData; From f82dc1b0919133870e3d63f717e22e8c2d9e1015 Mon Sep 17 00:00:00 2001 From: Richard Ma Date: Fri, 15 Oct 2021 14:09:34 -0700 Subject: [PATCH 6/6] big code refactor --- static/app/components/u2f/u2finterface.tsx | 27 ++++------------------ 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/static/app/components/u2f/u2finterface.tsx b/static/app/components/u2f/u2finterface.tsx index 2cfc781b639250..4f65d64b244ac5 100644 --- a/static/app/components/u2f/u2finterface.tsx +++ b/static/app/components/u2f/u2finterface.tsx @@ -49,7 +49,7 @@ class U2fInterface extends React.Component { } } - invokeU2fSetupFlow(promise) { + submitU2fResponse(promise) { promise .then(data => { this.setState( @@ -116,32 +116,13 @@ class U2fInterface extends React.Component { if (this.props.flowMode === 'sign') { promise = u2f.sign(this.props.challengeData.authenticateRequests); - this.invokeU2fSetupFlow(promise); } else if (this.props.flowMode === 'enroll') { - const {registerRequests, authenticateRequests, registeredKeys} = - this.props.challengeData; - if (registeredKeys.length !== 0) { - u2f - .register(registerRequests as any, registeredKeys as any) - .then(() => { - promise = u2f.register(registerRequests, authenticateRequests); - this.invokeU2fSetupFlow(promise); - }) - .catch(err => { - const failure = 'DUPLICATE_DEVICE'; - Sentry.captureException(err); - this.setState({ - deviceFailure: failure, - hasBeenTapped: false, - }); - }); - } else { - promise = u2f.register(registerRequests, authenticateRequests); - this.invokeU2fSetupFlow(promise); - } + const {registerRequests, registeredKeys} = this.props.challengeData; + promise = u2f.register(registerRequests as any, registeredKeys as any); } else { throw new Error(`Unsupported flow mode '${this.props.flowMode}'`); } + this.submitU2fResponse(promise); } onTryAgain = () => {