ducky/devices

Paddy 2015-12-22 Parent:a700ede02f91

18:b2fdf827758e Go to Latest

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

Add endpoint for retrieving devices. Add an endpoint for retrieving devices, either as a list or by ID. Stub endpoints for updating and deleting devices., along with TODOs marking them as things to still be completed. (Right now, accessing those endpoints is an insta-panic.) Simplify our handleCreateDevices by returning StatusUnauthorized if AuthUser fails, so we can reserve StatusForbidden for when auth succeeds but access is still denied. Also, delay the instantiation and allocation of a Response variable until we're actually going to use it. Create a handleGetDevices handler that authenticates the user, and if no ID is set, returns a list of all their Devices. If one or more IDs are set, only those Devices are returned. If ScopeViewPushToken is one of the scopes associated with the request, the push tokens for each Device will be included in the response. Otherwise, they will be omitted.

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 }