-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidsecret.go
183 lines (131 loc) · 3.09 KB
/
idsecret.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package uri
import (
"context"
"fmt"
"net/url"
"path/filepath"
"strconv"
"strings"
"github.com/aaronland/go-string/random"
)
const IDSECRET_SCHEME string = "idsecret"
type IdSecretURI struct {
URI
origin string
id string
secret string
secret_o string
label string
format string
prefix string
}
func init() {
ctx := context.Background()
RegisterURI(ctx, IDSECRET_SCHEME, NewIdSecretURI)
}
func NewIdSecretURI(ctx context.Context, str_uri string) (URI, error) {
u, err := url.Parse(str_uri)
if err != nil {
return nil, err
}
origin := strings.TrimLeft(u.Path, "/")
if origin == "" {
return nil, fmt.Errorf("Invalid path, '%s' resolves to nil origin after trimming", str_uri)
}
q := u.Query()
str_id := q.Get("id")
if str_id == "" {
return nil, fmt.Errorf("Missing ?id= parameter")
}
if q.Get("ensure-int") != "" {
_, err := strconv.ParseUint(str_id, 10, 64)
if err != nil {
return nil, fmt.Errorf("Failed to resolve integer value for ?id= parameter, %w", err)
}
}
secret := q.Get("secret")
secret_o := q.Get("secret_o")
label := q.Get("label")
format := q.Get("format")
prefix := q.Get("prefix")
rnd_opts := random.DefaultOptions()
rnd_opts.AlphaNumeric = true
if secret == "" {
s, err := random.String(rnd_opts)
if err != nil {
return nil, fmt.Errorf("Failed to derive new secret, %w", err)
}
secret = s
}
if secret_o == "" {
s, err := random.String(rnd_opts)
if err != nil {
return nil, fmt.Errorf("Failed to derive new original secret, %w", err)
}
secret_o = s
}
id_u := IdSecretURI{
origin: origin,
id: str_id,
secret: secret,
secret_o: secret_o,
label: label,
format: format,
prefix: prefix,
}
return &id_u, nil
}
func (u *IdSecretURI) Target(opts *url.Values) (string, error) {
str_id := u.id // strconv.FormatUint(u.id, 10)
prefix := Id2Path(u.id)
if u.prefix != "" {
prefix = u.prefix
}
secret := u.secret
format := u.format
label := u.label
if opts != nil {
format = opts.Get("format")
label = opts.Get("label")
original := opts.Get("original")
if original != "" {
secret = u.secret_o
}
}
if format == "" {
return "", fmt.Errorf("Missing format parameter")
}
if label == "" {
return "", fmt.Errorf("Missing label parameter")
}
fname := fmt.Sprintf("%s_%s_%s.%s", str_id, secret, label, format)
uri := filepath.Join(prefix, fname)
return uri, nil
}
func (u *IdSecretURI) Origin() string {
return u.origin
}
func (u *IdSecretURI) String() string {
q := url.Values{}
q.Set("id", u.id) // strconv.FormatUint(u.id, 10))
q.Set("secret", u.secret)
q.Set("secret_o", u.secret_o)
raw_uri := fmt.Sprintf("%s?%s", u.origin, q.Encode())
return fmt.Sprintf("%s:///%s", u.Scheme(), raw_uri)
}
func (u *IdSecretURI) Scheme() string {
return IDSECRET_SCHEME
}
func Id2Path(id string) string {
parts := []string{""}
input := id // strconv.FormatUint(id, 10)
for len(input) > 3 {
chunk := input[0:3]
input = input[3:]
parts = append(parts, chunk)
}
if len(input) > 0 {
parts = append(parts, input)
}
return filepath.Join(parts...)
}