uri.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package uri
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "io/ioutil"
  6. "net/url"
  7. "strings"
  8. "unicode"
  9. "github.com/pkg/errors"
  10. )
  11. // URI implements a parser for a URI format based on the the PKCS #11 URI Scheme
  12. // defined in https://tools.ietf.org/html/rfc7512
  13. //
  14. // These URIs will be used to define the key names in a KMS.
  15. type URI struct {
  16. *url.URL
  17. Values url.Values
  18. }
  19. // New creates a new URI from a scheme and key-value pairs.
  20. func New(scheme string, values url.Values) *URI {
  21. return &URI{
  22. URL: &url.URL{
  23. Scheme: scheme,
  24. Opaque: strings.ReplaceAll(values.Encode(), "&", ";"),
  25. },
  26. Values: values,
  27. }
  28. }
  29. // NewFile creates an uri for a file.
  30. func NewFile(path string) *URI {
  31. return &URI{
  32. URL: &url.URL{
  33. Scheme: "file",
  34. Path: path,
  35. },
  36. }
  37. }
  38. // HasScheme returns true if the given uri has the given scheme, false otherwise.
  39. func HasScheme(scheme, rawuri string) bool {
  40. u, err := url.Parse(rawuri)
  41. if err != nil {
  42. return false
  43. }
  44. return strings.EqualFold(u.Scheme, scheme)
  45. }
  46. // Parse returns the URI for the given string or an error.
  47. func Parse(rawuri string) (*URI, error) {
  48. u, err := url.Parse(rawuri)
  49. if err != nil {
  50. return nil, errors.Wrapf(err, "error parsing %s", rawuri)
  51. }
  52. if u.Scheme == "" {
  53. return nil, errors.Errorf("error parsing %s: scheme is missing", rawuri)
  54. }
  55. v, err := url.ParseQuery(u.Opaque)
  56. if err != nil {
  57. return nil, errors.Wrapf(err, "error parsing %s", rawuri)
  58. }
  59. return &URI{
  60. URL: u,
  61. Values: v,
  62. }, nil
  63. }
  64. // ParseWithScheme returns the URI for the given string only if it has the given
  65. // scheme.
  66. func ParseWithScheme(scheme, rawuri string) (*URI, error) {
  67. u, err := Parse(rawuri)
  68. if err != nil {
  69. return nil, err
  70. }
  71. if !strings.EqualFold(u.Scheme, scheme) {
  72. return nil, errors.Errorf("error parsing %s: scheme not expected", rawuri)
  73. }
  74. return u, nil
  75. }
  76. // Get returns the first value in the uri with the given key, it will return
  77. // empty string if that field is not present.
  78. func (u *URI) Get(key string) string {
  79. v := u.Values.Get(key)
  80. if v == "" {
  81. v = u.URL.Query().Get(key)
  82. }
  83. return v
  84. }
  85. // GetEncoded returns the first value in the uri with the given key, it will
  86. // return empty nil if that field is not present or is empty. If the return
  87. // value is hex encoded it will decode it and return it.
  88. func (u *URI) GetEncoded(key string) []byte {
  89. v := u.Get(key)
  90. if v == "" {
  91. return nil
  92. }
  93. if len(v)%2 == 0 {
  94. if b, err := hex.DecodeString(v); err == nil {
  95. return b
  96. }
  97. }
  98. return []byte(v)
  99. }
  100. // Pin returns the pin encoded in the url. It will read the pin from the
  101. // pin-value or the pin-source attributes.
  102. func (u *URI) Pin() string {
  103. if value := u.Get("pin-value"); value != "" {
  104. return value
  105. }
  106. if path := u.Get("pin-source"); path != "" {
  107. if b, err := readFile(path); err == nil {
  108. return string(bytes.TrimRightFunc(b, unicode.IsSpace))
  109. }
  110. }
  111. return ""
  112. }
  113. func readFile(path string) ([]byte, error) {
  114. u, err := url.Parse(path)
  115. if err == nil && (u.Scheme == "" || u.Scheme == "file") && u.Path != "" {
  116. path = u.Path
  117. }
  118. b, err := ioutil.ReadFile(path)
  119. if err != nil {
  120. return nil, errors.Wrapf(err, "error reading %s", path)
  121. }
  122. return b, nil
  123. }