Skip to content

Commit bf8eb69

Browse files
committed
AccountPage: SecretsFetcher: use U2fSigner
1 parent db1eed0 commit bf8eb69

File tree

1 file changed

+29
-58
lines changed

1 file changed

+29
-58
lines changed

frontend/pages/AccountPage.tsx

Lines changed: 29 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { U2fSigner } from 'components/U2F';
2-
import { DangerAlert } from 'f61ui/component/alerts';
3-
import { Button, PrimaryLabel } from 'f61ui/component/bootstrap';
2+
import { PrimaryLabel } from 'f61ui/component/bootstrap';
43
import { Breadcrumb } from 'f61ui/component/breadcrumbtrail';
54
import { ClipboardButton } from 'f61ui/component/clipboardbutton';
65
import { CommandIcon, CommandLink } from 'f61ui/component/CommandButton';
@@ -10,7 +9,6 @@ import { MonospaceContent } from 'f61ui/component/monospacecontent';
109
import { OptionalContent } from 'f61ui/component/optionalcontent';
1110
import { Result } from 'f61ui/component/result';
1211
import { SecretReveal } from 'f61ui/component/secretreveal';
13-
import { defaultErrorHandler } from 'f61ui/errors';
1412
import { relativeDateFormat, shouldAlwaysSucceed, unrecognizedValue } from 'f61ui/utils';
1513
import {
1614
AccountAddExternalU2FToken,
@@ -48,78 +46,51 @@ import { ExternalTokenKind, SecretKind } from 'generated/domain_types';
4846
import { AppDefaultLayout } from 'layout/appdefaultlayout';
4947
import * as React from 'react';
5048
import { folderRoute, importotptokenRoute } from 'routes';
51-
import { isU2FError, nativeSignResultToApiType, u2fErrorMsg, u2fSign } from 'u2ftypes';
5249

5350
interface SecretsFetcherProps {
5451
wrappedAccount: WrappedAccount;
5552
fetched: (secrets: ExposedSecret[]) => void;
5653
}
5754

5855
interface SecretsFetcherState {
59-
authing: boolean;
60-
authError?: string;
56+
secrets?: Result<ExposedSecret[]>;
6157
}
6258

6359
class SecretsFetcher extends React.Component<SecretsFetcherProps, SecretsFetcherState> {
64-
state: SecretsFetcherState = { authing: false };
65-
66-
componentDidMount() {
67-
// start fetching process automatically. in some rare cases the user might not
68-
// want this, but failed auth attempt timeouts are not dangerous and this reduces
69-
// extra clicks in the majority case
70-
shouldAlwaysSucceed(this.startSigning());
71-
}
60+
state: SecretsFetcherState = {};
7261

7362
render() {
74-
if (this.state.authing) {
75-
return (
76-
<div>
77-
<p>Please swipe your U2F token now ...</p>
78-
79-
<Loading />
80-
</div>
81-
);
63+
if (this.state.secrets) {
64+
const [, loadingOrError] = this.state.secrets.unwrap();
65+
return loadingOrError;
8266
}
8367

84-
const authErrorNode = this.state.authError && (
85-
<DangerAlert>{this.state.authError}</DangerAlert>
86-
);
87-
8868
return (
89-
<div>
90-
<Button
91-
label="Authenticate"
92-
click={() => {
93-
shouldAlwaysSucceed(this.startSigning());
94-
}}
95-
/>
96-
97-
{authErrorNode}
98-
</div>
69+
<U2fSigner
70+
challenge={this.props.wrappedAccount.ChallengeBundle}
71+
signed={(signature) => {
72+
const secrets = new Result<ExposedSecret[]>((x) => {
73+
this.setState({ secrets: x });
74+
});
75+
76+
this.setState({ secrets });
77+
78+
const secretsProm = getSecrets(this.props.wrappedAccount.Account.Id, signature);
79+
80+
secrets.load(() => secretsProm);
81+
82+
secretsProm.then(
83+
(secretsRaw) => {
84+
this.props.fetched(secretsRaw);
85+
},
86+
() => {
87+
// errors already handled in another handler
88+
},
89+
);
90+
}}
91+
/>
9992
);
10093
}
101-
102-
private async startSigning() {
103-
this.setState({ authing: true, authError: undefined });
104-
105-
try {
106-
const result = await u2fSign(this.props.wrappedAccount.ChallengeBundle.SignRequest);
107-
108-
if (isU2FError(result)) {
109-
this.setState({ authing: false, authError: u2fErrorMsg(result) });
110-
return;
111-
}
112-
113-
const secrets = await getSecrets(this.props.wrappedAccount.Account.Id, {
114-
Challenge: this.props.wrappedAccount.ChallengeBundle.Challenge,
115-
SignResult: nativeSignResultToApiType(result),
116-
});
117-
118-
this.props.fetched(secrets);
119-
} catch (e) {
120-
defaultErrorHandler(e);
121-
}
122-
}
12394
}
12495

12596
interface KeylistAccessorProps {

0 commit comments

Comments
 (0)