fix: fix webauthn entry cannot add bug (#960)

* fix: fix webauthn

* Update LoginPage.js

Co-authored-by: Yang Luo <hsluoyz@qq.com>
This commit is contained in:
Товарищ программист
2022-08-05 17:43:04 +08:00
committed by GitHub
parent e94ada9ea2
commit 98ef766fb4
2 changed files with 30 additions and 15 deletions

View File

@ -146,6 +146,16 @@ class LoginPage extends React.Component {
} }
onFinish(values) { onFinish(values) {
if (this.state.loginMethod === "webAuthn") {
let username = this.state.username;
if (username === null || username === "") {
username = values["username"];
}
this.signInWithWebAuthn(username);
return;
}
const application = this.getApplicationObj(); const application = this.getApplicationObj();
const ths = this; const ths = this;
@ -499,9 +509,14 @@ class LoginPage extends React.Component {
</Button> </Button>
) : ) :
( (
<Button type="primary" style={{width: "100%", marginBottom: "5px"}} onClick={() => this.signInWithWebAuthn()}> <Button
{i18next.t("login:Sign in with WebAuthn")} type="primary"
</Button> htmlType="submit"
style={{width: "100%", marginBottom: "5px"}}
disabled={!application.enablePassword}
>
{i18next.t("login:Sign in with WebAuthn")}
</Button>
) )
} }
{ {
@ -639,14 +654,14 @@ class LoginPage extends React.Component {
); );
} }
signInWithWebAuthn() { signInWithWebAuthn(username) {
if (this.state.username === null || this.state.username === "") { if (username === null || username === "") {
Setting.showMessage("error", "username is required for webauthn login"); Setting.showMessage("error", "username is required for webauthn login");
return; return;
} }
let application = this.getApplicationObj(); let application = this.getApplicationObj();
return fetch(`${Setting.ServerUrl}/api/webauthn/signin/begin?owner=${application.organization}&name=${this.state.username}`, { return fetch(`${Setting.ServerUrl}/api/webauthn/signin/begin?owner=${application.organization}&name=${username}`, {
method: "GET", method: "GET",
credentials: "include" credentials: "include"
}) })
@ -657,13 +672,13 @@ class LoginPage extends React.Component {
throw credentialRequestOptions.status.msg; throw credentialRequestOptions.status.msg;
} }
credentialRequestOptions.certificate.challenge = UserWebauthnBackend.webAuthnBufferDecode(credentialRequestOptions.certificate.challenge); credentialRequestOptions.publicKey.challenge = UserWebauthnBackend.webAuthnBufferDecode(credentialRequestOptions.publicKey.challenge);
credentialRequestOptions.certificate.allowCredentials.forEach(function(listItem) { credentialRequestOptions.publicKey.allowCredentials.forEach(function(listItem) {
listItem.id = UserWebauthnBackend.webAuthnBufferDecode(listItem.id); listItem.id = UserWebauthnBackend.webAuthnBufferDecode(listItem.id);
}); });
return navigator.credentials.get({ return navigator.credentials.get({
certificate: credentialRequestOptions.certificate publicKey: credentialRequestOptions.publicKey
}); });
}) })
.then((assertion) => { .then((assertion) => {

View File

@ -21,15 +21,15 @@ export function registerWebauthnCredential() {
}) })
.then(res => res.json()) .then(res => res.json())
.then((credentialCreationOptions) => { .then((credentialCreationOptions) => {
credentialCreationOptions.certificate.challenge = webAuthnBufferDecode(credentialCreationOptions.certificate.challenge); credentialCreationOptions.publicKey.challenge = webAuthnBufferDecode(credentialCreationOptions.publicKey.challenge);
credentialCreationOptions.certificate.user.id = webAuthnBufferDecode(credentialCreationOptions.certificate.user.id); credentialCreationOptions.publicKey.user.id = webAuthnBufferDecode(credentialCreationOptions.publicKey.user.id);
if (credentialCreationOptions.certificate.excludeCredentials) { if (credentialCreationOptions.publicKey.excludeCredentials) {
for (var i = 0; i < credentialCreationOptions.certificate.excludeCredentials.length; i++) { for (var i = 0; i < credentialCreationOptions.publicKey.excludeCredentials.length; i++) {
credentialCreationOptions.certificate.excludeCredentials[i].id = webAuthnBufferDecode(credentialCreationOptions.certificate.excludeCredentials[i].id); credentialCreationOptions.publicKey.excludeCredentials[i].id = webAuthnBufferDecode(credentialCreationOptions.publicKey.excludeCredentials[i].id);
} }
} }
return navigator.credentials.create({ return navigator.credentials.create({
certificate: credentialCreationOptions.certificate publicKey: credentialCreationOptions.publicKey
}); });
}) })
.then((credential) => { .then((credential) => {