Skip to content

Commit 41d797c

Browse files
authored
Merge pull request #1045 from Fenny/master
🔐 use uuid v4 in session
2 parents 474be8a + ebd507d commit 41d797c

18 files changed

+950
-3
lines changed

Diff for: internal/uuid/CONTRIBUTORS

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Paul Borman <[email protected]>
2+
bmatsuo
3+
shawnps
4+
theory
5+
jboverfelt
6+
dsymonds
7+
cd1
8+
wallclockbuilder
9+
dansouza

Diff for: internal/uuid/LICENSE

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright (c) 2009,2014 Google Inc. All rights reserved.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions are
5+
met:
6+
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above
10+
copyright notice, this list of conditions and the following disclaimer
11+
in the documentation and/or other materials provided with the
12+
distribution.
13+
* Neither the name of Google Inc. nor the names of its
14+
contributors may be used to endorse or promote products derived from
15+
this software without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Diff for: internal/uuid/dce.go

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright 2016 Google Inc. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package uuid
6+
7+
import (
8+
"encoding/binary"
9+
"fmt"
10+
"os"
11+
)
12+
13+
// A Domain represents a Version 2 domain
14+
type Domain byte
15+
16+
// Domain constants for DCE Security (Version 2) UUIDs.
17+
const (
18+
Person = Domain(0)
19+
Group = Domain(1)
20+
Org = Domain(2)
21+
)
22+
23+
// NewDCESecurity returns a DCE Security (Version 2) UUID.
24+
//
25+
// The domain should be one of Person, Group or Org.
26+
// On a POSIX system the id should be the users UID for the Person
27+
// domain and the users GID for the Group. The meaning of id for
28+
// the domain Org or on non-POSIX systems is site defined.
29+
//
30+
// For a given domain/id pair the same token may be returned for up to
31+
// 7 minutes and 10 seconds.
32+
func NewDCESecurity(domain Domain, id uint32) (UUID, error) {
33+
uuid, err := NewUUID()
34+
if err == nil {
35+
uuid[6] = (uuid[6] & 0x0f) | 0x20 // Version 2
36+
uuid[9] = byte(domain)
37+
binary.BigEndian.PutUint32(uuid[0:], id)
38+
}
39+
return uuid, err
40+
}
41+
42+
// NewDCEPerson returns a DCE Security (Version 2) UUID in the person
43+
// domain with the id returned by os.Getuid.
44+
//
45+
// NewDCESecurity(Person, uint32(os.Getuid()))
46+
func NewDCEPerson() (UUID, error) {
47+
return NewDCESecurity(Person, uint32(os.Getuid()))
48+
}
49+
50+
// NewDCEGroup returns a DCE Security (Version 2) UUID in the group
51+
// domain with the id returned by os.Getgid.
52+
//
53+
// NewDCESecurity(Group, uint32(os.Getgid()))
54+
func NewDCEGroup() (UUID, error) {
55+
return NewDCESecurity(Group, uint32(os.Getgid()))
56+
}
57+
58+
// Domain returns the domain for a Version 2 UUID. Domains are only defined
59+
// for Version 2 UUIDs.
60+
func (uuid UUID) Domain() Domain {
61+
return Domain(uuid[9])
62+
}
63+
64+
// ID returns the id for a Version 2 UUID. IDs are only defined for Version 2
65+
// UUIDs.
66+
func (uuid UUID) ID() uint32 {
67+
return binary.BigEndian.Uint32(uuid[0:4])
68+
}
69+
70+
func (d Domain) String() string {
71+
switch d {
72+
case Person:
73+
return "Person"
74+
case Group:
75+
return "Group"
76+
case Org:
77+
return "Org"
78+
}
79+
return fmt.Sprintf("Domain%d", int(d))
80+
}

Diff for: internal/uuid/doc.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2016 Google Inc. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Package uuid generates and inspects UUIDs.
6+
//
7+
// UUIDs are based on RFC 4122 and DCE 1.1: Authentication and Security
8+
// Services.
9+
//
10+
// A UUID is a 16 byte (128 bit) array. UUIDs may be used as keys to
11+
// maps or compared directly.
12+
package uuid

