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

[v16] feat: Backfill WebauthnDevice.ResidentKey field #45395

Merged
merged 1 commit into from
Aug 13, 2024
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
19 changes: 15 additions & 4 deletions lib/auth/webauthn/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ func (f *loginFlow) finish(ctx context.Context, user string, resp *wantypes.Cred
}

// Update last used timestamp and device counter.
if err := setCounterAndTimestamps(dev, credential); err != nil {
if err := updateCredentialAndTimestamps(dev, credential, discoverableLogin); err != nil {
return nil, trace.Wrap(err)
}
// Retroactively write the credential RPID, now that it cleared authn.
Expand Down Expand Up @@ -420,15 +420,26 @@ func findDeviceByID(devices []*types.MFADevice, id []byte) (*types.MFADevice, bo
return nil, false
}

func setCounterAndTimestamps(dev *types.MFADevice, credential *wan.Credential) error {
switch d := dev.Device.(type) {
func updateCredentialAndTimestamps(
dest *types.MFADevice,
credential *wan.Credential,
discoverableLogin bool,
) error {
switch d := dest.Device.(type) {
case *types.MFADevice_U2F:
d.U2F.Counter = credential.Authenticator.SignCount
case *types.MFADevice_Webauthn:
d.Webauthn.SignatureCounter = credential.Authenticator.SignCount

// Backfill ResidentKey field.
// This may happen if an authenticator created for "MFA" was actually
// resident all along (eg, Safari/Touch ID registrations).
if discoverableLogin && !d.Webauthn.ResidentKey {
d.Webauthn.ResidentKey = true
}
default:
return trace.BadParameter("unexpected device type for webauthn: %T", d)
}
dev.LastUsed = time.Now()
dest.LastUsed = time.Now()
return nil
}
62 changes: 62 additions & 0 deletions lib/auth/webauthn/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,68 @@ func TestLoginFlow_userVerification(t *testing.T) {
}
}

func TestPasswordlessFlow_backfillResidentKey(t *testing.T) {
t.Parallel()

key, err := mocku2f.Create()
require.NoError(t, err, "Create failed")
key.SetPasswordless()

const user = "llama"
const origin = "https://example.com"
webIdentity := newFakeIdentity(user)
webConfig := &types.Webauthn{RPID: "example.com"}
ctx := context.Background()

// Register passwordless device as MFA.
// (Providers might make it passwordless regardless.)
rf := &wanlib.RegistrationFlow{
Webauthn: webConfig,
Identity: webIdentity,
}
cc, err := rf.Begin(ctx, user, false /* passwordless */)
require.NoError(t, err, "Begin failed")
ccr, err := key.SignCredentialCreation(origin, cc)
require.NoError(t, err, "SignCredentialCreation failed")
mfaDev, err := rf.Finish(ctx, wanlib.RegisterResponse{
User: user,
DeviceName: "mydevice",
CreationResponse: ccr,
})
require.NoError(t, err, "Finish failed")
// Sanity check.
require.False(t,
mfaDev.GetWebauthn().ResidentKey,
"MFA device unexpectedly marked as resident key",
)

// Set the UserHandle in our fake key. A "regular" authenticator would save
// the handle during registration, but our fake only does that for
// "passwordless" registrations.
wla, err := webIdentity.GetWebauthnLocalAuth(ctx, user)
require.NoError(t, err, "GetWebauthnLocalAuth failed")
key.UserHandle = wla.UserID

t.Run("ok", func(t *testing.T) {
lf := &wanlib.PasswordlessFlow{
Webauthn: webConfig,
Identity: webIdentity,
}

// Login.
assertion, err := lf.Begin(ctx)
require.NoError(t, err, "Begin failed")
assertionResp, err := key.SignAssertion(origin, assertion)
require.NoError(t, err, "SignAssertion failed")
loginData, err := lf.Finish(ctx, assertionResp)
require.NoError(t, err, "Finish failed")

// Verify that we corrected the ResidentKey field.
mfaDev := loginData.Device
assert.True(t, mfaDev.GetWebauthn().ResidentKey, "ResidentKey field not corrected")
})
}

type fakeIdentity struct {
User *types.UserV2
// MappedUser is used as the reply to GetTeleportUserByWebauthnID.
Expand Down
Loading