mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-20 01:53:57 +09:00
Backport #39792 Fixes #39790 Signed-off-by: choksi2212 <choksi2212@users.noreply.github.com> Signed-off-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: Manas Choksi <162600900+choksi2212@users.noreply.github.com> Co-authored-by: choksi2212 <choksi2212@users.noreply.github.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
86 lines
2.7 KiB
Go
86 lines
2.7 KiB
Go
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package webauthn
|
|
|
|
import (
|
|
"context"
|
|
"encoding/binary"
|
|
"encoding/gob"
|
|
|
|
"gitea.dev/models/auth"
|
|
user_model "gitea.dev/models/user"
|
|
"gitea.dev/modules/setting"
|
|
"gitea.dev/modules/util"
|
|
|
|
"github.com/go-webauthn/webauthn/protocol"
|
|
"github.com/go-webauthn/webauthn/webauthn"
|
|
)
|
|
|
|
// WebAuthn represents the global WebAuthn instance
|
|
var WebAuthn *webauthn.WebAuthn
|
|
|
|
// Init initializes the WebAuthn instance from the config.
|
|
func Init() {
|
|
gob.Register(&webauthn.SessionData{}) // TODO: CHI-SESSION-GOB-REGISTER.
|
|
|
|
appURL, _ := protocol.FullyQualifiedOrigin(setting.AppURL)
|
|
|
|
WebAuthn = &webauthn.WebAuthn{
|
|
Config: &webauthn.Config{
|
|
RPDisplayName: setting.AppName,
|
|
RPID: setting.AppDomain,
|
|
RPOrigins: []string{appURL},
|
|
AttestationPreference: protocol.PreferNoAttestation, // Gitea never verifies attestation
|
|
// WebKit (Safari on iOS and macOS) returns an unsolicited "appid" client
|
|
// extension output even when the relying party did not request it. Since the
|
|
// webauthn library upgrade to v0.18, the default policy fails such ceremonies
|
|
// ('Client returned the appid extension output which was not requested'), breaking
|
|
// every passkey and security-key login from Safari on iOS. Ignore unsolicited
|
|
// extension outputs, the same policy other relying parties adopted for the
|
|
// same WebKit behavior.
|
|
ExtensionsUnsolicitedOutputPolicy: protocol.UnsolicitedOutputPolicyIgnore,
|
|
},
|
|
}
|
|
}
|
|
|
|
// user represents an implementation of webauthn.User based on User model
|
|
type user struct {
|
|
ctx context.Context
|
|
User *user_model.User
|
|
|
|
defaultAuthFlags protocol.AuthenticatorFlags
|
|
}
|
|
|
|
var _ webauthn.User = (*user)(nil)
|
|
|
|
func NewWebAuthnUser(ctx context.Context, u *user_model.User, defaultAuthFlags ...protocol.AuthenticatorFlags) webauthn.User {
|
|
return &user{ctx: ctx, User: u, defaultAuthFlags: util.OptionalArg(defaultAuthFlags)}
|
|
}
|
|
|
|
// WebAuthnID implements the webauthn.User interface
|
|
func (u *user) WebAuthnID() []byte {
|
|
id := make([]byte, 8)
|
|
binary.PutVarint(id, u.User.ID)
|
|
return id
|
|
}
|
|
|
|
// WebAuthnName implements the webauthn.User interface
|
|
func (u *user) WebAuthnName() string {
|
|
return util.IfZero(u.User.LoginName, u.User.Name)
|
|
}
|
|
|
|
// WebAuthnDisplayName implements the webauthn.User interface
|
|
func (u *user) WebAuthnDisplayName() string {
|
|
return u.User.DisplayName()
|
|
}
|
|
|
|
// WebAuthnCredentials implements the webauthn.User interface
|
|
func (u *user) WebAuthnCredentials() []webauthn.Credential {
|
|
dbCreds, err := auth.GetWebAuthnCredentialsByUID(u.ctx, u.User.ID)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return dbCreds.ToCredentials(u.defaultAuthFlags)
|
|
}
|