Use fibre error fields instead of logging twice

This commit is contained in:
Tobie Morgan Hitchcock 2017-02-24 00:49:41 +00:00
parent a5aefdb8ba
commit 79812544e8
3 changed files with 82 additions and 169 deletions

12
glide.lock generated
View file

@ -1,5 +1,5 @@
hash: 323da77f9dc4a58b21e5b955a28edf6d4489cc74d3d6e61a4b2a60bdfe373b70 hash: 323da77f9dc4a58b21e5b955a28edf6d4489cc74d3d6e61a4b2a60bdfe373b70
updated: 2017-02-23T22:04:50.907304222Z updated: 2017-02-24T00:48:37.913219964Z
imports: imports:
- name: cloud.google.com/go - name: cloud.google.com/go
version: 9b68cf4865e93f379a337b7e957f33de60397a48 version: 9b68cf4865e93f379a337b7e957f33de60397a48
@ -17,7 +17,7 @@ imports:
- name: github.com/abcum/emitr - name: github.com/abcum/emitr
version: 67359a63b282c8056e52cf25488937aaddbc2a8b version: 67359a63b282c8056e52cf25488937aaddbc2a8b
- name: github.com/abcum/fibre - name: github.com/abcum/fibre
version: ddff7a6e8f7a32a42ab1c201c807cce867cbc7c6 version: cfe61e4d3b8a1085f93e82e2fa5f04cac5b0a6f2
subpackages: subpackages:
- mw - mw
- name: github.com/abcum/ptree - name: github.com/abcum/ptree
@ -142,7 +142,7 @@ imports:
- bcrypt - bcrypt
- blowfish - blowfish
- name: golang.org/x/net - name: golang.org/x/net
version: 6b27048ae5e6ad1ef927e72e437531493de612fe version: dd2d9a67c97da0afa00d5726e28086007a0acce5
subpackages: subpackages:
- context - context
- context/ctxhttp - context/ctxhttp
@ -160,11 +160,11 @@ imports:
- jws - jws
- jwt - jwt
- name: golang.org/x/sys - name: golang.org/x/sys
version: 075e574b89e4c2d22f2286a7e2b919519c6f3547 version: e4594059fe4cde2daf423055a596c2cd1e6c9adf
subpackages: subpackages:
- unix - unix
- name: google.golang.org/api - name: google.golang.org/api
version: bc20c61134e1d25265dd60049f5735381e79b631 version: 64485db7e8c8be51e572801d06cdbcfadd3546c1
subpackages: subpackages:
- googleapi/transport - googleapi/transport
- internal - internal
@ -197,7 +197,7 @@ imports:
- googleapis/logging/v2 - googleapis/logging/v2
- googleapis/rpc/status - googleapis/rpc/status
- name: google.golang.org/grpc - name: google.golang.org/grpc
version: 20633fa172ac711ac6a77fd573ed13f23ec56dcb version: 34384f34de585705f1a6783a158d2ec8af29f618
subpackages: subpackages:
- codes - codes
- credentials - credentials

View file