Diff for: internal/uuid/hash.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2016 Google Inc. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package uuid
6+
7+
import (
8+
"crypto/md5"
9+
"crypto/sha1"
10+
"hash"
11+
)
12+
13+
// Well known namespace IDs and UUIDs
14+
var (
15+
NameSpaceDNS = Must(Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8"))
16+
NameSpaceURL = Must(Parse("6ba7b811-9dad-11d1-80b4-00c04fd430c8"))
17+
NameSpaceOID = Must(Parse("6ba7b812-9dad-11d1-80b4-00c04fd430c8"))
18+
NameSpaceX500 = Must(Parse("6ba7b814-9dad-11d1-80b4-00c04fd430c8"))
19+
Nil UUID // empty UUID, all zeros
20+
)
21+
22+
// NewHash returns a new UUID derived from the hash of space concatenated with
23+
// data generated by h. The hash should be at least 16 byte in length. The
24+
// first 16 bytes of the hash are used to form the UUID. The version of the
25+
// UUID will be the lower 4 bits of version. NewHash is used to implement
26+
// NewMD5 and NewSHA1.
27+
func NewHash(h hash.Hash, space UUID, data []byte, version int) UUID {
28+
h.Reset()
29+
h.Write(space[:])
30+
h.Write(data)
31+
s := h.Sum(nil)
32+
var uuid UUID
33+
copy(uuid[:], s)
34+
uuid[6] = (uuid[6] & 0x0f) | uint8((version&0xf)<<4)
35+
uuid[8] = (uuid[8] & 0x3f) | 0x80 // RFC 4122 variant
36+
return uuid
37+
}
38+
39+
// NewMD5 returns a new MD5 (Version 3) UUID based on the
40+
// supplied name space and data. It is the same as calling:
41+
//
42+
// NewHash(md5.New(), space, data, 3)
43+
func NewMD5(space UUID, data []byte) UUID {
44+
return NewHash(md5.New(), space, data, 3)
45+
}
46+
47+
// NewSHA1 returns a new SHA1 (Version 5) UUID based on the
48+
// supplied name space and data. It is the same as calling:
49+
//
50+
// NewHash(sha1.New(), space, data, 5)
51+
func NewSHA1(space UUID, data []byte) UUID {
52+
return NewHash(sha1.New(), space, data, 5)
53+
}

Diff for: internal/uuid/marshal.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2016 Google Inc. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package uuid
6+
7+
import "fmt"
8+
9+
// MarshalText implements encoding.TextMarshaler.
10+
func (uuid UUID) MarshalText() ([]byte, error) {
11+
var js [36]byte
12+
encodeHex(js[:], uuid)
13+
return js[:], nil
14+
}
15+
16+
// UnmarshalText implements encoding.TextUnmarshaler.
17+
func (uuid *UUID) UnmarshalText(data []byte) error {
18+
id, err := ParseBytes(data)
19+
if err != nil {
20+
return err
21+
}
22+
*uuid = id
23+
return nil
24+
}
25+
26+
// MarshalBinary implements encoding.BinaryMarshaler.
27+
func (uuid UUID) MarshalBinary() ([]byte, error) {
28+
return uuid[:], nil
29+
}
30+
31+
// UnmarshalBinary implements encoding.BinaryUnmarshaler.
32+
func (uuid *UUID) UnmarshalBinary(data []byte) error {
33+
if len(data) != 16 {
34+
return fmt.Errorf("invalid UUID (got %d bytes)", len(data))
35+
}
36+
copy(uuid[:], data)
37+
return nil
38+
}

Diff for: internal/uuid/node.go

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright 2016 Google Inc. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package uuid
6+
7+
import (
8+
"sync"
9+
)
10+
11+
var (
12+
nodeMu sync.Mutex
13+
ifname string // name of interface being used
14+
nodeID [6]byte // hardware for version 1 UUIDs
15+
zeroID [6]byte // nodeID with only 0's
16+
)
17+
18+
// NodeInterface returns the name of the interface from which the NodeID was
19+
// derived. The interface "user" is returned if the NodeID was set by
20+
// SetNodeID.
21+
func NodeInterface() string {
22+
defer nodeMu.Unlock()
23+
nodeMu.Lock()
24+
return ifname
25+
}
26+
27+
// SetNodeInterface selects the hardware address to be used for Version 1 UUIDs.
28+
// If name is "" then the first usable interface found will be used or a random
29+
// Node ID will be generated. If a named interface cannot be found then false
30+
// is returned.
31+
//
32+
// SetNodeInterface never fails when name is "".
33+
func SetNodeInterface(name string) bool {
34+
defer nodeMu.Unlock()
35+
nodeMu.Lock()
36+
return setNodeInterface(name)
37+
}
38+
39+
func setNodeInterface(name string) bool {
40+
iname, addr := getHardwareInterface(name) // null implementation for js
41+
if iname != "" && addr != nil {
42+
ifname = iname
43+
copy(nodeID[:], addr)
44+
return true
45+
}
46+
47+
// We found no interfaces with a valid hardware address. If name
48+
// does not specify a specific interface generate a random Node ID
49+
// (section 4.1.6)
50+
if name == "" {
51+
ifname = "random"
52+
randomBits(nodeID[:])
53+
return true
54+
}
55+
return false
56+
}
57+
58+
// NodeID returns a slice of a copy of the current Node ID, setting the Node ID
59+
// if not already set.
60+
func NodeID() []byte {
61+
defer nodeMu.Unlock()
62+
nodeMu.Lock()
63+
if nodeID == zeroID {
64+
setNodeInterface("")
65+
}
66+
nid := nodeID
67+
return nid[:]
68+
}
69+
70+
// SetNodeID sets the Node ID to be used for Version 1 UUIDs. The first 6 bytes
71+
// of id are used. If id is less than 6 bytes then false is returned and the
72+
// Node ID is not set.
73+
func SetNodeID(id []byte) bool {
74+
if len(id) < 6 {
75+
return false
76+
}
77+
defer nodeMu.Unlock()
78+
nodeMu.Lock()
79+
copy(nodeID[:], id)
80+
ifname = "user"
81+
return true
82+
}
83+
84+
// NodeID returns the 6 byte node id encoded in uuid. It returns nil if uuid is
85+
// not valid. The NodeID is only well defined for version 1 and 2 UUIDs.
86+
func (uuid UUID) NodeID() []byte {
87+
var node [6]byte
88+
copy(node[:], uuid[10:])
89+
return node[:]
90+
}

Diff for: internal/uuid/node_js.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2017 Google Inc. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build js
6+
7+
package uuid
8+
9+
// getHardwareInterface returns nil values for the JS version of the code.
10+
// This remvoves the "net" dependency, because it is not used in the browser.
11+
// Using the "net" library inflates the size of the transpiled JS code by 673k bytes.
12+
func getHardwareInterface(name string) (string, []byte) { return "", nil }

Diff for: internal/uuid/node_net.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2017 Google Inc. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build !js
6+
7+
package uuid
8+
9+
import "net"
10+
11+
var interfaces []net.Interface // cached list of interfaces
12+
13+
// getHardwareInterface returns the name and hardware address of interface name.
14+
// If name is "" then the name and hardware address of one of the system's
15+
// interfaces is returned. If no interfaces are found (name does not exist or
16+
// there are no interfaces) then "", nil is returned.
17+
//
18+
// Only addresses of at least 6 bytes are returned.
19+
func getHardwareInterface(name string) (string, []byte) {
20+
if interfaces == nil {
21+
var err error
22+
interfaces, err = net.Interfaces()
23+
if err != nil {
24+
return "", nil
25+
}
26+
}
27+
for _, ifs := range interfaces {
28+
if len(ifs.HardwareAddr) >= 6 && (name == "" || name == ifs.Name) {
29+
return ifs.Name, ifs.HardwareAddr
30+
}
31+
}
32+
return "", nil
33+
}

0 commit comments

Comments
 (0)