OSDN Git Service

Add accesstoken authz error return (#266)
authoryahtoo <yahtoo.ma@gmail.com>
Tue, 9 Jan 2018 05:37:19 +0000 (13:37 +0800)
committerPaladz <yzhu101@uottawa.ca>
Tue, 9 Jan 2018 05:37:19 +0000 (13:37 +0800)
blockchain/errors.go
blockchain/rpc_reactor.go
node/node.go

index 4f2800f..4dc727f 100644 (file)
@@ -45,9 +45,9 @@ var errorFormatter = httperror.Formatter{
        IsTemporary: isTemporary,
        Errors: map[error]httperror.Info{
                // General error namespace (0xx)
-               context.DeadlineExceeded: {408, "BTM001", "Request timed out"},
-               httpjson.ErrBadRequest:   {400, "BTM003", "Invalid request body"},
-               txbuilder.ErrMissingFields: {400, "BTM010", "One or more fields are missing"},
+               context.DeadlineExceeded:     {408, "BTM001", "Request timed out"},
+               httpjson.ErrBadRequest:       {400, "BTM003", "Invalid request body"},
+               txbuilder.ErrMissingFields:   {400, "BTM010", "One or more fields are missing"},
                rpc.ErrWrongNetwork:          {502, "BTM104", "A peer core is operating on a different blockchain network"},
                protocol.ErrTheDistantFuture: {400, "BTM105", "Requested height is too far ahead"},
 
@@ -84,5 +84,7 @@ var errorFormatter = httperror.Formatter{
                account.ErrInsufficient: {400, "BTM760", "Insufficient funds for tx"},
                account.ErrReserved:     {400, "BTM761", "Some outputs are reserved; try again"},
 
+               //accesstoken authz err namespace (86x)
+               errNotAuthenticated: {401, "BTM860", "Request could not be authenticated"},
        },
 }
index ee48bb6..f3944b4 100644 (file)
@@ -6,12 +6,18 @@ import (
 
        log "github.com/sirupsen/logrus"
 
+       "github.com/bytom/blockchain/accesstoken"
        "github.com/bytom/dashboard"
        "github.com/bytom/errors"
+       "github.com/bytom/net/http/authn"
        "github.com/bytom/net/http/httpjson"
        "github.com/bytom/net/http/static"
 )
 
+var (
+       errNotAuthenticated = errors.New("not authenticated")
+)
+
 // json handler
 func jsonHandler(f interface{}) http.Handler {
        h, err := httpjson.Handler(f, errorFormatter.Write)
@@ -128,3 +134,20 @@ func (bcr *BlockchainReactor) BuildHandler() {
 
        bcr.handler = handler
 }
+
+func AuthHandler(handler http.Handler, accessTokens *accesstoken.CredentialStore) http.Handler {
+
+       authenticator := authn.NewAPI(accessTokens)
+
+       return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
+               // TODO(tessr): check that this path exists; return early if this path isn't legit
+               req, err := authenticator.Authenticate(req)
+               if err != nil {
+                       log.WithField("error", errors.Wrap(err, "Serve")).Error("Authenticate fail")
+                       err = errors.Sub(errNotAuthenticated, err)
+                       errorFormatter.Write(req.Context(), rw, err)
+                       return
+               }
+               handler.ServeHTTP(rw, req)
+       })
+}
index aaf4b95..1a07ee7 100755 (executable)
@@ -28,7 +28,6 @@ import (
        cfg "github.com/bytom/config"
        "github.com/bytom/env"
        "github.com/bytom/errors"
-       "github.com/bytom/net/http/authn"
        "github.com/bytom/p2p"
        "github.com/bytom/protocol"
        "github.com/bytom/types"
@@ -89,22 +88,6 @@ func (wh *waitHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
        wh.h.ServeHTTP(w, req)
 }
 
-func AuthHandler(handler http.Handler, accessTokens *accesstoken.CredentialStore) http.Handler {
-
-       authenticator := authn.NewAPI(accessTokens)
-
-       return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
-               // TODO(tessr): check that this path exists; return early if this path isn't legit
-               req, err := authenticator.Authenticate(req)
-               if err != nil {
-                       log.WithField("error", errors.Wrap(err, "Serve")).Error("Authenticate fail")
-
-                       return
-               }
-               handler.ServeHTTP(rw, req)
-       })
-}
-
 func rpcInit(h *bc.BlockchainReactor, config *cfg.Config, accessTokens *accesstoken.CredentialStore) {
        // The waitHandler accepts incoming requests, but blocks until its underlying
        // handler is set, when the second phase is complete.
@@ -116,7 +99,7 @@ func rpcInit(h *bc.BlockchainReactor, config *cfg.Config, accessTokens *accessto
        var handler http.Handler = mux
 
        if config.Auth.Disable == false {
-               handler = AuthHandler(handler, accessTokens)
+               handler = bc.AuthHandler(handler, accessTokens)
        }
        handler = RedirectHandler(handler)