ducky/devices
ducky/devices/apiv1/devices.go
Fix JSON pointers in errors when creating devices. Our JSON pointers used to point at the root of the document, but the properties were actually in objects in an array keyed off of "devices", so we had to update the field property of our errors to match. While we were in there, we fixed the "insufficient" error for a Device's name to be "missing" when the name is an empty string. So far, the only way for a Device's name for it to be insufficient is _for it to be the empty string_, but if in the future we raise the minimum length of the Device name, there will be a distinction and I'd like the code to recognise it.
| paddy@15 | 1 package apiv1 |
| paddy@15 | 2 |
| paddy@15 | 3 import ( |
| paddy@16 | 4 "errors" |
| paddy@15 | 5 "time" |
| paddy@15 | 6 |
| paddy@16 | 7 "code.secondbit.org/api.hg" |
| paddy@15 | 8 "code.secondbit.org/ducky/devices.hg" |
| paddy@15 | 9 "code.secondbit.org/uuid.hg" |
| paddy@15 | 10 ) |
| paddy@15 | 11 |
| paddy@16 | 12 var ( |
| paddy@16 | 13 errUnauthorizedLastSeen = errors.New("not authorized to set last seen") |
| paddy@16 | 14 errUnauthorizedCreated = errors.New("not authorized to set created") |
| paddy@16 | 15 errUnauthorizedOwner = errors.New("not authorized to set owner") |
| paddy@16 | 16 errInvalidDeviceType = errors.New("device type invalid") |
| paddy@16 | 17 errDeviceNameTooShort = errors.New("device name too short") |
| paddy@16 | 18 errDeviceNameTooLong = errors.New("device name too long") |
| paddy@16 | 19 ) |
| paddy@16 | 20 |
| paddy@15 | 21 // Device represents a device as exposed through the API. It is its |
| paddy@15 | 22 // own type as the business logic and the API have different requirements |
| paddy@15 | 23 // for the Device type, and require different representations. |
| paddy@15 | 24 type Device struct { |
| paddy@15 | 25 ID uuid.ID `json:"id"` |
| paddy@15 | 26 Name string `json:"name,omitempty"` |
| paddy@15 | 27 Owner uuid.ID `json:"owner,omitempty"` |
| paddy@15 | 28 Type devices.DeviceType `json:"type,omitempty"` |
| paddy@15 | 29 Created time.Time `json:"created,omitempty"` |
| paddy@15 | 30 LastSeen time.Time `json:"lastSeen,omitempty"` |
| paddy@15 | 31 PushToken string `json:"pushToken,omitempty"` |
| paddy@15 | 32 } |
| paddy@15 | 33 |
| paddy@15 | 34 // DeviceChange represents a set of changes to a device that will be used |
| paddy@15 | 35 // to update that device. It is its own type as the business logic and the |
| paddy@15 | 36 // API have different requirements for the DeviceChange type, and require |
| paddy@15 | 37 // different representations. |
| paddy@15 | 38 type DeviceChange struct { |
| paddy@15 | 39 DeviceID uuid.ID `json:"id"` |
| paddy@15 | 40 Name *string `json:"name,omitempty"` |
| paddy@15 | 41 Owner *uuid.ID `json:"owner,omitempty"` |
| paddy@15 | 42 Type *devices.DeviceType `json:"type,omitempty"` |
| paddy@15 | 43 LastSeen *time.Time `json:"lastSeen,omitempty"` |
| paddy@15 | 44 PushToken *string `json:"pushToken,omitempty"` |
| paddy@15 | 45 } |
| paddy@15 | 46 |
| paddy@15 | 47 func apiDeviceFromCore(d devices.Device, includePushToken bool) Device { |
| paddy@15 | 48 device := Device{ |
| paddy@15 | 49 ID: d.ID, |
| paddy@15 | 50 Name: d.Name, |
| paddy@15 | 51 Owner: d.Owner, |
| paddy@15 | 52 Type: d.Type, |
| paddy@15 | 53 Created: d.Created, |
| paddy@15 | 54 LastSeen: d.LastSeen, |
| paddy@15 | 55 } |
| paddy@15 | 56 if includePushToken { |
| paddy@15 | 57 device.PushToken = d.PushToken |
| paddy@15 | 58 } |
| paddy@15 | 59 return device |
| paddy@15 | 60 } |
| paddy@15 | 61 |
| paddy@15 | 62 func changeFromAPI(d DeviceChange) devices.DeviceChange { |
| paddy@15 | 63 return devices.DeviceChange{ |
| paddy@15 | 64 DeviceID: d.DeviceID, |
| paddy@15 | 65 Name: d.Name, |
| paddy@15 | 66 Owner: d.Owner, |
| paddy@15 | 67 Type: d.Type, |
| paddy@15 | 68 LastSeen: d.LastSeen, |
| paddy@15 | 69 PushToken: d.PushToken, |
| paddy@15 | 70 } |
| paddy@15 | 71 } |
| paddy@15 | 72 |
| paddy@15 | 73 func createDevicesFromChanges(changes []DeviceChange) []devices.Device { |
| paddy@15 | 74 newDevices := make([]devices.Device, 0, len(changes)) |
| paddy@15 | 75 for _, change := range changes { |
| paddy@15 | 76 newDevices = append(newDevices, devices.ApplyChange(devices.Device{}, changeFromAPI(change))) |
| paddy@15 | 77 } |
| paddy@15 | 78 return newDevices |
| paddy@15 | 79 } |
| paddy@15 | 80 |
| paddy@16 | 81 func validateDeviceCreation(d devices.Device, scopes []string, user uuid.ID) error { |
| paddy@16 | 82 canImport := api.CheckScopes(scopes, ScopeImport.ID) |
| paddy@16 | 83 canCreateOtherUserDevices := api.CheckScopes(scopes, ScopeCreateOtherUserDevices.ID) |
| paddy@16 | 84 if !d.LastSeen.IsZero() && !canImport { |
| paddy@16 | 85 return errUnauthorizedLastSeen |
| paddy@16 | 86 } |
| paddy@16 | 87 if !d.Created.IsZero() && !canImport { |
| paddy@16 | 88 return errUnauthorizedCreated |
| paddy@16 | 89 } |
| paddy@16 | 90 if !d.Owner.Equal(user) && !canCreateOtherUserDevices { |
| paddy@16 | 91 return errUnauthorizedOwner |
| paddy@16 | 92 } |
| paddy@16 | 93 if !devices.IsValidDeviceType(d.Type) { |
| paddy@16 | 94 return errInvalidDeviceType |
| paddy@16 | 95 } |
| paddy@16 | 96 if len(d.Name) < devices.MinDeviceNameLength { |
| paddy@16 | 97 return errDeviceNameTooShort |
| paddy@16 | 98 } |
| paddy@16 | 99 if len(d.Name) > devices.MaxDeviceNameLength { |
| paddy@16 | 100 return errDeviceNameTooLong |
| paddy@16 | 101 } |
| paddy@15 | 102 return nil |
| paddy@15 | 103 } |