From 4082e50b4090371d36c1ba400aee0a881442664b Mon Sep 17 00:00:00 2001 From: Tino Rusch Date: Thu, 25 Oct 2018 12:19:09 +0200 Subject: [PATCH 1/5] improve error logging in http middleware. --- middleware.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/middleware.go b/middleware.go index e8eedd5..bfa6715 100644 --- a/middleware.go +++ b/middleware.go @@ -2,7 +2,10 @@ package jwt import ( "context" + "fmt" "net/http" + + "github.com/sirupsen/logrus" ) type claimContextKeyType string @@ -27,19 +30,30 @@ func ClaimsToContextMiddleware(handler http.Handler, header string, idpKey inter // RequireClaim checks if the requests claims contain a specific value for a specific key func RequireClaim(handler http.Handler, claimKey, expectedClaimValue string) http.Handler { + log := logrus.WithField("require-claim", claimKey) + log = log.WithField("expected-value", expectedClaimValue) return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { claims := ClaimsFromContext(r.Context()) if claims == nil { - http.Error(w, "not authorized: failed to get token from context", http.StatusUnauthorized) + msg := "not authorized: failed to get token from context" + log.Debug(msg) + http.Error(w, msg, http.StatusUnauthorized) return } + log = log.WithFields("claims", claims) claimVal, ok := claims[claimKey].(string) if !ok { - http.Error(w, "not authorized: claim value has wrong type", http.StatusUnauthorized) + msg := "not authorized: claim value has wrong type" + log = log.WithField("actual-type", fmt.Sprintf("%T", claims[claimKey])) + log.Debug(msg) + http.Error(w, msg, http.StatusUnauthorized) return } if claimVal != expectedClaimValue { - http.Error(w, "not authorized: calim value has unexpected content", http.StatusUnauthorized) + msg := "not authorized: claim value has unexpected content" + log = log.WithField("actual-content", claims[claimKey]) + log.Debug(msg) + http.Error(w, msg, http.StatusUnauthorized) return } handler.ServeHTTP(w, r) From 56a8ec1e3f74f6cbc1e94ffa882eeca11ea90af8 Mon Sep 17 00:00:00 2001 From: Tino Rusch Date: Thu, 25 Oct 2018 12:28:57 +0200 Subject: [PATCH 2/5] fixed silly bug. --- middleware.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/middleware.go b/middleware.go index bfa6715..56a2ea8 100644 --- a/middleware.go +++ b/middleware.go @@ -40,7 +40,7 @@ func RequireClaim(handler http.Handler, claimKey, expectedClaimValue string) htt http.Error(w, msg, http.StatusUnauthorized) return } - log = log.WithFields("claims", claims) + log = log.WithField("claims", claims) claimVal, ok := claims[claimKey].(string) if !ok { msg := "not authorized: claim value has wrong type" From f449e04b21fe24bc2ea85422d197d42c29d6e797 Mon Sep 17 00:00:00 2001 From: Tino Rusch Date: Fri, 23 Aug 2019 09:42:42 +0200 Subject: [PATCH 3/5] improve logger setup in http middleware. --- middleware.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/middleware.go b/middleware.go index 56a2ea8..f221685 100644 --- a/middleware.go +++ b/middleware.go @@ -30,8 +30,9 @@ func ClaimsToContextMiddleware(handler http.Handler, header string, idpKey inter // RequireClaim checks if the requests claims contain a specific value for a specific key func RequireClaim(handler http.Handler, claimKey, expectedClaimValue string) http.Handler { - log := logrus.WithField("require-claim", claimKey) - log = log.WithField("expected-value", expectedClaimValue) + log := logrus. + WithField("require-claim", claimKey). + WithField("expected-value", expectedClaimValue) return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { claims := ClaimsFromContext(r.Context()) if claims == nil { From 492de1e0e1295230f7c1cf4aad8a4ecd20f30096 Mon Sep 17 00:00:00 2001 From: Tino Rusch Date: Fri, 23 Aug 2019 09:49:57 +0200 Subject: [PATCH 4/5] upgrade required go version and dependency management. --- .travis.yml | 12 ++--- Gopkg.lock | 123 ---------------------------------------------------- Gopkg.toml | 11 ----- go.mod | 13 ++++++ go.sum | 20 +++++++++ 5 files changed, 40 insertions(+), 139 deletions(-) delete mode 100644 Gopkg.lock delete mode 100644 Gopkg.toml create mode 100644 go.mod create mode 100644 go.sum diff --git a/.travis.yml b/.travis.yml index 8e5d93b..8d8f6c2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,19 +1,21 @@ language: go go: - - 1.8.x - - 1.9.x + - 1.12 + - 1.11 - master +env: + - GO111MODULE=on + before_install: - - "go get -v github.com/golang/dep/cmd/dep \ - github.com/onsi/ginkgo/ginkgo \ + - "go get -v github.com/onsi/ginkgo/ginkgo \ golang.org/x/tools/cmd/cover \ github.com/mattn/goveralls" install: - - dep ensure --vendor-only -v + - go install -v . script: - go test -v -covermode=count -coverprofile=coverage.out diff --git a/Gopkg.lock b/Gopkg.lock deleted file mode 100644 index 40c351c..0000000 --- a/Gopkg.lock +++ /dev/null @@ -1,123 +0,0 @@ -# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. - - -[[projects]] - digest = "1:3e93e899f8457138a891b3ccedcacf8fe9074865c848f7028e1892bdae280676" - name = "github.com/dgrijalva/jwt-go" - packages = ["."] - pruneopts = "" - revision = "dbeaa9332f19a944acb5736b4456cfcc02140e29" - version = "v3.1.0" - -[[projects]] - digest = "1:32b27072cd55bd2fb7244de0425943d125da6a552ae2b6517cdd965a662baf18" - name = "github.com/onsi/ginkgo" - packages = [ - ".", - "config", - "internal/codelocation", - "internal/containernode", - "internal/failer", - "internal/leafnodes", - "internal/remote", - "internal/spec", - "internal/spec_iterator", - "internal/specrunner", - "internal/suite", - "internal/testingtproxy", - "internal/writer", - "reporters", - "reporters/stenographer", - "reporters/stenographer/support/go-colorable", - "reporters/stenographer/support/go-isatty", - "types", - ] - pruneopts = "" - revision = "9eda700730cba42af70d53180f9dcce9266bc2bc" - version = "v1.4.0" - -[[projects]] - digest = "1:44febbaa3af725dccbe3d83b21ef155f7136670a419bb24a91383bb27d6dcbd4" - name = "github.com/onsi/gomega" - packages = [ - ".", - "format", - "internal/assertion", - "internal/asyncassertion", - "internal/oraclematcher", - "internal/testingtsupport", - "matchers", - "matchers/support/goraph/bipartitegraph", - "matchers/support/goraph/edge", - "matchers/support/goraph/node", - "matchers/support/goraph/util", - "types", - ] - pruneopts = "" - revision = "003f63b7f4cff3fc95357005358af2de0f5fe152" - version = "v1.3.0" - -[[projects]] - branch = "master" - digest = "1:f3ee2a699dd02cac37fbca1d028f1121ce0e48143a0f9abd293d3141dcfe6b92" - name = "golang.org/x/net" - packages = [ - "html", - "html/atom", - "html/charset", - ] - pruneopts = "" - revision = "5ccada7d0a7ba9aeb5d3aca8d3501b4c2a509fec" - -[[projects]] - branch = "master" - digest = "1:aba25123b0b02601b134eaf08bd84afe8e12cab70e43770e7b38d8eef8f1d93e" - name = "golang.org/x/sys" - packages = ["unix"] - pruneopts = "" - revision = "2c42eef0765b9837fbdab12011af7830f55f88f0" - -[[projects]] - branch = "master" - digest = "1:9f9562b058a594dae0c8e2388c1a802664111736845bc08885246f0e4bf99bdd" - name = "golang.org/x/text" - packages = [ - "encoding", - "encoding/charmap", - "encoding/htmlindex", - "encoding/internal", - "encoding/internal/identifier", - "encoding/japanese", - "encoding/korean", - "encoding/simplifiedchinese", - "encoding/traditionalchinese", - "encoding/unicode", - "internal/gen", - "internal/tag", - "internal/utf8internal", - "language", - "runes", - "transform", - "unicode/cldr", - ] - pruneopts = "" - revision = "e19ae1496984b1c655b8044a65c0300a3c878dd3" - -[[projects]] - branch = "v2" - digest = "1:4b4e5848dfe7f316f95f754df071bebfb40cf4482da62e17e7e1aebdf11f4918" - name = "gopkg.in/yaml.v2" - packages = ["."] - pruneopts = "" - revision = "d670f9405373e636a5a2765eea47fac0c9bc91a4" - -[solve-meta] - analyzer-name = "dep" - analyzer-version = 1 - input-imports = [ - "github.com/dgrijalva/jwt-go", - "github.com/onsi/ginkgo", - "github.com/onsi/gomega", - ] - solver-name = "gps-cdcl" - solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml deleted file mode 100644 index 27df3ee..0000000 --- a/Gopkg.toml +++ /dev/null @@ -1,11 +0,0 @@ -[[constraint]] - name = "github.com/dgrijalva/jwt-go" - version = "3.1.0" - -[[constraint]] - name = "github.com/onsi/ginkgo" - version = "1.4.0" - -[[constraint]] - name = "github.com/onsi/gomega" - version = "1.3.0" diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ce2207e --- /dev/null +++ b/go.mod @@ -0,0 +1,13 @@ +module github.com/contiamo/jwt + +go 1.12 + +require ( + github.com/dgrijalva/jwt-go v3.1.0+incompatible + github.com/golang/protobuf v1.3.2 // indirect + github.com/onsi/ginkgo v1.4.0 + github.com/onsi/gomega v1.3.0 + golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7 // indirect + golang.org/x/sys v0.0.0-20190422165155-953cdadca894 // indirect + gopkg.in/yaml.v2 v2.2.2 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..ed9a9dd --- /dev/null +++ b/go.sum @@ -0,0 +1,20 @@ +github.com/dgrijalva/jwt-go v3.1.0+incompatible h1:FFziAwDQQ2dz1XClWMkwvukur3evtZx7x/wMHKM1i20= +github.com/dgrijalva/jwt-go v3.1.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/onsi/ginkgo v1.4.0 h1:n60/4GZK0Sr9O2iuGKq876Aoa0ER2ydgpMOBwzJ8e2c= +github.com/onsi/ginkgo v1.4.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/gomega v1.3.0 h1:yPHEatyQC4jN3vdfvqJXG7O9vfC6LhaAV1NEdYpP+h0= +github.com/onsi/gomega v1.3.0/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7 h1:fHDIZ2oxGnUZRN6WgWFCbYBjH9uqVPRCUVUDhs0wnbA= +golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= From f61d188e4b187c14f857dc236d6983a8070ae8b3 Mon Sep 17 00:00:00 2001 From: Tino Rusch Date: Fri, 23 Aug 2019 09:55:09 +0200 Subject: [PATCH 5/5] remove ginko from travis install. --- .travis.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8d8f6c2..612c493 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,9 +10,7 @@ env: - GO111MODULE=on before_install: - - "go get -v github.com/onsi/ginkgo/ginkgo \ - golang.org/x/tools/cmd/cover \ - github.com/mattn/goveralls" + - "go get -v golang.org/x/tools/cmd/cover github.com/mattn/goveralls" install: - go install -v .