ducky/devices
ducky/devices/vendor/github.com/pborman/uuid/uuid.go
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.
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/vendor/github.com/pborman/uuid/uuid.go Sat Dec 19 00:18:25 2015 -0800 1.3 @@ -0,0 +1,163 @@ 1.4 +// Copyright 2011 Google Inc. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style 1.6 +// license that can be found in the LICENSE file. 1.7 + 1.8 +package uuid 1.9 + 1.10 +import ( 1.11 + "bytes" 1.12 + "crypto/rand" 1.13 + "fmt" 1.14 + "io" 1.15 + "strings" 1.16 +) 1.17 + 1.18 +// A UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC 1.19 +// 4122. 1.20 +type UUID []byte 1.21 + 1.22 +// A Version represents a UUIDs version. 1.23 +type Version byte 1.24 + 1.25 +// A Variant represents a UUIDs variant. 1.26 +type Variant byte 1.27 + 1.28 +// Constants returned by Variant. 1.29 +const ( 1.30 + Invalid = Variant(iota) // Invalid UUID 1.31 + RFC4122 // The variant specified in RFC4122 1.32 + Reserved // Reserved, NCS backward compatibility. 1.33 + Microsoft // Reserved, Microsoft Corporation backward compatibility. 1.34 + Future // Reserved for future definition. 1.35 +) 1.36 + 1.37 +var rander = rand.Reader // random function 1.38 + 1.39 +// New returns a new random (version 4) UUID as a string. It is a convenience 1.40 +// function for NewRandom().String(). 1.41 +func New() string { 1.42 + return NewRandom().String() 1.43 +} 1.44 + 1.45 +// Parse decodes s into a UUID or returns nil. Both the UUID form of 1.46 +// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and 1.47 +// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx are decoded. 1.48 +func Parse(s string) UUID { 1.49 + if len(s) == 36+9 { 1.50 + if strings.ToLower(s[:9]) != "urn:uuid:" { 1.51 + return nil 1.52 + } 1.53 + s = s[9:] 1.54 + } else if len(s) != 36 { 1.55 + return nil 1.56 + } 1.57 + if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' { 1.58 + return nil 1.59 + } 1.60 + uuid := make([]byte, 16) 1.61 + for i, x := range []int{ 1.62 + 0, 2, 4, 6, 1.63 + 9, 11, 1.64 + 14, 16, 1.65 + 19, 21, 1.66 + 24, 26, 28, 30, 32, 34} { 1.67 + if v, ok := xtob(s[x:]); !ok { 1.68 + return nil 1.69 + } else { 1.70 + uuid[i] = v 1.71 + } 1.72 + } 1.73 + return uuid 1.74 +} 1.75 + 1.76 +// Equal returns true if uuid1 and uuid2 are equal. 1.77 +func Equal(uuid1, uuid2 UUID) bool { 1.78 + return bytes.Equal(uuid1, uuid2) 1.79 +} 1.80 + 1.81 +// String returns the string form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx 1.82 +// , or "" if uuid is invalid. 1.83 +func (uuid UUID) String() string { 1.84 + if uuid == nil || len(uuid) != 16 { 1.85 + return "" 1.86 + } 1.87 + b := []byte(uuid) 1.88 + return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x", 1.89 + b[:4], b[4:6], b[6:8], b[8:10], b[10:]) 1.90 +} 1.91 + 1.92 +// URN returns the RFC 2141 URN form of uuid, 1.93 +// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, or "" if uuid is invalid. 1.94 +func (uuid UUID) URN() string { 1.95 + if uuid == nil || len(uuid) != 16 { 1.96 + return "" 1.97 + } 1.98 + b := []byte(uuid) 1.99 + return fmt.Sprintf("urn:uuid:%08x-%04x-%04x-%04x-%012x", 1.100 + b[:4], b[4:6], b[6:8], b[8:10], b[10:]) 1.101 +} 1.102 + 1.103 +// Variant returns the variant encoded in uuid. It returns Invalid if 1.104 +// uuid is invalid. 1.105 +func (uuid UUID) Variant() Variant { 1.106 + if len(uuid) != 16 { 1.107 + return Invalid 1.108 + } 1.109 + switch { 1.110 + case (uuid[8] & 0xc0) == 0x80: 1.111 + return RFC4122 1.112 + case (uuid[8] & 0xe0) == 0xc0: 1.113 + return Microsoft 1.114 + case (uuid[8] & 0xe0) == 0xe0: 1.115 + return Future 1.116 + default: 1.117 + return Reserved 1.118 + } 1.119 + panic("unreachable") 1.120 +} 1.121 + 1.122 +// Version returns the verison of uuid. It returns false if uuid is not 1.123 +// valid. 1.124 +func (uuid UUID) Version() (Version, bool) { 1.125 + if len(uuid) != 16 { 1.126 + return 0, false 1.127 + } 1.128 + return Version(uuid[6] >> 4), true 1.129 +} 1.130 + 1.131 +func (v Version) String() string { 1.132 + if v > 15 { 1.133 + return fmt.Sprintf("BAD_VERSION_%d", v) 1.134 + } 1.135 + return fmt.Sprintf("VERSION_%d", v) 1.136 +} 1.137 + 1.138 +func (v Variant) String() string { 1.139 + switch v { 1.140 + case RFC4122: 1.141 + return "RFC4122" 1.142 + case Reserved: 1.143 + return "Reserved" 1.144 + case Microsoft: 1.145 + return "Microsoft" 1.146 + case Future: 1.147 + return "Future" 1.148 + case Invalid: 1.149 + return "Invalid" 1.150 + } 1.151 + return fmt.Sprintf("BadVariant%d", int(v)) 1.152 +} 1.153 + 1.154 +// SetRand sets the random number generator to r, which implents io.Reader. 1.155 +// If r.Read returns an error when the package requests random data then 1.156 +// a panic will be issued. 1.157 +// 1.158 +// Calling SetRand with nil sets the random number generator to the default 1.159 +// generator. 1.160 +func SetRand(r io.Reader) { 1.161 + if r == nil { 1.162 + rander = rand.Reader 1.163 + return 1.164 + } 1.165 + rander = r 1.166 +}