ducky/devices

Paddy 2016-01-02 Parent:a700ede02f91

20:ed1b5ba69551 Go to Latest

ducky/devices/vendor/github.com/pborman/uuid/dce.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 "encoding/binary"
paddy@0 9 "fmt"
paddy@0 10 "os"
paddy@0 11 )
paddy@0 12
paddy@0 13 // A Domain represents a Version 2 domain
paddy@0 14 type Domain byte
paddy@0 15
paddy@0 16 // Domain constants for DCE Security (Version 2) UUIDs.
paddy@0 17 const (
paddy@0 18 Person = Domain(0)
paddy@0 19 Group = Domain(1)
paddy@0 20 Org = Domain(2)
paddy@0 21 )
paddy@0 22
paddy@0 23 // NewDCESecurity returns a DCE Security (Version 2) UUID.
paddy@0 24 //
paddy@0 25 // The domain should be one of Person, Group or Org.
paddy@0 26 // On a POSIX system the id should be the users UID for the Person
paddy@0 27 // domain and the users GID for the Group. The meaning of id for
paddy@0 28 // the domain Org or on non-POSIX systems is site defined.
paddy@0 29 //
paddy@0 30 // For a given domain/id pair the same token may be returned for up to
paddy@0 31 // 7 minutes and 10 seconds.
paddy@0 32 func NewDCESecurity(domain Domain, id uint32) UUID {
paddy@0 33 uuid := NewUUID()
paddy@0 34 if uuid != nil {
paddy@0 35 uuid[6] = (uuid[6] & 0x0f) | 0x20 // Version 2
paddy@0 36 uuid[9] = byte(domain)
paddy@0 37 binary.BigEndian.PutUint32(uuid[0:], id)
paddy@0 38 }
paddy@0 39 return uuid
paddy@0 40 }
paddy@0 41
paddy@0 42 // NewDCEPerson returns a DCE Security (Version 2) UUID in the person
paddy@0 43 // domain with the id returned by os.Getuid.
paddy@0 44 //
paddy@0 45 // NewDCEPerson(Person, uint32(os.Getuid()))
paddy@0 46 func NewDCEPerson() UUID {
paddy@0 47 return NewDCESecurity(Person, uint32(os.Getuid()))
paddy@0 48 }
paddy@0 49
paddy@0 50 // NewDCEGroup returns a DCE Security (Version 2) UUID in the group
paddy@0 51 // domain with the id returned by os.Getgid.
paddy@0 52 //
paddy@0 53 // NewDCEGroup(Group, uint32(os.Getgid()))
paddy@0 54 func NewDCEGroup() UUID {
paddy@0 55 return NewDCESecurity(Group, uint32(os.Getgid()))
paddy@0 56 }
paddy@0 57
paddy@0 58 // Domain returns the domain for a Version 2 UUID or false.
paddy@0 59 func (uuid UUID) Domain() (Domain, bool) {
paddy@0 60 if v, _ := uuid.Version(); v != 2 {
paddy@0 61 return 0, false
paddy@0 62 }
paddy@0 63 return Domain(uuid[9]), true
paddy@0 64 }
paddy@0 65
paddy@0 66 // Id returns the id for a Version 2 UUID or false.
paddy@0 67 func (uuid UUID) Id() (uint32, bool) {
paddy@0 68 if v, _ := uuid.Version(); v != 2 {
paddy@0 69 return 0, false
paddy@0 70 }
paddy@0 71 return binary.BigEndian.Uint32(uuid[0:4]), true
paddy@0 72 }
paddy@0 73
paddy@0 74 func (d Domain) String() string {
paddy@0 75 switch d {
paddy@0 76 case Person:
paddy@0 77 return "Person"
paddy@0 78 case Group:
paddy@0 79 return "Group"
paddy@0 80 case Org:
paddy@0 81 return "Org"
paddy@0 82 }
paddy@0 83 return fmt.Sprintf("Domain%d", int(d))
paddy@0 84 }