From b4a8f10cdca11ef522c9a8eec6f8a52f6636806b Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Sun, 18 Mar 2018 21:38:21 +0000 Subject: [PATCH] Ensure query variables are consistent All request variables which are assigned to an sql query are now specified in one location, ensuring that they are consistent and always present for all queries. --- cnf/auth.go | 6 +++--- db/db.go | 59 +++++++++++++++++++++------------------------------ web/signin.go | 12 ----------- web/signup.go | 12 ----------- 4 files changed, 27 insertions(+), 62 deletions(-) diff --git a/cnf/auth.go b/cnf/auth.go index b106af5d..0d48252e 100644 --- a/cnf/auth.go +++ b/cnf/auth.go @@ -31,12 +31,12 @@ type Auth struct { // Reset resets the authentication data. func (a *Auth) Reset() *Auth { - // Remove any saved session data - a.Data = nil - // Reset the authentication level a.Kind = AuthNO + // Remove any saved session data + a.Data = nil + // Clear any authenticated scope a.Scope = "" diff --git a/db/db.go b/db/db.go index a88515d5..25602d9c 100644 --- a/db/db.go +++ b/db/db.go @@ -106,41 +106,6 @@ func Execute(fib *fibre.Context, txt interface{}, vars map[string]interface{}) ( fib = fib.WithContext(nctx) defer span.Finish() - // If no preset variables have been defined - // then ensure that the variables is - // instantiated for future use. - - if vars == nil { - vars = make(map[string]interface{}) - } - - // Ensure that we have a unique id assigned - // to this fibre connection, as we need it - // to detect unique websocket notifications. - - if fib.Get(ctxKeyId) == nil { - fib.Set(ctxKeyId, uuid.New().String()) - } - - // Ensure that the IP address of the - // user signing in is available so that - // it can be used within signin queries. - - vars[varKeyIp] = fib.IP().String() - - // Ensure that the website origin of the - // user signing in is available so that - // it can be used within signin queries. - - vars[varKeyOrigin] = fib.Origin() - - // Ensure that the current authentication data - // is made available as a runtime variable to - // the query layer. - - vars[varKeyAuth] = fib.Get(varKeyAuth).(*cnf.Auth).Data - vars[varKeyScope] = fib.Get(varKeyAuth).(*cnf.Auth).Scope - // Parse the received SQL batch query strings // into SQL ASTs, using any immutable preset // variables if set. @@ -183,6 +148,30 @@ func Process(fib *fibre.Context, ast *sql.Query, vars map[string]interface{}) (o fib.Set(ctxKeyId, uuid.New().String()) } + // Ensure that the IP address of the + // user signing in is available so that + // it can be used within signin queries. + + vars[varKeyIp] = fib.IP().String() + + // Ensure that the website origin of the + // user signing in is available so that + // it can be used within signin queries. + + vars[varKeyOrigin] = fib.Origin() + + // Ensure that the current authentication + // data is made available as a runtime + // variable to the query layer. + + vars[varKeyAuth] = fib.Get(varKeyAuth).(*cnf.Auth).Data + + // Ensure that the current authentication + // scope is made available as a runtime + // variable to the query layer. + + vars[varKeyScope] = fib.Get(varKeyAuth).(*cnf.Auth).Scope + // Create a new context so that we can quit // all goroutine workers if the http client // itself is closed before finishing. diff --git a/web/signin.go b/web/signin.go index c1722712..e718a688 100644 --- a/web/signin.go +++ b/web/signin.go @@ -69,18 +69,6 @@ func signinInternal(c *fibre.Context, vars map[string]interface{}) (str string, d, dok := vars[varKeyDb].(string) s, sok := vars[varKeySc].(string) - // Ensure that the IP address of the - // user signing in is available so that - // it can be used within signin queries. - - vars[varKeyIp] = c.IP().String() - - // Ensure that the website origin of the - // user signing in is available so that - // it can be used within signin queries. - - vars[varKeyOrigin] = c.Origin() - // If we have a namespace, database, and // scope defined, then we are logging in // to the scope level. diff --git a/web/signup.go b/web/signup.go index e05e521b..f24976e4 100644 --- a/web/signup.go +++ b/web/signup.go @@ -68,18 +68,6 @@ func signupInternal(c *fibre.Context, vars map[string]interface{}) (str string, d, dok := vars[varKeyDb].(string) s, sok := vars[varKeySc].(string) - // Ensure that the IP address of the - // user signing up is available so that - // it can be used within signup queries. - - vars[varKeyIp] = c.IP().String() - - // Ensure that the website origin of the - // user signing up is available so that - // it can be used within signup queries. - - vars[varKeyOrigin] = c.Origin() - // If we have a namespace, database, and // scope defined, then we are logging in // to the scope level.