|
4 | 4 | package webauthn |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "context" |
7 | 8 | "encoding/binary" |
8 | 9 | "encoding/gob" |
9 | 10 |
|
10 | 11 | "code.gitea.io/gitea/models/auth" |
11 | | - "code.gitea.io/gitea/models/db" |
12 | 12 | user_model "code.gitea.io/gitea/models/user" |
13 | 13 | "code.gitea.io/gitea/modules/setting" |
| 14 | + "code.gitea.io/gitea/modules/util" |
14 | 15 |
|
15 | 16 | "github.com/go-webauthn/webauthn/protocol" |
16 | 17 | "github.com/go-webauthn/webauthn/webauthn" |
@@ -38,40 +39,42 @@ func Init() { |
38 | 39 | } |
39 | 40 | } |
40 | 41 |
|
41 | | -// User represents an implementation of webauthn.User based on User model |
42 | | -type User user_model.User |
| 42 | +// user represents an implementation of webauthn.User based on User model |
| 43 | +type user struct { |
| 44 | + ctx context.Context |
| 45 | + User *user_model.User |
| 46 | + |
| 47 | + defaultAuthFlags protocol.AuthenticatorFlags |
| 48 | +} |
| 49 | + |
| 50 | +var _ webauthn.User = (*user)(nil) |
| 51 | + |
| 52 | +func NewWebAuthnUser(ctx context.Context, u *user_model.User, defaultAuthFlags ...protocol.AuthenticatorFlags) webauthn.User { |
| 53 | + return &user{ctx: ctx, User: u, defaultAuthFlags: util.OptionalArg(defaultAuthFlags)} |
| 54 | +} |
43 | 55 |
|
44 | 56 | // WebAuthnID implements the webauthn.User interface |
45 | | -func (u *User) WebAuthnID() []byte { |
| 57 | +func (u *user) WebAuthnID() []byte { |
46 | 58 | id := make([]byte, 8) |
47 | | - binary.PutVarint(id, u.ID) |
| 59 | + binary.PutVarint(id, u.User.ID) |
48 | 60 | return id |
49 | 61 | } |
50 | 62 |
|
51 | 63 | // WebAuthnName implements the webauthn.User interface |
52 | | -func (u *User) WebAuthnName() string { |
53 | | - if u.LoginName == "" { |
54 | | - return u.Name |
55 | | - } |
56 | | - return u.LoginName |
| 64 | +func (u *user) WebAuthnName() string { |
| 65 | + return util.IfZero(u.User.LoginName, u.User.Name) |
57 | 66 | } |
58 | 67 |
|
59 | 68 | // WebAuthnDisplayName implements the webauthn.User interface |
60 | | -func (u *User) WebAuthnDisplayName() string { |
61 | | - return (*user_model.User)(u).DisplayName() |
62 | | -} |
63 | | - |
64 | | -// WebAuthnIcon implements the webauthn.User interface |
65 | | -func (u *User) WebAuthnIcon() string { |
66 | | - return (*user_model.User)(u).AvatarLink(db.DefaultContext) |
| 69 | +func (u *user) WebAuthnDisplayName() string { |
| 70 | + return u.User.DisplayName() |
67 | 71 | } |
68 | 72 |
|
69 | 73 | // WebAuthnCredentials implements the webauthn.User interface |
70 | | -func (u *User) WebAuthnCredentials() []webauthn.Credential { |
71 | | - dbCreds, err := auth.GetWebAuthnCredentialsByUID(db.DefaultContext, u.ID) |
| 74 | +func (u *user) WebAuthnCredentials() []webauthn.Credential { |
| 75 | + dbCreds, err := auth.GetWebAuthnCredentialsByUID(u.ctx, u.User.ID) |
72 | 76 | if err != nil { |
73 | 77 | return nil |
74 | 78 | } |
75 | | - |
76 | | - return dbCreds.ToCredentials() |
| 79 | + return dbCreds.ToCredentials(u.defaultAuthFlags) |
77 | 80 | } |
0 commit comments