Skip to content
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

Fix: passkey login not working anymore #32623

Merged
merged 19 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions models/auth/webauthn.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"

"github.com/go-webauthn/webauthn/protocol"
"github.com/go-webauthn/webauthn/webauthn"
)

Expand Down Expand Up @@ -50,6 +51,7 @@ type WebAuthnCredential struct {
PublicKey []byte
AttestationType string
AAGUID []byte
Flags protocol.AuthenticatorFlags
SignCount uint32 `xorm:"BIGINT"`
CloneWarning bool
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
Expand Down Expand Up @@ -102,6 +104,12 @@ func (list WebAuthnCredentialList) ToCredentials() []webauthn.Credential {
SignCount: cred.SignCount,
CloneWarning: cred.CloneWarning,
},
Flags: webauthn.CredentialFlags{
UserPresent: cred.Flags.HasUserPresent(),
UserVerified: cred.Flags.HasUserVerified(),
BackupEligible: cred.Flags.HasBackupEligible(),
BackupState: cred.Flags.HasBackupState(),
},
})
}
return creds
Expand Down Expand Up @@ -158,13 +166,27 @@ func GetWebAuthnCredentialByCredID(ctx context.Context, userID int64, credID []b

// CreateCredential will create a new WebAuthnCredential from the given Credential
func CreateCredential(ctx context.Context, userID int64, name string, cred *webauthn.Credential) (*WebAuthnCredential, error) {
var flags protocol.AuthenticatorFlags
if cred.Flags.UserPresent {
flags |= protocol.FlagUserPresent
}
if cred.Flags.UserVerified {
flags |= protocol.FlagUserVerified
}
if cred.Flags.BackupEligible {
flags |= protocol.FlagBackupEligible
}
if cred.Flags.BackupState {
flags |= protocol.FlagBackupState
}
c := &WebAuthnCredential{
UserID: userID,
Name: name,
CredentialID: cred.ID,
PublicKey: cred.PublicKey,
AttestationType: cred.AttestationType,
AAGUID: cred.Authenticator.AAGUID,
Flags: flags,
SignCount: cred.Authenticator.SignCount,
CloneWarning: false,
}
Expand Down
1 change: 1 addition & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ func prepareMigrationTasks() []*migration {
newMigration(307, "Fix milestone deadline_unix when there is no due date", v1_23.FixMilestoneNoDueDate),
newMigration(308, "Add index(user_id, is_deleted) for action table", v1_23.AddNewIndexForUserDashboard),
newMigration(309, "Improve Notification table indices", v1_23.ImproveNotificationTableIndices),
newMigration(310, "Add flags on table webauthn_credential", v1_23.AddFlagsOnWebAuthnCredential),
}
return preparedMigrations
}
Expand Down
25 changes: 25 additions & 0 deletions models/migrations/v1_23/v310.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package v1_23 //nolint

import (
"github.com/go-webauthn/webauthn/protocol"
"xorm.io/xorm"
)

type WebAuthnCredential struct {
Flags protocol.AuthenticatorFlags
}

func (cred WebAuthnCredential) TableName() string {
return "webauthn_credential"
}

func AddFlagsOnWebAuthnCredential(x *xorm.Engine) error {
if err := x.Sync(new(WebAuthnCredential)); err != nil {
return err
}
_, err := x.Exec("UPDATE webauthn_credential SET flags = 29 WHERE id > 0")
return err
}
Loading