Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 27 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,33 @@
Release notes for the Go module. The GitHub release also lists every commit
since the last tag; this file says what changes for you when you upgrade.

## v1.12.1 (unreleased)

This release adds one migration: passkey credentials gain a nullable `flags`
column (postgres SMALLINT, SQLite INTEGER; mongo needs nothing). It is not
backfilled. A passkey registered before it fills its flags in on its next
sign-in.

### Passkeys

- Synced passkeys (iCloud Keychain, Google Password Manager, any
authenticator that reports itself backup eligible) can sign in. authsome
never stored a credential's WebAuthn flags, and go-webauthn refuses an
assertion whose backup-eligible bit differs from the stored one, so these
sign-ins failed with "Backup Eligible flag inconsistency". Registration now
stores the flags and every sign-in saves what go-webauthn settled on.
- A passkey that verified its user (PIN or biometric) signs in without a
second factor again when built against go-webauthn v0.18. From v0.18 the
credential go-webauthn returns carries the stored "user verified" bit, not
the assertion's, so every passkey sign-in was asked for a code. authsome now
reads it from the sign-in's own assertion, on any go-webauthn version.
- `Credential` gains `Flags *uint8`, nil for a passkey stored before this
release. A store that implements the new optional `passkey.FlagsStore`
(`UpdateCredentialFlags`) keeps flags across sign-ins; the built-in stores
all do. A `Store` without it keeps working, with each sign-in's backup bits
taken from its own assertion.
- go-webauthn is now v0.18.2.

## v1.12.0 (unreleased)

