From b09d309f8135f7796c499a3d1356b7c956b9472c Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Thu, 4 Oct 2018 19:05:43 +0200 Subject: [PATCH] Fix broken build after dep update --- internal/auth/identity_context.go | 2 +- internal/auth/jwt.go | 21 +++++++++++---------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/internal/auth/identity_context.go b/internal/auth/identity_context.go index d67192a79..0e7b416f8 100644 --- a/internal/auth/identity_context.go +++ b/internal/auth/identity_context.go @@ -24,7 +24,7 @@ func getIdentityClaimFromContext(ctx context.Context) (uint64, error) { return 0, errors.Wrap(err, "failed to authorize request") } else if !jwt.Valid { return 0, errors.New("JWT not valid") - } else if idInterface, has := claims.Get("sub"); !has { + } else if idInterface, has := claims["sub"]; !has { return 0, errors.New("Malformed JWT claims") } else if idString, ok := idInterface.(string); !ok { return 0, errors.New("Malformed JWT claims (sub not a string)") diff --git a/internal/auth/jwt.go b/internal/auth/jwt.go index 96220802f..c752b7d67 100644 --- a/internal/auth/jwt.go +++ b/internal/auth/jwt.go @@ -6,22 +6,23 @@ import ( "strconv" "time" + "github.com/dgrijalva/jwt-go" "github.com/go-chi/jwtauth" "github.com/titpetric/factory/resputil" ) -type jwt struct { +type token struct { expiry int64 cookieDomain string tokenAuth *jwtauth.JWTAuth } -func JWT() (*jwt, error) { +func JWT() (*token, error) { if err := flags.Validate(); err != nil { return nil, err } - jwt := &jwt{ + jwt := &token{ expiry: flags.jwt.Expiry, cookieDomain: flags.jwt.CookieDomain, tokenAuth: jwtauth.New("HS256", []byte(flags.jwt.Secret), nil), @@ -35,21 +36,21 @@ func JWT() (*jwt, error) { } // Verifies JWT and stores it into context -func (t *jwt) Verifier() func(http.Handler) http.Handler { +func (t *token) Verifier() func(http.Handler) http.Handler { return jwtauth.Verifier(t.tokenAuth) } -func (t *jwt) Encode(identity Identifiable) string { - claims := jwtauth.Claims{} - claims.Set("sub", strconv.FormatUint(identity.Identity(), 10)) - claims.SetExpiryIn(time.Duration(t.expiry) * time.Minute) +func (t *token) Encode(identity Identifiable) string { + claims := jwt.StandardClaims{} + claims.Subject = strconv.FormatUint(identity.Identity(), 10) + claims.ExpiresAt = time.Now().Add(time.Duration(t.expiry) * time.Minute).Unix() _, jwt, _ := t.tokenAuth.Encode(claims) return jwt } // Extracts and authenticates JWT from context, validates claims -func (t *jwt) Authenticator() func(http.Handler) http.Handler { +func (t *token) Authenticator() func(http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var ctx = r.Context() @@ -69,7 +70,7 @@ func (t *jwt) Authenticator() func(http.Handler) http.Handler { } // Extracts and authenticates JWT from context, validates claims -func (t *jwt) SetCookie(w http.ResponseWriter, r *http.Request, identity Identifiable) { +func (t *token) SetCookie(w http.ResponseWriter, r *http.Request, identity Identifiable) { cookie := &http.Cookie{ Name: "jwt", Expires: time.Now().Add(time.Duration(t.expiry) * time.Minute),