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
14 changes: 10 additions & 4 deletions lib/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4803,13 +4803,19 @@ func (a *Server) GetHeadlessAuthentication(ctx context.Context, name string) (*t
return nil, trace.Wrap(err)
}

// wait for the headless authentication to be updated with valid login details
// by the login process. If the headless authentication is already updated,
// Wait will return it immediately.
sub, err := a.headlessAuthenticationWatcher.Subscribe(ctx, name)
if err != nil {
return nil, trace.Wrap(err)
}
defer sub.Close()

waitCtx, cancel := context.WithTimeout(ctx, defaults.HTTPRequestTimeout)
defer cancel()

headlessAuthn, err := a.headlessAuthenticationWatcher.Wait(waitCtx, name, func(ha *types.HeadlessAuthentication) (bool, error) {
// wait for the headless authentication to be updated with valid login details
// by the login process. If the headless authentication is already updated,
// Wait will return it immediately.
headlessAuthn, err := a.headlessAuthenticationWatcher.WaitForUpdate(waitCtx, sub, func(ha *types.HeadlessAuthentication) (bool, error) {
return services.ValidateHeadlessAuthentication(ha) == nil, nil
})
return headlessAuthn, trace.Wrap(err)
Expand Down
10 changes: 8 additions & 2 deletions lib/auth/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,16 @@ func (s *Server) authenticateHeadless(ctx context.Context, req AuthenticateUserR
return nil, trace.Wrap(err)
}

sub, err := s.headlessAuthenticationWatcher.Subscribe(ctx, req.HeadlessAuthenticationID)
if err != nil {
return nil, trace.Wrap(err)
}
defer sub.Close()

// Wait for a headless authenticated stub to be inserted by an authenticated
// call to GetHeadlessAuthentication. We do this to avoid immediately inserting
// backend items from an unauthenticated endpoint.
headlessAuthnStub, err := s.headlessAuthenticationWatcher.Wait(ctx, req.HeadlessAuthenticationID, func(ha *types.HeadlessAuthentication) (bool, error) {
headlessAuthnStub, err := s.headlessAuthenticationWatcher.WaitForUpdate(ctx, sub, func(ha *types.HeadlessAuthentication) (bool, error) {
// Only headless authentication stub can be inserted without the standard validation.
if services.ValidateHeadlessAuthentication(ha) == nil {
return false, trace.AlreadyExists("headless auth request already exists")
Expand All @@ -381,7 +387,7 @@ func (s *Server) authenticateHeadless(ctx context.Context, req AuthenticateUserR
}

// Wait for the request to be approved/denied.
headlessAuthn, err = s.headlessAuthenticationWatcher.Wait(ctx, req.HeadlessAuthenticationID, func(ha *types.HeadlessAuthentication) (bool, error) {
headlessAuthn, err = s.headlessAuthenticationWatcher.WaitForUpdate(ctx, sub, func(ha *types.HeadlessAuthentication) (bool, error) {
switch ha.State {
case types.HeadlessAuthenticationState_HEADLESS_AUTHENTICATION_STATE_APPROVED:
if ha.MfaDevice == nil {
Expand Down
Loading