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"},
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"},
},
}
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)
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)
+ })
+}
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"
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.
var handler http.Handler = mux
if config.Auth.Disable == false {
- handler = AuthHandler(handler, accessTokens)
+ handler = bc.AuthHandler(handler, accessTokens)
}
handler = RedirectHandler(handler)