Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'u2f-api' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
try {
triggerTimer();
setState(State.WaitTouch);
const signRequest = await initiateU2FSignin();
const signRequests: u2fApi.SignRequest[] = [];
for (var i in signRequest.registeredKeys) {
const r = signRequest.registeredKeys[i];
signRequests.push({
appId: signRequest.appId,
challenge: signRequest.challenge,
keyHandle: r.keyHandle,
version: r.version,
})
}
const signResponse = await u2fApi.sign(signRequests, signInTimeout);
// If the request was initiated and the user changed 2FA method in the meantime,
// the process is interrupted to avoid updating state of unmounted component.
if (!mounted.current) return;
setState(State.SigninInProgress);
const res = await completeU2FSignin(signResponse, redirectionURL);
onSignInSuccessCallback(res ? res.redirect : undefined);
} catch (err) {
// If the request was initiated and the user changed 2FA method in the meantime,
// the process is interrupted to avoid updating state of unmounted component.
if (!mounted.current) return;
console.error(err);
onSignInErrorCallback(new Error("Failed to initiate security key sign in process"));
setState(State.Failure);
}
}, [onSignInSuccessCallback, onSignInErrorCallback, redirectionURL, mounted, triggerTimer, props.authenticationLevel, props.registered]);
]);
const key = {
appId: location.origin,
challenge: DigitalBitboxUsb.webSafe64(challenge.toString('base64')),
version: 'U2F_V2',
keyHandle: DigitalBitboxUsb.webSafe64(kh.toString('base64'))
};
// Set timeout to 35 seconds for Windows 10 to wait for user confirmation
// Keep 3 seconds for other plaforms so that polling is fast enough
let timeout;
navigator.platform.indexOf('Win') >= 0 &&
(navigator.userAgent.indexOf('Windows NT 10.0') != -1 ||
navigator.userAgent.indexOf('Windows 10.0') != -1)
? (timeout = 35)
: (timeout = 3);
u2f
.sign(key, timeout)
.then(localCallback)
.catch(err => {
callback(undefined, err);
});
}
};
invokeU2fFlow = () => {
let 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}'`);
}
promise
.then(data => {
this.setState(
{
hasBeenTapped: true,
},
() => {
const u2fResponse = JSON.stringify(data);
const challenge = JSON.stringify(this.props.challengeData);
// eslint-disable-next-line react/no-direct-mutation-state
this.state.responseElement.value = u2fResponse;
onInit: async (token: string) => {
try {
dispatch(registerSecurityKey());
const registerRequest = await AutheliaService.completeSecurityKeyRegistrationIdentityValidation(token);
const registerRequests: U2fApi.RegisterRequest[] = [];
for(var i in registerRequest.registerRequests) {
const r = registerRequest.registerRequests[i];
registerRequests.push({
appId: registerRequest.appId,
challenge: r.challenge,
version: r.version,
})
}
const registerResponse = await U2fApi.register(registerRequests, [], 60);
await AutheliaService.completeSecurityKeyRegistration(registerResponse);
dispatch(registerSecurityKeySuccess());
setTimeout(() => {
ownProps.history.push('/');
}, 2000);
} catch(err) {
console.error(err);
dispatch(registerSecurityKeyFailure(err.message));
}
},
onBackClicked: () => {
async function triggerSecurityKeySigning(dispatch: Dispatch, props: Props) {
let err, result;
dispatch(securityKeySign());
[err, result] = await to(requestSigning());
if (err) {
dispatch(securityKeySignFailure(err.message));
return;
}
[err, result] = await to(u2fApi.sign(result, 60));
if (err) {
dispatch(securityKeySignFailure(err.message));
return;
}
[err, result] = await to(completeSecurityKeySigning(result as SignResponse));
if (err) {
dispatch(securityKeySignFailure(err.message));
return;
}
dispatch(securityKeySignSuccess());
await redirectUponAuthentication(props);
}
.then(function (signRequest: U2f.Request) {
notifier.info(UserMessages.PLEASE_TOUCH_TOKEN);
return U2fApi.sign(signRequest, 60);
})
.then(function (signResponse: U2fApi.SignResponse) {
): Promise {
const keyHandle = wrapApdu(apdu, scrambleKey);
const challenge = Buffer.from(
"0000000000000000000000000000000000000000000000000000000000000000",
"hex"
);
const signRequest = {
version: "U2F_V2",
keyHandle: webSafe64(keyHandle.toString("base64")),
challenge: webSafe64(challenge.toString("base64")),
appId: location.origin
};
if (debug) {
debug("=> " + apdu.toString("hex"));
}
return sign(signRequest, timeoutMillis / 1000).then(response => {
const { signatureData } = response;
if (typeof signatureData === "string") {
const data = Buffer.from(normal64(signatureData), "base64");
let result;
if (!unwrap) {
result = data;
} else {
result = data.slice(5);
}
if (debug) {
debug("<= " + result.toString("hex"));
}
return result;
} else {
throw response;
}
async function triggerSecurityKeySigning(dispatch: Dispatch, redirectionUrl: string | null) {
dispatch(securityKeySign());
const signRequest = await AutheliaService.requestSigning();
const signRequests: u2fApi.SignRequest[] = [];
for (var i in signRequest.registeredKeys) {
const r = signRequest.registeredKeys[i];
signRequests.push({
appId: signRequest.appId,
challenge: signRequest.challenge,
keyHandle: r.keyHandle,
version: r.version,
})
}
const signResponse = await u2fApi.sign(signRequests, 60);
const response = await AutheliaService.completeSecurityKeySigning(signResponse, redirectionUrl);
dispatch(securityKeySignSuccess());
if (response) {
window.location.href = response.redirect;
return;
}
await handleSuccess(dispatch, 1000);
}
invokeU2fFlow = () => {
let 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}'`);
}
promise
.then(data => {
this.setState(
{
hasBeenTapped: true,
},
() => {
const u2fResponse = JSON.stringify(data);
const challenge = JSON.stringify(this.props.challengeData);
static listen = (observer: *) => {
let unsubscribed = false;
isSupported().then(supported => {
if (unsubscribed) return;
if (supported) {
observer.next({ type: "add", descriptor: null });
observer.complete();
} else {
observer.error(
new TransportError(
"U2F browser support is needed for Ledger. " +
"Please use Chrome, Opera or Firefox with a U2F extension. " +
"Also make sure you're on an HTTPS connection",
"U2FNotSupported"
)
);
}
});
return {