Validate device creation.
Update our uuid package to the latest, which is now based on the GitHub
fork instead of the Google Code. Also, update our api package to its latest
version, which now needs the pqarrays package as a dependency.
We fleshed out the validateDeviceCreation. We now pass in the scopes we have
(for broad access control) and the user ID (for fine-grained access control).
This helper returns the first error it encounters, though it should probably
return a slice so we can return multiple errors all at once.
Before we even decode the request to create a Device, let's check if the user is
even logged in. If we can't ascertain that or they're not, there's no point in
even consuming the memory necessary to read the request, because we know we're
not going to use it anyways.
Finally actually validate the devices we're creating, and return an appropriate
error for each error we can get.
Also, the api.CheckScopes helper function now takes the scopes passed in as a
string slice, and we have an api.GetScopes helper function to retrieve the
scopes associated with the request. Let's not keep parsing that.
We need two new scopes to control access for device creation; ScopeImport lets
users import devices in and is pretty much admin access.
ScopeCreateOtherUserDevices allows a user to create Devices that are owned by
another user.
7 "code.secondbit.org/api.hg"
8 "code.secondbit.org/ducky/devices.hg"
9 "code.secondbit.org/uuid.hg"
13 errUnauthorizedLastSeen = errors.New("not authorized to set last seen")
14 errUnauthorizedCreated = errors.New("not authorized to set created")
15 errUnauthorizedOwner = errors.New("not authorized to set owner")
16 errInvalidDeviceType = errors.New("device type invalid")
17 errDeviceNameTooShort = errors.New("device name too short")
18 errDeviceNameTooLong = errors.New("device name too long")
21 // Device represents a device as exposed through the API. It is its
22 // own type as the business logic and the API have different requirements
23 // for the Device type, and require different representations.
25 ID uuid.ID `json:"id"`
26 Name string `json:"name,omitempty"`
27 Owner uuid.ID `json:"owner,omitempty"`
28 Type devices.DeviceType `json:"type,omitempty"`
29 Created time.Time `json:"created,omitempty"`
30 LastSeen time.Time `json:"lastSeen,omitempty"`
31 PushToken string `json:"pushToken,omitempty"`
34 // DeviceChange represents a set of changes to a device that will be used
35 // to update that device. It is its own type as the business logic and the
36 // API have different requirements for the DeviceChange type, and require
37 // different representations.
38 type DeviceChange struct {
39 DeviceID uuid.ID `json:"id"`
40 Name *string `json:"name,omitempty"`
41 Owner *uuid.ID `json:"owner,omitempty"`
42 Type *devices.DeviceType `json:"type,omitempty"`
43 LastSeen *time.Time `json:"lastSeen,omitempty"`
44 PushToken *string `json:"pushToken,omitempty"`
47 func apiDeviceFromCore(d devices.Device, includePushToken bool) Device {
57 device.PushToken = d.PushToken
62 func changeFromAPI(d DeviceChange) devices.DeviceChange {
63 return devices.DeviceChange{
69 PushToken: d.PushToken,
73 func createDevicesFromChanges(changes []DeviceChange) []devices.Device {
74 newDevices := make([]devices.Device, 0, len(changes))
75 for _, change := range changes {
76 newDevices = append(newDevices, devices.ApplyChange(devices.Device{}, changeFromAPI(change)))
81 func validateDeviceCreation(d devices.Device, scopes []string, user uuid.ID) error {
82 canImport := api.CheckScopes(scopes, ScopeImport.ID)
83 canCreateOtherUserDevices := api.CheckScopes(scopes, ScopeCreateOtherUserDevices.ID)
84 if !d.LastSeen.IsZero() && !canImport {
85 return errUnauthorizedLastSeen
87 if !d.Created.IsZero() && !canImport {
88 return errUnauthorizedCreated
90 if !d.Owner.Equal(user) && !canCreateOtherUserDevices {
91 return errUnauthorizedOwner
93 if !devices.IsValidDeviceType(d.Type) {
94 return errInvalidDeviceType
96 if len(d.Name) < devices.MinDeviceNameLength {
97 return errDeviceNameTooShort
99 if len(d.Name) > devices.MaxDeviceNameLength {
100 return errDeviceNameTooLong