You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dce.go 2.0KB

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