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.
20 tokenError tokenType = iota
30 func (t tokenType) String() string {
49 return "unknown token"
53 type stateFunc func(*lexer) stateFunc
71 func lex(input string) *lexer {
74 tokens: make(chan token),
80 func (l *lexer) nextToken() token {
84 func (l *lexer) run() {
85 for l.state = lexStart; l.state != nil; { // TODO(paddy): default state
90 func (l *lexer) emit(t tokenType) {
92 if len(l.omitted) < 1 {
93 val = l.input[l.start:l.pos]
96 for _, pos := range l.omitted {
97 val += l.input[start:pos]
101 val += l.input[start:l.pos]
104 l.tokens <- token{typ: t, val: val}
106 l.omitted = l.omitted[0:0]
109 func (l *lexer) next() rune {
110 if l.pos >= len(l.input) {
115 r, l.width = utf8.DecodeRuneInString(l.input[l.pos:])
120 func (l *lexer) omit() {
121 l.omitted = append(l.omitted, l.pos-1)
124 func (l *lexer) ignore() {
128 func (l *lexer) backup() {
132 func (l *lexer) peek() rune {
138 func (l *lexer) accept(valid string) bool {
139 if strings.IndexRune(valid, l.next()) >= 0 {
146 func (l *lexer) acceptRun(valid string) {
147 for strings.IndexRune(valid, l.next()) >= 0 {
152 func (l *lexer) errorf(format string, args ...interface{}) stateFunc {
153 l.tokens <- token{tokenError, fmt.Sprintf(format, args...)}
157 func (l *lexer) consumeWhitespace() {
158 for unicode.IsSpace(l.peek()) {
162 l.emit(tokenWhitespace)
166 func lexStart(l *lexer) stateFunc {
167 l.consumeWhitespace()
171 func lexArrayStart(l *lexer) stateFunc {
172 if strings.HasPrefix(l.input[l.pos:], leftDelim) {
175 return l.errorf("expected array to start before %s", l.input[l.pos:])
178 func lexLeftDelim(l *lexer) stateFunc {
179 l.pos += len(leftDelim)
180 l.emit(tokenArrayStart)
185 func lexRightDelim(l *lexer) stateFunc {
186 l.pos += len(rightDelim)
187 l.emit(tokenArrayEnd)
192 func lexItem(l *lexer) stateFunc {
193 l.consumeWhitespace()
194 if strings.HasPrefix(l.input[l.pos:], rightDelim) {
197 if strings.HasPrefix(l.input[l.pos:], leftDelim) {
200 switch r := l.peek(); {
202 return l.errorf("unclosed array")
204 return l.errorf("empty item in array")
205 case unicode.IsSpace(r):
206 l.consumeWhitespace()
209 return lexQuotedString
215 func lexQuotedString(l *lexer) stateFunc {
217 l.ignore() // ignore the open quote
219 switch r := l.next(); {
221 return l.errorf("unclosed quoted string")
231 // always skip over the character following a \
234 return l.errorf("unclosed quoted string")
240 func lexString(l *lexer) stateFunc {
242 if strings.HasPrefix(l.input[l.pos:], leftDelim) {
243 return l.errorf(leftDelim + " in unquoted string")
245 if strings.HasPrefix(l.input[l.pos:], rightDelim) {
246 if l.pos <= l.start {
247 return l.errorf(rightDelim + " in unquoted string")
249 if string(l.input[l.start:l.pos]) == "NULL" {
256 switch r := l.next(); {
258 return l.errorf("eof while parsing string")
260 return l.errorf("\" in unquoted string")
261 case unicode.IsSpace(r):
262 return l.errorf("unquoted empty string")
264 return l.errorf("\\ in unquoted string")
267 if l.pos <= l.start {
268 return l.errorf("unquoted empty string")
270 if string(l.input[l.start:l.pos]) == "NULL" {
280 func lexSeparator(l *lexer) stateFunc {
281 if strings.HasPrefix(l.input[l.pos:], rightDelim) {
286 l.emit(tokenSeparator)
289 if l.arrayDepth > 0 {
290 return l.errorf("unclosed array")
296 return l.errorf("expected %s, none found before %s\n", separator, l.input[l.pos:])