ducky/devices

Paddy 2015-12-19 Parent:c24a6c5fcd8c Child:ed1b5ba69551

16:a700ede02f91 Go to Latest

ducky/devices/apiv1/devices.go

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.

History
     1.1 --- a/apiv1/devices.go	Mon Dec 14 00:12:33 2015 -0800
     1.2 +++ b/apiv1/devices.go	Sat Dec 19 00:18:25 2015 -0800
     1.3 @@ -1,12 +1,23 @@
     1.4  package apiv1
     1.5  
     1.6  import (
     1.7 +	"errors"
     1.8  	"time"
     1.9  
    1.10 +	"code.secondbit.org/api.hg"
    1.11  	"code.secondbit.org/ducky/devices.hg"
    1.12  	"code.secondbit.org/uuid.hg"
    1.13  )
    1.14  
    1.15 +var (
    1.16 +	errUnauthorizedLastSeen = errors.New("not authorized to set last seen")
    1.17 +	errUnauthorizedCreated  = errors.New("not authorized to set created")
    1.18 +	errUnauthorizedOwner    = errors.New("not authorized to set owner")
    1.19 +	errInvalidDeviceType    = errors.New("device type invalid")
    1.20 +	errDeviceNameTooShort   = errors.New("device name too short")
    1.21 +	errDeviceNameTooLong    = errors.New("device name too long")
    1.22 +)
    1.23 +
    1.24  // Device represents a device as exposed through the API. It is its
    1.25  // own type as the business logic and the API have different requirements
    1.26  // for the Device type, and require different representations.
    1.27 @@ -67,6 +78,26 @@
    1.28  	return newDevices
    1.29  }
    1.30  
    1.31 -func validateDeviceCreation(d devices.Device) error {
    1.32 +func validateDeviceCreation(d devices.Device, scopes []string, user uuid.ID) error {
    1.33 +	canImport := api.CheckScopes(scopes, ScopeImport.ID)
    1.34 +	canCreateOtherUserDevices := api.CheckScopes(scopes, ScopeCreateOtherUserDevices.ID)
    1.35 +	if !d.LastSeen.IsZero() && !canImport {
    1.36 +		return errUnauthorizedLastSeen
    1.37 +	}
    1.38 +	if !d.Created.IsZero() && !canImport {
    1.39 +		return errUnauthorizedCreated
    1.40 +	}
    1.41 +	if !d.Owner.Equal(user) && !canCreateOtherUserDevices {
    1.42 +		return errUnauthorizedOwner
    1.43 +	}
    1.44 +	if !devices.IsValidDeviceType(d.Type) {
    1.45 +		return errInvalidDeviceType
    1.46 +	}
    1.47 +	if len(d.Name) < devices.MinDeviceNameLength {
    1.48 +		return errDeviceNameTooShort
    1.49 +	}
    1.50 +	if len(d.Name) > devices.MaxDeviceNameLength {
    1.51 +		return errDeviceNameTooLong
    1.52 +	}
    1.53  	return nil
    1.54  }