From 07ea6e5b4eb95addae57db2b8153cb5ced8e3e8b Mon Sep 17 00:00:00 2001 From: Umputun Date: Mon, 21 May 2018 19:16:16 -0500 Subject: [PATCH 01/20] switch session store to jwt, no refresh yet #30 --- Gopkg.lock | 8 +- Gopkg.toml | 4 - app/main.go | 44 ++--- app/rest/api/rest.go | 2 - app/rest/api/rest_test.go | 10 +- app/rest/auth/auth.go | 46 ++---- app/rest/auth/auth_test.go | 75 ++++++++- app/rest/auth/jwt.go | 124 ++++++++++++++ app/rest/auth/jwt_test.go | 155 ++++++++++++++++++ app/rest/auth/provider.go | 105 +++++------- app/rest/auth/provider_test.go | 74 ++++----- app/rest/auth/providers.go | 4 - vendor/github.com/dgrijalva/jwt-go/.gitignore | 4 + .../github.com/dgrijalva/jwt-go/.travis.yml | 13 ++ vendor/github.com/dgrijalva/jwt-go/LICENSE | 8 + .../dgrijalva/jwt-go/MIGRATION_GUIDE.md | 97 +++++++++++ vendor/github.com/dgrijalva/jwt-go/README.md | 100 +++++++++++ .../dgrijalva/jwt-go/VERSION_HISTORY.md | 118 +++++++++++++ vendor/github.com/dgrijalva/jwt-go/claims.go | 134 +++++++++++++++ vendor/github.com/dgrijalva/jwt-go/doc.go | 4 + vendor/github.com/dgrijalva/jwt-go/ecdsa.go | 148 +++++++++++++++++ .../dgrijalva/jwt-go/ecdsa_utils.go | 67 ++++++++ vendor/github.com/dgrijalva/jwt-go/errors.go | 59 +++++++ vendor/github.com/dgrijalva/jwt-go/hmac.go | 95 +++++++++++ .../github.com/dgrijalva/jwt-go/map_claims.go | 94 +++++++++++ vendor/github.com/dgrijalva/jwt-go/none.go | 52 ++++++ vendor/github.com/dgrijalva/jwt-go/parser.go | 148 +++++++++++++++++ vendor/github.com/dgrijalva/jwt-go/rsa.go | 101 ++++++++++++ vendor/github.com/dgrijalva/jwt-go/rsa_pss.go | 126 ++++++++++++++ .../github.com/dgrijalva/jwt-go/rsa_utils.go | 101 ++++++++++++ .../dgrijalva/jwt-go/signing_method.go | 35 ++++ vendor/github.com/dgrijalva/jwt-go/token.go | 108 ++++++++++++ 32 files changed, 2068 insertions(+), 195 deletions(-) create mode 100644 app/rest/auth/jwt.go create mode 100644 app/rest/auth/jwt_test.go create mode 100644 vendor/github.com/dgrijalva/jwt-go/.gitignore create mode 100644 vendor/github.com/dgrijalva/jwt-go/.travis.yml create mode 100644 vendor/github.com/dgrijalva/jwt-go/LICENSE create mode 100644 vendor/github.com/dgrijalva/jwt-go/MIGRATION_GUIDE.md create mode 100644 vendor/github.com/dgrijalva/jwt-go/README.md create mode 100644 vendor/github.com/dgrijalva/jwt-go/VERSION_HISTORY.md create mode 100644 vendor/github.com/dgrijalva/jwt-go/claims.go create mode 100644 vendor/github.com/dgrijalva/jwt-go/doc.go create mode 100644 vendor/github.com/dgrijalva/jwt-go/ecdsa.go create mode 100644 vendor/github.com/dgrijalva/jwt-go/ecdsa_utils.go create mode 100644 vendor/github.com/dgrijalva/jwt-go/errors.go create mode 100644 vendor/github.com/dgrijalva/jwt-go/hmac.go create mode 100644 vendor/github.com/dgrijalva/jwt-go/map_claims.go create mode 100644 vendor/github.com/dgrijalva/jwt-go/none.go create mode 100644 vendor/github.com/dgrijalva/jwt-go/parser.go create mode 100644 vendor/github.com/dgrijalva/jwt-go/rsa.go create mode 100644 vendor/github.com/dgrijalva/jwt-go/rsa_pss.go create mode 100644 vendor/github.com/dgrijalva/jwt-go/rsa_utils.go create mode 100644 vendor/github.com/dgrijalva/jwt-go/signing_method.go create mode 100644 vendor/github.com/dgrijalva/jwt-go/token.go diff --git a/Gopkg.lock b/Gopkg.lock index e2aeb0fb..8fd5e8d0 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -31,6 +31,12 @@ revision = "346938d642f2ec3594ed81d874461961cd0faa76" version = "v1.1.0" +[[projects]] + name = "github.com/dgrijalva/jwt-go" + packages = ["."] + revision = "06ea1031745cb8b3dab3f6a236daf2b0aa468b7e" + version = "v3.2.0" + [[projects]] name = "github.com/didip/tollbooth" packages = [ @@ -213,6 +219,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "01a2d611ea5b99d8ca13e5b12fdeddc9db5a791b054172e1f49e612579151623" + inputs-digest = "879764a044d263d9d6e943970fe58f3b3fdabbe1344dfcaf9b5bbab95040a175" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index 2ce75caa..41038451 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -10,10 +10,6 @@ name = "github.com/google/uuid" version = "0.2.0" -[[constraint]] - branch = "master" - name = "github.com/gorilla/context" - [[constraint]] branch = "master" name = "github.com/hashicorp/logutils" diff --git a/app/main.go b/app/main.go index 9554c7f5..784426df 100644 --- a/app/main.go +++ b/app/main.go @@ -9,7 +9,6 @@ import ( "time" "github.com/coreos/bbolt" - "github.com/gorilla/sessions" "github.com/hashicorp/logutils" "github.com/jessevdk/go-flags" "github.com/pkg/errors" @@ -34,17 +33,15 @@ var opts struct { BackupLocation string `long:"backup" env:"BACKUP_PATH" default:"./var/backup" description:"backups location"` MaxBackupFiles int `long:"max-back" env:"MAX_BACKUP_FILES" default:"10" description:"max backups to keep"` - - SessionStore string `long:"session" env:"SESSION_STORE" default:"./var/session" description:"session store location"` AvatarStore string `long:"avatars" env:"AVATAR_STORE" default:"./var/avatars" description:"avatars location"` - MaxCommentSize int `long:"max-comment" env:"MAX_COMMENT_SIZE" default:"2048" description:"max comment size"` - SecretKey string `long:"secret" env:"SECRET" required:"true" description:"secret key"` ImageProxy bool `long:"img-proxy" env:"IMG_PROXY" description:"enable image proxy"` + + MaxCommentSize int `long:"max-comment" env:"MAX_COMMENT_SIZE" default:"2048" description:"max comment size"` MaxCachedItems int `long:"max-cache-items" env:"MAX_CACHE_ITEMS" default:"1000" description:"max cached items"` MaxCachedValue int `long:"max-cache-value" env:"MAX_CACHE_VALUE" default:"65536" description:"max size of cached value"` - - LowScore int `long:"low-score" env:"LOW_SCORE" default:"-5" description:"low score threshold"` - CriticalScore int `long:"critical-score" env:"CRITICAL_SCORE" default:"-10" description:"critical score threshold"` + SecretKey string `long:"secret" env:"SECRET" required:"true" description:"secret key"` + LowScore int `long:"low-score" env:"LOW_SCORE" default:"-5" description:"low score threshold"` + CriticalScore int `long:"critical-score" env:"CRITICAL_SCORE" default:"-10" description:"critical score threshold"` GoogleCID string `long:"google-cid" env:"REMARK_GOOGLE_CID" description:"Google OAuth client ID"` GoogleCSEC string `long:"google-csec" env:"REMARK_GOOGLE_CSEC" description:"Google OAuth client secret"` @@ -71,7 +68,7 @@ func main() { setupLog(opts.Dbg) log.Print("[INFO] started remark") - if err := makeDirs(opts.BoltPath, opts.SessionStore, opts.BackupLocation, opts.AvatarStore); err != nil { + if err := makeDirs(opts.BoltPath, opts.BackupLocation, opts.AvatarStore); err != nil { log.Fatalf("[ERROR] can't create directories, %+v", err) } @@ -88,15 +85,6 @@ func main() { MaxCommentSize: opts.MaxCommentSize, } - sessionStore := func() sessions.Store { - sess := sessions.NewFilesystemStore(opts.SessionStore, []byte(opts.SecretKey)) - sess.Options.HttpOnly = true - sess.Options.Secure = true - sess.Options.MaxAge = 3600 * 24 * 365 - sess.Options.Path = "/" - return sess - }() - exporter := migrator.Remark{DataStore: &dataService} cache := rest.NewLoadingCache(rest.MaxValueSize(opts.MaxCachedValue), rest.MaxKeys(opts.MaxCachedItems), rest.PostFlushFn(postFlushFn)) @@ -125,11 +113,10 @@ func main() { WebRoot: opts.WebRoot, ImageProxy: proxy.Image{Enabled: opts.ImageProxy, RoutePath: "/api/v1/img", RemarkURL: opts.RemarkURL}, Authenticator: auth.Authenticator{ - Admins: opts.Admins, - SessionStore: sessionStore, - Providers: makeAuthProviders(sessionStore, avatarProxy), - AvatarProxy: avatarProxy, - DevPasswd: opts.DevPasswd, + Admins: opts.Admins, + Providers: makeAuthProviders(avatarProxy), + AvatarProxy: avatarProxy, + DevPasswd: opts.DevPasswd, }, Cache: cache, } @@ -193,15 +180,14 @@ func makeDirs(dirs ...string) error { return nil } -func makeAuthProviders(sessionStore sessions.Store, avatarProxy *proxy.Avatar) (providers []auth.Provider) { +func makeAuthProviders(avatarProxy *proxy.Avatar) (providers []auth.Provider) { makeParams := func(cid, secret string) auth.Params { return auth.Params{ - AvatarProxy: avatarProxy, - SessionStore: sessionStore, - RemarkURL: opts.RemarkURL, - Cid: cid, - Csecret: secret, + AvatarProxy: avatarProxy, + RemarkURL: opts.RemarkURL, + Cid: cid, + Csecret: secret, } } diff --git a/app/rest/api/rest.go b/app/rest/api/rest.go index a7457185..717fec68 100644 --- a/app/rest/api/rest.go +++ b/app/rest/api/rest.go @@ -17,7 +17,6 @@ import ( "github.com/go-chi/chi" "github.com/go-chi/chi/middleware" "github.com/go-chi/render" - "github.com/gorilla/context" "github.com/pkg/errors" "gopkg.in/russross/blackfriday.v2" @@ -80,7 +79,6 @@ func (s *Rest) routes() chi.Router { router.Use(middleware.RealIP, Recoverer) router.Use(middleware.Throttle(1000), middleware.Timeout(60*time.Second)) router.Use(AppInfo("remark42", s.Version), Ping) - router.Use(context.ClearHandler) // if you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler s.adminService = admin{ dataService: s.DataService, diff --git a/app/rest/api/rest_test.go b/app/rest/api/rest_test.go index 53d19c00..9a6ee5c3 100644 --- a/app/rest/api/rest_test.go +++ b/app/rest/api/rest_test.go @@ -14,7 +14,6 @@ import ( "time" "github.com/coreos/bbolt" - "github.com/gorilla/sessions" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -469,11 +468,10 @@ func prep(t *testing.T) (srv *Rest, ts *httptest.Server) { srv = &Rest{ DataService: dataStore, Authenticator: auth.Authenticator{ - SessionStore: sessions.NewFilesystemStore("/tmp", []byte("blah")), - DevPasswd: "password", - Providers: nil, - AvatarProxy: &proxy.Avatar{StorePath: "/tmp", RoutePath: "/api/v1/avatar"}, - Admins: []string{"a1", "a2"}, + DevPasswd: "password", + Providers: nil, + AvatarProxy: &proxy.Avatar{StorePath: "/tmp", RoutePath: "/api/v1/avatar"}, + Admins: []string{"a1", "a2"}, }, Exporter: &migrator.Remark{DataStore: &dataStore}, Cache: &mockCache{}, diff --git a/app/rest/auth/auth.go b/app/rest/auth/auth.go index fc7dc8ac..e86ec5b9 100644 --- a/app/rest/auth/auth.go +++ b/app/rest/auth/auth.go @@ -7,9 +7,6 @@ import ( "net/http" "strings" - "github.com/gorilla/sessions" - "github.com/pkg/errors" - "github.com/umputun/remark/app/rest" "github.com/umputun/remark/app/rest/proxy" "github.com/umputun/remark/app/store" @@ -17,11 +14,11 @@ import ( // Authenticator is top level auth object providing middlewares type Authenticator struct { - SessionStore sessions.Store - AvatarProxy *proxy.Avatar - Admins []string - Providers []Provider - DevPasswd string + AvatarProxy *proxy.Avatar + Admins []string + Providers []Provider + DevPasswd string + JWTService JWT } var devUser = store.User{ @@ -44,8 +41,9 @@ func (a *Authenticator) Auth(reqAuth bool) func(http.Handler) http.Handler { return } - session, err := a.SessionStore.Get(r, "remark") + claims, err := a.JWTService.Get(r) if err != nil && reqAuth { // in full auth lack of session causes Unauthorized + log.Printf("[WARN] failed auth, %s", err) http.Error(w, "Unauthorized", http.StatusUnauthorized) return } @@ -55,24 +53,13 @@ func (a *Authenticator) Auth(reqAuth bool) func(http.Handler) http.Handler { return } - uinfoData, ok := session.Values["uinfo"] - if !ok && reqAuth { + if claims.User == nil && reqAuth { http.Error(w, "Unauthorized", http.StatusUnauthorized) return } - if xsrfError := a.checkXSRF(r, session); xsrfError != nil { - if reqAuth { - log.Printf("[WARN] %s", xsrfError.Error()) - http.Error(w, "Unauthorized", http.StatusUnauthorized) - return - } - h.ServeHTTP(w, r) // in anonymous mode just pass it to next handler - return - } - - if ok { // if uinfo in session, populate to context - user := uinfoData.(store.User) + if claims.User != nil { // if uinfo in session, populate to context + user := *claims.User for _, admin := range a.Admins { if admin == user.ID { user.Admin = true @@ -89,19 +76,6 @@ func (a *Authenticator) Auth(reqAuth bool) func(http.Handler) http.Handler { return f } -func (a *Authenticator) checkXSRF(r *http.Request, session *sessions.Session) error { - xsrfToken := r.Header.Get("X-XSRF-TOKEN") - sessionToken, headerOk := session.Values["xsrf_token"] - if !headerOk || xsrfToken == "" || sessionToken == nil { - return errors.New(" no xsrf_token in session") - } - - if xsrfToken != sessionToken { - return errors.Errorf("xsrf header not matched session token, %q != %q", xsrfToken, sessionToken) - } - return nil -} - // AdminOnly allows access to admins func (a *Authenticator) AdminOnly(next http.Handler) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) { diff --git a/app/rest/auth/auth_test.go b/app/rest/auth/auth_test.go index f71d3396..a07df6b5 100644 --- a/app/rest/auth/auth_test.go +++ b/app/rest/auth/auth_test.go @@ -3,6 +3,7 @@ package auth import ( "encoding/base64" "net/http" + "net/http/cookiejar" "net/http/httptest" "testing" "time" @@ -13,9 +14,73 @@ import ( "github.com/stretchr/testify/require" ) +func TestAuthJWTCookie(t *testing.T) { + a := Authenticator{DevPasswd: "123456", JWTService: JWT{secret: "xyz 12345", secureCookies: false}} + router := chi.NewRouter() + router.With(a.Auth(true)).Get("/auth", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(201) + }) + server := httptest.NewServer(router) + defer server.Close() + + expiration := int(time.Duration(365 * 24 * time.Hour).Seconds()) + req, err := http.NewRequest("GET", server.URL+"/auth", nil) + require.Nil(t, err) + req.AddCookie(&http.Cookie{Name: "JWT", Value: testJwtValid, HttpOnly: true, Path: "/", MaxAge: expiration, Secure: false}) + req.Header.Add("X-XSRF-TOKEN", "random id") + + client := &http.Client{Timeout: 5 * time.Second} + resp, err := client.Do(req) + require.NoError(t, err) + assert.Equal(t, 201, resp.StatusCode, "valid auth user") + + req, err = http.NewRequest("GET", server.URL+"/auth", nil) + require.Nil(t, err) + req.AddCookie(&http.Cookie{Name: "JWT", Value: testJwtValid, HttpOnly: true, Path: "/", MaxAge: expiration, Secure: false}) + req.Header.Add("X-XSRF-TOKEN", "wrong id") + resp, err = client.Do(req) + require.NoError(t, err) + assert.Equal(t, 401, resp.StatusCode, "xsrf mismatch") + + req, err = http.NewRequest("GET", server.URL+"/auth", nil) + require.Nil(t, err) + req.AddCookie(&http.Cookie{Name: "JWT", Value: testJwtExpired, HttpOnly: true, Path: "/", MaxAge: expiration, Secure: false}) + req.Header.Add("X-XSRF-TOKEN", "random id") + resp, err = client.Do(req) + require.NoError(t, err) + assert.Equal(t, 401, resp.StatusCode, "token expired") +} + +func TestAuthJWTHeader(t *testing.T) { + a := Authenticator{DevPasswd: "123456", JWTService: JWT{secret: "xyz 12345", secureCookies: false}} + + router := chi.NewRouter() + router.With(a.Auth(true)).Get("/auth", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(201) + }) + server := httptest.NewServer(router) + defer server.Close() + + jar, err := cookiejar.New(nil) + require.Nil(t, err) + client := &http.Client{Jar: jar, Timeout: 5 * time.Second} + req, err := http.NewRequest("GET", server.URL+"/auth", nil) + require.Nil(t, err) + req.Header.Add("X-JWT", testJwtValid) + resp, err := client.Do(req) + require.NoError(t, err) + assert.Equal(t, 201, resp.StatusCode, "valid auth user") + + req, err = http.NewRequest("GET", server.URL+"/auth", nil) + require.Nil(t, err) + req.Header.Add("X-JWT", testJwtExpired) + resp, err = client.Do(req) + require.NoError(t, err) + assert.Equal(t, 401, resp.StatusCode, "invalid auth token") +} func TestAuthRequired(t *testing.T) { - store := mockStore{} - a := Authenticator{SessionStore: &store, DevPasswd: "123456"} + + a := Authenticator{DevPasswd: "123456"} router := chi.NewRouter() router.With(a.Auth(true)).Get("/auth", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(201) @@ -43,8 +108,7 @@ func TestAuthRequired(t *testing.T) { } func TestAuthNotRequired(t *testing.T) { - store := mockStore{} - a := Authenticator{SessionStore: &store, DevPasswd: "123456"} + a := Authenticator{DevPasswd: "123456"} router := chi.NewRouter() router.With(a.Auth(false)).Get("/auth", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(201) @@ -72,8 +136,7 @@ func TestAuthNotRequired(t *testing.T) { } func TestAdminRequired(t *testing.T) { - store := mockStore{} - a := Authenticator{SessionStore: &store, DevPasswd: "123456"} + a := Authenticator{DevPasswd: "123456"} router := chi.NewRouter() router.With(a.Auth(true), a.AdminOnly).Get("/auth", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(201) diff --git a/app/rest/auth/jwt.go b/app/rest/auth/jwt.go new file mode 100644 index 00000000..d8231d22 --- /dev/null +++ b/app/rest/auth/jwt.go @@ -0,0 +1,124 @@ +package auth + +import ( + "log" + "net/http" + "time" + + jwt "github.com/dgrijalva/jwt-go" + "github.com/pkg/errors" + + "github.com/umputun/remark/app/store" +) + +// JWT wraps jwt operations +// supports both header and cookie jwt +type JWT struct { + secret string + secureCookies bool +} + +// CustomClaims stores user info for auth and state & from from login +type CustomClaims struct { + jwt.StandardClaims + User *store.User `json:"user,omitempty"` + + State string `json:"state,omitempty"` + From string `json:"from,omitempty"` +} + +const jwtCookieName = "JWT" +const jwtHeaderKey = "X-JWT" +const xsrfCookieName = "XSRF-TOKEN" +const xsrfHeaderKey = "X-XSRF-TOKEN" + +// Set creates jwt cookie with xsrf cookie and put it to ResponseWriter +func (j *JWT) Set(w http.ResponseWriter, claims *CustomClaims) error { + token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) + tokenString, err := token.SignedString([]byte([]byte(j.secret))) + if err != nil { + return errors.Wrap(err, "can't sign jwt token") + } + + expiration := int(time.Duration(365 * 24 * time.Hour).Seconds()) + + jwtCookie := http.Cookie{Name: jwtCookieName, Value: tokenString, HttpOnly: true, Path: "/", + MaxAge: expiration, Secure: j.secureCookies} + http.SetCookie(w, &jwtCookie) + + jti := claims.Id + xsrfCookie := http.Cookie{Name: xsrfCookieName, Value: jti, HttpOnly: false, Path: "/", + MaxAge: expiration, Secure: j.secureCookies} + http.SetCookie(w, &xsrfCookie) + + return nil +} + +// Get jwt from header or cookie +// if cookie used verify xsrf token to match +func (j *JWT) Get(r *http.Request) (*CustomClaims, error) { + + fromCookie := false + tokenString := "" + + if tokenHeader := r.Header.Get(jwtHeaderKey); tokenHeader != "" { + tokenString = tokenHeader + } + + if tokenString == "" { + fromCookie = true + jc, err := r.Cookie(jwtCookieName) + if err != nil { + return nil, errors.Wrap(err, "jwt cookie was not presented") + } + tokenString = jc.Value + } + + token, err := jwt.ParseWithClaims(tokenString, &CustomClaims{}, func(token *jwt.Token) (interface{}, error) { + if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { + return nil, errors.Errorf("unexpected signing method: %v", token.Header["alg"]) + } + return []byte(j.secret), nil + }) + if err != nil { + return nil, errors.Wrap(err, "can't parse jwt") + } + + claims, ok := token.Claims.(*CustomClaims) + if !ok || !token.Valid { + return nil, errors.New("invalid jwt") + } + + if fromCookie && claims.User != nil { + xsrf := r.Header.Get(xsrfHeaderKey) + if claims.Id != xsrf { + log.Printf("[WARN] xsrf not matched jti, %s != %s", xsrf, claims.Id) + return nil, errors.New("xsrf mismatch") + } + } + + return claims, nil +} + +// Reset token's cookies +func (j *JWT) Reset(w http.ResponseWriter) { + jwtCookie := http.Cookie{Name: jwtCookieName, Value: "", HttpOnly: false, Path: "/", + MaxAge: -1, Expires: time.Unix(0, 0), Secure: true} + http.SetCookie(w, &jwtCookie) + + xsrfCookie := http.Cookie{Name: xsrfCookieName, Value: "", HttpOnly: false, Path: "/", + MaxAge: -1, Expires: time.Unix(0, 0), Secure: true} + http.SetCookie(w, &xsrfCookie) +} + +func (j *JWT) verify(claims CustomClaims) error { + + if time.Now().Unix() > claims.ExpiresAt { + return errors.Errorf("token exp failed %d:%d", claims.ExpiresAt, time.Now().Unix()) + } + + if time.Now().Unix() < claims.NotBefore { + return errors.Errorf("token nbf failed %d:%d", claims.NotBefore, time.Now().Unix()) + } + return nil +} diff --git a/app/rest/auth/jwt_test.go b/app/rest/auth/jwt_test.go new file mode 100644 index 00000000..ca62b4ce --- /dev/null +++ b/app/rest/auth/jwt_test.go @@ -0,0 +1,155 @@ +package auth + +import ( + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/stretchr/testify/require" + + "github.com/stretchr/testify/assert" + "github.com/umputun/remark/app/store" + + jwt "github.com/dgrijalva/jwt-go" +) + +var testJwtValid = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjI3ODkxOTE4MjIsImp0aSI6InJhbmRvbSBpZCI" + + "sImlzcyI6InJlbWFyazQyIiwibmJmIjoxNTI2ODg0MjIyLCJ1c2VyIjp7Im5hbWUiOiJuYW1lMSIsImlkIjoiaWQxIiwicGljdHVyZS" + + "I6IiIsImFkbWluIjpmYWxzZX0sInN0YXRlIjoiMTIzNDU2IiwiZnJvbSI6ImZyb20ifQ._loFgh3g45gr9TtGqvM3N584I_6EHEOJnYb6Py84stQ" + +var testJwtExpired = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MjY4ODc4MjIsImp0aSI6InJhbmRvbSBpZCIs" + "ImlzcyI6InJlbWFyazQyIiwibmJmIjoxNTI2ODg0MjIyLCJ1c2VyIjp7Im5hbWUiOiJuYW1lMSIsImlkIjoiaWQxIiwicGljdHVyZSI6IiI" + + "sImFkbWluIjpmYWxzZX0sInN0YXRlIjoiMTIzNDU2IiwiZnJvbSI6ImZyb20ifQ.4_dCrY9ihyfZIedz-kZwBTxmxU1a52V7IqeJrOqTzE4" + +func TestJWT_Set(t *testing.T) { + j := JWT{secret: "xyz 12345"} + + claims := &CustomClaims{ + State: "123456", + From: "from", + User: &store.User{ + ID: "id1", + Name: "name1", + }, + StandardClaims: jwt.StandardClaims{ + Id: "random id", + Issuer: "remark42", + ExpiresAt: time.Date(2058, 5, 21, 1, 30, 22, 0, time.Local).Unix(), + NotBefore: time.Date(2018, 5, 21, 1, 30, 22, 0, time.Local).Unix(), + }, + } + + rr := httptest.NewRecorder() + err := j.Set(rr, claims) + assert.Nil(t, err) + cookies := rr.Result().Cookies() + t.Log(cookies) + require.Equal(t, 2, len(cookies)) + assert.Equal(t, "JWT", cookies[0].Name) + assert.Equal(t, testJwtValid, cookies[0].Value) + + assert.Equal(t, "XSRF-TOKEN", cookies[1].Name) + assert.Equal(t, "random id", cookies[1].Value) +} + +func TestJWT_GetFromHeader(t *testing.T) { + j := JWT{secret: "xyz 12345"} + + req := httptest.NewRequest("GET", "/", nil) + req.Header.Add(jwtHeaderKey, testJwtValid) + claims, err := j.Get(req) + assert.Nil(t, err) + assert.Equal(t, &store.User{Name: "name1", ID: "id1", Picture: "", Admin: false, Blocked: false, IP: ""}, claims.User) + assert.Equal(t, "remark42", claims.Issuer) + + req = httptest.NewRequest("GET", "/", nil) + req.Header.Add(jwtHeaderKey, testJwtExpired) + _, err = j.Get(req) + assert.NotNil(t, err) + assert.True(t, strings.HasPrefix(err.Error(), "can't parse jwt: token is expired by"), err.Error()) + + req = httptest.NewRequest("GET", "/", nil) + req.Header.Add(jwtHeaderKey, "bad bad token") + _, err = j.Get(req) + assert.NotNil(t, err) + assert.True(t, strings.HasPrefix(err.Error(), "can't parse jwt: token contains an invalid number of segments"), err.Error()) + +} + +func TestJWT_SetAndGetWithCookies(t *testing.T) { + j := JWT{secret: "xyz 12345"} + + claims := &CustomClaims{ + State: "123456", + From: "from", + User: &store.User{ + ID: "id1", + Name: "name1", + }, + StandardClaims: jwt.StandardClaims{ + Id: "random id", + Issuer: "remark42", + ExpiresAt: time.Date(2058, 5, 21, 1, 30, 22, 0, time.Local).Unix(), + NotBefore: time.Date(2018, 5, 21, 1, 30, 22, 0, time.Local).Unix(), + }, + } + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/valid" { + j.Set(w, claims) + w.WriteHeader(200) + } + })) + defer ts.Close() + + resp, err := http.Get(ts.URL + "/valid") + require.Nil(t, err) + assert.Equal(t, 200, resp.StatusCode) + + req := httptest.NewRequest("GET", "/valid", nil) + req.AddCookie(resp.Cookies()[0]) + req.Header.Add(xsrfHeaderKey, "random id") + claims, err = j.Get(req) + assert.Nil(t, err) + assert.Equal(t, &store.User{Name: "name1", ID: "id1", Picture: "", Admin: false, Blocked: false, IP: ""}, claims.User) + assert.Equal(t, "remark42", claims.Issuer) +} + +func TestJWT_SetAndGetWithCookiesExpired(t *testing.T) { + j := JWT{secret: "xyz 12345"} + + claims := &CustomClaims{ + State: "123456", + From: "from", + User: &store.User{ + ID: "id1", + Name: "name1", + }, + StandardClaims: jwt.StandardClaims{ + Id: "random id", + Issuer: "remark42", + ExpiresAt: time.Date(2018, 5, 21, 1, 35, 22, 0, time.Local).Unix(), + NotBefore: time.Date(2018, 5, 21, 1, 30, 22, 0, time.Local).Unix(), + }, + } + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/expired" { + j.Set(w, claims) + w.WriteHeader(200) + } + })) + defer ts.Close() + + resp, err := http.Get(ts.URL + "/expired") + require.Nil(t, err) + assert.Equal(t, 200, resp.StatusCode) + + req := httptest.NewRequest("GET", "/expired", nil) + req.AddCookie(resp.Cookies()[0]) + req.Header.Add(xsrfHeaderKey, "random id") + _, err = j.Get(req) + assert.NotNil(t, err) + assert.True(t, strings.HasPrefix(err.Error(), "can't parse jwt: token is expired by"), err.Error()) +} diff --git a/app/rest/auth/provider.go b/app/rest/auth/provider.go index a7099c95..8fb34ea7 100644 --- a/app/rest/auth/provider.go +++ b/app/rest/auth/provider.go @@ -10,11 +10,12 @@ import ( "io/ioutil" "log" "net/http" + "strings" "time" + jwt "github.com/dgrijalva/jwt-go" "github.com/go-chi/chi" "github.com/go-chi/render" - "github.com/gorilla/sessions" "golang.org/x/oauth2" "github.com/umputun/remark/app/rest" @@ -24,26 +25,25 @@ import ( // Provider represents oauth2 provider type Provider struct { - sessions.Store - Name string RedirectURL string InfoURL string Endpoint oauth2.Endpoint Scopes []string MapUser func(userData, []byte) store.User // map info from InfoURL to User + Secret string avatarProxy *proxy.Avatar conf *oauth2.Config + jwtService *JWT } // Params to make initialized and ready to use provider type Params struct { - Cid string - Csecret string - SessionStore sessions.Store - RemarkURL string - AvatarProxy *proxy.Avatar + Cid string + Csecret string + RemarkURL string + AvatarProxy *proxy.Avatar } type userData map[string]interface{} @@ -68,8 +68,9 @@ func initProvider(p Params, provider Provider) Provider { } provider.conf = &conf - provider.Store = p.SessionStore provider.avatarProxy = p.AvatarProxy + provider.jwtService = &JWT{secret: provider.Secret, secureCookies: strings.HasPrefix(p.RemarkURL, "https://")} + return provider } @@ -87,52 +88,47 @@ func (p Provider) loginHandler(w http.ResponseWriter, r *http.Request) { // make state (random) and store in session state := p.randToken() - session, err := p.Get(r, "remark") - if err != nil { - log.Printf("[DEBUG] can't get session, %s", err) + + claims := &CustomClaims{ + State: state, + From: r.URL.Query().Get("from"), + StandardClaims: jwt.StandardClaims{ + Id: p.randToken(), + Issuer: "remark42", + ExpiresAt: time.Now().Add(30 * time.Minute).Unix(), + NotBefore: time.Now().Add(-1 * time.Minute).Unix(), + }, } - session.Values["state"] = state - - if from := r.URL.Query().Get("from"); from != "" { - session.Values["from"] = from - } - - log.Printf("[DEBUG] login, %+v", session.Values) - if err := session.Save(r, w); err != nil { - rest.SendErrorJSON(w, r, http.StatusInternalServerError, err, "failed to save state") + if err := p.jwtService.Set(w, claims); err != nil { + rest.SendErrorJSON(w, r, http.StatusInternalServerError, err, "failed to set jwt") return } // return login url loginURL := p.conf.AuthCodeURL(state) log.Printf("[DEBUG] login url %s", loginURL) - http.Redirect(w, r, loginURL, http.StatusTemporaryRedirect) + + http.Redirect(w, r, loginURL, http.StatusFound) } // authHandler fills user info and redirects to "from" url. This is callback url redirected locally by browser // GET /callback func (p Provider) authHandler(w http.ResponseWriter, r *http.Request) { - session, err := p.Get(r, "remark") + oauthClaims, err := p.jwtService.Get(r) if err != nil { - rest.SendErrorJSON(w, r, http.StatusInternalServerError, err, "failed to get session") - return - } - - // compare saved state to the one from redirect url - retrievedState, ok := session.Values["state"] - if !ok { - http.Error(w, "missing state in store", http.StatusUnauthorized) + rest.SendErrorJSON(w, r, http.StatusInternalServerError, err, "failed to get jwt") return } + retrievedState := oauthClaims.State if retrievedState == "" || retrievedState != r.URL.Query().Get("state") { http.Error(w, fmt.Sprintf("unexpected state %v", retrievedState), http.StatusUnauthorized) return } - log.Printf("[DEBUG] auth, %+v", session.Values) + log.Printf("[DEBUG] auth with state %s", retrievedState) tok, err := p.conf.Exchange(context.Background(), r.URL.Query().Get("code")) if err != nil { rest.SendErrorJSON(w, r, http.StatusInternalServerError, err, "exchange failed") @@ -173,26 +169,27 @@ func (p Provider) authHandler(w http.ResponseWriter, r *http.Request) { log.Printf("[WARN] failed to proxy avatar, %s", e) } } - session.Values["uinfo"] = u - xsrfToken := p.randToken() - session.Values["xsrf_token"] = xsrfToken - - xsrfCookie := http.Cookie{Name: "XSRF-TOKEN", Value: xsrfToken, HttpOnly: false, Path: "/", - MaxAge: 3600 * 24 * 365, Secure: true, + authClaims := &CustomClaims{ + User: &u, + StandardClaims: jwt.StandardClaims{ + Issuer: "remark42", + Id: p.randToken(), + ExpiresAt: time.Now().Add(7 * 24 * time.Hour).Unix(), + NotBefore: time.Now().Add(-1 * time.Minute).Unix(), + }, } - http.SetCookie(w, &xsrfCookie) - if err = session.Save(r, w); err != nil { + if err = p.jwtService.Set(w, authClaims); err != nil { rest.SendErrorJSON(w, r, http.StatusInternalServerError, err, "failed to save user info") return } - log.Printf("[DEBUG] user info %+v", session.Values["uinfo"]) + log.Printf("[DEBUG] user info %+v", u) // redirect to back url if presented in login query params - if fromURL, ok := session.Values["from"]; ok { - http.Redirect(w, r, fromURL.(string), http.StatusTemporaryRedirect) + if oauthClaims.From != "" { + http.Redirect(w, r, oauthClaims.From, http.StatusTemporaryRedirect) return } render.JSON(w, r, jData) @@ -200,26 +197,8 @@ func (p Provider) authHandler(w http.ResponseWriter, r *http.Request) { // LogoutHandler - GET /logout func (p Provider) LogoutHandler(w http.ResponseWriter, r *http.Request) { - session, err := p.Get(r, "remark") - if err != nil { - rest.SendErrorJSON(w, r, http.StatusBadRequest, err, "failed to get session") - return - } - - session.Values["uinfo"], session.Values["from"], session.Values["state"] = "", "", "" - delete(session.Values, "uinfo") - delete(session.Values, "from") - delete(session.Values, "state") - delete(session.Values, "xsrf_token") - xsrfCookie := http.Cookie{Name: "XSRF-TOKEN", Value: "", HttpOnly: false, Path: "/", - MaxAge: -1, Expires: time.Unix(0, 0), Secure: true} - http.SetCookie(w, &xsrfCookie) - - if err = session.Save(r, w); err != nil { - rest.SendErrorJSON(w, r, http.StatusInternalServerError, err, "failed to reset user info") - return - } - log.Printf("[DEBUG] logout, %+v", session.Values) + p.jwtService.Reset(w) + log.Printf("[DEBUG] logout") } func (p Provider) randToken() string { diff --git a/app/rest/auth/provider_test.go b/app/rest/auth/provider_test.go index 2a775ef3..31e5665c 100644 --- a/app/rest/auth/provider_test.go +++ b/app/rest/auth/provider_test.go @@ -6,11 +6,11 @@ import ( "io/ioutil" "log" "net/http" + "net/http/cookiejar" "strings" "testing" "time" - "github.com/gorilla/sessions" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "golang.org/x/oauth2" @@ -20,21 +20,28 @@ import ( func TestLogin(t *testing.T) { - sessionStore := &mockStore{values: make(map[interface{}]interface{})} - - _, ts, ots := mockProvider(t, sessionStore, 8981, 8982) + _, ts, ots := mockProvider(t, 8981, 8982) defer func() { ts.Close() ots.Close() }() - resp, err := http.Get("http://localhost:8981/login") + jar, err := cookiejar.New(nil) + client := &http.Client{Jar: jar, Timeout: 5 * time.Second} + resp, err := client.Get("http://localhost:8981/login") assert.Nil(t, err) assert.Equal(t, 200, resp.StatusCode) body, err := ioutil.ReadAll(resp.Body) assert.Nil(t, err) t.Logf("resp %s", string(body)) t.Logf("headers: %+v", resp.Header) + + assert.Equal(t, 2, len(resp.Cookies())) + assert.Equal(t, "JWT", resp.Cookies()[0].Name) + assert.NotEqual(t, "", resp.Cookies()[0].Value, "jwt set") + assert.Equal(t, "XSRF-TOKEN", resp.Cookies()[1].Name) + assert.NotEqual(t, "", resp.Cookies()[1].Value, "xsrf cookie set") + u := store.User{} err = json.Unmarshal(body, &u) assert.Nil(t, err) @@ -43,29 +50,33 @@ func TestLogin(t *testing.T) { } func TestLogout(t *testing.T) { - sessionStore := &mockStore{values: make(map[interface{}]interface{})} - _, ts, ots := mockProvider(t, sessionStore, 8691, 8692) + _, ts, ots := mockProvider(t, 8691, 8692) defer func() { ts.Close() ots.Close() }() - resp, err := http.Get("http://localhost:8691/login") + jar, err := cookiejar.New(nil) + require.Nil(t, err) + client := &http.Client{Jar: jar, Timeout: 5 * time.Second} + + resp, err := client.Get("http://localhost:8691/login") + require.Nil(t, err) + assert.Equal(t, 200, resp.StatusCode) + assert.Equal(t, 2, len(resp.Cookies())) + resp, err = client.Get("http://localhost:8691/logout") require.Nil(t, err) assert.Equal(t, 200, resp.StatusCode) - _, err = http.Get("http://localhost:8691/logout") - require.Nil(t, err) - assert.Equal(t, 200, resp.StatusCode) - - s, err := sessionStore.Get(nil, "remark") - assert.Nil(t, err) - t.Log(s.Values) - assert.Equal(t, 0, len(s.Values)) + assert.Equal(t, 2, len(resp.Cookies())) + assert.Equal(t, "JWT", resp.Cookies()[0].Name, "jwt cookie cleared") + assert.Equal(t, "", resp.Cookies()[0].Value) + assert.Equal(t, "XSRF-TOKEN", resp.Cookies()[1].Name, "xsrf cookie cleared") + assert.Equal(t, "", resp.Cookies()[1].Value) } -func mockProvider(t *testing.T, sessStore sessions.Store, loginPort, authPort int) (provider Provider, ts *http.Server, oauth *http.Server) { +func mockProvider(t *testing.T, loginPort, authPort int) (provider Provider, ts *http.Server, oauth *http.Server) { provider = Provider{ Name: "mock", @@ -84,9 +95,10 @@ func mockProvider(t *testing.T, sessStore sessions.Store, loginPort, authPort in } return userInfo }, + jwtService: &JWT{secret: "12345", secureCookies: false}, } - provider = initProvider(Params{SessionStore: sessStore, Cid: "cid", Csecret: "csecret"}, provider) + provider = initProvider(Params{Cid: "cid", Csecret: "csecret"}, provider) ts = &http.Server{Addr: fmt.Sprintf(":%d", loginPort), Handler: provider.Routes()} @@ -132,29 +144,3 @@ func mockProvider(t *testing.T, sessStore sessions.Store, loginPort, authPort in time.Sleep(time.Millisecond * 100) // let the start return provider, ts, oauth } - -type mockStore struct { - values map[interface{}]interface{} -} - -func (ms *mockStore) Get(r *http.Request, name string) (*sessions.Session, error) { - if ms.values == nil { - ms.values = make(map[interface{}]interface{}) - } - s := sessions.NewSession(ms, name) - s.Values = ms.values - return s, nil -} - -func (ms *mockStore) New(r *http.Request, name string) (*sessions.Session, error) { - ms.values = make(map[interface{}]interface{}) - return &sessions.Session{Values: ms.values}, nil -} - -func (ms *mockStore) Save(r *http.Request, w http.ResponseWriter, s *sessions.Session) error { - if ms.values == nil { - ms.values = make(map[interface{}]interface{}) - } - ms.values = s.Values - return nil -} diff --git a/app/rest/auth/providers.go b/app/rest/auth/providers.go index f92489d5..f1b42661 100644 --- a/app/rest/auth/providers.go +++ b/app/rest/auth/providers.go @@ -20,7 +20,6 @@ func NewGoogle(p Params) Provider { RedirectURL: p.RemarkURL + "/auth/google/callback", Scopes: []string{"https://www.googleapis.com/auth/userinfo.email"}, InfoURL: "https://www.googleapis.com/oauth2/v3/userinfo", - Store: p.SessionStore, MapUser: func(data userData, _ []byte) store.User { userInfo := store.User{ // encode email with provider name to avoid collision if same id returned by other provider @@ -44,7 +43,6 @@ func NewGithub(p Params) Provider { RedirectURL: p.RemarkURL + "/auth/github/callback", Scopes: []string{"user:email"}, InfoURL: "https://api.github.com/user", - Store: p.SessionStore, MapUser: func(data userData, _ []byte) store.User { userInfo := store.User{ ID: "github_" + store.EncodeID(data.value("login")), @@ -83,7 +81,6 @@ func NewFacebook(p Params) Provider { RedirectURL: p.RemarkURL + "/auth/facebook/callback", Scopes: []string{"public_profile"}, InfoURL: "https://graph.facebook.com/me?fields=id,name,picture", - Store: p.SessionStore, MapUser: func(data userData, bdata []byte) store.User { userInfo := store.User{ ID: "facebook_" + store.EncodeID(data.value("id")), @@ -113,7 +110,6 @@ func NewDisqus(p Params) Provider { RedirectURL: p.RemarkURL + "/auth/disqus/callback", Scopes: []string{"read"}, InfoURL: "https://disqus.com/api/3.0/users/details.json", - Store: p.SessionStore, MapUser: func(data userData, _ []byte) store.User { userInfo := store.User{ ID: "disqus_" + store.EncodeID(data.value("login")), diff --git a/vendor/github.com/dgrijalva/jwt-go/.gitignore b/vendor/github.com/dgrijalva/jwt-go/.gitignore new file mode 100644 index 00000000..80bed650 --- /dev/null +++ b/vendor/github.com/dgrijalva/jwt-go/.gitignore @@ -0,0 +1,4 @@ +.DS_Store +bin + + diff --git a/vendor/github.com/dgrijalva/jwt-go/.travis.yml b/vendor/github.com/dgrijalva/jwt-go/.travis.yml new file mode 100644 index 00000000..1027f56c --- /dev/null +++ b/vendor/github.com/dgrijalva/jwt-go/.travis.yml @@ -0,0 +1,13 @@ +language: go + +script: + - go vet ./... + - go test -v ./... + +go: + - 1.3 + - 1.4 + - 1.5 + - 1.6 + - 1.7 + - tip diff --git a/vendor/github.com/dgrijalva/jwt-go/LICENSE b/vendor/github.com/dgrijalva/jwt-go/LICENSE new file mode 100644 index 00000000..df83a9c2 --- /dev/null +++ b/vendor/github.com/dgrijalva/jwt-go/LICENSE @@ -0,0 +1,8 @@ +Copyright (c) 2012 Dave Grijalva + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/vendor/github.com/dgrijalva/jwt-go/MIGRATION_GUIDE.md b/vendor/github.com/dgrijalva/jwt-go/MIGRATION_GUIDE.md new file mode 100644 index 00000000..7fc1f793 --- /dev/null +++ b/vendor/github.com/dgrijalva/jwt-go/MIGRATION_GUIDE.md @@ -0,0 +1,97 @@ +## Migration Guide from v2 -> v3 + +Version 3 adds several new, frequently requested features. To do so, it introduces a few breaking changes. We've worked to keep these as minimal as possible. This guide explains the breaking changes and how you can quickly update your code. + +### `Token.Claims` is now an interface type + +The most requested feature from the 2.0 verison of this library was the ability to provide a custom type to the JSON parser for claims. This was implemented by introducing a new interface, `Claims`, to replace `map[string]interface{}`. We also included two concrete implementations of `Claims`: `MapClaims` and `StandardClaims`. + +`MapClaims` is an alias for `map[string]interface{}` with built in validation behavior. It is the default claims type when using `Parse`. The usage is unchanged except you must type cast the claims property. + +The old example for parsing a token looked like this.. + +```go + if token, err := jwt.Parse(tokenString, keyLookupFunc); err == nil { + fmt.Printf("Token for user %v expires %v", token.Claims["user"], token.Claims["exp"]) + } +``` + +is now directly mapped to... + +```go + if token, err := jwt.Parse(tokenString, keyLookupFunc); err == nil { + claims := token.Claims.(jwt.MapClaims) + fmt.Printf("Token for user %v expires %v", claims["user"], claims["exp"]) + } +``` + +`StandardClaims` is designed to be embedded in your custom type. You can supply a custom claims type with the new `ParseWithClaims` function. Here's an example of using a custom claims type. + +```go + type MyCustomClaims struct { + User string + *StandardClaims + } + + if token, err := jwt.ParseWithClaims(tokenString, &MyCustomClaims{}, keyLookupFunc); err == nil { + claims := token.Claims.(*MyCustomClaims) + fmt.Printf("Token for user %v expires %v", claims.User, claims.StandardClaims.ExpiresAt) + } +``` + +### `ParseFromRequest` has been moved + +To keep this library focused on the tokens without becoming overburdened with complex request processing logic, `ParseFromRequest` and its new companion `ParseFromRequestWithClaims` have been moved to a subpackage, `request`. The method signatues have also been augmented to receive a new argument: `Extractor`. + +`Extractors` do the work of picking the token string out of a request. The interface is simple and composable. + +This simple parsing example: + +```go + if token, err := jwt.ParseFromRequest(tokenString, req, keyLookupFunc); err == nil { + fmt.Printf("Token for user %v expires %v", token.Claims["user"], token.Claims["exp"]) + } +``` + +is directly mapped to: + +```go + if token, err := request.ParseFromRequest(req, request.OAuth2Extractor, keyLookupFunc); err == nil { + claims := token.Claims.(jwt.MapClaims) + fmt.Printf("Token for user %v expires %v", claims["user"], claims["exp"]) + } +``` + +There are several concrete `Extractor` types provided for your convenience: + +* `HeaderExtractor` will search a list of headers until one contains content. +* `ArgumentExtractor` will search a list of keys in request query and form arguments until one contains content. +* `MultiExtractor` will try a list of `Extractors` in order until one returns content. +* `AuthorizationHeaderExtractor` will look in the `Authorization` header for a `Bearer` token. +* `OAuth2Extractor` searches the places an OAuth2 token would be specified (per the spec): `Authorization` header and `access_token` argument +* `PostExtractionFilter` wraps an `Extractor`, allowing you to process the content before it's parsed. A simple example is stripping the `Bearer ` text from a header + + +### RSA signing methods no longer accept `[]byte` keys + +Due to a [critical vulnerability](https://auth0.com/blog/2015/03/31/critical-vulnerabilities-in-json-web-token-libraries/), we've decided the convenience of accepting `[]byte` instead of `rsa.PublicKey` or `rsa.PrivateKey` isn't worth the risk of misuse. + +To replace this behavior, we've added two helper methods: `ParseRSAPrivateKeyFromPEM(key []byte) (*rsa.PrivateKey, error)` and `ParseRSAPublicKeyFromPEM(key []byte) (*rsa.PublicKey, error)`. These are just simple helpers for unpacking PEM encoded PKCS1 and PKCS8 keys. If your keys are encoded any other way, all you need to do is convert them to the `crypto/rsa` package's types. + +```go + func keyLookupFunc(*Token) (interface{}, error) { + // Don't forget to validate the alg is what you expect: + if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok { + return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"]) + } + + // Look up key + key, err := lookupPublicKey(token.Header["kid"]) + if err != nil { + return nil, err + } + + // Unpack key from PEM encoded PKCS8 + return jwt.ParseRSAPublicKeyFromPEM(key) + } +``` diff --git a/vendor/github.com/dgrijalva/jwt-go/README.md b/vendor/github.com/dgrijalva/jwt-go/README.md new file mode 100644 index 00000000..d358d881 --- /dev/null +++ b/vendor/github.com/dgrijalva/jwt-go/README.md @@ -0,0 +1,100 @@ +# jwt-go + +[![Build Status](https://travis-ci.org/dgrijalva/jwt-go.svg?branch=master)](https://travis-ci.org/dgrijalva/jwt-go) +[![GoDoc](https://godoc.org/github.com/dgrijalva/jwt-go?status.svg)](https://godoc.org/github.com/dgrijalva/jwt-go) + +A [go](http://www.golang.org) (or 'golang' for search engine friendliness) implementation of [JSON Web Tokens](http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html) + +**NEW VERSION COMING:** There have been a lot of improvements suggested since the version 3.0.0 released in 2016. I'm working now on cutting two different releases: 3.2.0 will contain any non-breaking changes or enhancements. 4.0.0 will follow shortly which will include breaking changes. See the 4.0.0 milestone to get an idea of what's coming. If you have other ideas, or would like to participate in 4.0.0, now's the time. If you depend on this library and don't want to be interrupted, I recommend you use your dependency mangement tool to pin to version 3. + +**SECURITY NOTICE:** Some older versions of Go have a security issue in the cryotp/elliptic. Recommendation is to upgrade to at least 1.8.3. See issue #216 for more detail. + +**SECURITY NOTICE:** It's important that you [validate the `alg` presented is what you expect](https://auth0.com/blog/2015/03/31/critical-vulnerabilities-in-json-web-token-libraries/). This library attempts to make it easy to do the right thing by requiring key types match the expected alg, but you should take the extra step to verify it in your usage. See the examples provided. + +## What the heck is a JWT? + +JWT.io has [a great introduction](https://jwt.io/introduction) to JSON Web Tokens. + +In short, it's a signed JSON object that does something useful (for example, authentication). It's commonly used for `Bearer` tokens in Oauth 2. A token is made of three parts, separated by `.`'s. The first two parts are JSON objects, that have been [base64url](http://tools.ietf.org/html/rfc4648) encoded. The last part is the signature, encoded the same way. + +The first part is called the header. It contains the necessary information for verifying the last part, the signature. For example, which encryption method was used for signing and what key was used. + +The part in the middle is the interesting bit. It's called the Claims and contains the actual stuff you care about. Refer to [the RFC](http://self-issued.info/docs/draft-jones-json-web-token.html) for information about reserved keys and the proper way to add your own. + +## What's in the box? + +This library supports the parsing and verification as well as the generation and signing of JWTs. Current supported signing algorithms are HMAC SHA, RSA, RSA-PSS, and ECDSA, though hooks are present for adding your own. + +## Examples + +See [the project documentation](https://godoc.org/github.com/dgrijalva/jwt-go) for examples of usage: + +* [Simple example of parsing and validating a token](https://godoc.org/github.com/dgrijalva/jwt-go#example-Parse--Hmac) +* [Simple example of building and signing a token](https://godoc.org/github.com/dgrijalva/jwt-go#example-New--Hmac) +* [Directory of Examples](https://godoc.org/github.com/dgrijalva/jwt-go#pkg-examples) + +## Extensions + +This library publishes all the necessary components for adding your own signing methods. Simply implement the `SigningMethod` interface and register a factory method using `RegisterSigningMethod`. + +Here's an example of an extension that integrates with the Google App Engine signing tools: https://github.com/someone1/gcp-jwt-go + +## Compliance + +This library was last reviewed to comply with [RTF 7519](http://www.rfc-editor.org/info/rfc7519) dated May 2015 with a few notable differences: + +* In order to protect against accidental use of [Unsecured JWTs](http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html#UnsecuredJWT), tokens using `alg=none` will only be accepted if the constant `jwt.UnsafeAllowNoneSignatureType` is provided as the key. + +## Project Status & Versioning + +This library is considered production ready. Feedback and feature requests are appreciated. The API should be considered stable. There should be very few backwards-incompatible changes outside of major version updates (and only with good reason). + +This project uses [Semantic Versioning 2.0.0](http://semver.org). Accepted pull requests will land on `master`. Periodically, versions will be tagged from `master`. You can find all the releases on [the project releases page](https://github.com/dgrijalva/jwt-go/releases). + +While we try to make it obvious when we make breaking changes, there isn't a great mechanism for pushing announcements out to users. You may want to use this alternative package include: `gopkg.in/dgrijalva/jwt-go.v3`. It will do the right thing WRT semantic versioning. + +**BREAKING CHANGES:*** +* Version 3.0.0 includes _a lot_ of changes from the 2.x line, including a few that break the API. We've tried to break as few things as possible, so there should just be a few type signature changes. A full list of breaking changes is available in `VERSION_HISTORY.md`. See `MIGRATION_GUIDE.md` for more information on updating your code. + +## Usage Tips + +### Signing vs Encryption + +A token is simply a JSON object that is signed by its author. this tells you exactly two things about the data: + +* The author of the token was in the possession of the signing secret +* The data has not been modified since it was signed + +It's important to know that JWT does not provide encryption, which means anyone who has access to the token can read its contents. If you need to protect (encrypt) the data, there is a companion spec, `JWE`, that provides this functionality. JWE is currently outside the scope of this library. + +### Choosing a Signing Method + +There are several signing methods available, and you should probably take the time to learn about the various options before choosing one. The principal design decision is most likely going to be symmetric vs asymmetric. + +Symmetric signing methods, such as HSA, use only a single secret. This is probably the simplest signing method to use since any `[]byte` can be used as a valid secret. They are also slightly computationally faster to use, though this rarely is enough to matter. Symmetric signing methods work the best when both producers and consumers of tokens are trusted, or even the same system. Since the same secret is used to both sign and validate tokens, you can't easily distribute the key for validation. + +Asymmetric signing methods, such as RSA, use different keys for signing and verifying tokens. This makes it possible to produce tokens with a private key, and allow any consumer to access the public key for verification. + +### Signing Methods and Key Types + +Each signing method expects a different object type for its signing keys. See the package documentation for details. Here are the most common ones: + +* The [HMAC signing method](https://godoc.org/github.com/dgrijalva/jwt-go#SigningMethodHMAC) (`HS256`,`HS384`,`HS512`) expect `[]byte` values for signing and validation +* The [RSA signing method](https://godoc.org/github.com/dgrijalva/jwt-go#SigningMethodRSA) (`RS256`,`RS384`,`RS512`) expect `*rsa.PrivateKey` for signing and `*rsa.PublicKey` for validation +* The [ECDSA signing method](https://godoc.org/github.com/dgrijalva/jwt-go#SigningMethodECDSA) (`ES256`,`ES384`,`ES512`) expect `*ecdsa.PrivateKey` for signing and `*ecdsa.PublicKey` for validation + +### JWT and OAuth + +It's worth mentioning that OAuth and JWT are not the same thing. A JWT token is simply a signed JSON object. It can be used anywhere such a thing is useful. There is some confusion, though, as JWT is the most common type of bearer token used in OAuth2 authentication. + +Without going too far down the rabbit hole, here's a description of the interaction of these technologies: + +* OAuth is a protocol for allowing an identity provider to be separate from the service a user is logging in to. For example, whenever you use Facebook to log into a different service (Yelp, Spotify, etc), you are using OAuth. +* OAuth defines several options for passing around authentication data. One popular method is called a "bearer token". A bearer token is simply a string that _should_ only be held by an authenticated user. Thus, simply presenting this token proves your identity. You can probably derive from here why a JWT might make a good bearer token. +* Because bearer tokens are used for authentication, it's important they're kept secret. This is why transactions that use bearer tokens typically happen over SSL. + +## More + +Documentation can be found [on godoc.org](http://godoc.org/github.com/dgrijalva/jwt-go). + +The command line utility included in this project (cmd/jwt) provides a straightforward example of token creation and parsing as well as a useful tool for debugging your own integration. You'll also find several implementation examples in the documentation. diff --git a/vendor/github.com/dgrijalva/jwt-go/VERSION_HISTORY.md b/vendor/github.com/dgrijalva/jwt-go/VERSION_HISTORY.md new file mode 100644 index 00000000..63702983 --- /dev/null +++ b/vendor/github.com/dgrijalva/jwt-go/VERSION_HISTORY.md @@ -0,0 +1,118 @@ +## `jwt-go` Version History + +#### 3.2.0 + +* Added method `ParseUnverified` to allow users to split up the tasks of parsing and validation +* HMAC signing method returns `ErrInvalidKeyType` instead of `ErrInvalidKey` where appropriate +* Added options to `request.ParseFromRequest`, which allows for an arbitrary list of modifiers to parsing behavior. Initial set include `WithClaims` and `WithParser`. Existing usage of this function will continue to work as before. +* Deprecated `ParseFromRequestWithClaims` to simplify API in the future. + +#### 3.1.0 + +* Improvements to `jwt` command line tool +* Added `SkipClaimsValidation` option to `Parser` +* Documentation updates + +#### 3.0.0 + +* **Compatibility Breaking Changes**: See MIGRATION_GUIDE.md for tips on updating your code + * Dropped support for `[]byte` keys when using RSA signing methods. This convenience feature could contribute to security vulnerabilities involving mismatched key types with signing methods. + * `ParseFromRequest` has been moved to `request` subpackage and usage has changed + * The `Claims` property on `Token` is now type `Claims` instead of `map[string]interface{}`. The default value is type `MapClaims`, which is an alias to `map[string]interface{}`. This makes it possible to use a custom type when decoding claims. +* Other Additions and Changes + * Added `Claims` interface type to allow users to decode the claims into a custom type + * Added `ParseWithClaims`, which takes a third argument of type `Claims`. Use this function instead of `Parse` if you have a custom type you'd like to decode into. + * Dramatically improved the functionality and flexibility of `ParseFromRequest`, which is now in the `request` subpackage + * Added `ParseFromRequestWithClaims` which is the `FromRequest` equivalent of `ParseWithClaims` + * Added new interface type `Extractor`, which is used for extracting JWT strings from http requests. Used with `ParseFromRequest` and `ParseFromRequestWithClaims`. + * Added several new, more specific, validation errors to error type bitmask + * Moved examples from README to executable example files + * Signing method registry is now thread safe + * Added new property to `ValidationError`, which contains the raw error returned by calls made by parse/verify (such as those returned by keyfunc or json parser) + +#### 2.7.0 + +This will likely be the last backwards compatible release before 3.0.0, excluding essential bug fixes. + +* Added new option `-show` to the `jwt` command that will just output the decoded token without verifying +* Error text for expired tokens includes how long it's been expired +* Fixed incorrect error returned from `ParseRSAPublicKeyFromPEM` +* Documentation updates + +#### 2.6.0 + +* Exposed inner error within ValidationError +* Fixed validation errors when using UseJSONNumber flag +* Added several unit tests + +#### 2.5.0 + +* Added support for signing method none. You shouldn't use this. The API tries to make this clear. +* Updated/fixed some documentation +* Added more helpful error message when trying to parse tokens that begin with `BEARER ` + +#### 2.4.0 + +* Added new type, Parser, to allow for configuration of various parsing parameters + * You can now specify a list of valid signing methods. Anything outside this set will be rejected. + * You can now opt to use the `json.Number` type instead of `float64` when parsing token JSON +* Added support for [Travis CI](https://travis-ci.org/dgrijalva/jwt-go) +* Fixed some bugs with ECDSA parsing + +#### 2.3.0 + +* Added support for ECDSA signing methods +* Added support for RSA PSS signing methods (requires go v1.4) + +#### 2.2.0 + +* Gracefully handle a `nil` `Keyfunc` being passed to `Parse`. Result will now be the parsed token and an error, instead of a panic. + +#### 2.1.0 + +Backwards compatible API change that was missed in 2.0.0. + +* The `SignedString` method on `Token` now takes `interface{}` instead of `[]byte` + +#### 2.0.0 + +There were two major reasons for breaking backwards compatibility with this update. The first was a refactor required to expand the width of the RSA and HMAC-SHA signing implementations. There will likely be no required code changes to support this change. + +The second update, while unfortunately requiring a small change in integration, is required to open up this library to other signing methods. Not all keys used for all signing methods have a single standard on-disk representation. Requiring `[]byte` as the type for all keys proved too limiting. Additionally, this implementation allows for pre-parsed tokens to be reused, which might matter in an application that parses a high volume of tokens with a small set of keys. Backwards compatibilty has been maintained for passing `[]byte` to the RSA signing methods, but they will also accept `*rsa.PublicKey` and `*rsa.PrivateKey`. + +It is likely the only integration change required here will be to change `func(t *jwt.Token) ([]byte, error)` to `func(t *jwt.Token) (interface{}, error)` when calling `Parse`. + +* **Compatibility Breaking Changes** + * `SigningMethodHS256` is now `*SigningMethodHMAC` instead of `type struct` + * `SigningMethodRS256` is now `*SigningMethodRSA` instead of `type struct` + * `KeyFunc` now returns `interface{}` instead of `[]byte` + * `SigningMethod.Sign` now takes `interface{}` instead of `[]byte` for the key + * `SigningMethod.Verify` now takes `interface{}` instead of `[]byte` for the key +* Renamed type `SigningMethodHS256` to `SigningMethodHMAC`. Specific sizes are now just instances of this type. + * Added public package global `SigningMethodHS256` + * Added public package global `SigningMethodHS384` + * Added public package global `SigningMethodHS512` +* Renamed type `SigningMethodRS256` to `SigningMethodRSA`. Specific sizes are now just instances of this type. + * Added public package global `SigningMethodRS256` + * Added public package global `SigningMethodRS384` + * Added public package global `SigningMethodRS512` +* Moved sample private key for HMAC tests from an inline value to a file on disk. Value is unchanged. +* Refactored the RSA implementation to be easier to read +* Exposed helper methods `ParseRSAPrivateKeyFromPEM` and `ParseRSAPublicKeyFromPEM` + +#### 1.0.2 + +* Fixed bug in parsing public keys from certificates +* Added more tests around the parsing of keys for RS256 +* Code refactoring in RS256 implementation. No functional changes + +#### 1.0.1 + +* Fixed panic if RS256 signing method was passed an invalid key + +#### 1.0.0 + +* First versioned release +* API stabilized +* Supports creating, signing, parsing, and validating JWT tokens +* Supports RS256 and HS256 signing methods \ No newline at end of file diff --git a/vendor/github.com/dgrijalva/jwt-go/claims.go b/vendor/github.com/dgrijalva/jwt-go/claims.go new file mode 100644 index 00000000..f0228f02 --- /dev/null +++ b/vendor/github.com/dgrijalva/jwt-go/claims.go @@ -0,0 +1,134 @@ +package jwt + +import ( + "crypto/subtle" + "fmt" + "time" +) + +// For a type to be a Claims object, it must just have a Valid method that determines +// if the token is invalid for any supported reason +type Claims interface { + Valid() error +} + +// Structured version of Claims Section, as referenced at +// https://tools.ietf.org/html/rfc7519#section-4.1 +// See examples for how to use this with your own claim types +type StandardClaims struct { + Audience string `json:"aud,omitempty"` + ExpiresAt int64 `json:"exp,omitempty"` + Id string `json:"jti,omitempty"` + IssuedAt int64 `json:"iat,omitempty"` + Issuer string `json:"iss,omitempty"` + NotBefore int64 `json:"nbf,omitempty"` + Subject string `json:"sub,omitempty"` +} + +// Validates time based claims "exp, iat, nbf". +// There is no accounting for clock skew. +// As well, if any of the above claims are not in the token, it will still +// be considered a valid claim. +func (c StandardClaims) Valid() error { + vErr := new(ValidationError) + now := TimeFunc().Unix() + + // The claims below are optional, by default, so if they are set to the + // default value in Go, let's not fail the verification for them. + if c.VerifyExpiresAt(now, false) == false { + delta := time.Unix(now, 0).Sub(time.Unix(c.ExpiresAt, 0)) + vErr.Inner = fmt.Errorf("token is expired by %v", delta) + vErr.Errors |= ValidationErrorExpired + } + + if c.VerifyIssuedAt(now, false) == false { + vErr.Inner = fmt.Errorf("Token used before issued") + vErr.Errors |= ValidationErrorIssuedAt + } + + if c.VerifyNotBefore(now, false) == false { + vErr.Inner = fmt.Errorf("token is not valid yet") + vErr.Errors |= ValidationErrorNotValidYet + } + + if vErr.valid() { + return nil + } + + return vErr +} + +// Compares the aud claim against cmp. +// If required is false, this method will return true if the value matches or is unset +func (c *StandardClaims) VerifyAudience(cmp string, req bool) bool { + return verifyAud(c.Audience, cmp, req) +} + +// Compares the exp claim against cmp. +// If required is false, this method will return true if the value matches or is unset +func (c *StandardClaims) VerifyExpiresAt(cmp int64, req bool) bool { + return verifyExp(c.ExpiresAt, cmp, req) +} + +// Compares the iat claim against cmp. +// If required is false, this method will return true if the value matches or is unset +func (c *StandardClaims) VerifyIssuedAt(cmp int64, req bool) bool { + return verifyIat(c.IssuedAt, cmp, req) +} + +// Compares the iss claim against cmp. +// If required is false, this method will return true if the value matches or is unset +func (c *StandardClaims) VerifyIssuer(cmp string, req bool) bool { + return verifyIss(c.Issuer, cmp, req) +} + +// Compares the nbf claim against cmp. +// If required is false, this method will return true if the value matches or is unset +func (c *StandardClaims) VerifyNotBefore(cmp int64, req bool) bool { + return verifyNbf(c.NotBefore, cmp, req) +} + +// ----- helpers + +func verifyAud(aud string, cmp string, required bool) bool { + if aud == "" { + return !required + } + if subtle.ConstantTimeCompare([]byte(aud), []byte(cmp)) != 0 { + return true + } else { + return false + } +} + +func verifyExp(exp int64, now int64, required bool) bool { + if exp == 0 { + return !required + } + return now <= exp +} + +func verifyIat(iat int64, now int64, required bool) bool { + if iat == 0 { + return !required + } + return now >= iat +} + +func verifyIss(iss string, cmp string, required bool) bool { + if iss == "" { + return !required + } + if subtle.ConstantTimeCompare([]byte(iss), []byte(cmp)) != 0 { + return true + } else { + return false + } +} + +func verifyNbf(nbf int64, now int64, required bool) bool { + if nbf == 0 { + return !required + } + return now >= nbf +} diff --git a/vendor/github.com/dgrijalva/jwt-go/doc.go b/vendor/github.com/dgrijalva/jwt-go/doc.go new file mode 100644 index 00000000..a86dc1a3 --- /dev/null +++ b/vendor/github.com/dgrijalva/jwt-go/doc.go @@ -0,0 +1,4 @@ +// Package jwt is a Go implementation of JSON Web Tokens: http://self-issued.info/docs/draft-jones-json-web-token.html +// +// See README.md for more info. +package jwt diff --git a/vendor/github.com/dgrijalva/jwt-go/ecdsa.go b/vendor/github.com/dgrijalva/jwt-go/ecdsa.go new file mode 100644 index 00000000..f9773812 --- /dev/null +++ b/vendor/github.com/dgrijalva/jwt-go/ecdsa.go @@ -0,0 +1,148 @@ +package jwt + +import ( + "crypto" + "crypto/ecdsa" + "crypto/rand" + "errors" + "math/big" +) + +var ( + // Sadly this is missing from crypto/ecdsa compared to crypto/rsa + ErrECDSAVerification = errors.New("crypto/ecdsa: verification error") +) + +// Implements the ECDSA family of signing methods signing methods +// Expects *ecdsa.PrivateKey for signing and *ecdsa.PublicKey for verification +type SigningMethodECDSA struct { + Name string + Hash crypto.Hash + KeySize int + CurveBits int +} + +// Specific instances for EC256 and company +var ( + SigningMethodES256 *SigningMethodECDSA + SigningMethodES384 *SigningMethodECDSA + SigningMethodES512 *SigningMethodECDSA +) + +func init() { + // ES256 + SigningMethodES256 = &SigningMethodECDSA{"ES256", crypto.SHA256, 32, 256} + RegisterSigningMethod(SigningMethodES256.Alg(), func() SigningMethod { + return SigningMethodES256 + }) + + // ES384 + SigningMethodES384 = &SigningMethodECDSA{"ES384", crypto.SHA384, 48, 384} + RegisterSigningMethod(SigningMethodES384.Alg(), func() SigningMethod { + return SigningMethodES384 + }) + + // ES512 + SigningMethodES512 = &SigningMethodECDSA{"ES512", crypto.SHA512, 66, 521} + RegisterSigningMethod(SigningMethodES512.Alg(), func() SigningMethod { + return SigningMethodES512 + }) +} + +func (m *SigningMethodECDSA) Alg() string { + return m.Name +} + +// Implements the Verify method from SigningMethod +// For this verify method, key must be an ecdsa.PublicKey struct +func (m *SigningMethodECDSA) Verify(signingString, signature string, key interface{}) error { + var err error + + // Decode the signature + var sig []byte + if sig, err = DecodeSegment(signature); err != nil { + return err + } + + // Get the key + var ecdsaKey *ecdsa.PublicKey + switch k := key.(type) { + case *ecdsa.PublicKey: + ecdsaKey = k + default: + return ErrInvalidKeyType + } + + if len(sig) != 2*m.KeySize { + return ErrECDSAVerification + } + + r := big.NewInt(0).SetBytes(sig[:m.KeySize]) + s := big.NewInt(0).SetBytes(sig[m.KeySize:]) + + // Create hasher + if !m.Hash.Available() { + return ErrHashUnavailable + } + hasher := m.Hash.New() + hasher.Write([]byte(signingString)) + + // Verify the signature + if verifystatus := ecdsa.Verify(ecdsaKey, hasher.Sum(nil), r, s); verifystatus == true { + return nil + } else { + return ErrECDSAVerification + } +} + +// Implements the Sign method from SigningMethod +// For this signing method, key must be an ecdsa.PrivateKey struct +func (m *SigningMethodECDSA) Sign(signingString string, key interface{}) (string, error) { + // Get the key + var ecdsaKey *ecdsa.PrivateKey + switch k := key.(type) { + case *ecdsa.PrivateKey: + ecdsaKey = k + default: + return "", ErrInvalidKeyType + } + + // Create the hasher + if !m.Hash.Available() { + return "", ErrHashUnavailable + } + + hasher := m.Hash.New() + hasher.Write([]byte(signingString)) + + // Sign the string and return r, s + if r, s, err := ecdsa.Sign(rand.Reader, ecdsaKey, hasher.Sum(nil)); err == nil { + curveBits := ecdsaKey.Curve.Params().BitSize + + if m.CurveBits != curveBits { + return "", ErrInvalidKey + } + + keyBytes := curveBits / 8 + if curveBits%8 > 0 { + keyBytes += 1 + } + + // We serialize the outpus (r and s) into big-endian byte arrays and pad + // them with zeros on the left to make sure the sizes work out. Both arrays + // must be keyBytes long, and the output must be 2*keyBytes long. + rBytes := r.Bytes() + rBytesPadded := make([]byte, keyBytes) + copy(rBytesPadded[keyBytes-len(rBytes):], rBytes) + + sBytes := s.Bytes() + sBytesPadded := make([]byte, keyBytes) + copy(sBytesPadded[keyBytes-len(sBytes):], sBytes) + + out := append(rBytesPadded, sBytesPadded...) + + return EncodeSegment(out), nil + } else { + return "", err + } +} diff --git a/vendor/github.com/dgrijalva/jwt-go/ecdsa_utils.go b/vendor/github.com/dgrijalva/jwt-go/ecdsa_utils.go new file mode 100644 index 00000000..d19624b7 --- /dev/null +++ b/vendor/github.com/dgrijalva/jwt-go/ecdsa_utils.go @@ -0,0 +1,67 @@ +package jwt + +import ( + "crypto/ecdsa" + "crypto/x509" + "encoding/pem" + "errors" +) + +var ( + ErrNotECPublicKey = errors.New("Key is not a valid ECDSA public key") + ErrNotECPrivateKey = errors.New("Key is not a valid ECDSA private key") +) + +// Parse PEM encoded Elliptic Curve Private Key Structure +func ParseECPrivateKeyFromPEM(key []byte) (*ecdsa.PrivateKey, error) { + var err error + + // Parse PEM block + var block *pem.Block + if block, _ = pem.Decode(key); block == nil { + return nil, ErrKeyMustBePEMEncoded + } + + // Parse the key + var parsedKey interface{} + if parsedKey, err = x509.ParseECPrivateKey(block.Bytes); err != nil { + return nil, err + } + + var pkey *ecdsa.PrivateKey + var ok bool + if pkey, ok = parsedKey.(*ecdsa.PrivateKey); !ok { + return nil, ErrNotECPrivateKey + } + + return pkey, nil +} + +// Parse PEM encoded PKCS1 or PKCS8 public key +func ParseECPublicKeyFromPEM(key []byte) (*ecdsa.PublicKey, error) { + var err error + + // Parse PEM block + var block *pem.Block + if block, _ = pem.Decode(key); block == nil { + return nil, ErrKeyMustBePEMEncoded + } + + // Parse the key + var parsedKey interface{} + if parsedKey, err = x509.ParsePKIXPublicKey(block.Bytes); err != nil { + if cert, err := x509.ParseCertificate(block.Bytes); err == nil { + parsedKey = cert.PublicKey + } else { + return nil, err + } + } + + var pkey *ecdsa.PublicKey + var ok bool + if pkey, ok = parsedKey.(*ecdsa.PublicKey); !ok { + return nil, ErrNotECPublicKey + } + + return pkey, nil +} diff --git a/vendor/github.com/dgrijalva/jwt-go/errors.go b/vendor/github.com/dgrijalva/jwt-go/errors.go new file mode 100644 index 00000000..1c93024a --- /dev/null +++ b/vendor/github.com/dgrijalva/jwt-go/errors.go @@ -0,0 +1,59 @@ +package jwt + +import ( + "errors" +) + +// Error constants +var ( + ErrInvalidKey = errors.New("key is invalid") + ErrInvalidKeyType = errors.New("key is of invalid type") + ErrHashUnavailable = errors.New("the requested hash function is unavailable") +) + +// The errors that might occur when parsing and validating a token +const ( + ValidationErrorMalformed uint32 = 1 << iota // Token is malformed + ValidationErrorUnverifiable // Token could not be verified because of signing problems + ValidationErrorSignatureInvalid // Signature validation failed + + // Standard Claim validation errors + ValidationErrorAudience // AUD validation failed + ValidationErrorExpired // EXP validation failed + ValidationErrorIssuedAt // IAT validation failed + ValidationErrorIssuer // ISS validation failed + ValidationErrorNotValidYet // NBF validation failed + ValidationErrorId // JTI validation failed + ValidationErrorClaimsInvalid // Generic claims validation error +) + +// Helper for constructing a ValidationError with a string error message +func NewValidationError(errorText string, errorFlags uint32) *ValidationError { + return &ValidationError{ + text: errorText, + Errors: errorFlags, + } +} + +// The error from Parse if token is not valid +type ValidationError struct { + Inner error // stores the error returned by external dependencies, i.e.: KeyFunc + Errors uint32 // bitfield. see ValidationError... constants + text string // errors that do not have a valid error just have text +} + +// Validation error is an error type +func (e ValidationError) Error() string { + if e.Inner != nil { + return e.Inner.Error() + } else if e.text != "" { + return e.text + } else { + return "token is invalid" + } +} + +// No errors +func (e *ValidationError) valid() bool { + return e.Errors == 0 +} diff --git a/vendor/github.com/dgrijalva/jwt-go/hmac.go b/vendor/github.com/dgrijalva/jwt-go/hmac.go new file mode 100644 index 00000000..addbe5d4 --- /dev/null +++ b/vendor/github.com/dgrijalva/jwt-go/hmac.go @@ -0,0 +1,95 @@ +package jwt + +import ( + "crypto" + "crypto/hmac" + "errors" +) + +// Implements the HMAC-SHA family of signing methods signing methods +// Expects key type of []byte for both signing and validation +type SigningMethodHMAC struct { + Name string + Hash crypto.Hash +} + +// Specific instances for HS256 and company +var ( + SigningMethodHS256 *SigningMethodHMAC + SigningMethodHS384 *SigningMethodHMAC + SigningMethodHS512 *SigningMethodHMAC + ErrSignatureInvalid = errors.New("signature is invalid") +) + +func init() { + // HS256 + SigningMethodHS256 = &SigningMethodHMAC{"HS256", crypto.SHA256} + RegisterSigningMethod(SigningMethodHS256.Alg(), func() SigningMethod { + return SigningMethodHS256 + }) + + // HS384 + SigningMethodHS384 = &SigningMethodHMAC{"HS384", crypto.SHA384} + RegisterSigningMethod(SigningMethodHS384.Alg(), func() SigningMethod { + return SigningMethodHS384 + }) + + // HS512 + SigningMethodHS512 = &SigningMethodHMAC{"HS512", crypto.SHA512} + RegisterSigningMethod(SigningMethodHS512.Alg(), func() SigningMethod { + return SigningMethodHS512 + }) +} + +func (m *SigningMethodHMAC) Alg() string { + return m.Name +} + +// Verify the signature of HSXXX tokens. Returns nil if the signature is valid. +func (m *SigningMethodHMAC) Verify(signingString, signature string, key interface{}) error { + // Verify the key is the right type + keyBytes, ok := key.([]byte) + if !ok { + return ErrInvalidKeyType + } + + // Decode signature, for comparison + sig, err := DecodeSegment(signature) + if err != nil { + return err + } + + // Can we use the specified hashing method? + if !m.Hash.Available() { + return ErrHashUnavailable + } + + // This signing method is symmetric, so we validate the signature + // by reproducing the signature from the signing string and key, then + // comparing that against the provided signature. + hasher := hmac.New(m.Hash.New, keyBytes) + hasher.Write([]byte(signingString)) + if !hmac.Equal(sig, hasher.Sum(nil)) { + return ErrSignatureInvalid + } + + // No validation errors. Signature is good. + return nil +} + +// Implements the Sign method from SigningMethod for this signing method. +// Key must be []byte +func (m *SigningMethodHMAC) Sign(signingString string, key interface{}) (string, error) { + if keyBytes, ok := key.([]byte); ok { + if !m.Hash.Available() { + return "", ErrHashUnavailable + } + + hasher := hmac.New(m.Hash.New, keyBytes) + hasher.Write([]byte(signingString)) + + return EncodeSegment(hasher.Sum(nil)), nil + } + + return "", ErrInvalidKeyType +} diff --git a/vendor/github.com/dgrijalva/jwt-go/map_claims.go b/vendor/github.com/dgrijalva/jwt-go/map_claims.go new file mode 100644 index 00000000..291213c4 --- /dev/null +++ b/vendor/github.com/dgrijalva/jwt-go/map_claims.go @@ -0,0 +1,94 @@ +package jwt + +import ( + "encoding/json" + "errors" + // "fmt" +) + +// Claims type that uses the map[string]interface{} for JSON decoding +// This is the default claims type if you don't supply one +type MapClaims map[string]interface{} + +// Compares the aud claim against cmp. +// If required is false, this method will return true if the value matches or is unset +func (m MapClaims) VerifyAudience(cmp string, req bool) bool { + aud, _ := m["aud"].(string) + return verifyAud(aud, cmp, req) +} + +// Compares the exp claim against cmp. +// If required is false, this method will return true if the value matches or is unset +func (m MapClaims) VerifyExpiresAt(cmp int64, req bool) bool { + switch exp := m["exp"].(type) { + case float64: + return verifyExp(int64(exp), cmp, req) + case json.Number: + v, _ := exp.Int64() + return verifyExp(v, cmp, req) + } + return req == false +} + +// Compares the iat claim against cmp. +// If required is false, this method will return true if the value matches or is unset +func (m MapClaims) VerifyIssuedAt(cmp int64, req bool) bool { + switch iat := m["iat"].(type) { + case float64: + return verifyIat(int64(iat), cmp, req) + case json.Number: + v, _ := iat.Int64() + return verifyIat(v, cmp, req) + } + return req == false +} + +// Compares the iss claim against cmp. +// If required is false, this method will return true if the value matches or is unset +func (m MapClaims) VerifyIssuer(cmp string, req bool) bool { + iss, _ := m["iss"].(string) + return verifyIss(iss, cmp, req) +} + +// Compares the nbf claim against cmp. +// If required is false, this method will return true if the value matches or is unset +func (m MapClaims) VerifyNotBefore(cmp int64, req bool) bool { + switch nbf := m["nbf"].(type) { + case float64: + return verifyNbf(int64(nbf), cmp, req) + case json.Number: + v, _ := nbf.Int64() + return verifyNbf(v, cmp, req) + } + return req == false +} + +// Validates time based claims "exp, iat, nbf". +// There is no accounting for clock skew. +// As well, if any of the above claims are not in the token, it will still +// be considered a valid claim. +func (m MapClaims) Valid() error { + vErr := new(ValidationError) + now := TimeFunc().Unix() + + if m.VerifyExpiresAt(now, false) == false { + vErr.Inner = errors.New("Token is expired") + vErr.Errors |= ValidationErrorExpired + } + + if m.VerifyIssuedAt(now, false) == false { + vErr.Inner = errors.New("Token used before issued") + vErr.Errors |= ValidationErrorIssuedAt + } + + if m.VerifyNotBefore(now, false) == false { + vErr.Inner = errors.New("Token is not valid yet") + vErr.Errors |= ValidationErrorNotValidYet + } + + if vErr.valid() { + return nil + } + + return vErr +} diff --git a/vendor/github.com/dgrijalva/jwt-go/none.go b/vendor/github.com/dgrijalva/jwt-go/none.go new file mode 100644 index 00000000..f04d189d --- /dev/null +++ b/vendor/github.com/dgrijalva/jwt-go/none.go @@ -0,0 +1,52 @@ +package jwt + +// Implements the none signing method. This is required by the spec +// but you probably should never use it. +var SigningMethodNone *signingMethodNone + +const UnsafeAllowNoneSignatureType unsafeNoneMagicConstant = "none signing method allowed" + +var NoneSignatureTypeDisallowedError error + +type signingMethodNone struct{} +type unsafeNoneMagicConstant string + +func init() { + SigningMethodNone = &signingMethodNone{} + NoneSignatureTypeDisallowedError = NewValidationError("'none' signature type is not allowed", ValidationErrorSignatureInvalid) + + RegisterSigningMethod(SigningMethodNone.Alg(), func() SigningMethod { + return SigningMethodNone + }) +} + +func (m *signingMethodNone) Alg() string { + return "none" +} + +// Only allow 'none' alg type if UnsafeAllowNoneSignatureType is specified as the key +func (m *signingMethodNone) Verify(signingString, signature string, key interface{}) (err error) { + // Key must be UnsafeAllowNoneSignatureType to prevent accidentally + // accepting 'none' signing method + if _, ok := key.(unsafeNoneMagicConstant); !ok { + return NoneSignatureTypeDisallowedError + } + // If signing method is none, signature must be an empty string + if signature != "" { + return NewValidationError( + "'none' signing method with non-empty signature", + ValidationErrorSignatureInvalid, + ) + } + + // Accept 'none' signing method. + return nil +} + +// Only allow 'none' signing if UnsafeAllowNoneSignatureType is specified as the key +func (m *signingMethodNone) Sign(signingString string, key interface{}) (string, error) { + if _, ok := key.(unsafeNoneMagicConstant); ok { + return "", nil + } + return "", NoneSignatureTypeDisallowedError +} diff --git a/vendor/github.com/dgrijalva/jwt-go/parser.go b/vendor/github.com/dgrijalva/jwt-go/parser.go new file mode 100644 index 00000000..d6901d9a --- /dev/null +++ b/vendor/github.com/dgrijalva/jwt-go/parser.go @@ -0,0 +1,148 @@ +package jwt + +import ( + "bytes" + "encoding/json" + "fmt" + "strings" +) + +type Parser struct { + ValidMethods []string // If populated, only these methods will be considered valid + UseJSONNumber bool // Use JSON Number format in JSON decoder + SkipClaimsValidation bool // Skip claims validation during token parsing +} + +// Parse, validate, and return a token. +// keyFunc will receive the parsed token and should return the key for validating. +// If everything is kosher, err will be nil +func (p *Parser) Parse(tokenString string, keyFunc Keyfunc) (*Token, error) { + return p.ParseWithClaims(tokenString, MapClaims{}, keyFunc) +} + +func (p *Parser) ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc) (*Token, error) { + token, parts, err := p.ParseUnverified(tokenString, claims) + if err != nil { + return token, err + } + + // Verify signing method is in the required set + if p.ValidMethods != nil { + var signingMethodValid = false + var alg = token.Method.Alg() + for _, m := range p.ValidMethods { + if m == alg { + signingMethodValid = true + break + } + } + if !signingMethodValid { + // signing method is not in the listed set + return token, NewValidationError(fmt.Sprintf("signing method %v is invalid", alg), ValidationErrorSignatureInvalid) + } + } + + // Lookup key + var key interface{} + if keyFunc == nil { + // keyFunc was not provided. short circuiting validation + return token, NewValidationError("no Keyfunc was provided.", ValidationErrorUnverifiable) + } + if key, err = keyFunc(token); err != nil { + // keyFunc returned an error + if ve, ok := err.(*ValidationError); ok { + return token, ve + } + return token, &ValidationError{Inner: err, Errors: ValidationErrorUnverifiable} + } + + vErr := &ValidationError{} + + // Validate Claims + if !p.SkipClaimsValidation { + if err := token.Claims.Valid(); err != nil { + + // If the Claims Valid returned an error, check if it is a validation error, + // If it was another error type, create a ValidationError with a generic ClaimsInvalid flag set + if e, ok := err.(*ValidationError); !ok { + vErr = &ValidationError{Inner: err, Errors: ValidationErrorClaimsInvalid} + } else { + vErr = e + } + } + } + + // Perform validation + token.Signature = parts[2] + if err = token.Method.Verify(strings.Join(parts[0:2], "."), token.Signature, key); err != nil { + vErr.Inner = err + vErr.Errors |= ValidationErrorSignatureInvalid + } + + if vErr.valid() { + token.Valid = true + return token, nil + } + + return token, vErr +} + +// WARNING: Don't use this method unless you know what you're doing +// +// This method parses the token but doesn't validate the signature. It's only +// ever useful in cases where you know the signature is valid (because it has +// been checked previously in the stack) and you want to extract values from +// it. +func (p *Parser) ParseUnverified(tokenString string, claims Claims) (token *Token, parts []string, err error) { + parts = strings.Split(tokenString, ".") + if len(parts) != 3 { + return nil, parts, NewValidationError("token contains an invalid number of segments", ValidationErrorMalformed) + } + + token = &Token{Raw: tokenString} + + // parse Header + var headerBytes []byte + if headerBytes, err = DecodeSegment(parts[0]); err != nil { + if strings.HasPrefix(strings.ToLower(tokenString), "bearer ") { + return token, parts, NewValidationError("tokenstring should not contain 'bearer '", ValidationErrorMalformed) + } + return token, parts, &ValidationError{Inner: err, Errors: ValidationErrorMalformed} + } + if err = json.Unmarshal(headerBytes, &token.Header); err != nil { + return token, parts, &ValidationError{Inner: err, Errors: ValidationErrorMalformed} + } + + // parse Claims + var claimBytes []byte + token.Claims = claims + + if claimBytes, err = DecodeSegment(parts[1]); err != nil { + return token, parts, &ValidationError{Inner: err, Errors: ValidationErrorMalformed} + } + dec := json.NewDecoder(bytes.NewBuffer(claimBytes)) + if p.UseJSONNumber { + dec.UseNumber() + } + // JSON Decode. Special case for map type to avoid weird pointer behavior + if c, ok := token.Claims.(MapClaims); ok { + err = dec.Decode(&c) + } else { + err = dec.Decode(&claims) + } + // Handle decode error + if err != nil { + return token, parts, &ValidationError{Inner: err, Errors: ValidationErrorMalformed} + } + + // Lookup signature method + if method, ok := token.Header["alg"].(string); ok { + if token.Method = GetSigningMethod(method); token.Method == nil { + return token, parts, NewValidationError("signing method (alg) is unavailable.", ValidationErrorUnverifiable) + } + } else { + return token, parts, NewValidationError("signing method (alg) is unspecified.", ValidationErrorUnverifiable) + } + + return token, parts, nil +} diff --git a/vendor/github.com/dgrijalva/jwt-go/rsa.go b/vendor/github.com/dgrijalva/jwt-go/rsa.go new file mode 100644 index 00000000..e4caf1ca --- /dev/null +++ b/vendor/github.com/dgrijalva/jwt-go/rsa.go @@ -0,0 +1,101 @@ +package jwt + +import ( + "crypto" + "crypto/rand" + "crypto/rsa" +) + +// Implements the RSA family of signing methods signing methods +// Expects *rsa.PrivateKey for signing and *rsa.PublicKey for validation +type SigningMethodRSA struct { + Name string + Hash crypto.Hash +} + +// Specific instances for RS256 and company +var ( + SigningMethodRS256 *SigningMethodRSA + SigningMethodRS384 *SigningMethodRSA + SigningMethodRS512 *SigningMethodRSA +) + +func init() { + // RS256 + SigningMethodRS256 = &SigningMethodRSA{"RS256", crypto.SHA256} + RegisterSigningMethod(SigningMethodRS256.Alg(), func() SigningMethod { + return SigningMethodRS256 + }) + + // RS384 + SigningMethodRS384 = &SigningMethodRSA{"RS384", crypto.SHA384} + RegisterSigningMethod(SigningMethodRS384.Alg(), func() SigningMethod { + return SigningMethodRS384 + }) + + // RS512 + SigningMethodRS512 = &SigningMethodRSA{"RS512", crypto.SHA512} + RegisterSigningMethod(SigningMethodRS512.Alg(), func() SigningMethod { + return SigningMethodRS512 + }) +} + +func (m *SigningMethodRSA) Alg() string { + return m.Name +} + +// Implements the Verify method from SigningMethod +// For this signing method, must be an *rsa.PublicKey structure. +func (m *SigningMethodRSA) Verify(signingString, signature string, key interface{}) error { + var err error + + // Decode the signature + var sig []byte + if sig, err = DecodeSegment(signature); err != nil { + return err + } + + var rsaKey *rsa.PublicKey + var ok bool + + if rsaKey, ok = key.(*rsa.PublicKey); !ok { + return ErrInvalidKeyType + } + + // Create hasher + if !m.Hash.Available() { + return ErrHashUnavailable + } + hasher := m.Hash.New() + hasher.Write([]byte(signingString)) + + // Verify the signature + return rsa.VerifyPKCS1v15(rsaKey, m.Hash, hasher.Sum(nil), sig) +} + +// Implements the Sign method from SigningMethod +// For this signing method, must be an *rsa.PrivateKey structure. +func (m *SigningMethodRSA) Sign(signingString string, key interface{}) (string, error) { + var rsaKey *rsa.PrivateKey + var ok bool + + // Validate type of key + if rsaKey, ok = key.(*rsa.PrivateKey); !ok { + return "", ErrInvalidKey + } + + // Create the hasher + if !m.Hash.Available() { + return "", ErrHashUnavailable + } + + hasher := m.Hash.New() + hasher.Write([]byte(signingString)) + + // Sign the string and return the encoded bytes + if sigBytes, err := rsa.SignPKCS1v15(rand.Reader, rsaKey, m.Hash, hasher.Sum(nil)); err == nil { + return EncodeSegment(sigBytes), nil + } else { + return "", err + } +} diff --git a/vendor/github.com/dgrijalva/jwt-go/rsa_pss.go b/vendor/github.com/dgrijalva/jwt-go/rsa_pss.go new file mode 100644 index 00000000..10ee9db8 --- /dev/null +++ b/vendor/github.com/dgrijalva/jwt-go/rsa_pss.go @@ -0,0 +1,126 @@ +// +build go1.4 + +package jwt + +import ( + "crypto" + "crypto/rand" + "crypto/rsa" +) + +// Implements the RSAPSS family of signing methods signing methods +type SigningMethodRSAPSS struct { + *SigningMethodRSA + Options *rsa.PSSOptions +} + +// Specific instances for RS/PS and company +var ( + SigningMethodPS256 *SigningMethodRSAPSS + SigningMethodPS384 *SigningMethodRSAPSS + SigningMethodPS512 *SigningMethodRSAPSS +) + +func init() { + // PS256 + SigningMethodPS256 = &SigningMethodRSAPSS{ + &SigningMethodRSA{ + Name: "PS256", + Hash: crypto.SHA256, + }, + &rsa.PSSOptions{ + SaltLength: rsa.PSSSaltLengthAuto, + Hash: crypto.SHA256, + }, + } + RegisterSigningMethod(SigningMethodPS256.Alg(), func() SigningMethod { + return SigningMethodPS256 + }) + + // PS384 + SigningMethodPS384 = &SigningMethodRSAPSS{ + &SigningMethodRSA{ + Name: "PS384", + Hash: crypto.SHA384, + }, + &rsa.PSSOptions{ + SaltLength: rsa.PSSSaltLengthAuto, + Hash: crypto.SHA384, + }, + } + RegisterSigningMethod(SigningMethodPS384.Alg(), func() SigningMethod { + return SigningMethodPS384 + }) + + // PS512 + SigningMethodPS512 = &SigningMethodRSAPSS{ + &SigningMethodRSA{ + Name: "PS512", + Hash: crypto.SHA512, + }, + &rsa.PSSOptions{ + SaltLength: rsa.PSSSaltLengthAuto, + Hash: crypto.SHA512, + }, + } + RegisterSigningMethod(SigningMethodPS512.Alg(), func() SigningMethod { + return SigningMethodPS512 + }) +} + +// Implements the Verify method from SigningMethod +// For this verify method, key must be an rsa.PublicKey struct +func (m *SigningMethodRSAPSS) Verify(signingString, signature string, key interface{}) error { + var err error + + // Decode the signature + var sig []byte + if sig, err = DecodeSegment(signature); err != nil { + return err + } + + var rsaKey *rsa.PublicKey + switch k := key.(type) { + case *rsa.PublicKey: + rsaKey = k + default: + return ErrInvalidKey + } + + // Create hasher + if !m.Hash.Available() { + return ErrHashUnavailable + } + hasher := m.Hash.New() + hasher.Write([]byte(signingString)) + + return rsa.VerifyPSS(rsaKey, m.Hash, hasher.Sum(nil), sig, m.Options) +} + +// Implements the Sign method from SigningMethod +// For this signing method, key must be an rsa.PrivateKey struct +func (m *SigningMethodRSAPSS) Sign(signingString string, key interface{}) (string, error) { + var rsaKey *rsa.PrivateKey + + switch k := key.(type) { + case *rsa.PrivateKey: + rsaKey = k + default: + return "", ErrInvalidKeyType + } + + // Create the hasher + if !m.Hash.Available() { + return "", ErrHashUnavailable + } + + hasher := m.Hash.New() + hasher.Write([]byte(signingString)) + + // Sign the string and return the encoded bytes + if sigBytes, err := rsa.SignPSS(rand.Reader, rsaKey, m.Hash, hasher.Sum(nil), m.Options); err == nil { + return EncodeSegment(sigBytes), nil + } else { + return "", err + } +} diff --git a/vendor/github.com/dgrijalva/jwt-go/rsa_utils.go b/vendor/github.com/dgrijalva/jwt-go/rsa_utils.go new file mode 100644 index 00000000..a5ababf9 --- /dev/null +++ b/vendor/github.com/dgrijalva/jwt-go/rsa_utils.go @@ -0,0 +1,101 @@ +package jwt + +import ( + "crypto/rsa" + "crypto/x509" + "encoding/pem" + "errors" +) + +var ( + ErrKeyMustBePEMEncoded = errors.New("Invalid Key: Key must be PEM encoded PKCS1 or PKCS8 private key") + ErrNotRSAPrivateKey = errors.New("Key is not a valid RSA private key") + ErrNotRSAPublicKey = errors.New("Key is not a valid RSA public key") +) + +// Parse PEM encoded PKCS1 or PKCS8 private key +func ParseRSAPrivateKeyFromPEM(key []byte) (*rsa.PrivateKey, error) { + var err error + + // Parse PEM block + var block *pem.Block + if block, _ = pem.Decode(key); block == nil { + return nil, ErrKeyMustBePEMEncoded + } + + var parsedKey interface{} + if parsedKey, err = x509.ParsePKCS1PrivateKey(block.Bytes); err != nil { + if parsedKey, err = x509.ParsePKCS8PrivateKey(block.Bytes); err != nil { + return nil, err + } + } + + var pkey *rsa.PrivateKey + var ok bool + if pkey, ok = parsedKey.(*rsa.PrivateKey); !ok { + return nil, ErrNotRSAPrivateKey + } + + return pkey, nil +} + +// Parse PEM encoded PKCS1 or PKCS8 private key protected with password +func ParseRSAPrivateKeyFromPEMWithPassword(key []byte, password string) (*rsa.PrivateKey, error) { + var err error + + // Parse PEM block + var block *pem.Block + if block, _ = pem.Decode(key); block == nil { + return nil, ErrKeyMustBePEMEncoded + } + + var parsedKey interface{} + + var blockDecrypted []byte + if blockDecrypted, err = x509.DecryptPEMBlock(block, []byte(password)); err != nil { + return nil, err + } + + if parsedKey, err = x509.ParsePKCS1PrivateKey(blockDecrypted); err != nil { + if parsedKey, err = x509.ParsePKCS8PrivateKey(blockDecrypted); err != nil { + return nil, err + } + } + + var pkey *rsa.PrivateKey + var ok bool + if pkey, ok = parsedKey.(*rsa.PrivateKey); !ok { + return nil, ErrNotRSAPrivateKey + } + + return pkey, nil +} + +// Parse PEM encoded PKCS1 or PKCS8 public key +func ParseRSAPublicKeyFromPEM(key []byte) (*rsa.PublicKey, error) { + var err error + + // Parse PEM block + var block *pem.Block + if block, _ = pem.Decode(key); block == nil { + return nil, ErrKeyMustBePEMEncoded + } + + // Parse the key + var parsedKey interface{} + if parsedKey, err = x509.ParsePKIXPublicKey(block.Bytes); err != nil { + if cert, err := x509.ParseCertificate(block.Bytes); err == nil { + parsedKey = cert.PublicKey + } else { + return nil, err + } + } + + var pkey *rsa.PublicKey + var ok bool + if pkey, ok = parsedKey.(*rsa.PublicKey); !ok { + return nil, ErrNotRSAPublicKey + } + + return pkey, nil +} diff --git a/vendor/github.com/dgrijalva/jwt-go/signing_method.go b/vendor/github.com/dgrijalva/jwt-go/signing_method.go new file mode 100644 index 00000000..ed1f212b --- /dev/null +++ b/vendor/github.com/dgrijalva/jwt-go/signing_method.go @@ -0,0 +1,35 @@ +package jwt + +import ( + "sync" +) + +var signingMethods = map[string]func() SigningMethod{} +var signingMethodLock = new(sync.RWMutex) + +// Implement SigningMethod to add new methods for signing or verifying tokens. +type SigningMethod interface { + Verify(signingString, signature string, key interface{}) error // Returns nil if signature is valid + Sign(signingString string, key interface{}) (string, error) // Returns encoded signature or error + Alg() string // returns the alg identifier for this method (example: 'HS256') +} + +// Register the "alg" name and a factory function for signing method. +// This is typically done during init() in the method's implementation +func RegisterSigningMethod(alg string, f func() SigningMethod) { + signingMethodLock.Lock() + defer signingMethodLock.Unlock() + + signingMethods[alg] = f +} + +// Get a signing method from an "alg" string +func GetSigningMethod(alg string) (method SigningMethod) { + signingMethodLock.RLock() + defer signingMethodLock.RUnlock() + + if methodF, ok := signingMethods[alg]; ok { + method = methodF() + } + return +} diff --git a/vendor/github.com/dgrijalva/jwt-go/token.go b/vendor/github.com/dgrijalva/jwt-go/token.go new file mode 100644 index 00000000..d637e086 --- /dev/null +++ b/vendor/github.com/dgrijalva/jwt-go/token.go @@ -0,0 +1,108 @@ +package jwt + +import ( + "encoding/base64" + "encoding/json" + "strings" + "time" +) + +// TimeFunc provides the current time when parsing token to validate "exp" claim (expiration time). +// You can override it to use another time value. This is useful for testing or if your +// server uses a different time zone than your tokens. +var TimeFunc = time.Now + +// Parse methods use this callback function to supply +// the key for verification. The function receives the parsed, +// but unverified Token. This allows you to use properties in the +// Header of the token (such as `kid`) to identify which key to use. +type Keyfunc func(*Token) (interface{}, error) + +// A JWT Token. Different fields will be used depending on whether you're +// creating or parsing/verifying a token. +type Token struct { + Raw string // The raw token. Populated when you Parse a token + Method SigningMethod // The signing method used or to be used + Header map[string]interface{} // The first segment of the token + Claims Claims // The second segment of the token + Signature string // The third segment of the token. Populated when you Parse a token + Valid bool // Is the token valid? Populated when you Parse/Verify a token +} + +// Create a new Token. Takes a signing method +func New(method SigningMethod) *Token { + return NewWithClaims(method, MapClaims{}) +} + +func NewWithClaims(method SigningMethod, claims Claims) *Token { + return &Token{ + Header: map[string]interface{}{ + "typ": "JWT", + "alg": method.Alg(), + }, + Claims: claims, + Method: method, + } +} + +// Get the complete, signed token +func (t *Token) SignedString(key interface{}) (string, error) { + var sig, sstr string + var err error + if sstr, err = t.SigningString(); err != nil { + return "", err + } + if sig, err = t.Method.Sign(sstr, key); err != nil { + return "", err + } + return strings.Join([]string{sstr, sig}, "."), nil +} + +// Generate the signing string. This is the +// most expensive part of the whole deal. Unless you +// need this for something special, just go straight for +// the SignedString. +func (t *Token) SigningString() (string, error) { + var err error + parts := make([]string, 2) + for i, _ := range parts { + var jsonValue []byte + if i == 0 { + if jsonValue, err = json.Marshal(t.Header); err != nil { + return "", err + } + } else { + if jsonValue, err = json.Marshal(t.Claims); err != nil { + return "", err + } + } + + parts[i] = EncodeSegment(jsonValue) + } + return strings.Join(parts, "."), nil +} + +// Parse, validate, and return a token. +// keyFunc will receive the parsed token and should return the key for validating. +// If everything is kosher, err will be nil +func Parse(tokenString string, keyFunc Keyfunc) (*Token, error) { + return new(Parser).Parse(tokenString, keyFunc) +} + +func ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc) (*Token, error) { + return new(Parser).ParseWithClaims(tokenString, claims, keyFunc) +} + +// Encode JWT specific base64url encoding with padding stripped +func EncodeSegment(seg []byte) string { + return strings.TrimRight(base64.URLEncoding.EncodeToString(seg), "=") +} + +// Decode JWT specific base64url encoding with padding stripped +func DecodeSegment(seg string) ([]byte, error) { + if l := len(seg) % 4; l > 0 { + seg += strings.Repeat("=", 4-l) + } + + return base64.URLEncoding.DecodeString(seg) +} From 21e9fcbb1441b756db9e391185bac3737b92662c Mon Sep 17 00:00:00 2001 From: Umputun Date: Mon, 21 May 2018 19:17:44 -0500 Subject: [PATCH 02/20] remove store parameter --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 2ec8d857..2ce0764c 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,6 @@ Remark42 is a self-hosted, lightweight, and simple (yet functional) comment engi | --max-back | MAX_BACKUP_FILES | `10` | no | max backup files to keep | | --max-cache-items | MAX_CACHE_ITEMS | `1000` | no | max number of cached items, 0-unlimited | | --max-cache-value | MAX_CACHE_VALUE | `65536` | no | max size of cached value, o-unlimited | -| --session | SESSION_STORE | `/tmp` | no | path to session store directory | | --secret | SECRET | | no | secret key, required | | --max-comment | MAX_COMMENT_SIZE | 2048 | no | comment's size limit | | --google-cid | REMARK_GOOGLE_CID | | no | Google OAuth client ID | From 1ccbc4175dad080872fc910f952c3e5d12e0f548 Mon Sep 17 00:00:00 2001 From: Umputun Date: Mon, 21 May 2018 19:30:39 -0500 Subject: [PATCH 03/20] lint: convetion warn for time and typo in double []byte --- app/rest/auth/jwt.go | 8 ++++---- app/rest/auth/provider.go | 1 - 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/app/rest/auth/jwt.go b/app/rest/auth/jwt.go index d8231d22..97ea05f4 100644 --- a/app/rest/auth/jwt.go +++ b/app/rest/auth/jwt.go @@ -35,20 +35,20 @@ const xsrfHeaderKey = "X-XSRF-TOKEN" // Set creates jwt cookie with xsrf cookie and put it to ResponseWriter func (j *JWT) Set(w http.ResponseWriter, claims *CustomClaims) error { token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) - tokenString, err := token.SignedString([]byte([]byte(j.secret))) + tokenString, err := token.SignedString([]byte(j.secret)) if err != nil { return errors.Wrap(err, "can't sign jwt token") } - expiration := int(time.Duration(365 * 24 * time.Hour).Seconds()) + cookieExpiration := 365 * 24 * 3600 // 1year jwtCookie := http.Cookie{Name: jwtCookieName, Value: tokenString, HttpOnly: true, Path: "/", - MaxAge: expiration, Secure: j.secureCookies} + MaxAge: cookieExpiration, Secure: j.secureCookies} http.SetCookie(w, &jwtCookie) jti := claims.Id xsrfCookie := http.Cookie{Name: xsrfCookieName, Value: jti, HttpOnly: false, Path: "/", - MaxAge: expiration, Secure: j.secureCookies} + MaxAge: cookieExpiration, Secure: j.secureCookies} http.SetCookie(w, &xsrfCookie) return nil diff --git a/app/rest/auth/provider.go b/app/rest/auth/provider.go index 8fb34ea7..9f20cd10 100644 --- a/app/rest/auth/provider.go +++ b/app/rest/auth/provider.go @@ -176,7 +176,6 @@ func (p Provider) authHandler(w http.ResponseWriter, r *http.Request) { Issuer: "remark42", Id: p.randToken(), ExpiresAt: time.Now().Add(7 * 24 * time.Hour).Unix(), - NotBefore: time.Now().Add(-1 * time.Minute).Unix(), }, } From 27ab7455212c3056e02f8151c0ec610f302215f0 Mon Sep 17 00:00:00 2001 From: Umputun Date: Mon, 21 May 2018 22:59:01 -0500 Subject: [PATCH 04/20] add jwt refresh --- app/main.go | 8 ++++-- app/rest/auth/auth.go | 5 +++- app/rest/auth/auth_test.go | 7 ++--- app/rest/auth/jwt.go | 52 +++++++++++++++++++++++----------- app/rest/auth/jwt_test.go | 51 ++++++++++++++++++++++++++++----- app/rest/auth/provider.go | 14 ++++----- app/rest/auth/provider_test.go | 3 +- 7 files changed, 98 insertions(+), 42 deletions(-) diff --git a/app/main.go b/app/main.go index 784426df..df459943 100644 --- a/app/main.go +++ b/app/main.go @@ -106,6 +106,8 @@ func main() { RemarkURL: strings.TrimSuffix(opts.RemarkURL, "/"), } + jwtService := auth.NewJWT(opts.SecretKey, strings.HasPrefix(opts.RemarkURL, "https://"), time.Duration(7*24*time.Hour)) + srv := api.Rest{ Version: revision, DataService: dataService, @@ -113,8 +115,9 @@ func main() { WebRoot: opts.WebRoot, ImageProxy: proxy.Image{Enabled: opts.ImageProxy, RoutePath: "/api/v1/img", RemarkURL: opts.RemarkURL}, Authenticator: auth.Authenticator{ + JWTService: jwtService, Admins: opts.Admins, - Providers: makeAuthProviders(avatarProxy), + Providers: makeAuthProviders(jwtService, avatarProxy), AvatarProxy: avatarProxy, DevPasswd: opts.DevPasswd, }, @@ -180,10 +183,11 @@ func makeDirs(dirs ...string) error { return nil } -func makeAuthProviders(avatarProxy *proxy.Avatar) (providers []auth.Provider) { +func makeAuthProviders(jwtService *auth.JWT, avatarProxy *proxy.Avatar) (providers []auth.Provider) { makeParams := func(cid, secret string) auth.Params { return auth.Params{ + JwtService: jwtService, AvatarProxy: avatarProxy, RemarkURL: opts.RemarkURL, Cid: cid, diff --git a/app/rest/auth/auth.go b/app/rest/auth/auth.go index e86ec5b9..aac4594a 100644 --- a/app/rest/auth/auth.go +++ b/app/rest/auth/auth.go @@ -14,11 +14,11 @@ import ( // Authenticator is top level auth object providing middlewares type Authenticator struct { + JWTService *JWT AvatarProxy *proxy.Avatar Admins []string Providers []Provider DevPasswd string - JWTService JWT } var devUser = store.User{ @@ -65,6 +65,9 @@ func (a *Authenticator) Auth(reqAuth bool) func(http.Handler) http.Handler { user.Admin = true break } + if _, err := a.JWTService.Refresh(w, r); err != nil { + log.Printf("[WARN] can't refresh jwt, %s", err) + } } r = rest.SetUserInfo(r, user) diff --git a/app/rest/auth/auth_test.go b/app/rest/auth/auth_test.go index a07df6b5..b6df5ac2 100644 --- a/app/rest/auth/auth_test.go +++ b/app/rest/auth/auth_test.go @@ -9,13 +9,12 @@ import ( "time" "github.com/go-chi/chi" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestAuthJWTCookie(t *testing.T) { - a := Authenticator{DevPasswd: "123456", JWTService: JWT{secret: "xyz 12345", secureCookies: false}} + a := Authenticator{DevPasswd: "123456", JWTService: NewJWT("xyz 12345", false, time.Hour)} router := chi.NewRouter() router.With(a.Auth(true)).Get("/auth", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(201) @@ -52,8 +51,7 @@ func TestAuthJWTCookie(t *testing.T) { } func TestAuthJWTHeader(t *testing.T) { - a := Authenticator{DevPasswd: "123456", JWTService: JWT{secret: "xyz 12345", secureCookies: false}} - + a := Authenticator{DevPasswd: "123456", JWTService: NewJWT("xyz 12345", false, time.Hour)} router := chi.NewRouter() router.With(a.Auth(true)).Get("/auth", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(201) @@ -79,7 +77,6 @@ func TestAuthJWTHeader(t *testing.T) { assert.Equal(t, 401, resp.StatusCode, "invalid auth token") } func TestAuthRequired(t *testing.T) { - a := Authenticator{DevPasswd: "123456"} router := chi.NewRouter() router.With(a.Auth(true)).Get("/auth", func(w http.ResponseWriter, r *http.Request) { diff --git a/app/rest/auth/jwt.go b/app/rest/auth/jwt.go index 97ea05f4..73525435 100644 --- a/app/rest/auth/jwt.go +++ b/app/rest/auth/jwt.go @@ -16,15 +16,15 @@ import ( type JWT struct { secret string secureCookies bool + exp time.Duration } // CustomClaims stores user info for auth and state & from from login type CustomClaims struct { jwt.StandardClaims - User *store.User `json:"user,omitempty"` - - State string `json:"state,omitempty"` - From string `json:"from,omitempty"` + User *store.User `json:"user,omitempty"` + State string `json:"state,omitempty"` + From string `json:"from,omitempty"` } const jwtCookieName = "JWT" @@ -32,15 +32,29 @@ const jwtHeaderKey = "X-JWT" const xsrfCookieName = "XSRF-TOKEN" const xsrfHeaderKey = "X-XSRF-TOKEN" +// NewJWT makes JWT service +func NewJWT(secret string, secureCookies bool, exp time.Duration) *JWT { + res := JWT{ + secret: secret, + secureCookies: secureCookies, + exp: exp, + } + return &res +} + // Set creates jwt cookie with xsrf cookie and put it to ResponseWriter +// accepts claims and sets expiration func (j *JWT) Set(w http.ResponseWriter, claims *CustomClaims) error { + if claims.ExpiresAt == 0 { + claims.ExpiresAt = time.Now().Add(j.exp).Unix() + } token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) tokenString, err := token.SignedString([]byte(j.secret)) if err != nil { return errors.Wrap(err, "can't sign jwt token") } - cookieExpiration := 365 * 24 * 3600 // 1year + cookieExpiration := 365 * 24 * 3600 // 1 year jwtCookie := http.Cookie{Name: jwtCookieName, Value: tokenString, HttpOnly: true, Path: "/", MaxAge: cookieExpiration, Secure: j.secureCookies} @@ -100,6 +114,22 @@ func (j *JWT) Get(r *http.Request) (*CustomClaims, error) { return claims, nil } +// Refresh gets jwt from request, checks if it will be expiring soon and create new onw +func (j *JWT) Refresh(w http.ResponseWriter, r *http.Request) (*CustomClaims, error) { + claims, err := j.Get(r) + if err != nil { + return nil, err + } + untilExp := time.Unix(claims.ExpiresAt, 0).Sub(time.Now()).Seconds() + log.Print(untilExp) + if untilExp < j.exp.Seconds()/2 { + claims.ExpiresAt = time.Now().Add(j.exp).Unix() + e := j.Set(w, claims) + return claims, e + } + return claims, nil +} + // Reset token's cookies func (j *JWT) Reset(w http.ResponseWriter) { jwtCookie := http.Cookie{Name: jwtCookieName, Value: "", HttpOnly: false, Path: "/", @@ -110,15 +140,3 @@ func (j *JWT) Reset(w http.ResponseWriter) { MaxAge: -1, Expires: time.Unix(0, 0), Secure: true} http.SetCookie(w, &xsrfCookie) } - -func (j *JWT) verify(claims CustomClaims) error { - - if time.Now().Unix() > claims.ExpiresAt { - return errors.Errorf("token exp failed %d:%d", claims.ExpiresAt, time.Now().Unix()) - } - - if time.Now().Unix() < claims.NotBefore { - return errors.Errorf("token nbf failed %d:%d", claims.NotBefore, time.Now().Unix()) - } - return nil -} diff --git a/app/rest/auth/jwt_test.go b/app/rest/auth/jwt_test.go index ca62b4ce..3cdf00ae 100644 --- a/app/rest/auth/jwt_test.go +++ b/app/rest/auth/jwt_test.go @@ -7,12 +7,11 @@ import ( "testing" "time" + jwt "github.com/dgrijalva/jwt-go" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/stretchr/testify/assert" "github.com/umputun/remark/app/store" - - jwt "github.com/dgrijalva/jwt-go" ) var testJwtValid = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjI3ODkxOTE4MjIsImp0aSI6InJhbmRvbSBpZCI" + @@ -23,7 +22,7 @@ var testJwtExpired = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MjY4ODc4M "sImFkbWluIjpmYWxzZX0sInN0YXRlIjoiMTIzNDU2IiwiZnJvbSI6ImZyb20ifQ.4_dCrY9ihyfZIedz-kZwBTxmxU1a52V7IqeJrOqTzE4" func TestJWT_Set(t *testing.T) { - j := JWT{secret: "xyz 12345"} + j := NewJWT("xyz 12345", false, time.Hour) claims := &CustomClaims{ State: "123456", @@ -54,7 +53,7 @@ func TestJWT_Set(t *testing.T) { } func TestJWT_GetFromHeader(t *testing.T) { - j := JWT{secret: "xyz 12345"} + j := NewJWT("xyz 12345", false, time.Hour) req := httptest.NewRequest("GET", "/", nil) req.Header.Add(jwtHeaderKey, testJwtValid) @@ -78,7 +77,7 @@ func TestJWT_GetFromHeader(t *testing.T) { } func TestJWT_SetAndGetWithCookies(t *testing.T) { - j := JWT{secret: "xyz 12345"} + j := NewJWT("xyz 12345", false, time.Hour) claims := &CustomClaims{ State: "123456", @@ -117,7 +116,7 @@ func TestJWT_SetAndGetWithCookies(t *testing.T) { } func TestJWT_SetAndGetWithCookiesExpired(t *testing.T) { - j := JWT{secret: "xyz 12345"} + j := NewJWT("xyz 12345", false, time.Hour) claims := &CustomClaims{ State: "123456", @@ -153,3 +152,41 @@ func TestJWT_SetAndGetWithCookiesExpired(t *testing.T) { assert.NotNil(t, err) assert.True(t, strings.HasPrefix(err.Error(), "can't parse jwt: token is expired by"), err.Error()) } + +func TestJWT_Refresh(t *testing.T) { + j := NewJWT("xyz 12345", false, 2*time.Second) + + claims := &CustomClaims{ + State: "123456", + From: "from", + User: &store.User{ + ID: "id1", + Name: "name1", + }, + StandardClaims: jwt.StandardClaims{ + Id: "random id", + Issuer: "remark42", + }, + } + // set token + rr := httptest.NewRecorder() + err := j.Set(rr, claims) + assert.Nil(t, err) + cookies := rr.Result().Cookies() + require.Equal(t, 2, len(cookies)) + + req, err := http.NewRequest("GET", "http://example.com/blah", nil) + require.Nil(t, err) + req.AddCookie(cookies[0]) + req.Header.Add(xsrfHeaderKey, "random id") + + claims2, err := j.Refresh(rr, req) + require.Nil(t, err) + assert.Equal(t, claims.ExpiresAt, claims2.ExpiresAt, "no refresh yet") + + time.Sleep(1 * time.Second) + claims2, err = j.Refresh(rr, req) + assert.Nil(t, err) + assert.True(t, claims.ExpiresAt < claims2.ExpiresAt, "refreshed") + t.Log(claims.ExpiresAt, claims2.ExpiresAt) +} diff --git a/app/rest/auth/provider.go b/app/rest/auth/provider.go index 9f20cd10..b620a8f7 100644 --- a/app/rest/auth/provider.go +++ b/app/rest/auth/provider.go @@ -10,7 +10,6 @@ import ( "io/ioutil" "log" "net/http" - "strings" "time" jwt "github.com/dgrijalva/jwt-go" @@ -44,6 +43,7 @@ type Params struct { Csecret string RemarkURL string AvatarProxy *proxy.Avatar + JwtService *JWT } type userData map[string]interface{} @@ -69,8 +69,7 @@ func initProvider(p Params, provider Provider) Provider { provider.conf = &conf provider.avatarProxy = p.AvatarProxy - provider.jwtService = &JWT{secret: provider.Secret, secureCookies: strings.HasPrefix(p.RemarkURL, "https://")} - + provider.jwtService = p.JwtService return provider } @@ -89,7 +88,7 @@ func (p Provider) loginHandler(w http.ResponseWriter, r *http.Request) { // make state (random) and store in session state := p.randToken() - claims := &CustomClaims{ + claims := CustomClaims{ State: state, From: r.URL.Query().Get("from"), StandardClaims: jwt.StandardClaims{ @@ -100,7 +99,7 @@ func (p Provider) loginHandler(w http.ResponseWriter, r *http.Request) { }, } - if err := p.jwtService.Set(w, claims); err != nil { + if err := p.jwtService.Set(w, &claims); err != nil { rest.SendErrorJSON(w, r, http.StatusInternalServerError, err, "failed to set jwt") return } @@ -173,9 +172,8 @@ func (p Provider) authHandler(w http.ResponseWriter, r *http.Request) { authClaims := &CustomClaims{ User: &u, StandardClaims: jwt.StandardClaims{ - Issuer: "remark42", - Id: p.randToken(), - ExpiresAt: time.Now().Add(7 * 24 * time.Hour).Unix(), + Issuer: "remark42", + Id: p.randToken(), }, } diff --git a/app/rest/auth/provider_test.go b/app/rest/auth/provider_test.go index 31e5665c..e6f6dcd6 100644 --- a/app/rest/auth/provider_test.go +++ b/app/rest/auth/provider_test.go @@ -95,10 +95,9 @@ func mockProvider(t *testing.T, loginPort, authPort int) (provider Provider, ts } return userInfo }, - jwtService: &JWT{secret: "12345", secureCookies: false}, } - provider = initProvider(Params{Cid: "cid", Csecret: "csecret"}, provider) + provider = initProvider(Params{Cid: "cid", Csecret: "csecret", JwtService: NewJWT("12345", false, time.Hour)}, provider) ts = &http.Server{Addr: fmt.Sprintf(":%d", loginPort), Handler: provider.Routes()} From 6ab30fc9b66180ace12ecce9369176f0f7e09437 Mon Sep 17 00:00:00 2001 From: Umputun Date: Mon, 21 May 2018 23:06:35 -0500 Subject: [PATCH 05/20] lint: duration conv --- app/main.go | 2 +- app/rest/auth/jwt.go | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/app/main.go b/app/main.go index df459943..3b86d3d9 100644 --- a/app/main.go +++ b/app/main.go @@ -106,7 +106,7 @@ func main() { RemarkURL: strings.TrimSuffix(opts.RemarkURL, "/"), } - jwtService := auth.NewJWT(opts.SecretKey, strings.HasPrefix(opts.RemarkURL, "https://"), time.Duration(7*24*time.Hour)) + jwtService := auth.NewJWT(opts.SecretKey, strings.HasPrefix(opts.RemarkURL, "https://"), 7*24*time.Hour) srv := api.Rest{ Version: revision, diff --git a/app/rest/auth/jwt.go b/app/rest/auth/jwt.go index 73525435..dc5163d4 100644 --- a/app/rest/auth/jwt.go +++ b/app/rest/auth/jwt.go @@ -120,9 +120,8 @@ func (j *JWT) Refresh(w http.ResponseWriter, r *http.Request) (*CustomClaims, er if err != nil { return nil, err } - untilExp := time.Unix(claims.ExpiresAt, 0).Sub(time.Now()).Seconds() - log.Print(untilExp) - if untilExp < j.exp.Seconds()/2 { + untilExp := claims.ExpiresAt - time.Now().Unix() + if untilExp <= int64(j.exp.Seconds()/2) { claims.ExpiresAt = time.Now().Add(j.exp).Unix() e := j.Set(w, claims) return claims, e From cb93aab6641f8a38bf42c260fd0ca3c425ec05df Mon Sep 17 00:00:00 2001 From: Umputun Date: Mon, 21 May 2018 23:47:58 -0500 Subject: [PATCH 06/20] fix protocol mismatch for jwt logout --- app/rest/auth/jwt.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/rest/auth/jwt.go b/app/rest/auth/jwt.go index dc5163d4..63c6d482 100644 --- a/app/rest/auth/jwt.go +++ b/app/rest/auth/jwt.go @@ -132,10 +132,10 @@ func (j *JWT) Refresh(w http.ResponseWriter, r *http.Request) (*CustomClaims, er // Reset token's cookies func (j *JWT) Reset(w http.ResponseWriter) { jwtCookie := http.Cookie{Name: jwtCookieName, Value: "", HttpOnly: false, Path: "/", - MaxAge: -1, Expires: time.Unix(0, 0), Secure: true} + MaxAge: -1, Expires: time.Unix(0, 0), Secure: j.secureCookies} http.SetCookie(w, &jwtCookie) xsrfCookie := http.Cookie{Name: xsrfCookieName, Value: "", HttpOnly: false, Path: "/", - MaxAge: -1, Expires: time.Unix(0, 0), Secure: true} + MaxAge: -1, Expires: time.Unix(0, 0), Secure: j.secureCookies} http.SetCookie(w, &xsrfCookie) } From 189b74130106529fd153e977b2e466e2aef25d8e Mon Sep 17 00:00:00 2001 From: Umputun Date: Mon, 21 May 2018 23:54:26 -0500 Subject: [PATCH 07/20] hide warn with xsrf values --- app/rest/auth/jwt.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/rest/auth/jwt.go b/app/rest/auth/jwt.go index 63c6d482..25da24ed 100644 --- a/app/rest/auth/jwt.go +++ b/app/rest/auth/jwt.go @@ -1,7 +1,6 @@ package auth import ( - "log" "net/http" "time" @@ -22,9 +21,11 @@ type JWT struct { // CustomClaims stores user info for auth and state & from from login type CustomClaims struct { jwt.StandardClaims - User *store.User `json:"user,omitempty"` - State string `json:"state,omitempty"` - From string `json:"from,omitempty"` + User *store.User `json:"user,omitempty"` + + // state and from used for oauth handshake + State string `json:"state,omitempty"` + From string `json:"from,omitempty"` } const jwtCookieName = "JWT" @@ -43,7 +44,7 @@ func NewJWT(secret string, secureCookies bool, exp time.Duration) *JWT { } // Set creates jwt cookie with xsrf cookie and put it to ResponseWriter -// accepts claims and sets expiration +// accepts claims and sets expiration if none defined func (j *JWT) Set(w http.ResponseWriter, claims *CustomClaims) error { if claims.ExpiresAt == 0 { claims.ExpiresAt = time.Now().Add(j.exp).Unix() @@ -106,7 +107,6 @@ func (j *JWT) Get(r *http.Request) (*CustomClaims, error) { if fromCookie && claims.User != nil { xsrf := r.Header.Get(xsrfHeaderKey) if claims.Id != xsrf { - log.Printf("[WARN] xsrf not matched jti, %s != %s", xsrf, claims.Id) return nil, errors.New("xsrf mismatch") } } From d7fc68d721d418053deba8ab17dff845413735d9 Mon Sep 17 00:00:00 2001 From: Umputun Date: Tue, 22 May 2018 00:03:03 -0500 Subject: [PATCH 08/20] fix to many jwt refreshes --- app/rest/auth/auth.go | 12 ++++++------ app/rest/auth/jwt.go | 7 +++---- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/app/rest/auth/auth.go b/app/rest/auth/auth.go index aac4594a..05e559f2 100644 --- a/app/rest/auth/auth.go +++ b/app/rest/auth/auth.go @@ -48,7 +48,7 @@ func (a *Authenticator) Auth(reqAuth bool) func(http.Handler) http.Handler { return } - if err != nil { // in anonymous mode just pass it to next handler + if err != nil { // in anonymous mode just pass it to the next handler h.ServeHTTP(w, r) return } @@ -58,18 +58,18 @@ func (a *Authenticator) Auth(reqAuth bool) func(http.Handler) http.Handler { return } - if claims.User != nil { // if uinfo in session, populate to context + if claims.User != nil { // if uinfo in token populate it to context user := *claims.User for _, admin := range a.Admins { if admin == user.ID { user.Admin = true break } - if _, err := a.JWTService.Refresh(w, r); err != nil { - log.Printf("[WARN] can't refresh jwt, %s", err) - } } - + // refresh token if it close to expiration + if _, err := a.JWTService.Refresh(w, r); err != nil { + log.Printf("[WARN] can't refresh jwt, %s", err) + } r = rest.SetUserInfo(r, user) } h.ServeHTTP(w, r) diff --git a/app/rest/auth/jwt.go b/app/rest/auth/jwt.go index 25da24ed..3daaa7f6 100644 --- a/app/rest/auth/jwt.go +++ b/app/rest/auth/jwt.go @@ -61,8 +61,7 @@ func (j *JWT) Set(w http.ResponseWriter, claims *CustomClaims) error { MaxAge: cookieExpiration, Secure: j.secureCookies} http.SetCookie(w, &jwtCookie) - jti := claims.Id - xsrfCookie := http.Cookie{Name: xsrfCookieName, Value: jti, HttpOnly: false, Path: "/", + xsrfCookie := http.Cookie{Name: xsrfCookieName, Value: claims.Id, HttpOnly: false, Path: "/", MaxAge: cookieExpiration, Secure: j.secureCookies} http.SetCookie(w, &xsrfCookie) @@ -70,7 +69,7 @@ func (j *JWT) Set(w http.ResponseWriter, claims *CustomClaims) error { } // Get jwt from header or cookie -// if cookie used verify xsrf token to match +// if cookie used, verify xsrf token to match func (j *JWT) Get(r *http.Request) (*CustomClaims, error) { fromCookie := false @@ -114,7 +113,7 @@ func (j *JWT) Get(r *http.Request) (*CustomClaims, error) { return claims, nil } -// Refresh gets jwt from request, checks if it will be expiring soon and create new onw +// Refresh gets jwt from request, checks if it will be expiring soon (1/2 of expiration) and create the new onw func (j *JWT) Refresh(w http.ResponseWriter, r *http.Request) (*CustomClaims, error) { claims, err := j.Get(r) if err != nil { From bc92154e581f61406a5f19195b08b418b70340ec Mon Sep 17 00:00:00 2001 From: Umputun Date: Tue, 22 May 2018 00:55:52 -0500 Subject: [PATCH 09/20] skip img prox if not hosted on https --- README.md | 2 +- app/rest/auth/provider.go | 5 ----- app/rest/proxy/image.go | 5 ++++- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 2ce0764c..e85d68f2 100644 --- a/README.md +++ b/README.md @@ -366,7 +366,7 @@ _all admin calls require auth and admin privilege_ * Each site stored in a separate boltbd file. * In order to migrate/move remark42 to another host boltbd files should be transferred. * Automatic backup process runs every 24h and exports all content in json-like format to `backup-remark-YYYYMMDD.gz`. -* Sessions implemented with [gorilla/sessions](https://github.com/gorilla/sessions) and file-system store under `SESSION_STORE` path. It uses HttpOnly, secure cookies. +* Authentication implemented with [jwt](https://github.com/dgrijalva/jwt-go) stored in a cookie. It uses HttpOnly, secure cookies. * All heavy REST calls cached internally, default expiration 4h * User's activity throttled globally (up to 1000 simultaneous requests) and limited locally (per user, up to 10 req/sec) * Request timeout set to 60sec diff --git a/app/rest/auth/provider.go b/app/rest/auth/provider.go index b620a8f7..2a8f69fc 100644 --- a/app/rest/auth/provider.go +++ b/app/rest/auth/provider.go @@ -4,7 +4,6 @@ import ( "context" "crypto/rand" "crypto/sha1" - "encoding/gob" "encoding/json" "fmt" "io/ioutil" @@ -209,7 +208,3 @@ func (p Provider) randToken() string { } return fmt.Sprintf("%x", s.Sum(nil)) } - -func init() { - gob.Register(store.User{}) -} diff --git a/app/rest/proxy/image.go b/app/rest/proxy/image.go index e668420b..3b211aa4 100644 --- a/app/rest/proxy/image.go +++ b/app/rest/proxy/image.go @@ -24,9 +24,10 @@ type Image struct { // Convert all img src links without https to proxied links func (p Image) Convert(commentHTML string) string { - if !p.Enabled { + if !p.Enabled || strings.HasPrefix(p.RemarkURL, "http://") { return commentHTML } + imgs, err := p.extract(commentHTML) if err != nil { return commentHTML @@ -108,10 +109,12 @@ func (p Image) extract(commentHTML string) ([]string, error) { // replace img links in commentHTML with route to proxy with base64 encoded original link func (p Image) replace(commentHTML string, imgs []string) string { + for _, img := range imgs { encodedImgURL := base64.URLEncoding.EncodeToString([]byte(img)) resImgURL := p.RemarkURL + p.RoutePath + "?src=" + encodedImgURL commentHTML = strings.Replace(commentHTML, img, resImgURL, -1) } + return commentHTML } From 6158653164d4607a8683eda804384d5f7f529c29 Mon Sep 17 00:00:00 2001 From: Umputun Date: Tue, 22 May 2018 01:10:14 -0500 Subject: [PATCH 10/20] trying to add coveralls --- .drone.yml | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/.drone.yml b/.drone.yml index f4c4bc06..c6946280 100644 --- a/.drone.yml +++ b/.drone.yml @@ -9,8 +9,11 @@ pipeline: commands: - cd app - go build -v ./... - - docker-master: + - go get golang.org/x/tools/cmd/cover + - go get github.com/mattn/goveralls + - go test -v -covermode=count -coverprofile=coverage.out $HOME/gopath/bin/goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN + + docker_master: image: plugins/docker repo: umputun/remark secrets: [ docker_username, docker_password ] @@ -21,7 +24,7 @@ pipeline: branch: [master, release/*] event: push - docker-branch: + docker_branch: image: plugins/docker repo: umputun/remark secrets: [ docker_username, docker_password ] @@ -32,7 +35,7 @@ pipeline: exclude: [master, release/*] event: push - docker-pullrequest: + docker_pullrequest: image: docker commands: - docker build . From 84ce31f18c2bef78ca0a7e9c57e0c0584ea92a7e Mon Sep 17 00:00:00 2001 From: Umputun Date: Tue, 22 May 2018 01:15:24 -0500 Subject: [PATCH 11/20] add git to drone build --- .drone.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.drone.yml b/.drone.yml index c6946280..52c27717 100644 --- a/.drone.yml +++ b/.drone.yml @@ -5,14 +5,15 @@ workspace: pipeline: build: - image: golang:1.9-alpine + image: golang:1.10-alpine commands: + - apk add --no-cache git - cd app - go build -v ./... - go get golang.org/x/tools/cmd/cover - go get github.com/mattn/goveralls - go test -v -covermode=count -coverprofile=coverage.out $HOME/gopath/bin/goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN - + docker_master: image: plugins/docker repo: umputun/remark From e42a1658f003324eb648adc34ab3b5c7a55edaea Mon Sep 17 00:00:00 2001 From: Umputun Date: Tue, 22 May 2018 01:18:05 -0500 Subject: [PATCH 12/20] fix goverlalls command --- .drone.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index 52c27717..a90b6b82 100644 --- a/.drone.yml +++ b/.drone.yml @@ -12,7 +12,8 @@ pipeline: - go build -v ./... - go get golang.org/x/tools/cmd/cover - go get github.com/mattn/goveralls - - go test -v -covermode=count -coverprofile=coverage.out $HOME/gopath/bin/goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN + - go test -v -covermode=count -coverprofile=coverage.out + - $HOME/gopath/bin/goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN docker_master: image: plugins/docker From 120b39c60e42c9b4a628694c02e6a95bb2f165bb Mon Sep 17 00:00:00 2001 From: Umputun Date: Tue, 22 May 2018 01:19:28 -0500 Subject: [PATCH 13/20] to base ws path --- .drone.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index a90b6b82..f1a8da63 100644 --- a/.drone.yml +++ b/.drone.yml @@ -13,7 +13,7 @@ pipeline: - go get golang.org/x/tools/cmd/cover - go get github.com/mattn/goveralls - go test -v -covermode=count -coverprofile=coverage.out - - $HOME/gopath/bin/goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN + - /go/bin/goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN docker_master: image: plugins/docker From 7d06f1b1fef029b239f2a42c1495cd7fe1422855 Mon Sep 17 00:00:00 2001 From: Umputun Date: Tue, 22 May 2018 01:31:51 -0500 Subject: [PATCH 14/20] revert coverall experiment --- .drone.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.drone.yml b/.drone.yml index f1a8da63..a0d6b306 100644 --- a/.drone.yml +++ b/.drone.yml @@ -10,10 +10,10 @@ pipeline: - apk add --no-cache git - cd app - go build -v ./... - - go get golang.org/x/tools/cmd/cover - - go get github.com/mattn/goveralls - - go test -v -covermode=count -coverprofile=coverage.out - - /go/bin/goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN + # - go get golang.org/x/tools/cmd/cover + # - go get github.com/mattn/goveralls + # - go test -v -covermode=count -coverprofile=coverage.out + # - /go/bin/goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN docker_master: image: plugins/docker From 282f61f6aa8f1bb0aeac366d31215cde9b0b1cb8 Mon Sep 17 00:00:00 2001 From: Umputun Date: Tue, 22 May 2018 01:32:26 -0500 Subject: [PATCH 15/20] remove git from build --- .drone.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index a0d6b306..b25f1b48 100644 --- a/.drone.yml +++ b/.drone.yml @@ -7,9 +7,9 @@ pipeline: build: image: golang:1.10-alpine commands: - - apk add --no-cache git - cd app - go build -v ./... + # - apk add --no-cache git # - go get golang.org/x/tools/cmd/cover # - go get github.com/mattn/goveralls # - go test -v -covermode=count -coverprofile=coverage.out From 786d16db56dd35ad1bcc8d86c738d19f57979921 Mon Sep 17 00:00:00 2001 From: Umputun Date: Tue, 22 May 2018 01:51:27 -0500 Subject: [PATCH 16/20] trying goveralls with travis --- .travis.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.travis.yml b/.travis.yml index 6c1d35df..aacc8678 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,16 @@ +language: go + +before_install: + - go get golang.org/x/tools/cmd/cover + - go get github.com/mattn/goveralls + install: - docker --version - docker-compose --version script: - docker build . + +after_success: + - go test -v -covermode=count -coverprofile=coverage.out + - goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN From 0c42019ebd9477b32618aef395692ad9ed89c221 Mon Sep 17 00:00:00 2001 From: Umputun Date: Tue, 22 May 2018 01:56:38 -0500 Subject: [PATCH 17/20] rung goveral from app --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index aacc8678..3b37525b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,5 +12,6 @@ script: - docker build . after_success: + - cd app - go test -v -covermode=count -coverprofile=coverage.out - goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN From 10be03a0beb3a956bdcf92b2890b8ff22eac027a Mon Sep 17 00:00:00 2001 From: Umputun Date: Tue, 22 May 2018 02:03:06 -0500 Subject: [PATCH 18/20] test all packages --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 3b37525b..6202ef22 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,5 +13,5 @@ script: after_success: - cd app - - go test -v -covermode=count -coverprofile=coverage.out + - go test $(go list -e ./... | grep -v vendor) -covermode=count -coverprofile=coverage.out - goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN From b325692279f72c4d80e9b3757775149bfb5c7eed Mon Sep 17 00:00:00 2001 From: Umputun Date: Tue, 22 May 2018 02:13:29 -0500 Subject: [PATCH 19/20] go v1.10 for travis --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 6202ef22..d3013444 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,8 @@ language: go +go: + - "1.10.x" + before_install: - go get golang.org/x/tools/cmd/cover - go get github.com/mattn/goveralls @@ -12,6 +15,5 @@ script: - docker build . after_success: - - cd app - go test $(go list -e ./... | grep -v vendor) -covermode=count -coverprofile=coverage.out - goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN From d7874ad9157ed6c60f5472be8054ac2d514e6090 Mon Sep 17 00:00:00 2001 From: Umputun Date: Tue, 22 May 2018 02:21:26 -0500 Subject: [PATCH 20/20] coverage badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e85d68f2..cf6c3f91 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# remark42 [![Build Status](https://travis-ci.org/umputun/remark.svg?branch=master)](https://travis-ci.org/umputun/remark) [![Go Report Card](https://goreportcard.com/badge/github.com/umputun/remark)](https://goreportcard.com/report/github.com/umputun/remark) +# remark42 [![Build Status](https://travis-ci.org/umputun/remark.svg?branch=master)](https://travis-ci.org/umputun/remark) [![Go Report Card](https://goreportcard.com/badge/github.com/umputun/remark)](https://goreportcard.com/report/github.com/umputun/remark) [![Coverage Status](https://coveralls.io/repos/github/umputun/remark/badge.svg?branch=develop)](https://coveralls.io/github/umputun/remark?branch=develop) Remark42 is a self-hosted, lightweight, and simple (yet functional) comment engine, which doesn't spy on users. It can be embedded into blogs, articles or any other place where readers add comments.