Skip to content
Draft
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
22 changes: 13 additions & 9 deletions backend/services/deviceauth/devauth/devauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,14 +343,14 @@ func (d *DevAuth) handlePreAuthDevice(
ctx context.Context,
aset *model.AuthSet,
) (*model.AuthSet, error) {
var deviceAlreadyAccepted bool
// check the device status
// if the device status is accepted then do not trigger provisioning workflow
// this needs to be checked before changing authentication set status
dev, err := d.db.GetDeviceById(ctx, aset.DeviceId)
if err != nil {
return nil, err
}
deviceAlreadyAccepted := dev.Status == model.DevStatusAccepted

// check if the device is in the decommissioning state
if dev.Decommissioning {
Expand All @@ -360,7 +360,7 @@ func (d *DevAuth) handlePreAuthDevice(
}

currentStatus := dev.Status
if dev.Status != model.DevStatusAccepted {
if !deviceAlreadyAccepted {
// auth set is ok for auto-accepting, check device limit
allow, err := d.canAcceptDevice(ctx)
if err != nil {
Expand Down Expand Up @@ -824,14 +824,18 @@ func (d *DevAuth) AcceptDeviceAuth(ctx context.Context, device_id string, auth_i
return err
}

// possible race, consider accept-count-unaccept pattern if that's problematic
allow, err := d.canAcceptDevice(ctx)
if err != nil {
return err
}
deviceAlreadyAccepted := dev.Status == model.DevStatusAccepted

if !deviceAlreadyAccepted {
// possible race, consider accept-count-unaccept pattern if that's problematic
allow, err := d.canAcceptDevice(ctx)
if err != nil {
return err
}

if !allow {
return ErrMaxDeviceCountReached
if !allow {
return ErrMaxDeviceCountReached
}
}

if err := d.setAuthSetStatus(ctx, device_id, auth_id, model.DevStatusAccepted); err != nil {
Expand Down
1 change: 0 additions & 1 deletion backend/services/deviceauth/devauth/devauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,6 @@ func TestDevAuthSubmitAuthRequestPreauth(t *testing.T) {
res: "dummytoken",
expectedWorkflows: map[string]error{
"update_device_status": nil,
"provision_device": nil,
"update_device_inventory": nil,
},
},
Expand Down