ducky/devices

Paddy 2016-01-02 Parent:a700ede02f91

20:ed1b5ba69551 Go to Latest

ducky/devices/vendor/github.com/pborman/uuid/uuid.go

Add updating devices to apiv1. We needed a way to be able to update devices after they were created. This is supported in the devices package, we just needed to expose it using apiv1 endpoints. In doing so, it became apparent that allowing users to change the Owner of their Devices wasn't properly thought through, and pending a reason to use it, I'm just removing it. The biggest issue came when trying to return usable error messages; we couldn't distinguish between "you don't own the device you're trying to update" and "you're not allowed to change the owner of the device". I also couldn't figure out _who should be able to_ change the owner of the device, which is generally an indication that I'm building a feature before I have a use case for it. To support this change, the apiv1.DeviceChange type needed its Owner property removed. I also needed to add deviceFromAPI and devicesFromAPI helpers to return devices.Device types from apiv1.Device types. There's now a new validateDeviceUpdate helper that checks to ensure that a device update request is valid and the user has the appropriate permissions. The createRequest type now accepts a slice of Devices, not a slice of DeviceChanges, because we want to pass the Owner in. A new updateRequest type is created, which accepts a DeviceChange to apply. A new handleUpdateDevice handler is created, which is assigned to the endpoint for PATCH requests against a device ID. It checks that the user is logged in, the Device they're trying to update exists, and that it's a valid update. If all of that is true, the device is updated and the updated device is returned. Finally, we had to add two new scopes to support new functionality: ScopeUpdateOtherUserDevices allows a user to update other user's devices, and ScopeUpdateLastSeen allows a user to update the LastSeen property of a device. Pending some better error messages, this should be a full implementation of updating a device, which leaves only the deletion endpoint to deal with.