Breaking changes start with "Breaking:" and say what to do about them.
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ toolchain go1.26.11
require (
github.com/coreos/go-oidc/v3 v3.21.0
github.com/crewjam/saml v0.6.3
github.com/go-webauthn/webauthn v0.17.4
github.com/go-webauthn/webauthn v0.18.2
github.com/moby/moby/api v1.55.0
github.com/oschwald/maxminddb-golang v1.13.1
github.com/pquerna/otp v1.5.0
Expand Down Expand Up @@ -123,7 +123,7 @@ require (
github.com/fatih/color v1.19.0 // indirect
github.com/felixge/httpsnoop v1.1.0 // indirect
github.com/fsnotify/fsnotify v1.10.1 // indirect
github.com/fxamacker/cbor/v2 v2.9.2 // indirect
github.com/fxamacker/cbor/v2 v2.9.4 // indirect
github.com/gabriel-vasile/mimetype v1.4.16 // indirect
github.com/go-logr/logr v1.4.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand All @@ -135,7 +135,7 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.30.3 // indirect
github.com/go-viper/mapstructure/v2 v2.5.0 // indirect
github.com/go-webauthn/x v0.2.6 // indirect
github.com/go-webauthn/x v0.3.1 // indirect
github.com/gobwas/httphead v0.1.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect
github.com/gobwas/ws v1.4.0 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ github.com/felixge/httpsnoop v1.1.0 h1:3YtUj32ZZkqZtt3sZZsClsymw/QDuVfpNhoA31zeO
github.com/felixge/httpsnoop v1.1.0/go.mod h1:Zqxgdd+1Rkcz8euOqdr7lqgCRJztwr5hp9vDSi5UZCE=
github.com/fsnotify/fsnotify v1.10.1 h1:b0/UzAf9yR5rhf3RPm9gf3ehBPpf0oZKIjtpKrx59Ho=
github.com/fsnotify/fsnotify v1.10.1/go.mod h1:TLheqan6HD6GBK6PrDWyDPBaEV8LspOxvPSjC+bVfgo=
github.com/fxamacker/cbor/v2 v2.9.2 h1:X4Ksno9+x3cz0TZv69ec1hxP/+tymuR8PXQJyDwfh78=
github.com/fxamacker/cbor/v2 v2.9.2/go.mod h1:vM4b+DJCtHn+zz7h3FFp/hDAI9WNWCsZj23V5ytsSxQ=
github.com/fxamacker/cbor/v2 v2.9.4 h1:xwjVlxEMR3S605oUlgBjKLTTeGFciYPGYCtF/35LKGo=
github.com/fxamacker/cbor/v2 v2.9.4/go.mod h1:vM4b+DJCtHn+zz7h3FFp/hDAI9WNWCsZj23V5ytsSxQ=
github.com/gabriel-vasile/mimetype v1.4.16 h1:5izNFuvA37st1dDRAcA7TtmngBmRgiIPDmTtiEvjx1E=
github.com/gabriel-vasile/mimetype v1.4.16/go.mod h1:3oFFwxX8kTklZwzIl2aQEDZT0lKY8xH92ST88bAwba4=
github.com/go-jose/go-jose/v4 v4.1.8 h1:k38WifTutpGuqt6Imq9xW1OgCvnWFri0Nz7FnfiACM8=
Expand Down Expand Up @@ -144,10 +144,10 @@ github.com/go-playground/validator/v10 v10.30.3/go.mod h1:4Axh7oCNGcoGkqLoE4YWt6
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-viper/mapstructure/v2 v2.5.0 h1:vM5IJoUAy3d7zRSVtIwQgBj7BiWtMPfmPEgAXnvj1Ro=
github.com/go-viper/mapstructure/v2 v2.5.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/go-webauthn/webauthn v0.17.4 h1:KFTSz3R2RYDiUn/0cDi3XTJgFenSG74eKTTHlqWhlxk=
github.com/go-webauthn/webauthn v0.17.4/go.mod h1:pZk63EE/BdztlmyS4Yc+9H5g4a8blNlbtGmdHQHbZX8=
github.com/go-webauthn/x v0.2.6 h1:TEyDuQAIiEgYpx60nKiBJIX/5nSUC8LxNbH+uf5U9uk=
github.com/go-webauthn/x v0.2.6/go.mod h1:45bA7YEqyQhRcQJ/TiBb46Ww8yqHBGvgEhQ3WWF0aDo=
github.com/go-webauthn/webauthn v0.18.2 h1:0BeftmEHU7i3Dv0VFwBtidy/ba37Vcdjvqst9EYu8Sk=
github.com/go-webauthn/webauthn v0.18.2/go.mod h1:hEXaOuLxvZ3zG9miZe3ehlyeVso9AtklXG+kTn36k+A=
github.com/go-webauthn/x v0.3.1 h1:1ff37z3XfmTTomkhlURgGizLIDyOvPgTt2t9nlzKLRo=
github.com/go-webauthn/x v0.3.1/go.mod h1:ZInxAynYXfBPvvm5gzKZ7geBlL23K71xASMgohHl/Rg=
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM=
github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
Expand Down
9 changes: 7 additions & 2 deletions plugins/passkey/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ type Credential struct {
SignCount uint32 `json:"sign_count"`
AAGUID []byte `json:"aaguid"`
DisplayName string `json:"display_name"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// Flags is the WebAuthn authenticator-data flag byte go-webauthn keeps
// with a credential: backup eligibility and state, and whether the
// authenticator has verified its user. Nil for a passkey registered before
// authsome stored them; its first sign-in fills them in.
Flags *uint8 `json:"-"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
89 changes: 89 additions & 0 deletions plugins/passkey/credential_flags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package passkey_test

import (
"context"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/xraph/authsome/plugins/passkey/passkeytest"
)

const (
backupEligible = 0x08
userVerified = 0x04
)

// A synced passkey (iCloud Keychain, Google Password Manager) is backup
// eligible. go-webauthn refuses a sign-in whose backup-eligible bit differs
// from the stored credential's, so the flags have to be kept from
// registration on. It signs in, again and again, and with two-step on its
// user verification stands in for the code.
func TestDiscoverableLogin_SyncedPasskeySignsIn(t *testing.T) {
f := newTwoStepFixture(t)
device := passkeytest.NewAuthenticator(testOrigin, "localhost")
device.Synced = true
f.register(t, device)

creds, err := f.store.ListUserCredentials(context.Background(), f.u.ID)
require.NoError(t, err)
require.Len(t, creds, 1)
require.NotNil(t, creds[0].Flags, "registration stores the flags")
assert.NotZero(t, *creds[0].Flags&backupEligible, "stored flags %08b", *creds[0].Flags)

for i := range 2 {
rec := f.login(t, device)
require.Equal(t, http.StatusOK, rec.Code, "sign-in %d: body=%s", i+1, rec.Body.String())
}
}

// A passkey stored before authsome kept flags has none. Its first sign-in
// takes the backup bits from its own assertion and stores what go-webauthn
// settled on; a user-verifying assertion still needs no code.
func TestDiscoverableLogin_PasskeyStoredWithoutFlags(t *testing.T) {
f := newTwoStepFixture(t)
device := passkeytest.NewAuthenticator(testOrigin, "localhost")
device.Synced = true
f.register(t, device)

ctx := context.Background()
creds, err := f.store.ListUserCredentials(ctx, f.u.ID)
require.NoError(t, err)
require.Len(t, creds, 1)
legacy := creds[0]
legacy.Flags = nil
require.NoError(t, f.store.DeleteCredential(ctx, legacy.CredentialID))
require.NoError(t, f.store.CreateCredential(ctx, legacy))

rec := f.login(t, device)
require.Equal(t, http.StatusOK, rec.Code, "body=%s", rec.Body.String())

stored, err := f.store.GetCredential(ctx, legacy.CredentialID)
require.NoError(t, err)
require.NotNil(t, stored.Flags, "the first sign-in stores the flags")
assert.NotZero(t, *stored.Flags&backupEligible, "stored flags %08b", *stored.Flags)

rec = f.login(t, device)
require.Equal(t, http.StatusOK, rec.Code, "second sign-in: body=%s", rec.Body.String())
}

// What a sign-in counts as verified comes from that sign-in's assertion,
// never from flags stored earlier: a passkey registered with user
// verification, then used with presence only, is asked for the code.
func TestDiscoverableLogin_StoredUVDoesNotStandInForTheAssertion(t *testing.T) {
f := newTwoStepFixture(t)
device := passkeytest.NewAuthenticator(testOrigin, "localhost")
f.register(t, device)

creds, err := f.store.ListUserCredentials(context.Background(), f.u.ID)
require.NoError(t, err)
require.Len(t, creds, 1)
require.NotNil(t, creds[0].Flags)
require.NotZero(t, *creds[0].Flags&userVerified, "registration verified the user")

device.NoUserVerification = true
rec := f.login(t, device)
assert.Equal(t, http.StatusForbidden, rec.Code, "body=%s", rec.Body.String())
}
17 changes: 12 additions & 5 deletions plugins/passkey/discoverable.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"time"

"github.com/go-webauthn/webauthn/protocol"
"github.com/go-webauthn/webauthn/webauthn"
"github.com/xraph/forge"

Expand Down Expand Up @@ -88,7 +89,7 @@ func clearCeremonyCookie(w http.ResponseWriter, secure bool) {
// the user and its webauthn.User view, WITHOUT requiring a pre-authenticated
// session — this is what makes passwordless login work. Errors on an
// unparseable handle, a missing engine, or an unknown user.
func (p *Plugin) resolveDiscoverableUser(ctx context.Context, userHandle []byte) (u *user.User, waUser webauthn.User, err error) {
func (p *Plugin) resolveDiscoverableUser(ctx context.Context, userHandle []byte, assertion *protocol.ParsedCredentialAssertionData) (u *user.User, waUser webauthn.User, err error) {
uid, perr := id.ParseUserID(string(userHandle))
if perr != nil {
return nil, nil, fmt.Errorf("passkey: invalid user handle: %w", perr)
Expand All @@ -100,7 +101,7 @@ func (p *Plugin) resolveDiscoverableUser(ctx context.Context, userHandle []byte)
if gerr != nil {
return nil, nil, gerr
}
return loaded, p.toWebAuthnUser(ctx, loaded), nil
return loaded, p.toWebAuthnUserFor(ctx, loaded, assertion), nil
}

// finishDiscoverableLogin completes a passwordless WebAuthn ceremony: it loads
Expand All @@ -124,17 +125,22 @@ func (p *Plugin) finishDiscoverableLogin(ctx forge.Context, ceremonyID string) (
return nil, middleware.InternalError(ctx, fmt.Errorf("failed to parse session: %w", unmarshalErr))
}

assertion, err := protocol.ParseCredentialRequestResponse(ctx.Request())
if err != nil {
return nil, forge.Unauthorized(fmt.Sprintf("passkey: finish discoverable login: %v", err))
}

var loggedIn *user.User
handler := func(_, userHandle []byte) (webauthn.User, error) {
u, wau, herr := p.resolveDiscoverableUser(ctx.Context(), userHandle)
u, wau, herr := p.resolveDiscoverableUser(ctx.Context(), userHandle, assertion)
if herr != nil {
return nil, herr
}
loggedIn = u
return wau, nil
}

cred, err := p.waForRequest(ctx.Request()).FinishDiscoverableLogin(handler, waSession, ctx.Request())
cred, err := p.waForRequest(ctx.Request()).ValidateDiscoverableLogin(handler, waSession, assertion)
if err != nil {
return nil, forge.Unauthorized(fmt.Sprintf("passkey: finish discoverable login: %v", err))
}
Expand All @@ -150,9 +156,10 @@ func (p *Plugin) finishDiscoverableLogin(ctx forge.Context, ceremonyID string) (

if p.store != nil {
_ = p.store.UpdateSignCount(ctx.Context(), cred.ID, cred.Authenticator.SignCount) //nolint:errcheck // best-effort update
p.saveFlags(ctx.Context(), cred)
}

sess, err := p.issueSession(ctx, loggedIn, cred.Flags.UserVerified, attempt)
sess, err := p.issueSession(ctx, loggedIn, assertionUV(assertion), attempt)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions plugins/passkey/discoverable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ func TestResolveDiscoverableUser(t *testing.T) {
u := seedPasskeyUser(t, eng)
ctx := context.Background()

gotUser, wau, err := p.resolveDiscoverableUser(ctx, []byte(u.ID.String()))
gotUser, wau, err := p.resolveDiscoverableUser(ctx, []byte(u.ID.String()), nil)
require.NoError(t, err)
require.NotNil(t, wau)
assert.Equal(t, u.ID.String(), gotUser.ID.String())
assert.Equal(t, []byte(u.ID.String()), wau.WebAuthnID(), "the webauthn user handle must round-trip")

_, _, err = p.resolveDiscoverableUser(ctx, []byte("not-a-valid-user-id"))
_, _, err = p.resolveDiscoverableUser(ctx, []byte("not-a-valid-user-id"), nil)
assert.Error(t, err, "an unparseable user handle must error")

_, _, err = p.resolveDiscoverableUser(ctx, []byte(id.NewUserID().String()))
_, _, err = p.resolveDiscoverableUser(ctx, []byte(id.NewUserID().String()), nil)
assert.Error(t, err, "an unknown user must error")
}

Expand Down
12 changes: 9 additions & 3 deletions plugins/passkey/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"time"

"github.com/go-webauthn/webauthn/protocol"
"github.com/go-webauthn/webauthn/webauthn"
"github.com/xraph/forge"
log "github.com/xraph/go-utils/log"
Expand Down Expand Up @@ -475,9 +476,13 @@ func (p *Plugin) handleLoginFinish(ctx forge.Context, _ *LoginFinishRequest) (*L
return nil, middleware.InternalError(ctx, fmt.Errorf("failed to parse session: %w", unmarshalErr))
}

wau := p.toWebAuthnUser(ctx.Context(), u)
assertion, err := protocol.ParseCredentialRequestResponse(ctx.Request())
if err != nil {
return nil, forge.Unauthorized(fmt.Sprintf("passkey: finish login: %v", err))
}
wau := p.toWebAuthnUserFor(ctx.Context(), u, assertion)

cred, err := p.waForRequest(ctx.Request()).FinishLogin(wau, session, ctx.Request())
cred, err := p.waForRequest(ctx.Request()).ValidateLogin(wau, session, assertion)
if err != nil {
return nil, forge.Unauthorized(fmt.Sprintf("passkey: finish login: %v", err))
}
Expand All @@ -491,9 +496,10 @@ func (p *Plugin) handleLoginFinish(ctx forge.Context, _ *LoginFinishRequest) (*L
return nil, forge.Unauthorized(cwErr.Error())
}

// Update sign count
// Update sign count and flags
if p.store != nil {
_ = p.store.UpdateSignCount(ctx.Context(), cred.ID, cred.Authenticator.SignCount) //nolint:errcheck // best-effort update
p.saveFlags(ctx.Context(), cred)
}

userIDStr := u.ID.String()
Expand Down
39 changes: 39 additions & 0 deletions plugins/passkey/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,28 @@ CREATE INDEX IF NOT EXISTS idx_authsome_passkey_credentials_user
_, err := exec.Exec(ctx, `
ALTER TABLE authsome_passkey_credentials
ALTER COLUMN sign_count TYPE BIGINT;
`)
return err
},
},
&migrate.Migration{
// go-webauthn checks a sign-in's backup-eligible bit against the
// one stored with the credential, and from v0.18 takes "user
// verified" from it too. NULL marks a passkey registered before
// the column; its first sign-in fills it in.
Name: "add_credential_flags",
Version: "20240201000003",
Up: func(ctx context.Context, exec migrate.Executor) error {
_, err := exec.Exec(ctx, `
ALTER TABLE authsome_passkey_credentials
ADD COLUMN IF NOT EXISTS flags SMALLINT;
`)
return err
},
Down: func(ctx context.Context, exec migrate.Executor) error {
_, err := exec.Exec(ctx, `
ALTER TABLE authsome_passkey_credentials
DROP COLUMN IF EXISTS flags;
`)
return err
},
Expand Down Expand Up @@ -105,5 +127,22 @@ CREATE INDEX IF NOT EXISTS idx_authsome_passkey_credentials_user
return err
},
},
&migrate.Migration{
// See the postgres migration of the same name.
Name: "add_credential_flags",
Version: "20240201000003",
Up: func(ctx context.Context, exec migrate.Executor) error {
_, err := exec.Exec(ctx, `
ALTER TABLE authsome_passkey_credentials ADD COLUMN flags INTEGER;
`)
return err
},
Down: func(ctx context.Context, exec migrate.Executor) error {
_, err := exec.Exec(ctx, `
ALTER TABLE authsome_passkey_credentials DROP COLUMN flags;
`)
return err
},
},
)
}
Loading
Loading