ducky/devices
16:a700ede02f91 Browse Files
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.
Godeps/Godeps.json apiv1/devices.go apiv1/handlers.go apiv1/scopes.go devices.go vendor/code.google.com/p/go-uuid/uuid/LICENSE vendor/code.google.com/p/go-uuid/uuid/dce.go vendor/code.google.com/p/go-uuid/uuid/doc.go vendor/code.google.com/p/go-uuid/uuid/hash.go vendor/code.google.com/p/go-uuid/uuid/json.go vendor/code.google.com/p/go-uuid/uuid/node.go vendor/code.google.com/p/go-uuid/uuid/time.go vendor/code.google.com/p/go-uuid/uuid/util.go vendor/code.google.com/p/go-uuid/uuid/uuid.go vendor/code.google.com/p/go-uuid/uuid/version1.go vendor/code.google.com/p/go-uuid/uuid/version4.go vendor/code.secondbit.org/api.hg/api.go vendor/code.secondbit.org/pqarrays.hg/.hgignore vendor/code.secondbit.org/pqarrays.hg/array.go vendor/code.secondbit.org/pqarrays.hg/lexer.go vendor/code.secondbit.org/pqarrays.hg/parser.go vendor/code.secondbit.org/uuid.hg/uuid.go vendor/github.com/pborman/uuid/CONTRIBUTORS vendor/github.com/pborman/uuid/LICENSE vendor/github.com/pborman/uuid/dce.go vendor/github.com/pborman/uuid/doc.go vendor/github.com/pborman/uuid/hash.go vendor/github.com/pborman/uuid/json.go vendor/github.com/pborman/uuid/node.go vendor/github.com/pborman/uuid/sql.go vendor/github.com/pborman/uuid/time.go vendor/github.com/pborman/uuid/util.go vendor/github.com/pborman/uuid/uuid.go vendor/github.com/pborman/uuid/version1.go vendor/github.com/pborman/uuid/version4.go
1.1 --- a/Godeps/Godeps.json Mon Dec 14 00:12:33 2015 -0800 1.2 +++ b/Godeps/Godeps.json Sat Dec 19 00:18:25 2015 -0800 1.3 @@ -11,14 +11,14 @@ 1.4 "Rev": "'75cd24fc2f2c2a2088577d12123ddee5f54e0675'" 1.5 }, 1.6 { 1.7 - "ImportPath": "code.google.com/p/go-uuid/uuid", 1.8 - "Comment": "null-15", 1.9 - "Rev": "'35bc42037350f0078e3c974c6ea690f1926603ab'" 1.10 + "ImportPath": "code.secondbit.org/api.hg", 1.11 + "Comment": "null-3", 1.12 + "Rev": "'9b954c219259c11ca1a78b3a9167cbeefd572cb3'" 1.13 }, 1.14 { 1.15 - "ImportPath": "code.secondbit.org/api.hg", 1.16 - "Comment": "null-2", 1.17 - "Rev": "'57c9412e80007115060984bcc29fdc24418ff5ce'" 1.18 + "ImportPath": "code.secondbit.org/pqarrays.hg", 1.19 + "Comment": "null-1", 1.20 + "Rev": "'bfe2a4af6bdfe9d6deab58549aa3c40fe7b6f610'" 1.21 }, 1.22 { 1.23 "ImportPath": "code.secondbit.org/scopes.hg/types", 1.24 @@ -32,8 +32,12 @@ 1.25 }, 1.26 { 1.27 "ImportPath": "code.secondbit.org/uuid.hg", 1.28 - "Comment": "null-6", 1.29 - "Rev": "'6b0a9efd13182f3a12bf8f79e01ba19e75a5076e'" 1.30 + "Comment": "null-8", 1.31 + "Rev": "'15ee0cb56e65a4d866745e413efc20114d691862'" 1.32 + }, 1.33 + { 1.34 + "ImportPath": "github.com/pborman/uuid", 1.35 + "Rev": "cccd189d45f7ac3368a0d127efb7f4d08ae0b655" 1.36 }, 1.37 { 1.38 "ImportPath": "golang.org/x/net/context",
2.1 --- a/apiv1/devices.go Mon Dec 14 00:12:33 2015 -0800 2.2 +++ b/apiv1/devices.go Sat Dec 19 00:18:25 2015 -0800 2.3 @@ -1,12 +1,23 @@ 2.4 package apiv1 2.5 2.6 import ( 2.7 + "errors" 2.8 "time" 2.9 2.10 + "code.secondbit.org/api.hg" 2.11 "code.secondbit.org/ducky/devices.hg" 2.12 "code.secondbit.org/uuid.hg" 2.13 ) 2.14 2.15 +var ( 2.16 + errUnauthorizedLastSeen = errors.New("not authorized to set last seen") 2.17 + errUnauthorizedCreated = errors.New("not authorized to set created") 2.18 + errUnauthorizedOwner = errors.New("not authorized to set owner") 2.19 + errInvalidDeviceType = errors.New("device type invalid") 2.20 + errDeviceNameTooShort = errors.New("device name too short") 2.21 + errDeviceNameTooLong = errors.New("device name too long") 2.22 +) 2.23 + 2.24 // Device represents a device as exposed through the API. It is its 2.25 // own type as the business logic and the API have different requirements 2.26 // for the Device type, and require different representations. 2.27 @@ -67,6 +78,26 @@ 2.28 return newDevices 2.29 } 2.30 2.31 -func validateDeviceCreation(d devices.Device) error { 2.32 +func validateDeviceCreation(d devices.Device, scopes []string, user uuid.ID) error { 2.33 + canImport := api.CheckScopes(scopes, ScopeImport.ID) 2.34 + canCreateOtherUserDevices := api.CheckScopes(scopes, ScopeCreateOtherUserDevices.ID) 2.35 + if !d.LastSeen.IsZero() && !canImport { 2.36 + return errUnauthorizedLastSeen 2.37 + } 2.38 + if !d.Created.IsZero() && !canImport { 2.39 + return errUnauthorizedCreated 2.40 + } 2.41 + if !d.Owner.Equal(user) && !canCreateOtherUserDevices { 2.42 + return errUnauthorizedOwner 2.43 + } 2.44 + if !devices.IsValidDeviceType(d.Type) { 2.45 + return errInvalidDeviceType 2.46 + } 2.47 + if len(d.Name) < devices.MinDeviceNameLength { 2.48 + return errDeviceNameTooShort 2.49 + } 2.50 + if len(d.Name) > devices.MaxDeviceNameLength { 2.51 + return errDeviceNameTooLong 2.52 + } 2.53 return nil 2.54 }
3.1 --- a/apiv1/handlers.go Mon Dec 14 00:12:33 2015 -0800 3.2 +++ b/apiv1/handlers.go Sat Dec 19 00:18:25 2015 -0800 3.3 @@ -17,19 +17,56 @@ 3.4 func handleCreateDevices(ctx context.Context, w http.ResponseWriter, r *http.Request) { 3.5 var req createRequest 3.6 var resp Response 3.7 - err := api.Decode(r, &req) 3.8 + 3.9 + userID, err := api.AuthUser(r) 3.10 + if err != nil { 3.11 + if err == api.ErrUserIDNotSet { 3.12 + api.Encode(w, r, http.StatusUnauthorized, Response{Errors: api.AccessDeniedError}) 3.13 + return 3.14 + } 3.15 + api.Encode(w, r, http.StatusForbidden, Response{Errors: api.AccessDeniedError}) 3.16 + return 3.17 + } 3.18 + 3.19 + err = api.Decode(r, &req) 3.20 if err != nil { 3.21 api.Encode(w, r, http.StatusBadRequest, Response{Errors: api.InvalidFormatError}) 3.22 return 3.23 } 3.24 3.25 devicesToCreate := createDevicesFromChanges(req.Devices) 3.26 + passedScopes := api.GetScopes(r) 3.27 for _, device := range devicesToCreate { 3.28 - err := validateDeviceCreation(device) 3.29 - if err != nil { 3.30 - // BUG(paddy): We still need to expose the validation error 3.31 + err := validateDeviceCreation(device, passedScopes, userID) 3.32 + if err == nil { 3.33 + continue 3.34 + } 3.35 + var requestErr api.RequestError 3.36 + switch err { 3.37 + case errUnauthorizedLastSeen: 3.38 + requestErr.Slug = api.RequestErrAccessDenied 3.39 + requestErr.Field = "/lastSeen" 3.40 + case errUnauthorizedCreated: 3.41 + requestErr.Slug = api.RequestErrAccessDenied 3.42 + requestErr.Field = "/created" 3.43 + case errUnauthorizedOwner: 3.44 + requestErr.Slug = api.RequestErrAccessDenied 3.45 + requestErr.Field = "/owner" 3.46 + case errInvalidDeviceType: 3.47 + requestErr.Slug = api.RequestErrInvalidValue 3.48 + requestErr.Field = "/type" 3.49 + case errDeviceNameTooShort: 3.50 + requestErr.Slug = api.RequestErrInsufficient 3.51 + requestErr.Field = "/name" 3.52 + case errDeviceNameTooLong: 3.53 + requestErr.Slug = api.RequestErrOverflow 3.54 + requestErr.Field = "/name" 3.55 + } 3.56 + if requestErr.Slug != "" { 3.57 + api.Encode(w, r, http.StatusBadRequest, Response{Errors: []api.RequestError{requestErr}}) 3.58 return 3.59 } 3.60 + api.Encode(w, r, http.StatusInternalServerError, Response{Errors: api.ActOfGodError}) 3.61 } 3.62 createdDevices, err := devices.CreateMany(devicesToCreate, ctx) 3.63 if err != nil { 3.64 @@ -39,7 +76,7 @@ 3.65 return 3.66 } 3.67 for _, device := range createdDevices { 3.68 - resp.Devices = append(resp.Devices, apiDeviceFromCore(device, api.CheckScopes(r, ScopeViewPushToken.ID))) 3.69 + resp.Devices = append(resp.Devices, apiDeviceFromCore(device, api.CheckScopes(passedScopes, ScopeViewPushToken.ID))) 3.70 } 3.71 api.Encode(w, r, http.StatusCreated, resp) 3.72 }
4.1 --- a/apiv1/scopes.go Mon Dec 14 00:12:33 2015 -0800 4.2 +++ b/apiv1/scopes.go Sat Dec 19 00:18:25 2015 -0800 4.3 @@ -10,4 +10,21 @@ 4.4 Name: "View device push tokens.", 4.5 Description: "View the push tokens that allow sending messages and notifications to your device. This can be used to force your device to open links, and should be granted with extreme caution.", 4.6 } 4.7 + 4.8 + // ScopeImport is a Scope that grants access to bulk importing Devices. It grants 4.9 + // what equates to admin permissions, including the ability to create Devices for 4.10 + // other users, and thus should be granted with extreme caution. 4.11 + ScopeImport = scopeTypes.Scope{ 4.12 + ID: "https://scopes.useducky.com/devices/import", 4.13 + Name: "Import devices.", 4.14 + Description: "Import devices into the system, including creating devices for other users. This should only ever be granted to system resources.", 4.15 + } 4.16 + 4.17 + // ScopeCreateOtherUserDevices is a Scope that grants the user the ability to create 4.18 + // Devices with an Owner property that doesn't match the authenticated user's ID. 4.19 + ScopeCreateOtherUserDevices = scopeTypes.Scope{ 4.20 + ID: "https://scopes.useducky.com/devices/otherUser/create", 4.21 + Name: "Create devices for other users.", 4.22 + Description: "Create devices like usual, but make a different user the owner of the device.", 4.23 + } 4.24 )
5.1 --- a/devices.go Mon Dec 14 00:12:33 2015 -0800 5.2 +++ b/devices.go Sat Dec 19 00:18:25 2015 -0800 5.3 @@ -16,6 +16,13 @@ 5.4 ErrDeviceNotFound = errors.New("device not found") 5.5 ) 5.6 5.7 +const ( 5.8 + // MinDeviceNameLength is the minimum length a Device name can be. 5.9 + MinDeviceNameLength = 1 5.10 + // MaxDeviceNameLength is the maximum length a Device name can be. 5.11 + MaxDeviceNameLength = 64 5.12 +) 5.13 + 5.14 // Device represents a specific device that updates can be pushed to. 5.15 type Device struct { 5.16 ID uuid.ID
6.1 --- a/vendor/code.google.com/p/go-uuid/uuid/LICENSE Mon Dec 14 00:12:33 2015 -0800 6.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 6.3 @@ -1,27 +0,0 @@ 6.4 -Copyright (c) 2009,2014 Google Inc. All rights reserved. 6.5 - 6.6 -Redistribution and use in source and binary forms, with or without 6.7 -modification, are permitted provided that the following conditions are 6.8 -met: 6.9 - 6.10 - * Redistributions of source code must retain the above copyright 6.11 -notice, this list of conditions and the following disclaimer. 6.12 - * Redistributions in binary form must reproduce the above 6.13 -copyright notice, this list of conditions and the following disclaimer 6.14 -in the documentation and/or other materials provided with the 6.15 -distribution. 6.16 - * Neither the name of Google Inc. nor the names of its 6.17 -contributors may be used to endorse or promote products derived from 6.18 -this software without specific prior written permission. 6.19 - 6.20 -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 6.21 -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 6.22 -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 6.23 -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 6.24 -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 6.25 -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 6.26 -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 6.27 -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 6.28 -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 6.29 -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 6.30 -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7.1 --- a/vendor/code.google.com/p/go-uuid/uuid/dce.go Mon Dec 14 00:12:33 2015 -0800 7.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 7.3 @@ -1,84 +0,0 @@ 7.4 -// Copyright 2011 Google Inc. All rights reserved. 7.5 -// Use of this source code is governed by a BSD-style 7.6 -// license that can be found in the LICENSE file. 7.7 - 7.8 -package uuid 7.9 - 7.10 -import ( 7.11 - "encoding/binary" 7.12 - "fmt" 7.13 - "os" 7.14 -) 7.15 - 7.16 -// A Domain represents a Version 2 domain 7.17 -type Domain byte 7.18 - 7.19 -// Domain constants for DCE Security (Version 2) UUIDs. 7.20 -const ( 7.21 - Person = Domain(0) 7.22 - Group = Domain(1) 7.23 - Org = Domain(2) 7.24 -) 7.25 - 7.26 -// NewDCESecurity returns a DCE Security (Version 2) UUID. 7.27 -// 7.28 -// The domain should be one of Person, Group or Org. 7.29 -// On a POSIX system the id should be the users UID for the Person 7.30 -// domain and the users GID for the Group. The meaning of id for 7.31 -// the domain Org or on non-POSIX systems is site defined. 7.32 -// 7.33 -// For a given domain/id pair the same token may be returned for up to 7.34 -// 7 minutes and 10 seconds. 7.35 -func NewDCESecurity(domain Domain, id uint32) UUID { 7.36 - uuid := NewUUID() 7.37 - if uuid != nil { 7.38 - uuid[6] = (uuid[6] & 0x0f) | 0x20 // Version 2 7.39 - uuid[9] = byte(domain) 7.40 - binary.BigEndian.PutUint32(uuid[0:], id) 7.41 - } 7.42 - return uuid 7.43 -} 7.44 - 7.45 -// NewDCEPerson returns a DCE Security (Version 2) UUID in the person 7.46 -// domain with the id returned by os.Getuid. 7.47 -// 7.48 -// NewDCEPerson(Person, uint32(os.Getuid())) 7.49 -func NewDCEPerson() UUID { 7.50 - return NewDCESecurity(Person, uint32(os.Getuid())) 7.51 -} 7.52 - 7.53 -// NewDCEGroup returns a DCE Security (Version 2) UUID in the group 7.54 -// domain with the id returned by os.Getgid. 7.55 -// 7.56 -// NewDCEGroup(Group, uint32(os.Getgid())) 7.57 -func NewDCEGroup() UUID { 7.58 - return NewDCESecurity(Group, uint32(os.Getgid())) 7.59 -} 7.60 - 7.61 -// Domain returns the domain for a Version 2 UUID or false. 7.62 -func (uuid UUID) Domain() (Domain, bool) { 7.63 - if v, _ := uuid.Version(); v != 2 { 7.64 - return 0, false 7.65 - } 7.66 - return Domain(uuid[9]), true 7.67 -} 7.68 - 7.69 -// Id returns the id for a Version 2 UUID or false. 7.70 -func (uuid UUID) Id() (uint32, bool) { 7.71 - if v, _ := uuid.Version(); v != 2 { 7.72 - return 0, false 7.73 - } 7.74 - return binary.BigEndian.Uint32(uuid[0:4]), true 7.75 -} 7.76 - 7.77 -func (d Domain) String() string { 7.78 - switch d { 7.79 - case Person: 7.80 - return "Person" 7.81 - case Group: 7.82 - return "Group" 7.83 - case Org: 7.84 - return "Org" 7.85 - } 7.86 - return fmt.Sprintf("Domain%d", int(d)) 7.87 -}
8.1 --- a/vendor/code.google.com/p/go-uuid/uuid/doc.go Mon Dec 14 00:12:33 2015 -0800 8.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 8.3 @@ -1,8 +0,0 @@ 8.4 -// Copyright 2011 Google Inc. All rights reserved. 8.5 -// Use of this source code is governed by a BSD-style 8.6 -// license that can be found in the LICENSE file. 8.7 - 8.8 -// The uuid package generates and inspects UUIDs. 8.9 -// 8.10 -// UUIDs are based on RFC 4122 and DCE 1.1: Authentication and Security Services. 8.11 -package uuid
9.1 --- a/vendor/code.google.com/p/go-uuid/uuid/hash.go Mon Dec 14 00:12:33 2015 -0800 9.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 9.3 @@ -1,53 +0,0 @@ 9.4 -// Copyright 2011 Google Inc. All rights reserved. 9.5 -// Use of this source code is governed by a BSD-style 9.6 -// license that can be found in the LICENSE file. 9.7 - 9.8 -package uuid 9.9 - 9.10 -import ( 9.11 - "crypto/md5" 9.12 - "crypto/sha1" 9.13 - "hash" 9.14 -) 9.15 - 9.16 -// Well known Name Space IDs and UUIDs 9.17 -var ( 9.18 - NameSpace_DNS = Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8") 9.19 - NameSpace_URL = Parse("6ba7b811-9dad-11d1-80b4-00c04fd430c8") 9.20 - NameSpace_OID = Parse("6ba7b812-9dad-11d1-80b4-00c04fd430c8") 9.21 - NameSpace_X500 = Parse("6ba7b814-9dad-11d1-80b4-00c04fd430c8") 9.22 - NIL = Parse("00000000-0000-0000-0000-000000000000") 9.23 -) 9.24 - 9.25 -// NewHash returns a new UUID dervied from the hash of space concatenated with 9.26 -// data generated by h. The hash should be at least 16 byte in length. The 9.27 -// first 16 bytes of the hash are used to form the UUID. The version of the 9.28 -// UUID will be the lower 4 bits of version. NewHash is used to implement 9.29 -// NewMD5 and NewSHA1. 9.30 -func NewHash(h hash.Hash, space UUID, data []byte, version int) UUID { 9.31 - h.Reset() 9.32 - h.Write(space) 9.33 - h.Write([]byte(data)) 9.34 - s := h.Sum(nil) 9.35 - uuid := make([]byte, 16) 9.36 - copy(uuid, s) 9.37 - uuid[6] = (uuid[6] & 0x0f) | uint8((version&0xf)<<4) 9.38 - uuid[8] = (uuid[8] & 0x3f) | 0x80 // RFC 4122 variant 9.39 - return uuid 9.40 -} 9.41 - 9.42 -// NewMD5 returns a new MD5 (Version 3) UUID based on the 9.43 -// supplied name space and data. 9.44 -// 9.45 -// NewHash(md5.New(), space, data, 3) 9.46 -func NewMD5(space UUID, data []byte) UUID { 9.47 - return NewHash(md5.New(), space, data, 3) 9.48 -} 9.49 - 9.50 -// NewSHA1 returns a new SHA1 (Version 5) UUID based on the 9.51 -// supplied name space and data. 9.52 -// 9.53 -// NewHash(sha1.New(), space, data, 5) 9.54 -func NewSHA1(space UUID, data []byte) UUID { 9.55 - return NewHash(sha1.New(), space, data, 5) 9.56 -}
10.1 --- a/vendor/code.google.com/p/go-uuid/uuid/json.go Mon Dec 14 00:12:33 2015 -0800 10.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 10.3 @@ -1,30 +0,0 @@ 10.4 -// Copyright 2014 Google Inc. All rights reserved. 10.5 -// Use of this source code is governed by a BSD-style 10.6 -// license that can be found in the LICENSE file. 10.7 - 10.8 -package uuid 10.9 - 10.10 -import "errors" 10.11 - 10.12 -func (u UUID) MarshalJSON() ([]byte, error) { 10.13 - if len(u) == 0 { 10.14 - return []byte(`""`), nil 10.15 - } 10.16 - return []byte(`"` + u.String() + `"`), nil 10.17 -} 10.18 - 10.19 -func (u *UUID) UnmarshalJSON(data []byte) error { 10.20 - if len(data) == 0 || string(data) == `""` { 10.21 - return nil 10.22 - } 10.23 - if len(data) < 2 || data[0] != '"' || data[len(data)-1] != '"' { 10.24 - return errors.New("invalid UUID format") 10.25 - } 10.26 - data = data[1 : len(data)-1] 10.27 - uu := Parse(string(data)) 10.28 - if uu == nil { 10.29 - return errors.New("invalid UUID format") 10.30 - } 10.31 - *u = uu 10.32 - return nil 10.33 -}
11.1 --- a/vendor/code.google.com/p/go-uuid/uuid/node.go Mon Dec 14 00:12:33 2015 -0800 11.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 11.3 @@ -1,101 +0,0 @@ 11.4 -// Copyright 2011 Google Inc. All rights reserved. 11.5 -// Use of this source code is governed by a BSD-style 11.6 -// license that can be found in the LICENSE file. 11.7 - 11.8 -package uuid 11.9 - 11.10 -import "net" 11.11 - 11.12 -var ( 11.13 - interfaces []net.Interface // cached list of interfaces 11.14 - ifname string // name of interface being used 11.15 - nodeID []byte // hardware for version 1 UUIDs 11.16 -) 11.17 - 11.18 -// NodeInterface returns the name of the interface from which the NodeID was 11.19 -// derived. The interface "user" is returned if the NodeID was set by 11.20 -// SetNodeID. 11.21 -func NodeInterface() string { 11.22 - return ifname 11.23 -} 11.24 - 11.25 -// SetNodeInterface selects the hardware address to be used for Version 1 UUIDs. 11.26 -// If name is "" then the first usable interface found will be used or a random 11.27 -// Node ID will be generated. If a named interface cannot be found then false 11.28 -// is returned. 11.29 -// 11.30 -// SetNodeInterface never fails when name is "". 11.31 -func SetNodeInterface(name string) bool { 11.32 - if interfaces == nil { 11.33 - var err error 11.34 - interfaces, err = net.Interfaces() 11.35 - if err != nil && name != "" { 11.36 - return false 11.37 - } 11.38 - } 11.39 - 11.40 - for _, ifs := range interfaces { 11.41 - if len(ifs.HardwareAddr) >= 6 && (name == "" || name == ifs.Name) { 11.42 - if setNodeID(ifs.HardwareAddr) { 11.43 - ifname = ifs.Name 11.44 - return true 11.45 - } 11.46 - } 11.47 - } 11.48 - 11.49 - // We found no interfaces with a valid hardware address. If name 11.50 - // does not specify a specific interface generate a random Node ID 11.51 - // (section 4.1.6) 11.52 - if name == "" { 11.53 - if nodeID == nil { 11.54 - nodeID = make([]byte, 6) 11.55 - } 11.56 - randomBits(nodeID) 11.57 - return true 11.58 - } 11.59 - return false 11.60 -} 11.61 - 11.62 -// NodeID returns a slice of a copy of the current Node ID, setting the Node ID 11.63 -// if not already set. 11.64 -func NodeID() []byte { 11.65 - if nodeID == nil { 11.66 - SetNodeInterface("") 11.67 - } 11.68 - nid := make([]byte, 6) 11.69 - copy(nid, nodeID) 11.70 - return nid 11.71 -} 11.72 - 11.73 -// SetNodeID sets the Node ID to be used for Version 1 UUIDs. The first 6 bytes 11.74 -// of id are used. If id is less than 6 bytes then false is returned and the 11.75 -// Node ID is not set. 11.76 -func SetNodeID(id []byte) bool { 11.77 - if setNodeID(id) { 11.78 - ifname = "user" 11.79 - return true 11.80 - } 11.81 - return false 11.82 -} 11.83 - 11.84 -func setNodeID(id []byte) bool { 11.85 - if len(id) < 6 { 11.86 - return false 11.87 - } 11.88 - if nodeID == nil { 11.89 - nodeID = make([]byte, 6) 11.90 - } 11.91 - copy(nodeID, id) 11.92 - return true 11.93 -} 11.94 - 11.95 -// NodeID returns the 6 byte node id encoded in uuid. It returns nil if uuid is 11.96 -// not valid. The NodeID is only well defined for version 1 and 2 UUIDs. 11.97 -func (uuid UUID) NodeID() []byte { 11.98 - if len(uuid) != 16 { 11.99 - return nil 11.100 - } 11.101 - node := make([]byte, 6) 11.102 - copy(node, uuid[10:]) 11.103 - return node 11.104 -}
12.1 --- a/vendor/code.google.com/p/go-uuid/uuid/time.go Mon Dec 14 00:12:33 2015 -0800 12.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 12.3 @@ -1,132 +0,0 @@ 12.4 -// Copyright 2014 Google Inc. All rights reserved. 12.5 -// Use of this source code is governed by a BSD-style 12.6 -// license that can be found in the LICENSE file. 12.7 - 12.8 -package uuid 12.9 - 12.10 -import ( 12.11 - "encoding/binary" 12.12 - "sync" 12.13 - "time" 12.14 -) 12.15 - 12.16 -// A Time represents a time as the number of 100's of nanoseconds since 15 Oct 12.17 -// 1582. 12.18 -type Time int64 12.19 - 12.20 -const ( 12.21 - lillian = 2299160 // Julian day of 15 Oct 1582 12.22 - unix = 2440587 // Julian day of 1 Jan 1970 12.23 - epoch = unix - lillian // Days between epochs 12.24 - g1582 = epoch * 86400 // seconds between epochs 12.25 - g1582ns100 = g1582 * 10000000 // 100s of a nanoseconds between epochs 12.26 -) 12.27 - 12.28 -var ( 12.29 - mu sync.Mutex 12.30 - lasttime uint64 // last time we returned 12.31 - clock_seq uint16 // clock sequence for this run 12.32 - 12.33 - timeNow = time.Now // for testing 12.34 -) 12.35 - 12.36 -// UnixTime converts t the number of seconds and nanoseconds using the Unix 12.37 -// epoch of 1 Jan 1970. 12.38 -func (t Time) UnixTime() (sec, nsec int64) { 12.39 - sec = int64(t - g1582ns100) 12.40 - nsec = (sec % 10000000) * 100 12.41 - sec /= 10000000 12.42 - return sec, nsec 12.43 -} 12.44 - 12.45 -// GetTime returns the current Time (100s of nanoseconds since 15 Oct 1582) and 12.46 -// clock sequence as well as adjusting the clock sequence as needed. An error 12.47 -// is returned if the current time cannot be determined. 12.48 -func GetTime() (Time, uint16, error) { 12.49 - defer mu.Unlock() 12.50 - mu.Lock() 12.51 - return getTime() 12.52 -} 12.53 - 12.54 -func getTime() (Time, uint16, error) { 12.55 - t := timeNow() 12.56 - 12.57 - // If we don't have a clock sequence already, set one. 12.58 - if clock_seq == 0 { 12.59 - setClockSequence(-1) 12.60 - } 12.61 - now := uint64(t.UnixNano()/100) + g1582ns100 12.62 - 12.63 - // If time has gone backwards with this clock sequence then we 12.64 - // increment the clock sequence 12.65 - if now <= lasttime { 12.66 - clock_seq = ((clock_seq + 1) & 0x3fff) | 0x8000 12.67 - } 12.68 - lasttime = now 12.69 - return Time(now), clock_seq, nil 12.70 -} 12.71 - 12.72 -// ClockSequence returns the current clock sequence, generating one if not 12.73 -// already set. The clock sequence is only used for Version 1 UUIDs. 12.74 -// 12.75 -// The uuid package does not use global static storage for the clock sequence or 12.76 -// the last time a UUID was generated. Unless SetClockSequence a new random 12.77 -// clock sequence is generated the first time a clock sequence is requested by 12.78 -// ClockSequence, GetTime, or NewUUID. (section 4.2.1.1) sequence is generated 12.79 -// for 12.80 -func ClockSequence() int { 12.81 - defer mu.Unlock() 12.82 - mu.Lock() 12.83 - return clockSequence() 12.84 -} 12.85 - 12.86 -func clockSequence() int { 12.87 - if clock_seq == 0 { 12.88 - setClockSequence(-1) 12.89 - } 12.90 - return int(clock_seq & 0x3fff) 12.91 -} 12.92 - 12.93 -// SetClockSeq sets the clock sequence to the lower 14 bits of seq. Setting to 12.94 -// -1 causes a new sequence to be generated. 12.95 -func SetClockSequence(seq int) { 12.96 - defer mu.Unlock() 12.97 - mu.Lock() 12.98 - setClockSequence(seq) 12.99 -} 12.100 - 12.101 -func setClockSequence(seq int) { 12.102 - if seq == -1 { 12.103 - var b [2]byte 12.104 - randomBits(b[:]) // clock sequence 12.105 - seq = int(b[0])<<8 | int(b[1]) 12.106 - } 12.107 - old_seq := clock_seq 12.108 - clock_seq = uint16(seq&0x3fff) | 0x8000 // Set our variant 12.109 - if old_seq != clock_seq { 12.110 - lasttime = 0 12.111 - } 12.112 -} 12.113 - 12.114 -// Time returns the time in 100s of nanoseconds since 15 Oct 1582 encoded in 12.115 -// uuid. It returns false if uuid is not valid. The time is only well defined 12.116 -// for version 1 and 2 UUIDs. 12.117 -func (uuid UUID) Time() (Time, bool) { 12.118 - if len(uuid) != 16 { 12.119 - return 0, false 12.120 - } 12.121 - time := int64(binary.BigEndian.Uint32(uuid[0:4])) 12.122 - time |= int64(binary.BigEndian.Uint16(uuid[4:6])) << 32 12.123 - time |= int64(binary.BigEndian.Uint16(uuid[6:8])&0xfff) << 48 12.124 - return Time(time), true 12.125 -} 12.126 - 12.127 -// ClockSequence returns the clock sequence encoded in uuid. It returns false 12.128 -// if uuid is not valid. The clock sequence is only well defined for version 1 12.129 -// and 2 UUIDs. 12.130 -func (uuid UUID) ClockSequence() (int, bool) { 12.131 - if len(uuid) != 16 { 12.132 - return 0, false 12.133 - } 12.134 - return int(binary.BigEndian.Uint16(uuid[8:10])) & 0x3fff, true 12.135 -}
13.1 --- a/vendor/code.google.com/p/go-uuid/uuid/util.go Mon Dec 14 00:12:33 2015 -0800 13.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 13.3 @@ -1,43 +0,0 @@ 13.4 -// Copyright 2011 Google Inc. All rights reserved. 13.5 -// Use of this source code is governed by a BSD-style 13.6 -// license that can be found in the LICENSE file. 13.7 - 13.8 -package uuid 13.9 - 13.10 -import ( 13.11 - "io" 13.12 -) 13.13 - 13.14 -// randomBits completely fills slice b with random data. 13.15 -func randomBits(b []byte) { 13.16 - if _, err := io.ReadFull(rander, b); err != nil { 13.17 - panic(err.Error()) // rand should never fail 13.18 - } 13.19 -} 13.20 - 13.21 -// xvalues returns the value of a byte as a hexadecimal digit or 255. 13.22 -var xvalues = []byte{ 13.23 - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 13.24 - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 13.25 - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 13.26 - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255, 255, 255, 255, 255, 13.27 - 255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255, 255, 255, 13.28 - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 13.29 - 255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255, 255, 255, 13.30 - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 13.31 - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 13.32 - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 13.33 - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 13.34 - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 13.35 - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 13.36 - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 13.37 - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 13.38 - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 13.39 -} 13.40 - 13.41 -// xtob converts the the first two hex bytes of x into a byte. 13.42 -func xtob(x string) (byte, bool) { 13.43 - b1 := xvalues[x[0]] 13.44 - b2 := xvalues[x[1]] 13.45 - return (b1 << 4) | b2, b1 != 255 && b2 != 255 13.46 -}
14.1 --- a/vendor/code.google.com/p/go-uuid/uuid/uuid.go Mon Dec 14 00:12:33 2015 -0800 14.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 14.3 @@ -1,163 +0,0 @@ 14.4 -// Copyright 2011 Google Inc. All rights reserved. 14.5 -// Use of this source code is governed by a BSD-style 14.6 -// license that can be found in the LICENSE file. 14.7 - 14.8 -package uuid 14.9 - 14.10 -import ( 14.11 - "bytes" 14.12 - "crypto/rand" 14.13 - "fmt" 14.14 - "io" 14.15 - "strings" 14.16 -) 14.17 - 14.18 -// A UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC 14.19 -// 4122. 14.20 -type UUID []byte 14.21 - 14.22 -// A Version represents a UUIDs version. 14.23 -type Version byte 14.24 - 14.25 -// A Variant represents a UUIDs variant. 14.26 -type Variant byte 14.27 - 14.28 -// Constants returned by Variant. 14.29 -const ( 14.30 - Invalid = Variant(iota) // Invalid UUID 14.31 - RFC4122 // The variant specified in RFC4122 14.32 - Reserved // Reserved, NCS backward compatibility. 14.33 - Microsoft // Reserved, Microsoft Corporation backward compatibility. 14.34 - Future // Reserved for future definition. 14.35 -) 14.36 - 14.37 -var rander = rand.Reader // random function 14.38 - 14.39 -// New returns a new random (version 4) UUID as a string. It is a convenience 14.40 -// function for NewRandom().String(). 14.41 -func New() string { 14.42 - return NewRandom().String() 14.43 -} 14.44 - 14.45 -// Parse decodes s into a UUID or returns nil. Both the UUID form of 14.46 -// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and 14.47 -// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx are decoded. 14.48 -func Parse(s string) UUID { 14.49 - if len(s) == 36+9 { 14.50 - if strings.ToLower(s[:9]) != "urn:uuid:" { 14.51 - return nil 14.52 - } 14.53 - s = s[9:] 14.54 - } else if len(s) != 36 { 14.55 - return nil 14.56 - } 14.57 - if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' { 14.58 - return nil 14.59 - } 14.60 - uuid := make([]byte, 16) 14.61 - for i, x := range []int{ 14.62 - 0, 2, 4, 6, 14.63 - 9, 11, 14.64 - 14, 16, 14.65 - 19, 21, 14.66 - 24, 26, 28, 30, 32, 34} { 14.67 - if v, ok := xtob(s[x:]); !ok { 14.68 - return nil 14.69 - } else { 14.70 - uuid[i] = v 14.71 - } 14.72 - } 14.73 - return uuid 14.74 -} 14.75 - 14.76 -// Equal returns true if uuid1 and uuid2 are equal. 14.77 -func Equal(uuid1, uuid2 UUID) bool { 14.78 - return bytes.Equal(uuid1, uuid2) 14.79 -} 14.80 - 14.81 -// String returns the string form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx 14.82 -// , or "" if uuid is invalid. 14.83 -func (uuid UUID) String() string { 14.84 - if uuid == nil || len(uuid) != 16 { 14.85 - return "" 14.86 - } 14.87 - b := []byte(uuid) 14.88 - return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x", 14.89 - b[:4], b[4:6], b[6:8], b[8:10], b[10:]) 14.90 -} 14.91 - 14.92 -// URN returns the RFC 2141 URN form of uuid, 14.93 -// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, or "" if uuid is invalid. 14.94 -func (uuid UUID) URN() string { 14.95 - if uuid == nil || len(uuid) != 16 { 14.96 - return "" 14.97 - } 14.98 - b := []byte(uuid) 14.99 - return fmt.Sprintf("urn:uuid:%08x-%04x-%04x-%04x-%012x", 14.100 - b[:4], b[4:6], b[6:8], b[8:10], b[10:]) 14.101 -} 14.102 - 14.103 -// Variant returns the variant encoded in uuid. It returns Invalid if 14.104 -// uuid is invalid. 14.105 -func (uuid UUID) Variant() Variant { 14.106 - if len(uuid) != 16 { 14.107 - return Invalid 14.108 - } 14.109 - switch { 14.110 - case (uuid[8] & 0xc0) == 0x80: 14.111 - return RFC4122 14.112 - case (uuid[8] & 0xe0) == 0xc0: 14.113 - return Microsoft 14.114 - case (uuid[8] & 0xe0) == 0xe0: 14.115 - return Future 14.116 - default: 14.117 - return Reserved 14.118 - } 14.119 - panic("unreachable") 14.120 -} 14.121 - 14.122 -// Version returns the verison of uuid. It returns false if uuid is not 14.123 -// valid. 14.124 -func (uuid UUID) Version() (Version, bool) { 14.125 - if len(uuid) != 16 { 14.126 - return 0, false 14.127 - } 14.128 - return Version(uuid[6] >> 4), true 14.129 -} 14.130 - 14.131 -func (v Version) String() string { 14.132 - if v > 15 { 14.133 - return fmt.Sprintf("BAD_VERSION_%d", v) 14.134 - } 14.135 - return fmt.Sprintf("VERSION_%d", v) 14.136 -} 14.137 - 14.138 -func (v Variant) String() string { 14.139 - switch v { 14.140 - case RFC4122: 14.141 - return "RFC4122" 14.142 - case Reserved: 14.143 - return "Reserved" 14.144 - case Microsoft: 14.145 - return "Microsoft" 14.146 - case Future: 14.147 - return "Future" 14.148 - case Invalid: 14.149 - return "Invalid" 14.150 - } 14.151 - return fmt.Sprintf("BadVariant%d", int(v)) 14.152 -} 14.153 - 14.154 -// SetRand sets the random number generator to r, which implents io.Reader. 14.155 -// If r.Read returns an error when the package requests random data then 14.156 -// a panic will be issued. 14.157 -// 14.158 -// Calling SetRand with nil sets the random number generator to the default 14.159 -// generator. 14.160 -func SetRand(r io.Reader) { 14.161 - if r == nil { 14.162 - rander = rand.Reader 14.163 - return 14.164 - } 14.165 - rander = r 14.166 -}
15.1 --- a/vendor/code.google.com/p/go-uuid/uuid/version1.go Mon Dec 14 00:12:33 2015 -0800 15.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 15.3 @@ -1,41 +0,0 @@ 15.4 -// Copyright 2011 Google Inc. All rights reserved. 15.5 -// Use of this source code is governed by a BSD-style 15.6 -// license that can be found in the LICENSE file. 15.7 - 15.8 -package uuid 15.9 - 15.10 -import ( 15.11 - "encoding/binary" 15.12 -) 15.13 - 15.14 -// NewUUID returns a Version 1 UUID based on the current NodeID and clock 15.15 -// sequence, and the current time. If the NodeID has not been set by SetNodeID 15.16 -// or SetNodeInterface then it will be set automatically. If the NodeID cannot 15.17 -// be set NewUUID returns nil. If clock sequence has not been set by 15.18 -// SetClockSequence then it will be set automatically. If GetTime fails to 15.19 -// return the current NewUUID returns nil. 15.20 -func NewUUID() UUID { 15.21 - if nodeID == nil { 15.22 - SetNodeInterface("") 15.23 - } 15.24 - 15.25 - now, seq, err := GetTime() 15.26 - if err != nil { 15.27 - return nil 15.28 - } 15.29 - 15.30 - uuid := make([]byte, 16) 15.31 - 15.32 - time_low := uint32(now & 0xffffffff) 15.33 - time_mid := uint16((now >> 32) & 0xffff) 15.34 - time_hi := uint16((now >> 48) & 0x0fff) 15.35 - time_hi |= 0x1000 // Version 1 15.36 - 15.37 - binary.BigEndian.PutUint32(uuid[0:], time_low) 15.38 - binary.BigEndian.PutUint16(uuid[4:], time_mid) 15.39 - binary.BigEndian.PutUint16(uuid[6:], time_hi) 15.40 - binary.BigEndian.PutUint16(uuid[8:], seq) 15.41 - copy(uuid[10:], nodeID) 15.42 - 15.43 - return uuid 15.44 -}
16.1 --- a/vendor/code.google.com/p/go-uuid/uuid/version4.go Mon Dec 14 00:12:33 2015 -0800 16.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 16.3 @@ -1,25 +0,0 @@ 16.4 -// Copyright 2011 Google Inc. All rights reserved. 16.5 -// Use of this source code is governed by a BSD-style 16.6 -// license that can be found in the LICENSE file. 16.7 - 16.8 -package uuid 16.9 - 16.10 -// Random returns a Random (Version 4) UUID or panics. 16.11 -// 16.12 -// The strength of the UUIDs is based on the strength of the crypto/rand 16.13 -// package. 16.14 -// 16.15 -// A note about uniqueness derived from from the UUID Wikipedia entry: 16.16 -// 16.17 -// Randomly generated UUIDs have 122 random bits. One's annual risk of being 16.18 -// hit by a meteorite is estimated to be one chance in 17 billion, that 16.19 -// means the probability is about 0.00000000006 (6 × 10−11), 16.20 -// equivalent to the odds of creating a few tens of trillions of UUIDs in a 16.21 -// year and having one duplicate. 16.22 -func NewRandom() UUID { 16.23 - uuid := make([]byte, 16) 16.24 - randomBits([]byte(uuid)) 16.25 - uuid[6] = (uuid[6] & 0x0f) | 0x40 // Version 4 16.26 - uuid[8] = (uuid[8] & 0x3f) | 0x80 // Variant is 10 16.27 - return uuid 16.28 -}
17.1 --- a/vendor/code.secondbit.org/api.hg/api.go Mon Dec 14 00:12:33 2015 -0800 17.2 +++ b/vendor/code.secondbit.org/api.hg/api.go Sat Dec 19 00:18:25 2015 -0800 17.3 @@ -5,6 +5,7 @@ 17.4 "errors" 17.5 "log" 17.6 "net/http" 17.7 + "sort" 17.8 "strings" 17.9 17.10 "bitbucket.org/ww/goautoneg" 17.11 @@ -29,6 +30,7 @@ 17.12 var ( 17.13 ActOfGodError = []RequestError{{Slug: RequestErrActOfGod}} 17.14 InvalidFormatError = []RequestError{{Slug: RequestErrInvalidFormat, Field: "/"}} 17.15 + AccessDeniedError = []RequestError{{Slug: RequestErrAccessDenied}} 17.16 17.17 Encoders = []string{"application/json"} 17.18 17.19 @@ -110,24 +112,26 @@ 17.20 }) 17.21 } 17.22 17.23 -func CheckScopes(r *http.Request, scopes ...string) bool { 17.24 - passedStr := r.Header.Get("scopes") 17.25 - passed := strings.Split(passedStr, " ") 17.26 - for _, scope := range scopes { 17.27 - var found bool 17.28 - for _, p := range passed { 17.29 - if scope == strings.TrimSpace(p) { 17.30 - found = true 17.31 - break 17.32 - } 17.33 - } 17.34 - if !found { 17.35 +func CheckScopes(scopes []string, checking ...string) bool { 17.36 + sort.Strings(scopes) 17.37 + for _, scope := range checking { 17.38 + found := sort.SearchStrings(scopes, scope) 17.39 + if found == len(scopes) || scopes[found] != scope { 17.40 return false 17.41 } 17.42 } 17.43 return true 17.44 } 17.45 17.46 +func GetScopes(r *http.Request) []string { 17.47 + scopes := strings.Split(r.Header.Get("scopes"), " ") 17.48 + for pos, scope := range scopes { 17.49 + scopes[pos] = strings.TrimSpace(scope) 17.50 + } 17.51 + sort.Strings(scopes) 17.52 + return scopes 17.53 +} 17.54 + 17.55 func AuthUser(r *http.Request) (uuid.ID, error) { 17.56 rawID := r.Header.Get("User-ID") 17.57 if rawID == "" {
18.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 18.2 +++ b/vendor/code.secondbit.org/pqarrays.hg/.hgignore Sat Dec 19 00:18:25 2015 -0800 18.3 @@ -0,0 +1,1 @@ 18.4 +cover.out
19.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 19.2 +++ b/vendor/code.secondbit.org/pqarrays.hg/array.go Sat Dec 19 00:18:25 2015 -0800 19.3 @@ -0,0 +1,48 @@ 19.4 +package pqarrays 19.5 + 19.6 +import ( 19.7 + "database/sql/driver" 19.8 + "errors" 19.9 + "strconv" 19.10 + "strings" 19.11 +) 19.12 + 19.13 +var ( 19.14 + ErrUnexpectedValueType = errors.New("expected value to be a string or []byte") 19.15 +) 19.16 + 19.17 +type StringArray []string 19.18 + 19.19 +func (s StringArray) Value() (driver.Value, error) { 19.20 + output := make([]string, 0, len(s)) 19.21 + for _, item := range s { 19.22 + item = strconv.Quote(item) 19.23 + item = strings.Replace(item, "'", "\\'", -1) 19.24 + output = append(output, item) 19.25 + } 19.26 + return []byte(`{` + strings.Join(output, ",") + `}`), nil 19.27 +} 19.28 + 19.29 +func (s *StringArray) Scan(value interface{}) error { 19.30 + *s = (*s)[:0] 19.31 + var input string 19.32 + if _, ok := value.(string); ok { 19.33 + input = value.(string) 19.34 + } else if _, ok := value.([]byte); ok { 19.35 + input = string(value.([]byte)) 19.36 + } else { 19.37 + return ErrUnexpectedValueType 19.38 + } 19.39 + l := lex(input) 19.40 + parsed, err := parse(l) 19.41 + if err != nil { 19.42 + return err 19.43 + } 19.44 + for _, item := range parsed { 19.45 + if item == nil { 19.46 + continue 19.47 + } 19.48 + *s = append(*s, *item) 19.49 + } 19.50 + return nil 19.51 +}
20.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 20.2 +++ b/vendor/code.secondbit.org/pqarrays.hg/lexer.go Sat Dec 19 00:18:25 2015 -0800 20.3 @@ -0,0 +1,298 @@ 20.4 +package pqarrays 20.5 + 20.6 +import ( 20.7 + "fmt" 20.8 + "strings" 20.9 + "unicode" 20.10 + "unicode/utf8" 20.11 +) 20.12 + 20.13 +const ( 20.14 + eof = -1 20.15 + leftDelim = "{" 20.16 + rightDelim = "}" 20.17 + separator = ',' 20.18 +) 20.19 + 20.20 +type tokenType int 20.21 + 20.22 +const ( 20.23 + tokenError tokenType = iota 20.24 + tokenWhitespace 20.25 + tokenArrayStart 20.26 + tokenString 20.27 + tokenNull 20.28 + tokenSeparator 20.29 + tokenArrayEnd 20.30 + tokenEOF 20.31 +) 20.32 + 20.33 +func (t tokenType) String() string { 20.34 + switch t { 20.35 + case tokenError: 20.36 + return "error" 20.37 + case tokenWhitespace: 20.38 + return "whitespace" 20.39 + case tokenArrayStart: 20.40 + return "array start" 20.41 + case tokenString: 20.42 + return "string" 20.43 + case tokenNull: 20.44 + return "null" 20.45 + case tokenSeparator: 20.46 + return "separator" 20.47 + case tokenArrayEnd: 20.48 + return "array end" 20.49 + case tokenEOF: 20.50 + return "eof" 20.51 + default: 20.52 + return "unknown token" 20.53 + } 20.54 +} 20.55 + 20.56 +type stateFunc func(*lexer) stateFunc 20.57 + 20.58 +type lexer struct { 20.59 + tokens chan token 20.60 + input string 20.61 + start int 20.62 + pos int 20.63 + omitted []int 20.64 + width int 20.65 + state stateFunc 20.66 + arrayDepth int 20.67 +} 20.68 + 20.69 +type token struct { 20.70 + typ tokenType 20.71 + val string 20.72 +} 20.73 + 20.74 +func lex(input string) *lexer { 20.75 + l := &lexer{ 20.76 + input: input, 20.77 + tokens: make(chan token), 20.78 + } 20.79 + go l.run() 20.80 + return l 20.81 +} 20.82 + 20.83 +func (l *lexer) nextToken() token { 20.84 + return <-l.tokens 20.85 +} 20.86 + 20.87 +func (l *lexer) run() { 20.88 + for l.state = lexStart; l.state != nil; { // TODO(paddy): default state 20.89 + l.state = l.state(l) 20.90 + } 20.91 +} 20.92 + 20.93 +func (l *lexer) emit(t tokenType) { 20.94 + var val string 20.95 + if len(l.omitted) < 1 { 20.96 + val = l.input[l.start:l.pos] 20.97 + } else { 20.98 + start := l.start 20.99 + for _, pos := range l.omitted { 20.100 + val += l.input[start:pos] 20.101 + start = pos + 1 20.102 + } 20.103 + if l.pos > start { 20.104 + val += l.input[start:l.pos] 20.105 + } 20.106 + } 20.107 + l.tokens <- token{typ: t, val: val} 20.108 + l.start = l.pos 20.109 + l.omitted = l.omitted[0:0] 20.110 +} 20.111 + 20.112 +func (l *lexer) next() rune { 20.113 + if l.pos >= len(l.input) { 20.114 + l.width = 0 20.115 + return eof 20.116 + } 20.117 + var r rune 20.118 + r, l.width = utf8.DecodeRuneInString(l.input[l.pos:]) 20.119 + l.pos += l.width 20.120 + return r 20.121 +} 20.122 + 20.123 +func (l *lexer) omit() { 20.124 + l.omitted = append(l.omitted, l.pos-1) 20.125 +} 20.126 + 20.127 +func (l *lexer) ignore() { 20.128 + l.start = l.pos 20.129 +} 20.130 + 20.131 +func (l *lexer) backup() { 20.132 + l.pos -= l.width 20.133 +} 20.134 + 20.135 +func (l *lexer) peek() rune { 20.136 + r := l.next() 20.137 + l.backup() 20.138 + return r 20.139 +} 20.140 + 20.141 +func (l *lexer) accept(valid string) bool { 20.142 + if strings.IndexRune(valid, l.next()) >= 0 { 20.143 + return true 20.144 + } 20.145 + l.backup() 20.146 + return false 20.147 +} 20.148 + 20.149 +func (l *lexer) acceptRun(valid string) { 20.150 + for strings.IndexRune(valid, l.next()) >= 0 { 20.151 + } 20.152 + l.backup() 20.153 +} 20.154 + 20.155 +func (l *lexer) errorf(format string, args ...interface{}) stateFunc { 20.156 + l.tokens <- token{tokenError, fmt.Sprintf(format, args...)} 20.157 + return nil 20.158 +} 20.159 + 20.160 +func (l *lexer) consumeWhitespace() { 20.161 + for unicode.IsSpace(l.peek()) { 20.162 + l.next() 20.163 + } 20.164 + if l.start > l.pos { 20.165 + l.emit(tokenWhitespace) 20.166 + } 20.167 +} 20.168 + 20.169 +func lexStart(l *lexer) stateFunc { 20.170 + l.consumeWhitespace() 20.171 + return lexArrayStart 20.172 +} 20.173 + 20.174 +func lexArrayStart(l *lexer) stateFunc { 20.175 + if strings.HasPrefix(l.input[l.pos:], leftDelim) { 20.176 + return lexLeftDelim 20.177 + } 20.178 + return l.errorf("expected array to start before %s", l.input[l.pos:]) 20.179 +} 20.180 + 20.181 +func lexLeftDelim(l *lexer) stateFunc { 20.182 + l.pos += len(leftDelim) 20.183 + l.emit(tokenArrayStart) 20.184 + l.arrayDepth += 1 20.185 + return lexItem 20.186 +} 20.187 + 20.188 +func lexRightDelim(l *lexer) stateFunc { 20.189 + l.pos += len(rightDelim) 20.190 + l.emit(tokenArrayEnd) 20.191 + l.arrayDepth -= 1 20.192 + return lexSeparator 20.193 +} 20.194 + 20.195 +func lexItem(l *lexer) stateFunc { 20.196 + l.consumeWhitespace() 20.197 + if strings.HasPrefix(l.input[l.pos:], rightDelim) { 20.198 + return lexRightDelim 20.199 + } 20.200 + if strings.HasPrefix(l.input[l.pos:], leftDelim) { 20.201 + return lexLeftDelim 20.202 + } 20.203 + switch r := l.peek(); { 20.204 + case r == eof: 20.205 + return l.errorf("unclosed array") 20.206 + case r == separator: 20.207 + return l.errorf("empty item in array") 20.208 + case unicode.IsSpace(r): 20.209 + l.consumeWhitespace() 20.210 + return lexItem 20.211 + case r == '"': 20.212 + return lexQuotedString 20.213 + default: 20.214 + return lexString 20.215 + } 20.216 +} 20.217 + 20.218 +func lexQuotedString(l *lexer) stateFunc { 20.219 + l.next() 20.220 + l.ignore() // ignore the open quote 20.221 + for { 20.222 + switch r := l.next(); { 20.223 + case r == eof: 20.224 + return l.errorf("unclosed quoted string") 20.225 + case r == '"': 20.226 + l.backup() 20.227 + l.emit(tokenString) 20.228 + l.next() 20.229 + l.ignore() 20.230 + return lexSeparator 20.231 + case r == '\\': 20.232 + // omit the \ itself 20.233 + l.omit() 20.234 + // always skip over the character following a \ 20.235 + l.next() 20.236 + if r == eof { 20.237 + return l.errorf("unclosed quoted string") 20.238 + } 20.239 + } 20.240 + } 20.241 +} 20.242 + 20.243 +func lexString(l *lexer) stateFunc { 20.244 + for { 20.245 + if strings.HasPrefix(l.input[l.pos:], leftDelim) { 20.246 + return l.errorf(leftDelim + " in unquoted string") 20.247 + } 20.248 + if strings.HasPrefix(l.input[l.pos:], rightDelim) { 20.249 + if l.pos <= l.start { 20.250 + return l.errorf(rightDelim + " in unquoted string") 20.251 + } 20.252 + if string(l.input[l.start:l.pos]) == "NULL" { 20.253 + l.emit(tokenNull) 20.254 + } else { 20.255 + l.emit(tokenString) 20.256 + } 20.257 + return lexRightDelim 20.258 + } 20.259 + switch r := l.next(); { 20.260 + case r == eof: 20.261 + return l.errorf("eof while parsing string") 20.262 + case r == '"': 20.263 + return l.errorf("\" in unquoted string") 20.264 + case unicode.IsSpace(r): 20.265 + return l.errorf("unquoted empty string") 20.266 + case r == '\\': 20.267 + return l.errorf("\\ in unquoted string") 20.268 + case r == separator: 20.269 + l.backup() 20.270 + if l.pos <= l.start { 20.271 + return l.errorf("unquoted empty string") 20.272 + } 20.273 + if string(l.input[l.start:l.pos]) == "NULL" { 20.274 + l.emit(tokenNull) 20.275 + } else { 20.276 + l.emit(tokenString) 20.277 + } 20.278 + return lexSeparator 20.279 + } 20.280 + } 20.281 +} 20.282 + 20.283 +func lexSeparator(l *lexer) stateFunc { 20.284 + if strings.HasPrefix(l.input[l.pos:], rightDelim) { 20.285 + return lexRightDelim 20.286 + } 20.287 + r := l.next() 20.288 + if r == separator { 20.289 + l.emit(tokenSeparator) 20.290 + return lexItem 20.291 + } else if r == eof { 20.292 + if l.arrayDepth > 0 { 20.293 + return l.errorf("unclosed array") 20.294 + } 20.295 + l.emit(tokenEOF) 20.296 + return nil 20.297 + } else { 20.298 + l.backup() 20.299 + return l.errorf("expected %s, none found before %s\n", separator, l.input[l.pos:]) 20.300 + } 20.301 +}
21.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 21.2 +++ b/vendor/code.secondbit.org/pqarrays.hg/parser.go Sat Dec 19 00:18:25 2015 -0800 21.3 @@ -0,0 +1,88 @@ 21.4 +package pqarrays 21.5 + 21.6 +import ( 21.7 + "errors" 21.8 +) 21.9 + 21.10 +func parse(l *lexer) ([]*string, error) { 21.11 + var parsed []*string 21.12 + pchan := make(chan *string) 21.13 + errchan := make(chan error) 21.14 + done := make(chan struct{}) 21.15 + go runParse(l, pchan, errchan, done) 21.16 + for { 21.17 + select { 21.18 + case err := <-errchan: 21.19 + return parsed, err 21.20 + case item := <-pchan: 21.21 + parsed = append(parsed, item) 21.22 + case <-done: 21.23 + return parsed, nil 21.24 + } 21.25 + } 21.26 +} 21.27 + 21.28 +func runParse(l *lexer, parsed chan *string, err chan error, done chan struct{}) { 21.29 + var state parseFunc = parseStart 21.30 + for { 21.31 + var e error 21.32 + state, e = state(l, parsed) 21.33 + if e != nil { 21.34 + err <- e 21.35 + break 21.36 + } 21.37 + if state == nil { 21.38 + break 21.39 + } 21.40 + } 21.41 + close(done) 21.42 +} 21.43 + 21.44 +type parseFunc func(*lexer, chan *string) (parseFunc, error) 21.45 + 21.46 +func parseEOF(l *lexer, parsed chan *string) (parseFunc, error) { 21.47 + tok := l.nextToken() 21.48 + if tok.typ == tokenWhitespace { 21.49 + tok = l.nextToken() 21.50 + } 21.51 + if tok.typ != tokenEOF { 21.52 + return nil, errors.New("expected EOF, got " + tok.typ.String()) 21.53 + } 21.54 + return nil, nil 21.55 +} 21.56 + 21.57 +func parseStringOrNull(l *lexer, parsed chan *string) (parseFunc, error) { 21.58 + tok := l.nextToken() 21.59 + if tok.typ == tokenWhitespace { 21.60 + tok = l.nextToken() 21.61 + } else if tok.typ == tokenString { 21.62 + parsed <- &tok.val 21.63 + return parseSeparatorOrDelim, nil 21.64 + } else if tok.typ == tokenNull { 21.65 + parsed <- nil 21.66 + return parseSeparatorOrDelim, nil 21.67 + } 21.68 + return nil, errors.New("expected string, got " + tok.typ.String()) 21.69 +} 21.70 + 21.71 +func parseSeparatorOrDelim(l *lexer, parsed chan *string) (parseFunc, error) { 21.72 + tok := l.nextToken() 21.73 + if tok.typ == tokenWhitespace { 21.74 + return parseSeparatorOrDelim, nil 21.75 + } else if tok.typ == tokenSeparator { 21.76 + return parseStringOrNull, nil 21.77 + } else if tok.typ == tokenArrayEnd { 21.78 + return parseEOF, nil 21.79 + } 21.80 + return nil, errors.New("expected separator or delim, got " + tok.typ.String()) 21.81 +} 21.82 + 21.83 +func parseStart(l *lexer, parsed chan *string) (parseFunc, error) { 21.84 + tok := l.nextToken() 21.85 + if tok.typ == tokenWhitespace { 21.86 + return parseStart, nil 21.87 + } else if tok.typ == tokenArrayStart { 21.88 + return parseStringOrNull, nil 21.89 + } 21.90 + return nil, errors.New("expected separator or delim, got " + tok.typ.String()) 21.91 +}
22.1 --- a/vendor/code.secondbit.org/uuid.hg/uuid.go Mon Dec 14 00:12:33 2015 -0800 22.2 +++ b/vendor/code.secondbit.org/uuid.hg/uuid.go Sat Dec 19 00:18:25 2015 -0800 22.3 @@ -2,11 +2,9 @@ 22.4 22.5 import ( 22.6 "database/sql/driver" 22.7 - "encoding/json" 22.8 - "encoding/xml" 22.9 "errors" 22.10 22.11 - "code.google.com/p/go-uuid/uuid" 22.12 + "github.com/pborman/uuid" 22.13 ) 22.14 22.15 var InvalidIDError = errors.New("Invalid ID format.") 22.16 @@ -45,11 +43,7 @@ 22.17 } 22.18 22.19 func (id ID) MarshalJSON() ([]byte, error) { 22.20 - return json.Marshal(id.String()) 22.21 -} 22.22 - 22.23 -func (id ID) MarshalXML(e *xml.Encoder, start xml.StartElement) error { 22.24 - return e.EncodeElement(id.String(), start) 22.25 + return uuid.UUID(id).MarshalJSON() 22.26 } 22.27 22.28 func (id ID) Value() (driver.Value, error) { 22.29 @@ -57,65 +51,11 @@ 22.30 } 22.31 22.32 func (id *ID) Scan(src interface{}) error { 22.33 - if src == nil { 22.34 - id = nil 22.35 - return nil 22.36 - } 22.37 - switch src.(type) { 22.38 - case []byte: 22.39 - if len(src.([]byte)) == 0 { 22.40 - *id = (*id)[:0] 22.41 - return nil 22.42 - } 22.43 - newID, err := Parse(string(src.([]byte))) 22.44 - if err != nil { 22.45 - return err 22.46 - } 22.47 - *id = append((*id)[:0], newID...) 22.48 - return nil 22.49 - case string: 22.50 - if len(src.(string)) == 0 { 22.51 - *id = (*id)[:0] 22.52 - return nil 22.53 - } 22.54 - newID, err := Parse(src.(string)) 22.55 - if err != nil { 22.56 - return err 22.57 - } 22.58 - *id = append((*id)[:0], newID...) 22.59 - return nil 22.60 - default: 22.61 - return InvalidIDError 22.62 - } 22.63 - return nil 22.64 + return (*uuid.UUID)(id).Scan(src) 22.65 } 22.66 22.67 func (id *ID) UnmarshalJSON(in []byte) error { 22.68 - var tmp string 22.69 - err := json.Unmarshal(in, &tmp) 22.70 - if err != nil { 22.71 - return err 22.72 - } 22.73 - newID, err := Parse(tmp) 22.74 - if err != nil { 22.75 - return err 22.76 - } 22.77 - *id = append((*id)[:0], newID...) 22.78 - return nil 22.79 -} 22.80 - 22.81 -func (id *ID) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { 22.82 - var tmp string 22.83 - err := d.DecodeElement(&tmp, &start) 22.84 - if err != nil { 22.85 - return err 22.86 - } 22.87 - newID, err := Parse(tmp) 22.88 - if err != nil { 22.89 - return err 22.90 - } 22.91 - *id = append((*id)[:0], newID...) 22.92 - return nil 22.93 + return (*uuid.UUID)(id).UnmarshalJSON(in) 22.94 } 22.95 22.96 func Parse(in string) (ID, error) {
23.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 23.2 +++ b/vendor/github.com/pborman/uuid/CONTRIBUTORS Sat Dec 19 00:18:25 2015 -0800 23.3 @@ -0,0 +1,1 @@ 23.4 +Paul Borman <borman@google.com>
24.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 24.2 +++ b/vendor/github.com/pborman/uuid/LICENSE Sat Dec 19 00:18:25 2015 -0800 24.3 @@ -0,0 +1,27 @@ 24.4 +Copyright (c) 2009,2014 Google Inc. All rights reserved. 24.5 + 24.6 +Redistribution and use in source and binary forms, with or without 24.7 +modification, are permitted provided that the following conditions are 24.8 +met: 24.9 + 24.10 + * Redistributions of source code must retain the above copyright 24.11 +notice, this list of conditions and the following disclaimer. 24.12 + * Redistributions in binary form must reproduce the above 24.13 +copyright notice, this list of conditions and the following disclaimer 24.14 +in the documentation and/or other materials provided with the 24.15 +distribution. 24.16 + * Neither the name of Google Inc. nor the names of its 24.17 +contributors may be used to endorse or promote products derived from 24.18 +this software without specific prior written permission. 24.19 + 24.20 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24.21 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24.22 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24.23 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24.24 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24.25 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24.26 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24.27 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24.28 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24.29 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24.30 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 25.2 +++ b/vendor/github.com/pborman/uuid/dce.go Sat Dec 19 00:18:25 2015 -0800 25.3 @@ -0,0 +1,84 @@ 25.4 +// Copyright 2011 Google Inc. All rights reserved. 25.5 +// Use of this source code is governed by a BSD-style 25.6 +// license that can be found in the LICENSE file. 25.7 + 25.8 +package uuid 25.9 + 25.10 +import ( 25.11 + "encoding/binary" 25.12 + "fmt" 25.13 + "os" 25.14 +) 25.15 + 25.16 +// A Domain represents a Version 2 domain 25.17 +type Domain byte 25.18 + 25.19 +// Domain constants for DCE Security (Version 2) UUIDs. 25.20 +const ( 25.21 + Person = Domain(0) 25.22 + Group = Domain(1) 25.23 + Org = Domain(2) 25.24 +) 25.25 + 25.26 +// NewDCESecurity returns a DCE Security (Version 2) UUID. 25.27 +// 25.28 +// The domain should be one of Person, Group or Org. 25.29 +// On a POSIX system the id should be the users UID for the Person 25.30 +// domain and the users GID for the Group. The meaning of id for 25.31 +// the domain Org or on non-POSIX systems is site defined. 25.32 +// 25.33 +// For a given domain/id pair the same token may be returned for up to 25.34 +// 7 minutes and 10 seconds. 25.35 +func NewDCESecurity(domain Domain, id uint32) UUID { 25.36 + uuid := NewUUID() 25.37 + if uuid != nil { 25.38 + uuid[6] = (uuid[6] & 0x0f) | 0x20 // Version 2 25.39 + uuid[9] = byte(domain) 25.40 + binary.BigEndian.PutUint32(uuid[0:], id) 25.41 + } 25.42 + return uuid 25.43 +} 25.44 + 25.45 +// NewDCEPerson returns a DCE Security (Version 2) UUID in the person 25.46 +// domain with the id returned by os.Getuid. 25.47 +// 25.48 +// NewDCEPerson(Person, uint32(os.Getuid())) 25.49 +func NewDCEPerson() UUID { 25.50 + return NewDCESecurity(Person, uint32(os.Getuid())) 25.51 +} 25.52 + 25.53 +// NewDCEGroup returns a DCE Security (Version 2) UUID in the group 25.54 +// domain with the id returned by os.Getgid. 25.55 +// 25.56 +// NewDCEGroup(Group, uint32(os.Getgid())) 25.57 +func NewDCEGroup() UUID { 25.58 + return NewDCESecurity(Group, uint32(os.Getgid())) 25.59 +} 25.60 + 25.61 +// Domain returns the domain for a Version 2 UUID or false. 25.62 +func (uuid UUID) Domain() (Domain, bool) { 25.63 + if v, _ := uuid.Version(); v != 2 { 25.64 + return 0, false 25.65 + } 25.66 + return Domain(uuid[9]), true 25.67 +} 25.68 + 25.69 +// Id returns the id for a Version 2 UUID or false. 25.70 +func (uuid UUID) Id() (uint32, bool) { 25.71 + if v, _ := uuid.Version(); v != 2 { 25.72 + return 0, false 25.73 + } 25.74 + return binary.BigEndian.Uint32(uuid[0:4]), true 25.75 +} 25.76 + 25.77 +func (d Domain) String() string { 25.78 + switch d { 25.79 + case Person: 25.80 + return "Person" 25.81 + case Group: 25.82 + return "Group" 25.83 + case Org: 25.84 + return "Org" 25.85 + } 25.86 + return fmt.Sprintf("Domain%d", int(d)) 25.87 +}
26.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 26.2 +++ b/vendor/github.com/pborman/uuid/doc.go Sat Dec 19 00:18:25 2015 -0800 26.3 @@ -0,0 +1,8 @@ 26.4 +// Copyright 2011 Google Inc. All rights reserved. 26.5 +// Use of this source code is governed by a BSD-style 26.6 +// license that can be found in the LICENSE file. 26.7 + 26.8 +// The uuid package generates and inspects UUIDs. 26.9 +// 26.10 +// UUIDs are based on RFC 4122 and DCE 1.1: Authentication and Security Services. 26.11 +package uuid
27.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 27.2 +++ b/vendor/github.com/pborman/uuid/hash.go Sat Dec 19 00:18:25 2015 -0800 27.3 @@ -0,0 +1,53 @@ 27.4 +// Copyright 2011 Google Inc. All rights reserved. 27.5 +// Use of this source code is governed by a BSD-style 27.6 +// license that can be found in the LICENSE file. 27.7 + 27.8 +package uuid 27.9 + 27.10 +import ( 27.11 + "crypto/md5" 27.12 + "crypto/sha1" 27.13 + "hash" 27.14 +) 27.15 + 27.16 +// Well known Name Space IDs and UUIDs 27.17 +var ( 27.18 + NameSpace_DNS = Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8") 27.19 + NameSpace_URL = Parse("6ba7b811-9dad-11d1-80b4-00c04fd430c8") 27.20 + NameSpace_OID = Parse("6ba7b812-9dad-11d1-80b4-00c04fd430c8") 27.21 + NameSpace_X500 = Parse("6ba7b814-9dad-11d1-80b4-00c04fd430c8") 27.22 + NIL = Parse("00000000-0000-0000-0000-000000000000") 27.23 +) 27.24 + 27.25 +// NewHash returns a new UUID dervied from the hash of space concatenated with 27.26 +// data generated by h. The hash should be at least 16 byte in length. The 27.27 +// first 16 bytes of the hash are used to form the UUID. The version of the 27.28 +// UUID will be the lower 4 bits of version. NewHash is used to implement 27.29 +// NewMD5 and NewSHA1. 27.30 +func NewHash(h hash.Hash, space UUID, data []byte, version int) UUID { 27.31 + h.Reset() 27.32 + h.Write(space) 27.33 + h.Write([]byte(data)) 27.34 + s := h.Sum(nil) 27.35 + uuid := make([]byte, 16) 27.36 + copy(uuid, s) 27.37 + uuid[6] = (uuid[6] & 0x0f) | uint8((version&0xf)<<4) 27.38 + uuid[8] = (uuid[8] & 0x3f) | 0x80 // RFC 4122 variant 27.39 + return uuid 27.40 +} 27.41 + 27.42 +// NewMD5 returns a new MD5 (Version 3) UUID based on the 27.43 +// supplied name space and data. 27.44 +// 27.45 +// NewHash(md5.New(), space, data, 3) 27.46 +func NewMD5(space UUID, data []byte) UUID { 27.47 + return NewHash(md5.New(), space, data, 3) 27.48 +} 27.49 + 27.50 +// NewSHA1 returns a new SHA1 (Version 5) UUID based on the 27.51 +// supplied name space and data. 27.52 +// 27.53 +// NewHash(sha1.New(), space, data, 5) 27.54 +func NewSHA1(space UUID, data []byte) UUID { 27.55 + return NewHash(sha1.New(), space, data, 5) 27.56 +}
28.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 28.2 +++ b/vendor/github.com/pborman/uuid/json.go Sat Dec 19 00:18:25 2015 -0800 28.3 @@ -0,0 +1,30 @@ 28.4 +// Copyright 2014 Google Inc. All rights reserved. 28.5 +// Use of this source code is governed by a BSD-style 28.6 +// license that can be found in the LICENSE file. 28.7 + 28.8 +package uuid 28.9 + 28.10 +import "errors" 28.11 + 28.12 +func (u UUID) MarshalJSON() ([]byte, error) { 28.13 + if len(u) == 0 { 28.14 + return []byte(`""`), nil 28.15 + } 28.16 + return []byte(`"` + u.String() + `"`), nil 28.17 +} 28.18 + 28.19 +func (u *UUID) UnmarshalJSON(data []byte) error { 28.20 + if len(data) == 0 || string(data) == `""` { 28.21 + return nil 28.22 + } 28.23 + if len(data) < 2 || data[0] != '"' || data[len(data)-1] != '"' { 28.24 + return errors.New("invalid UUID format") 28.25 + } 28.26 + data = data[1 : len(data)-1] 28.27 + uu := Parse(string(data)) 28.28 + if uu == nil { 28.29 + return errors.New("invalid UUID format") 28.30 + } 28.31 + *u = uu 28.32 + return nil 28.33 +}
29.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 29.2 +++ b/vendor/github.com/pborman/uuid/node.go Sat Dec 19 00:18:25 2015 -0800 29.3 @@ -0,0 +1,101 @@ 29.4 +// Copyright 2011 Google Inc. All rights reserved. 29.5 +// Use of this source code is governed by a BSD-style 29.6 +// license that can be found in the LICENSE file. 29.7 + 29.8 +package uuid 29.9 + 29.10 +import "net" 29.11 + 29.12 +var ( 29.13 + interfaces []net.Interface // cached list of interfaces 29.14 + ifname string // name of interface being used 29.15 + nodeID []byte // hardware for version 1 UUIDs 29.16 +) 29.17 + 29.18 +// NodeInterface returns the name of the interface from which the NodeID was 29.19 +// derived. The interface "user" is returned if the NodeID was set by 29.20 +// SetNodeID. 29.21 +func NodeInterface() string { 29.22 + return ifname 29.23 +} 29.24 + 29.25 +// SetNodeInterface selects the hardware address to be used for Version 1 UUIDs. 29.26 +// If name is "" then the first usable interface found will be used or a random 29.27 +// Node ID will be generated. If a named interface cannot be found then false 29.28 +// is returned. 29.29 +// 29.30 +// SetNodeInterface never fails when name is "". 29.31 +func SetNodeInterface(name string) bool { 29.32 + if interfaces == nil { 29.33 + var err error 29.34 + interfaces, err = net.Interfaces() 29.35 + if err != nil && name != "" { 29.36 + return false 29.37 + } 29.38 + } 29.39 + 29.40 + for _, ifs := range interfaces { 29.41 + if len(ifs.HardwareAddr) >= 6 && (name == "" || name == ifs.Name) { 29.42 + if setNodeID(ifs.HardwareAddr) { 29.43 + ifname = ifs.Name 29.44 + return true 29.45 + } 29.46 + } 29.47 + } 29.48 + 29.49 + // We found no interfaces with a valid hardware address. If name 29.50 + // does not specify a specific interface generate a random Node ID 29.51 + // (section 4.1.6) 29.52 + if name == "" { 29.53 + if nodeID == nil { 29.54 + nodeID = make([]byte, 6) 29.55 + } 29.56 + randomBits(nodeID) 29.57 + return true 29.58 + } 29.59 + return false 29.60 +} 29.61 + 29.62 +// NodeID returns a slice of a copy of the current Node ID, setting the Node ID 29.63 +// if not already set. 29.64 +func NodeID() []byte { 29.65 + if nodeID == nil { 29.66 + SetNodeInterface("") 29.67 + } 29.68 + nid := make([]byte, 6) 29.69 + copy(nid, nodeID) 29.70 + return nid 29.71 +} 29.72 + 29.73 +// SetNodeID sets the Node ID to be used for Version 1 UUIDs. The first 6 bytes 29.74 +// of id are used. If id is less than 6 bytes then false is returned and the 29.75 +// Node ID is not set. 29.76 +func SetNodeID(id []byte) bool { 29.77 + if setNodeID(id) { 29.78 + ifname = "user" 29.79 + return true 29.80 + } 29.81 + return false 29.82 +} 29.83 + 29.84 +func setNodeID(id []byte) bool { 29.85 + if len(id) < 6 { 29.86 + return false 29.87 + } 29.88 + if nodeID == nil { 29.89 + nodeID = make([]byte, 6) 29.90 + } 29.91 + copy(nodeID, id) 29.92 + return true 29.93 +} 29.94 + 29.95 +// NodeID returns the 6 byte node id encoded in uuid. It returns nil if uuid is 29.96 +// not valid. The NodeID is only well defined for version 1 and 2 UUIDs. 29.97 +func (uuid UUID) NodeID() []byte { 29.98 + if len(uuid) != 16 { 29.99 + return nil 29.100 + } 29.101 + node := make([]byte, 6) 29.102 + copy(node, uuid[10:]) 29.103 + return node 29.104 +}
30.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 30.2 +++ b/vendor/github.com/pborman/uuid/sql.go Sat Dec 19 00:18:25 2015 -0800 30.3 @@ -0,0 +1,40 @@ 30.4 +// Copyright 2015 Google Inc. All rights reserved. 30.5 +// Use of this source code is governed by a BSD-style 30.6 +// license that can be found in the LICENSE file. 30.7 + 30.8 +package uuid 30.9 + 30.10 +import ( 30.11 + "errors" 30.12 + "fmt" 30.13 +) 30.14 + 30.15 +// Scan implements sql.Scanner so UUIDs can be read from databases transparently 30.16 +// Currently, database types that map to string and []byte are supported. Please 30.17 +// consult database-specific driver documentation for matching types. 30.18 +func (uuid *UUID) Scan(src interface{}) error { 30.19 + switch src.(type) { 30.20 + case string: 30.21 + // see uuid.Parse for required string format 30.22 + parsed := Parse(src.(string)) 30.23 + 30.24 + if parsed == nil { 30.25 + return errors.New("Scan: invalid UUID format") 30.26 + } 30.27 + 30.28 + *uuid = parsed 30.29 + case []byte: 30.30 + // assumes a simple slice of bytes, just check validity and store 30.31 + u := UUID(src.([]byte)) 30.32 + 30.33 + if u.Variant() == Invalid { 30.34 + return errors.New("Scan: invalid UUID format") 30.35 + } 30.36 + 30.37 + *uuid = u 30.38 + default: 30.39 + return fmt.Errorf("Scan: unable to scan type %T into UUID", src) 30.40 + } 30.41 + 30.42 + return nil 30.43 +}
31.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 31.2 +++ b/vendor/github.com/pborman/uuid/time.go Sat Dec 19 00:18:25 2015 -0800 31.3 @@ -0,0 +1,132 @@ 31.4 +// Copyright 2014 Google Inc. All rights reserved. 31.5 +// Use of this source code is governed by a BSD-style 31.6 +// license that can be found in the LICENSE file. 31.7 + 31.8 +package uuid 31.9 + 31.10 +import ( 31.11 + "encoding/binary" 31.12 + "sync" 31.13 + "time" 31.14 +) 31.15 + 31.16 +// A Time represents a time as the number of 100's of nanoseconds since 15 Oct 31.17 +// 1582. 31.18 +type Time int64 31.19 + 31.20 +const ( 31.21 + lillian = 2299160 // Julian day of 15 Oct 1582 31.22 + unix = 2440587 // Julian day of 1 Jan 1970 31.23 + epoch = unix - lillian // Days between epochs 31.24 + g1582 = epoch * 86400 // seconds between epochs 31.25 + g1582ns100 = g1582 * 10000000 // 100s of a nanoseconds between epochs 31.26 +) 31.27 + 31.28 +var ( 31.29 + mu sync.Mutex 31.30 + lasttime uint64 // last time we returned 31.31 + clock_seq uint16 // clock sequence for this run 31.32 + 31.33 + timeNow = time.Now // for testing 31.34 +) 31.35 + 31.36 +// UnixTime converts t the number of seconds and nanoseconds using the Unix 31.37 +// epoch of 1 Jan 1970. 31.38 +func (t Time) UnixTime() (sec, nsec int64) { 31.39 + sec = int64(t - g1582ns100) 31.40 + nsec = (sec % 10000000) * 100 31.41 + sec /= 10000000 31.42 + return sec, nsec 31.43 +} 31.44 + 31.45 +// GetTime returns the current Time (100s of nanoseconds since 15 Oct 1582) and 31.46 +// clock sequence as well as adjusting the clock sequence as needed. An error 31.47 +// is returned if the current time cannot be determined. 31.48 +func GetTime() (Time, uint16, error) { 31.49 + defer mu.Unlock() 31.50 + mu.Lock() 31.51 + return getTime() 31.52 +} 31.53 + 31.54 +func getTime() (Time, uint16, error) { 31.55 + t := timeNow() 31.56 + 31.57 + // If we don't have a clock sequence already, set one. 31.58 + if clock_seq == 0 { 31.59 + setClockSequence(-1) 31.60 + } 31.61 + now := uint64(t.UnixNano()/100) + g1582ns100 31.62 + 31.63 + // If time has gone backwards with this clock sequence then we 31.64 + // increment the clock sequence 31.65 + if now <= lasttime { 31.66 + clock_seq = ((clock_seq + 1) & 0x3fff) | 0x8000 31.67 + } 31.68 + lasttime = now 31.69 + return Time(now), clock_seq, nil 31.70 +} 31.71 + 31.72 +// ClockSequence returns the current clock sequence, generating one if not 31.73 +// already set. The clock sequence is only used for Version 1 UUIDs. 31.74 +// 31.75 +// The uuid package does not use global static storage for the clock sequence or 31.76 +// the last time a UUID was generated. Unless SetClockSequence a new random 31.77 +// clock sequence is generated the first time a clock sequence is requested by 31.78 +// ClockSequence, GetTime, or NewUUID. (section 4.2.1.1) sequence is generated 31.79 +// for 31.80 +func ClockSequence() int { 31.81 + defer mu.Unlock() 31.82 + mu.Lock() 31.83 + return clockSequence() 31.84 +} 31.85 + 31.86 +func clockSequence() int { 31.87 + if clock_seq == 0 { 31.88 + setClockSequence(-1) 31.89 + } 31.90 + return int(clock_seq & 0x3fff) 31.91 +} 31.92 + 31.93 +// SetClockSeq sets the clock sequence to the lower 14 bits of seq. Setting to 31.94 +// -1 causes a new sequence to be generated. 31.95 +func SetClockSequence(seq int) { 31.96 + defer mu.Unlock() 31.97 + mu.Lock() 31.98 + setClockSequence(seq) 31.99 +} 31.100 + 31.101 +func setClockSequence(seq int) { 31.102 + if seq == -1 { 31.103 + var b [2]byte 31.104 + randomBits(b[:]) // clock sequence 31.105 + seq = int(b[0])<<8 | int(b[1]) 31.106 + } 31.107 + old_seq := clock_seq 31.108 + clock_seq = uint16(seq&0x3fff) | 0x8000 // Set our variant 31.109 + if old_seq != clock_seq { 31.110 + lasttime = 0 31.111 + } 31.112 +} 31.113 + 31.114 +// Time returns the time in 100s of nanoseconds since 15 Oct 1582 encoded in 31.115 +// uuid. It returns false if uuid is not valid. The time is only well defined 31.116 +// for version 1 and 2 UUIDs. 31.117 +func (uuid UUID) Time() (Time, bool) { 31.118 + if len(uuid) != 16 { 31.119 + return 0, false 31.120 + } 31.121 + time := int64(binary.BigEndian.Uint32(uuid[0:4])) 31.122 + time |= int64(binary.BigEndian.Uint16(uuid[4:6])) << 32 31.123 + time |= int64(binary.BigEndian.Uint16(uuid[6:8])&0xfff) << 48 31.124 + return Time(time), true 31.125 +} 31.126 + 31.127 +// ClockSequence returns the clock sequence encoded in uuid. It returns false 31.128 +// if uuid is not valid. The clock sequence is only well defined for version 1 31.129 +// and 2 UUIDs. 31.130 +func (uuid UUID) ClockSequence() (int, bool) { 31.131 + if len(uuid) != 16 { 31.132 + return 0, false 31.133 + } 31.134 + return int(binary.BigEndian.Uint16(uuid[8:10])) & 0x3fff, true 31.135 +}
32.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 32.2 +++ b/vendor/github.com/pborman/uuid/util.go Sat Dec 19 00:18:25 2015 -0800 32.3 @@ -0,0 +1,43 @@ 32.4 +// Copyright 2011 Google Inc. All rights reserved. 32.5 +// Use of this source code is governed by a BSD-style 32.6 +// license that can be found in the LICENSE file. 32.7 + 32.8 +package uuid 32.9 + 32.10 +import ( 32.11 + "io" 32.12 +) 32.13 + 32.14 +// randomBits completely fills slice b with random data. 32.15 +func randomBits(b []byte) { 32.16 + if _, err := io.ReadFull(rander, b); err != nil { 32.17 + panic(err.Error()) // rand should never fail 32.18 + } 32.19 +} 32.20 + 32.21 +// xvalues returns the value of a byte as a hexadecimal digit or 255. 32.22 +var xvalues = []byte{ 32.23 + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 32.24 + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 32.25 + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 32.26 + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255, 255, 255, 255, 255, 32.27 + 255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255, 255, 255, 32.28 + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 32.29 + 255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255, 255, 255, 32.30 + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 32.31 + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 32.32 + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 32.33 + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 32.34 + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 32.35 + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 32.36 + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 32.37 + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 32.38 + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 32.39 +} 32.40 + 32.41 +// xtob converts the the first two hex bytes of x into a byte. 32.42 +func xtob(x string) (byte, bool) { 32.43 + b1 := xvalues[x[0]] 32.44 + b2 := xvalues[x[1]] 32.45 + return (b1 << 4) | b2, b1 != 255 && b2 != 255 32.46 +}
33.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 33.2 +++ b/vendor/github.com/pborman/uuid/uuid.go Sat Dec 19 00:18:25 2015 -0800 33.3 @@ -0,0 +1,163 @@ 33.4 +// Copyright 2011 Google Inc. All rights reserved. 33.5 +// Use of this source code is governed by a BSD-style 33.6 +// license that can be found in the LICENSE file. 33.7 + 33.8 +package uuid 33.9 + 33.10 +import ( 33.11 + "bytes" 33.12 + "crypto/rand" 33.13 + "fmt" 33.14 + "io" 33.15 + "strings" 33.16 +) 33.17 + 33.18 +// A UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC 33.19 +// 4122. 33.20 +type UUID []byte 33.21 + 33.22 +// A Version represents a UUIDs version. 33.23 +type Version byte 33.24 + 33.25 +// A Variant represents a UUIDs variant. 33.26 +type Variant byte 33.27 + 33.28 +// Constants returned by Variant. 33.29 +const ( 33.30 + Invalid = Variant(iota) // Invalid UUID 33.31 + RFC4122 // The variant specified in RFC4122 33.32 + Reserved // Reserved, NCS backward compatibility. 33.33 + Microsoft // Reserved, Microsoft Corporation backward compatibility. 33.34 + Future // Reserved for future definition. 33.35 +) 33.36 + 33.37 +var rander = rand.Reader // random function 33.38 + 33.39 +// New returns a new random (version 4) UUID as a string. It is a convenience 33.40 +// function for NewRandom().String(). 33.41 +func New() string { 33.42 + return NewRandom().String() 33.43 +} 33.44 + 33.45 +// Parse decodes s into a UUID or returns nil. Both the UUID form of 33.46 +// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and 33.47 +// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx are decoded. 33.48 +func Parse(s string) UUID { 33.49 + if len(s) == 36+9 { 33.50 + if strings.ToLower(s[:9]) != "urn:uuid:" { 33.51 + return nil 33.52 + } 33.53 + s = s[9:] 33.54 + } else if len(s) != 36 { 33.55 + return nil 33.56 + } 33.57 + if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' { 33.58 + return nil 33.59 + } 33.60 + uuid := make([]byte, 16) 33.61 + for i, x := range []int{ 33.62 + 0, 2, 4, 6, 33.63 + 9, 11, 33.64 + 14, 16, 33.65 + 19, 21, 33.66 + 24, 26, 28, 30, 32, 34} { 33.67 + if v, ok := xtob(s[x:]); !ok { 33.68 + return nil 33.69 + } else { 33.70 + uuid[i] = v 33.71 + } 33.72 + } 33.73 + return uuid 33.74 +} 33.75 + 33.76 +// Equal returns true if uuid1 and uuid2 are equal. 33.77 +func Equal(uuid1, uuid2 UUID) bool { 33.78 + return bytes.Equal(uuid1, uuid2) 33.79 +} 33.80 + 33.81 +// String returns the string form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx 33.82 +// , or "" if uuid is invalid. 33.83 +func (uuid UUID) String() string { 33.84 + if uuid == nil || len(uuid) != 16 { 33.85 + return "" 33.86 + } 33.87 + b := []byte(uuid) 33.88 + return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x", 33.89 + b[:4], b[4:6], b[6:8], b[8:10], b[10:]) 33.90 +} 33.91 + 33.92 +// URN returns the RFC 2141 URN form of uuid, 33.93 +// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, or "" if uuid is invalid. 33.94 +func (uuid UUID) URN() string { 33.95 + if uuid == nil || len(uuid) != 16 { 33.96 + return "" 33.97 + } 33.98 + b := []byte(uuid) 33.99 + return fmt.Sprintf("urn:uuid:%08x-%04x-%04x-%04x-%012x", 33.100 + b[:4], b[4:6], b[6:8], b[8:10], b[10:]) 33.101 +} 33.102 + 33.103 +// Variant returns the variant encoded in uuid. It returns Invalid if 33.104 +// uuid is invalid. 33.105 +func (uuid UUID) Variant() Variant { 33.106 + if len(uuid) != 16 { 33.107 + return Invalid 33.108 + } 33.109 + switch { 33.110 + case (uuid[8] & 0xc0) == 0x80: 33.111 + return RFC4122 33.112 + case (uuid[8] & 0xe0) == 0xc0: 33.113 + return Microsoft 33.114 + case (uuid[8] & 0xe0) == 0xe0: 33.115 + return Future 33.116 + default: 33.117 + return Reserved 33.118 + } 33.119 + panic("unreachable") 33.120 +} 33.121 + 33.122 +// Version returns the verison of uuid. It returns false if uuid is not 33.123 +// valid. 33.124 +func (uuid UUID) Version() (Version, bool) { 33.125 + if len(uuid) != 16 { 33.126 + return 0, false 33.127 + } 33.128 + return Version(uuid[6] >> 4), true 33.129 +} 33.130 + 33.131 +func (v Version) String() string { 33.132 + if v > 15 { 33.133 + return fmt.Sprintf("BAD_VERSION_%d", v) 33.134 + } 33.135 + return fmt.Sprintf("VERSION_%d", v) 33.136 +} 33.137 + 33.138 +func (v Variant) String() string { 33.139 + switch v { 33.140 + case RFC4122: 33.141 + return "RFC4122" 33.142 + case Reserved: 33.143 + return "Reserved" 33.144 + case Microsoft: 33.145 + return "Microsoft" 33.146 + case Future: 33.147 + return "Future" 33.148 + case Invalid: 33.149 + return "Invalid" 33.150 + } 33.151 + return fmt.Sprintf("BadVariant%d", int(v)) 33.152 +} 33.153 + 33.154 +// SetRand sets the random number generator to r, which implents io.Reader. 33.155 +// If r.Read returns an error when the package requests random data then 33.156 +// a panic will be issued. 33.157 +// 33.158 +// Calling SetRand with nil sets the random number generator to the default 33.159 +// generator. 33.160 +func SetRand(r io.Reader) { 33.161 + if r == nil { 33.162 + rander = rand.Reader 33.163 + return 33.164 + } 33.165 + rander = r 33.166 +}
34.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 34.2 +++ b/vendor/github.com/pborman/uuid/version1.go Sat Dec 19 00:18:25 2015 -0800 34.3 @@ -0,0 +1,41 @@ 34.4 +// Copyright 2011 Google Inc. All rights reserved. 34.5 +// Use of this source code is governed by a BSD-style 34.6 +// license that can be found in the LICENSE file. 34.7 + 34.8 +package uuid 34.9 + 34.10 +import ( 34.11 + "encoding/binary" 34.12 +) 34.13 + 34.14 +// NewUUID returns a Version 1 UUID based on the current NodeID and clock 34.15 +// sequence, and the current time. If the NodeID has not been set by SetNodeID 34.16 +// or SetNodeInterface then it will be set automatically. If the NodeID cannot 34.17 +// be set NewUUID returns nil. If clock sequence has not been set by 34.18 +// SetClockSequence then it will be set automatically. If GetTime fails to 34.19 +// return the current NewUUID returns nil. 34.20 +func NewUUID() UUID { 34.21 + if nodeID == nil { 34.22 + SetNodeInterface("") 34.23 + } 34.24 + 34.25 + now, seq, err := GetTime() 34.26 + if err != nil { 34.27 + return nil 34.28 + } 34.29 + 34.30 + uuid := make([]byte, 16) 34.31 + 34.32 + time_low := uint32(now & 0xffffffff) 34.33 + time_mid := uint16((now >> 32) & 0xffff) 34.34 + time_hi := uint16((now >> 48) & 0x0fff) 34.35 + time_hi |= 0x1000 // Version 1 34.36 + 34.37 + binary.BigEndian.PutUint32(uuid[0:], time_low) 34.38 + binary.BigEndian.PutUint16(uuid[4:], time_mid) 34.39 + binary.BigEndian.PutUint16(uuid[6:], time_hi) 34.40 + binary.BigEndian.PutUint16(uuid[8:], seq) 34.41 + copy(uuid[10:], nodeID) 34.42 + 34.43 + return uuid 34.44 +}
35.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 35.2 +++ b/vendor/github.com/pborman/uuid/version4.go Sat Dec 19 00:18:25 2015 -0800 35.3 @@ -0,0 +1,25 @@ 35.4 +// Copyright 2011 Google Inc. All rights reserved. 35.5 +// Use of this source code is governed by a BSD-style 35.6 +// license that can be found in the LICENSE file. 35.7 + 35.8 +package uuid 35.9 + 35.10 +// Random returns a Random (Version 4) UUID or panics. 35.11 +// 35.12 +// The strength of the UUIDs is based on the strength of the crypto/rand 35.13 +// package. 35.14 +// 35.15 +// A note about uniqueness derived from from the UUID Wikipedia entry: 35.16 +// 35.17 +// Randomly generated UUIDs have 122 random bits. One's annual risk of being 35.18 +// hit by a meteorite is estimated to be one chance in 17 billion, that 35.19 +// means the probability is about 0.00000000006 (6 × 10−11), 35.20 +// equivalent to the odds of creating a few tens of trillions of UUIDs in a 35.21 +// year and having one duplicate. 35.22 +func NewRandom() UUID { 35.23 + uuid := make([]byte, 16) 35.24 + randomBits([]byte(uuid)) 35.25 + uuid[6] = (uuid[6] & 0x0f) | 0x40 // Version 4 35.26 + uuid[8] = (uuid[8] & 0x3f) | 0x80 // Variant is 10 35.27 + return uuid 35.28 +}