ducky/devices

Paddy 2016-01-02 Parent:a700ede02f91 Child:ed1b5ba69551

19:51ad0db105c8 Go to Latest

ducky/devices/apiv1/devices.go

Update trout to fix routing bug. Update to tip on trout to fix the routing bug that was causing us such issues. See the commit message of trout at 3df515f0cec5 for more details.

History
1 package apiv1
3 import (
4 "errors"
5 "time"
7 "code.secondbit.org/api.hg"
8 "code.secondbit.org/ducky/devices.hg"
9 "code.secondbit.org/uuid.hg"
10 )
12 var (
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")
19 )
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.
24 type Device struct {
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"`
32 }
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"`
45 }
47 func apiDeviceFromCore(d devices.Device, includePushToken bool) Device {
48 device := Device{
49 ID: d.ID,
50 Name: d.Name,
51 Owner: d.Owner,
52 Type: d.Type,
53 Created: d.Created,
54 LastSeen: d.LastSeen,
55 }
56 if includePushToken {
57 device.PushToken = d.PushToken
58 }
59 return device
60 }
62 func changeFromAPI(d DeviceChange) devices.DeviceChange {
63 return devices.DeviceChange{
64 DeviceID: d.DeviceID,
65 Name: d.Name,
66 Owner: d.Owner,
67 Type: d.Type,
68 LastSeen: d.LastSeen,
69 PushToken: d.PushToken,
70 }
71 }
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)))
77 }
78 return newDevices
79 }
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
86 }
87 if !d.Created.IsZero() && !canImport {
88 return errUnauthorizedCreated
89 }
90 if !d.Owner.Equal(user) && !canCreateOtherUserDevices {
91 return errUnauthorizedOwner
92 }
93 if !devices.IsValidDeviceType(d.Type) {
94 return errInvalidDeviceType
95 }
96 if len(d.Name) < devices.MinDeviceNameLength {
97 return errDeviceNameTooShort
98 }
99 if len(d.Name) > devices.MaxDeviceNameLength {
100 return errDeviceNameTooLong
101 }
102 return nil
103 }