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

View file

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

View file

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