devices: treat undefined device types as any (#2927)

This commit is contained in:
Caleb Doxsey 2022-01-12 11:04:35 -07:00 committed by GitHub
parent 73dd6b93c2
commit 4583ecc730
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 32 deletions

View file

@ -137,10 +137,7 @@ func (h *Handler) handleAuthenticate(w http.ResponseWriter, r *http.Request, sta
}
// get the stored device type
deviceType, err := webauthnutil.GetDeviceType(ctx, state.Client, deviceTypeParam)
if err != nil {
return fmt.Errorf("error retrieving webauthn device type: %w", err)
}
deviceType := webauthnutil.GetDeviceType(ctx, state.Client, deviceTypeParam)
// get the device credentials
knownDeviceCredentials, err := getKnownDeviceCredentials(ctx, state.Client, u.GetDeviceCredentialIds()...)
@ -232,10 +229,7 @@ func (h *Handler) handleRegister(w http.ResponseWriter, r *http.Request, state *
}
// get the stored device type
deviceType, err := webauthnutil.GetDeviceType(ctx, state.Client, deviceTypeParam)
if err != nil {
return fmt.Errorf("error retrieving webauthn device type: %w", err)
}
deviceType := webauthnutil.GetDeviceType(ctx, state.Client, deviceTypeParam)
creationOptions, err := webauthnutil.GetCreationOptionsForCredential(
state.SharedKey,
@ -370,10 +364,7 @@ func (h *Handler) handleView(w http.ResponseWriter, r *http.Request, state *Stat
}
// get the stored device type
deviceType, err := webauthnutil.GetDeviceType(ctx, state.Client, deviceTypeParam)
if err != nil {
return err
}
deviceType := webauthnutil.GetDeviceType(ctx, state.Client, deviceTypeParam)
creationOptions := webauthnutil.GenerateCreationOptions(state.SharedKey, deviceType, u)
requestOptions := webauthnutil.GenerateRequestOptions(state.SharedKey, deviceType, knownDeviceCredentials)

View file

@ -62,14 +62,14 @@ func GetDeviceType(
ctx context.Context,
client databroker.DataBrokerServiceClient,
deviceTypeID string,
) (*device.Type, error) {
) *device.Type {
deviceType, err := device.GetType(ctx, client, deviceTypeID)
if status.Code(err) == codes.NotFound {
var ok bool
deviceType, ok = predefinedDeviceTypes[deviceTypeID]
if ok {
err = nil
}
deviceType = predefinedDeviceTypes[deviceTypeID]
}
return deviceType, err
if deviceType == nil {
deviceType = proto.Clone(predefinedDeviceTypes[DefaultDeviceType]).(*device.Type)
deviceType.Id = deviceTypeID
}
return deviceType
}

View file

@ -35,8 +35,7 @@ func TestGetDeviceType(t *testing.T) {
}, nil
},
}
deviceType, err := GetDeviceType(ctx, client, "any")
assert.NoError(t, err)
deviceType := GetDeviceType(ctx, client, "any")
assert.Equal(t, "Example", deviceType.GetName())
})
t.Run("default", func(t *testing.T) {
@ -45,17 +44,7 @@ func TestGetDeviceType(t *testing.T) {
return nil, status.Error(codes.NotFound, "not found")
},
}
deviceType, err := GetDeviceType(ctx, client, "any")
assert.NoError(t, err)
deviceType := GetDeviceType(ctx, client, "any")
assert.Equal(t, "Any", deviceType.GetName())
})
t.Run("not found", func(t *testing.T) {
client := &mockDataBrokerServiceClient{
get: func(ctx context.Context, in *databroker.GetRequest, opts ...grpc.CallOption) (*databroker.GetResponse, error) {
return nil, status.Error(codes.NotFound, "not found")
},
}
_, err := GetDeviceType(ctx, client, "example")
assert.Error(t, err)
})
}