Skip to content

Commit e670ec4

Browse files
committed
store flags to database
1 parent bff0317 commit e670ec4

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

models/auth/webauthn.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"code.gitea.io/gitea/models/db"
1212
"code.gitea.io/gitea/modules/timeutil"
1313
"code.gitea.io/gitea/modules/util"
14+
"github.com/go-webauthn/webauthn/protocol"
1415

1516
"github.com/go-webauthn/webauthn/webauthn"
1617
)
@@ -50,6 +51,7 @@ type WebAuthnCredential struct {
5051
PublicKey []byte
5152
AttestationType string
5253
AAGUID []byte
54+
Flags protocol.AuthenticatorFlags
5355
SignCount uint32 `xorm:"BIGINT"`
5456
CloneWarning bool
5557
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
@@ -103,7 +105,10 @@ func (list WebAuthnCredentialList) ToCredentials() []webauthn.Credential {
103105
CloneWarning: cred.CloneWarning,
104106
},
105107
Flags: webauthn.CredentialFlags{
106-
BackupEligible: true,
108+
UserPresent: cred.Flags.HasUserPresent(),
109+
UserVerified: cred.Flags.HasUserVerified(),
110+
BackupEligible: cred.Flags.HasBackupEligible(),
111+
BackupState: cred.Flags.HasBackupState(),
107112
},
108113
})
109114
}
@@ -161,13 +166,27 @@ func GetWebAuthnCredentialByCredID(ctx context.Context, userID int64, credID []b
161166

162167
// CreateCredential will create a new WebAuthnCredential from the given Credential
163168
func CreateCredential(ctx context.Context, userID int64, name string, cred *webauthn.Credential) (*WebAuthnCredential, error) {
169+
var flags protocol.AuthenticatorFlags
170+
if cred.Flags.UserPresent {
171+
flags |= protocol.FlagUserPresent
172+
}
173+
if cred.Flags.UserVerified {
174+
flags |= protocol.FlagUserVerified
175+
}
176+
if cred.Flags.BackupEligible {
177+
flags |= protocol.FlagBackupEligible
178+
}
179+
if cred.Flags.BackupState {
180+
flags |= protocol.FlagBackupState
181+
}
164182
c := &WebAuthnCredential{
165183
UserID: userID,
166184
Name: name,
167185
CredentialID: cred.ID,
168186
PublicKey: cred.PublicKey,
169187
AttestationType: cred.AttestationType,
170188
AAGUID: cred.Authenticator.AAGUID,
189+
Flags: flags,
171190
SignCount: cred.Authenticator.SignCount,
172191
CloneWarning: false,
173192
}

models/migrations/migrations.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ func prepareMigrationTasks() []*migration {
367367
newMigration(307, "Fix milestone deadline_unix when there is no due date", v1_23.FixMilestoneNoDueDate),
368368
newMigration(308, "Add index(user_id, is_deleted) for action table", v1_23.AddNewIndexForUserDashboard),
369369
newMigration(309, "Improve Notification table indices", v1_23.ImproveNotificationTableIndices),
370+
newMigration(310, "Add flags on table webauthn_credential", v1_23.AddFlagsOnWebAuthnCredential),
370371
}
371372
return preparedMigrations
372373
}

models/migrations/v1_23/v310.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1_23 //nolint
5+
6+
import (
7+
"github.com/go-webauthn/webauthn/protocol"
8+
"xorm.io/xorm"
9+
)
10+
11+
func AddFlagsOnWebAuthnCredential(x *xorm.Engine) error {
12+
type WebAuthnCredential struct {
13+
Flags protocol.AuthenticatorFlags
14+
}
15+
if err := x.Sync(new(WebAuthnCredential)); err != nil {
16+
return err
17+
}
18+
_, err := x.Exec("UPDATE webauthn_credential SET flags = 29")
19+
return err
20+
}

0 commit comments

Comments
 (0)