@ -20,7 +20,6 @@ import (
"github.com/abcum/fibre" "github.com/abcum/fibre"
"github.com/abcum/surreal/db" "github.com/abcum/surreal/db"
"github.com/abcum/surreal/kvs" "github.com/abcum/surreal/kvs"
"github.com/abcum/surreal/log"
"github.com/abcum/surreal/mem" "github.com/abcum/surreal/mem"
"github.com/abcum/surreal/sql" "github.com/abcum/surreal/sql"
@ -62,15 +61,11 @@ func signin(c *fibre.Context) (err error) {
// Get the specified signin scope. // Get the specified signin scope.
if scp, err = mem.New(txn).GetSC(n, d, s); err != nil { if scp, err = mem.New(txn).GetSC(n, d, s); err != nil {
log.WithFields(map[string]interface{}{ return fibre.NewHTTPError(403).WithFields(map[string]interface{}{
"ns": n, "ns": n,
"db": d, "db": d,
"sc": s, "sc": s,
"ctx": c, }).WithMessage("Authentication scope does not exist")
"url": "/signin",
"id": c.Get("id"),
}).Debugln("Authentication scope does not exist")
return fibre.NewHTTPError(403)
} }
// Process the scope signin statement. // Process the scope signin statement.
@ -78,27 +73,19 @@ func signin(c *fibre.Context) (err error) {
qury := &sql.Query{Statements: []sql.Statement{scp.Signup}} qury := &sql.Query{Statements: []sql.Statement{scp.Signup}}
if res, err = db.Process(c, qury, vars); err != nil { if res, err = db.Process(c, qury, vars); err != nil {
log.WithFields(map[string]interface{}{ return fibre.NewHTTPError(501).WithFields(map[string]interface{}{
"ns": n, "ns": n,
"db": d, "db": d,
"sc": s, "sc": s,
"ctx": c, }).WithMessage("Authentication scope signin was unsuccessful")
"url": "/signin",
"id": c.Get("id"),
}).Debugln("Authentication scope signin was unsuccessful")
return fibre.NewHTTPError(501)
} }
if len(res) != 1 && len(res[0].Result) != 1 { if len(res) != 1 && len(res[0].Result) != 1 {
log.WithFields(map[string]interface{}{ return fibre.NewHTTPError(403).WithFields(map[string]interface{}{
"ns": n, "ns": n,
"db": d, "db": d,
"sc": s, "sc": s,
"ctx": c, }).WithMessage("Authentication scope signin was unsuccessful")
"url": "/signin",
"id": c.Get("id"),
}).Debugln("Authentication scope signin was unsuccessful")
return fibre.NewHTTPError(403)
} }
// Create a new token signer with the default claims. // Create a new token signer with the default claims.
@ -117,28 +104,14 @@ func signin(c *fibre.Context) (err error) {
// Try to create the final signed token as a string. // Try to create the final signed token as a string.
str, err = signr.SignedString(scp.Code) if str, err = signr.SignedString(scp.Code); err != nil {
if err != nil { return fibre.NewHTTPError(403).WithFields(map[string]interface{}{
log.WithFields(map[string]interface{}{
"ns": n, "ns": n,
"db": d, "db": d,
"sc": s, "sc": s,
"ctx": c, }).WithMessage("Problem with signing string")
"url": "/signin",
"id": c.Get("id"),
}).Debugln("Problem with signing string")
return fibre.NewHTTPError(403)
} }
log.WithFields(map[string]interface{}{
"ns": n,
"db": d,
"sc": s,
"ctx": c,
"url": "/signin",
"id": c.Get("id"),
}).Debugln("Authentication scope signin was successful")
return c.Text(200, str) return c.Text(200, str)
} }
@ -159,20 +132,16 @@ func signin(c *fibre.Context) (err error) {
p, pok := vars["pass"].(string) p, pok := vars["pass"].(string)
if !uok || len(u) == 0 || !pok || len(p) == 0 { if !uok || len(u) == 0 || !pok || len(p) == 0 {
log.WithPrefix("web").WithFields(map[string]interface{}{ return fibre.NewHTTPError(403).WithFields(map[string]interface{}{
"ns": n, "ns": n,
"nu": u, "db": d,
"ctx": c, "du": u,
"url": "/signin", }).WithMessage("Username or password is missing")
"id": c.Get("id"),
}).Debugln("Username or password is missing")
return fibre.NewHTTPError(403)
} }
// Start a new read transaction. // Start a new read transaction.
if txn, err = db.Begin(false); err != nil { if txn, err = db.Begin(false); err != nil {
log.Debugln("Transaction initialisation failure")
return fibre.NewHTTPError(500) return fibre.NewHTTPError(500)
} }
@ -183,30 +152,21 @@ func signin(c *fibre.Context) (err error) {
// Get the specified database login. // Get the specified database login.
if usr, err = mem.New(txn).GetDU(n, d, u); err != nil { if usr, err = mem.New(txn).GetDU(n, d, u); err != nil {
log.WithFields(map[string]interface{}{ return fibre.NewHTTPError(403).WithFields(map[string]interface{}{
"ns": n, "ns": n,
"db": d, "db": d,
"du": u, "du": u,
"ctx": c, }).WithMessage("Database login does not exist")
"url": "/signin",
"id": c.Get("id"),
}).Debugln("Database login does not exist")
return fibre.NewHTTPError(403)
} }
// Compare the hashed and stored passwords. // Compare the hashed and stored passwords.
err = bcrypt.CompareHashAndPassword(usr.Pass, []byte(p)) if err = bcrypt.CompareHashAndPassword(usr.Pass, []byte(p)); err != nil {
if err != nil { return fibre.NewHTTPError(403).WithFields(map[string]interface{}{
log.WithFields(map[string]interface{}{
"ns": n, "ns": n,
"db": d, "db": d,
"du": u, "du": u,
"ctx": c, }).WithMessage("Database signin was unsuccessful")
"url": "/signin",
"id": c.Get("id"),
}).Debugln("Database signin was unsuccessful")
return fibre.NewHTTPError(403)
} }
// Create a new token signer with the default claims. // Create a new token signer with the default claims.
@ -224,28 +184,14 @@ func signin(c *fibre.Context) (err error) {
// Try to create the final signed token as a string. // Try to create the final signed token as a string.
str, err = signr.SignedString(usr.Code) if str, err = signr.SignedString(usr.Code); err != nil {
if err != nil { return fibre.NewHTTPError(403).WithFields(map[string]interface{}{
log.WithFields(map[string]interface{}{
"ns": n, "ns": n,
"db": d, "db": d,
"du": u, "du": u,
"ctx": c, }).WithMessage("Problem with singing string")
"url": "/signin",
"id": c.Get("id"),
}).Debugln("Problem with signing string")
return fibre.NewHTTPError(403)
} }
log.WithFields(map[string]interface{}{
"ns": n,
"db": d,
"du": u,
"ctx": c,
"url": "/signin",
"id": c.Get("id"),
}).Debugln("Database signin was successful")
return c.Text(200, str) return c.Text(200, str)
} }
@ -266,14 +212,10 @@ func signin(c *fibre.Context) (err error) {
p, pok := vars["pass"].(string) p, pok := vars["pass"].(string)
if !uok || len(u) == 0 || !pok || len(p) == 0 { if !uok || len(u) == 0 || !pok || len(p) == 0 {
log.WithPrefix("web").WithFields(map[string]interface{}{ return fibre.NewHTTPError(403).WithFields(map[string]interface{}{
"ns": n, "ns": n,
"nu": u, "nu": u,
"ctx": c, }).WithMessage("Database signin was unsuccessful")
"url": "/signin",
"id": c.Get("id"),
}).Debugln("Username or password is missing")
return fibre.NewHTTPError(403)
} }
// Start a new read transaction. // Start a new read transaction.
@ -289,26 +231,19 @@ func signin(c *fibre.Context) (err error) {
// Get the specified namespace login. // Get the specified namespace login.
if usr, err = mem.New(txn).GetNU(n, u); err != nil { if usr, err = mem.New(txn).GetNU(n, u); err != nil {
log.WithPrefix("web").WithFields(map[string]interface{}{ return fibre.NewHTTPError(403).WithFields(map[string]interface{}{
"ns": n, "ns": n,
"nu": u, "nu": u,
"ctx": c, }).WithMessage("Namespace login does not exist")
"url": "/signin",
"id": c.Get("id"),
}).Debugln("Namespace login does not exist")
return fibre.NewHTTPError(403)
} }
// Compare the hashed and stored passwords. // Compare the hashed and stored passwords.
err = bcrypt.CompareHashAndPassword(usr.Pass, []byte(p)) if err = bcrypt.CompareHashAndPassword(usr.Pass, []byte(p)); err != nil {
if err != nil { return fibre.NewHTTPError(403).WithFields(map[string]interface{}{
log.WithPrefix("web").WithFields(map[string]interface{}{ "ns": n,
"NS": n, "nu": u,
"NU": u, }).WithMessage("Namespace signin was unsuccessful")
"URL": "/signin",
}).Debugln("Namespace signin was unsuccessful")
return fibre.NewHTTPError(403)
} }
// Create a new token signer with the default claims. // Create a new token signer with the default claims.
@ -325,25 +260,13 @@ func signin(c *fibre.Context) (err error) {
// Try to create the final signed token as a string. // Try to create the final signed token as a string.
str, err = signr.SignedString(usr.Code) if str, err = signr.SignedString(usr.Code); err != nil {
if err != nil { return fibre.NewHTTPError(403).WithFields(map[string]interface{}{
log.WithPrefix("web").WithFields(map[string]interface{}{
"ns": n, "ns": n,
"nu": u, "nu": u,
"ctx": c, }).WithMessage("Problem with singing string")
"url": "/signin",
}).Debugln("Problem with signing string")
return fibre.NewHTTPError(403)
} }
log.WithFields(map[string]interface{}{
"ns": n,
"du": u,
"ctx": c,
"url": "/signin",
"id": c.Get("id"),
}).Debugln("Namespace signin was successful")
return c.Text(200, str) return c.Text(200, str)
} }

View file

@ -18,7 +18,6 @@ import (
"github.com/abcum/fibre" "github.com/abcum/fibre"
"github.com/abcum/surreal/db" "github.com/abcum/surreal/db"
"github.com/abcum/surreal/kvs" "github.com/abcum/surreal/kvs"
"github.com/abcum/surreal/log"
"github.com/abcum/surreal/mem" "github.com/abcum/surreal/mem"
"github.com/abcum/surreal/sql" "github.com/abcum/surreal/sql"
) )
@ -56,14 +55,11 @@ func signup(c *fibre.Context) (err error) {
// Get the specified signin scope. // Get the specified signin scope.
if scp, err = mem.New(txn).GetSC(n, d, s); err != nil { if scp, err = mem.New(txn).GetSC(n, d, s); err != nil {
log.WithFields(map[string]interface{}{ return fibre.NewHTTPError(403).WithFields(map[string]interface{}{
"ns": n, "ns": n,
"db": d, "db": d,
"sc": s, "sc": s,
"ctx": c, }).WithMessage("Authentication scope does not exist")
"url": "/signup",
}).Debugln("Authentication scope does not exist")
return fibre.NewHTTPError(403)
} }
// Process the scope signup statement. // Process the scope signup statement.
@ -71,25 +67,19 @@ func signup(c *fibre.Context) (err error) {
qury := &sql.Query{Statements: []sql.Statement{scp.Signup}} qury := &sql.Query{Statements: []sql.Statement{scp.Signup}}
if res, err = db.Process(c, qury, vars); err != nil { if res, err = db.Process(c, qury, vars); err != nil {
log.WithFields(map[string]interface{}{ return fibre.NewHTTPError(501).WithFields(map[string]interface{}{
"ns": n, "ns": n,
"db": d, "db": d,
"sc": s, "sc": s,
"ctx": c, }).WithMessage("Authentication scope signup was unsuccessful")
"url": "/signup",
}).Debugln("Authentication scope signup was unsuccessful")
return fibre.NewHTTPError(501)
} }
if len(res) != 1 && len(res[0].Result) != 1 { if len(res) != 1 && len(res[0].Result) != 1 {
log.WithFields(map[string]interface{}{ return fibre.NewHTTPError(403).WithFields(map[string]interface{}{
"ns": n, "ns": n,
"db": d, "db": d,
"sc": s, "sc": s,
"ctx": c, }).WithMessage("Authentication scope signup was unsuccessful")
"url": "/signup",
}).Debugln("Authentication scope signup was unsuccessful")
return fibre.NewHTTPError(403)
} }
return c.Code(200) return c.Code(200)