History
paddy@0 1 // Copyright 2011 Google Inc. All rights reserved.
paddy@0 2 // Use of this source code is governed by a BSD-style
paddy@0 3 // license that can be found in the LICENSE file.
paddy@0 4
paddy@0 5 package uuid
paddy@0 6
paddy@0 7 import (
paddy@0 8 "bytes"
paddy@0 9 "crypto/rand"
paddy@0 10 "fmt"
paddy@0 11 "io"
paddy@0 12 "strings"
paddy@0 13 )
paddy@0 14
paddy@0 15 // A UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC
paddy@0 16 // 4122.
paddy@0 17 type UUID []byte
paddy@0 18
paddy@0 19 // A Version represents a UUIDs version.
paddy@0 20 type Version byte
paddy@0 21
paddy@0 22 // A Variant represents a UUIDs variant.
paddy@0 23 type Variant byte
paddy@0 24
paddy@0 25 // Constants returned by Variant.
paddy@0 26 const (
paddy@0 27 Invalid = Variant(iota) // Invalid UUID
paddy@0 28 RFC4122 // The variant specified in RFC4122
paddy@0 29 Reserved // Reserved, NCS backward compatibility.
paddy@0 30 Microsoft // Reserved, Microsoft Corporation backward compatibility.
paddy@0 31 Future // Reserved for future definition.
paddy@0 32 )
paddy@0 33
paddy@0 34 var rander = rand.Reader // random function
paddy@0 35
paddy@0 36 // New returns a new random (version 4) UUID as a string. It is a convenience
paddy@0 37 // function for NewRandom().String().
paddy@0 38 func New() string {
paddy@0 39 return NewRandom().String()
paddy@0 40 }
paddy@0 41
paddy@0 42 // Parse decodes s into a UUID or returns nil. Both the UUID form of
paddy@0 43 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and
paddy@0 44 // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx are decoded.
paddy@0 45 func Parse(s string) UUID {
paddy@0 46 if len(s) == 36+9 {
paddy@0 47 if strings.ToLower(s[:9]) != "urn:uuid:" {
paddy@0 48 return nil
paddy@0 49 }
paddy@0 50 s = s[9:]
paddy@0 51 } else if len(s) != 36 {
paddy@0 52 return nil
paddy@0 53 }
paddy@0 54 if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' {
paddy@0 55 return nil
paddy@0 56 }
paddy@0 57 uuid := make([]byte, 16)
paddy@0 58 for i, x := range []int{
paddy@0 59 0, 2, 4, 6,
paddy@0 60 9, 11,
paddy@0 61 14, 16,
paddy@0 62 19, 21,
paddy@0 63 24, 26, 28, 30, 32, 34} {
paddy@0 64 if v, ok := xtob(s[x:]); !ok {
paddy@0 65 return nil
paddy@0 66 } else {
paddy@0 67 uuid[i] = v
paddy@0 68 }
paddy@0 69 }
paddy@0 70 return uuid
paddy@0 71 }
paddy@0 72
paddy@0 73 // Equal returns true if uuid1 and uuid2 are equal.
paddy@0 74 func Equal(uuid1, uuid2 UUID) bool {
paddy@0 75 return bytes.Equal(uuid1, uuid2)
paddy@0 76 }
paddy@0 77
paddy@0 78 // String returns the string form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
paddy@0 79 // , or "" if uuid is invalid.
paddy@0 80 func (uuid UUID) String() string {
paddy@0 81 if uuid == nil || len(uuid) != 16 {
paddy@0 82 return ""
paddy@0 83 }
paddy@0 84 b := []byte(uuid)
paddy@0 85 return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x",
paddy@0 86 b[:4], b[4:6], b[6:8], b[8:10], b[10:])
paddy@0 87 }
paddy@0 88
paddy@0 89 // URN returns the RFC 2141 URN form of uuid,
paddy@0 90 // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, or "" if uuid is invalid.
paddy@0 91 func (uuid UUID) URN() string {
paddy@0 92 if uuid == nil || len(uuid) != 16 {
paddy@0 93 return ""
paddy@0 94 }
paddy@0 95 b := []byte(uuid)
paddy@0 96 return fmt.Sprintf("urn:uuid:%08x-%04x-%04x-%04x-%012x",
paddy@0 97 b[:4], b[4:6], b[6:8], b[8:10], b[10:])
paddy@0 98 }
paddy@0 99
paddy@0 100 // Variant returns the variant encoded in uuid. It returns Invalid if
paddy@0 101 // uuid is invalid.
paddy@0 102 func (uuid UUID) Variant() Variant {
paddy@0 103 if len(uuid) != 16 {
paddy@0 104 return Invalid
paddy@0 105 }
paddy@0 106 switch {
paddy@0 107 case (uuid[8] & 0xc0) == 0x80:
paddy@0 108 return RFC4122
paddy@0 109 case (uuid[8] & 0xe0) == 0xc0:
paddy@0 110 return Microsoft
paddy@0 111 case (uuid[8] & 0xe0) == 0xe0:
paddy@0 112 return Future
paddy@0 113 default:
paddy@0 114 return Reserved
paddy@0 115 }
paddy@0 116 panic("unreachable")
paddy@0 117 }
paddy@0 118
paddy@0 119 // Version returns the verison of uuid. It returns false if uuid is not
paddy@0 120 // valid.
paddy@0 121 func (uuid UUID) Version() (Version, bool) {
paddy@0 122 if len(uuid) != 16 {
paddy@0 123 return 0, false
paddy@0 124 }
paddy@0 125 return Version(uuid[6] >> 4), true
paddy@0 126 }
paddy@0 127
paddy@0 128 func (v Version) String() string {
paddy@0 129 if v > 15 {
paddy@0 130 return fmt.Sprintf("BAD_VERSION_%d", v)
paddy@0 131 }
paddy@0 132 return fmt.Sprintf("VERSION_%d", v)
paddy@0 133 }
paddy@0 134
paddy@0 135 func (v Variant) String() string {
paddy@0 136 switch v {
paddy@0 137 case RFC4122:
paddy@0 138 return "RFC4122"
paddy@0 139 case Reserved:
paddy@0 140 return "Reserved"
paddy@0 141 case Microsoft:
paddy@0 142 return "Microsoft"
paddy@0 143 case Future:
paddy@0 144 return "Future"
paddy@0 145 case Invalid:
paddy@0 146 return "Invalid"
paddy@0 147 }
paddy@0 148 return fmt.Sprintf("BadVariant%d", int(v))
paddy@0 149 }
paddy@0 150
paddy@0 151 // SetRand sets the random number generator to r, which implents io.Reader.
paddy@0 152 // If r.Read returns an error when the package requests random data then
paddy@0 153 // a panic will be issued.
paddy@0 154 //
paddy@0 155 // Calling SetRand with nil sets the random number generator to the default
paddy@0 156 // generator.
paddy@0 157 func SetRand(r io.Reader) {
paddy@0 158 if r == nil {
paddy@0 159 rander = rand.Reader
paddy@0 160 return
paddy@0 161 }
paddy@0 162 rander = r
paddy@0 163 }