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.
14 func (t *trie) match(input []string) ([]int, bool) {
22 for i := 0; i < num; i++ {
23 // if we're on a nil branch, we're at the end of our line
27 offset = pickNextBranch(b, offset, input[i])
30 // can't find it, bail
34 path, offset = backup(path)
35 offset = offset + 1 // we want the next index from the previously matched one
36 b = b.parent // we need to be picking a branch from our parent, again
37 i = i - 2 // back up to the choice before the one that got us here
39 path = append(path, offset)
40 b = b.children[offset]
52 methods map[string]http.Handler
55 func (b *branch) Less(i, j int) bool {
56 if b.children[i].key == b.children[j].key {
57 return b.children[j].isParam && !b.children[i].isParam
59 return b.children[i].key < b.children[j].key
62 func (b *branch) Len() int {
63 return len(b.children)
66 func (b *branch) Swap(i, j int) {
67 b.children[i], b.children[j] = b.children[j], b.children[i]
70 func (b *branch) addChild(key string, param bool) *branch {
71 child := &branch{key: key, parent: b, isParam: param}
72 if b.children == nil {
73 b.children = []*branch{child}
76 b.children = append(b.children, child)
81 func (b *branch) check(input string) bool {
82 if b.isParam && b.key != "" {
91 func (b *branch) setHandler(method string, handler http.Handler) {
93 b.methods = map[string]http.Handler{}
95 b.methods[method] = handler
98 func pickNextBranch(b *branch, offset int, input string) int {
99 count := len(b.children)
100 for i := offset; i < count; i++ {
101 if b.children[i].check(input) {
108 func backup(path []int) ([]int, int) {
109 last := len(path) - 1