From deb76405de56956bb14804286db503462d30bc12 Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Tue, 19 Sep 2017 04:39:36 +0000 Subject: Finish out most of the proxy functionality --- jws_validator.go | 9 +++++++-- main.go | 14 ++++++-------- util.go | 5 +++++ 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/jws_validator.go b/jws_validator.go index 0b2467f..9abaaae 100644 --- a/jws_validator.go +++ b/jws_validator.go @@ -1,11 +1,12 @@ package main import ( + "net/url" + "time" + "github.com/pkg/errors" "gopkg.in/square/go-jose.v2" "gopkg.in/square/go-jose.v2/jwt" - "net/url" - "time" ) // TODO @@ -26,6 +27,10 @@ type Claims struct { jwt.Claims } +func (c *Claims) Age() int64 { + return int64(time.Since(c.IssuedAt.Time()).Minutes()) +} + type JWSValidationContext struct { KeyFetcher JWKSFetcher Issuer string diff --git a/main.go b/main.go index 805c40d..0d13919 100644 --- a/main.go +++ b/main.go @@ -5,16 +5,16 @@ import ( "crypto/rand" "encoding/hex" "flag" - "github.com/golang/glog" - "github.com/gorilla/handlers" - "github.com/pkg/errors" "net/http" "net/http/httputil" "net/url" "os" - "strconv" "strings" "time" + + "github.com/golang/glog" + "github.com/gorilla/handlers" + "github.com/pkg/errors" ) const ( @@ -238,10 +238,8 @@ func AuthProxyController(w http.ResponseWriter, r *http.Request) { } r.Header["X-Forwarded-User"] = []string{claims.Subject} - r.Header["X-Forwarded-Token-Expires"] = []string{strconv.FormatInt(int64(claims.Expiry), 10)} - - age := time.Since(claims.IssuedAt.Time()).Minutes() - r.Header["X-Forwarded-Token-Age"] = []string{strconv.FormatInt(int64(age), 10)} + r.Header["X-Forwarded-Token-Age"] = StringListFromInt(claims.Age()) + r.Header["X-Forwarded-Token-Expires"] = StringListFromInt(int64(claims.Expiry)) ctx.reverseProxy.ServeHTTP(w, r) } diff --git a/util.go b/util.go index 7385dfd..dde34c0 100644 --- a/util.go +++ b/util.go @@ -4,6 +4,7 @@ import ( "crypto/sha256" "encoding/hex" "net/url" + "strconv" "strings" ) @@ -59,3 +60,7 @@ func Sha256Hex(v string) string { s256.Write([]byte(v)) return hex.EncodeToString(s256.Sum(nil)) } + +func StringListFromInt(i int64) []string { + return []string{strconv.FormatInt(i, 10)} +} -- cgit v1.2.3