Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

enhancement: add expiry date to jwt tokens (#418)

Guilherme Oenning642725c13e06
diff --git a/app/actions/actions_test.go b/app/actions/actions_test.go
index 8d20625ed..2916563fb 100644
--- a/app/actions/actions_test.go
+++ b/app/actions/actions_test.go
@@ -2,14 +2,13 @@ package actions_test
import (
"github.com/getfider/fider/app"
- "github.com/getfider/fider/app/models"
. "github.com/getfider/fider/app/pkg/assert"
"github.com/getfider/fider/app/pkg/jwt"
"github.com/getfider/fider/app/pkg/validate"
"github.com/getfider/fider/app/storage/inmemory"
)
-var jonSnowToken, _ = jwt.Encode(&models.OAuthClaims{
+var jonSnowToken, _ = jwt.Encode(&jwt.OAuthClaims{
OAuthID: "123",
OAuthName: "Jon Snow",
OAuthEmail: "[email protected]",
diff --git a/app/handlers/signin.go b/app/handlers/signin.go
index 2d832e9ed..1fbc4d0ca 100644
--- a/app/handlers/signin.go
+++ b/app/handlers/signin.go
@@ -103,17 +103,23 @@ func OAuthCallback(provider string) web.HandlerFunc {
}
}
- claims = models.FiderClaims{
+ claims = jwt.FiderClaims{
UserID: user.ID,
UserName: user.Name,
UserEmail: user.Email,
+ Metadata: jwt.Metadata{
+ ExpiresAt: time.Now().Add(365 * 24 * time.Hour).Unix(),
+ },
}
} else {
- claims = models.OAuthClaims{
+ claims = jwt.OAuthClaims{
OAuthID: oauthUser.ID.String(),
OAuthProvider: provider,
OAuthName: oauthUser.Name,
OAuthEmail: oauthUser.Email,
+ Metadata: jwt.Metadata{
+ ExpiresAt: time.Now().Add(10 * time.Minute).Unix(),
+ },
}
}
diff --git a/app/handlers/signin_test.go b/app/handlers/signin_test.go
index 71be15f49..0bf143c46 100644
--- a/app/handlers/signin_test.go
+++ b/app/handlers/signin_test.go
@@ -1,7 +1,7 @@
package handlers_test
import (
- "fmt"
+ "net/url"
"testing"
"time"
@@ -75,7 +75,12 @@ func TestCallbackHandler_ExistingUserAndProvider(t *testing.T) {
Execute(handlers.OAuthCallback(oauth.FacebookProvider))
Expect(code).Equals(http.StatusTemporaryRedirect)
- Expect(response.Header().Get("Location")).Equals("http://demo.test.fider.io?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyL2lkIjoxLCJ1c2VyL25hbWUiOiJKb24gU25vdyIsInVzZXIvZW1haWwiOiJqb24uc25vd0Bnb3QuY29tIn0.S7P8zTU0rVovmchNbwamBewYbO96GdJcOygn7tbsikw")
+
+ location, _ := url.Parse(response.Header().Get("Location"))
+ Expect(location.Host).Equals("demo.test.fider.io")
+ Expect(location.Scheme).Equals("http")
+ Expect(location.Path).Equals("")
+ ExpectFiderToken(location.Query().Get("token"), mock.JonSnow)
}
func TestCallbackHandler_SignUp(t *testing.T) {
@@ -87,7 +92,17 @@ func TestCallbackHandler_SignUp(t *testing.T) {
Execute(handlers.OAuthCallback(oauth.FacebookProvider))
Expect(code).Equals(http.StatusTemporaryRedirect)
- Expect(response.Header().Get("Location")).Equals("http://demo.test.fider.io/signup?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJvYXV0aC9pZCI6IkZCMTIzIiwib2F1dGgvcHJvdmlkZXIiOiJmYWNlYm9vayIsIm9hdXRoL25hbWUiOiJKb24gU25vdyIsIm9hdXRoL2VtYWlsIjoiam9uLnNub3dAZ290LmNvbSJ9.AFMEtMLxd2nAxPzMVeGVrJomn-n56WdhLvgYSmSq008")
+
+ location, _ := url.Parse(response.Header().Get("Location"))
+ Expect(location.Host).Equals("demo.test.fider.io")
+ Expect(location.Scheme).Equals("http")
+ Expect(location.Path).Equals("/signup")
+ ExpectOAuthToken(location.Query().Get("token"), &jwt.OAuthClaims{
+ OAuthProvider: "facebook",
+ OAuthID: "FB123",
+ OAuthName: "Jon Snow",
+ OAuthEmail: "[email protected]",
+ })
}
func TestCallbackHandler_NewUser(t *testing.T) {
@@ -103,7 +118,12 @@ func TestCallbackHandler_NewUser(t *testing.T) {
Expect(user.Name).Equals("Some Facebook Guy")
Expect(code).Equals(http.StatusTemporaryRedirect)
- Expect(response.Header().Get("Location")).Equals("http://avengers.test.fider.io?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyL2lkIjozLCJ1c2VyL25hbWUiOiJTb21lIEZhY2Vib29rIEd1eSIsInVzZXIvZW1haWwiOiJzb21lLmd1eUBmYWNlYm9vay5jb20ifQ.ydyGDIZZHbJ-mgvpAsXTZnbs1rBH6cTVjHctZEACUOo")
+
+ location, _ := url.Parse(response.Header().Get("Location"))
+ Expect(location.Host).Equals("avengers.test.fider.io")
+ Expect(location.Scheme).Equals("http")
+ Expect(location.Path).Equals("")
+ ExpectFiderToken(location.Query().Get("token"), user)
}
func TestCallbackHandler_NewUserWithoutEmail(t *testing.T) {
@@ -125,16 +145,26 @@ func TestCallbackHandler_NewUserWithoutEmail(t *testing.T) {
user, err := services.Users.GetByID(3)
Expect(err).IsNil()
+ Expect(user.ID).Equals(3)
Expect(user.Name).Equals("Some Guy")
Expect(user.Providers).HasLen(1)
user, err = services.Users.GetByID(4)
Expect(err).IsNil()
+ Expect(user.ID).Equals(4)
Expect(user.Name).Equals("Mark")
Expect(user.Providers).HasLen(1)
Expect(code).Equals(http.StatusTemporaryRedirect)
- Expect(response.Header().Get("Location")).Equals("http://demo.test.fider.io?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyL2lkIjo0LCJ1c2VyL25hbWUiOiJNYXJrIiwidXNlci9lbWFpbCI6IiJ9.G93ZTFcDuHiIlYbDvMnjhoDebeJZMifWbv9v0rayQOI")
+
+ location, _ := url.Parse(response.Header().Get("Location"))
+ Expect(location.Host).Equals("demo.test.fider.io")
+ Expect(location.Scheme).Equals("http")
+ Expect(location.Path).Equals("")
+ ExpectFiderToken(location.Query().Get("token"), &models.User{
+ ID: 4,
+ Name: "Mark",
+ })
}
func TestCallbackHandler_ExistingUser_WithoutEmail(t *testing.T) {
@@ -158,7 +188,15 @@ func TestCallbackHandler_ExistingUser_WithoutEmail(t *testing.T) {
Expect(errors.Cause(err)).Equals(app.ErrNotFound)
Expect(code).Equals(http.StatusTemporaryRedirect)
- Expect(response.Header().Get("Location")).Equals("http://demo.test.fider.io?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyL2lkIjozLCJ1c2VyL25hbWUiOiJTb21lIEZhY2Vib29rIEd1eSIsInVzZXIvZW1haWwiOiIifQ.DBcMmKSrGxiXGVCq9QT716xfw3M4kzP-Njazsk6vaFI")
+
+ location, _ := url.Parse(response.Header().Get("Location"))
+ Expect(location.Host).Equals("demo.test.fider.io")
+ Expect(location.Scheme).Equals("http")
+ Expect(location.Path).Equals("")
+ ExpectFiderToken(location.Query().Get("token"), &models.User{
+ ID: 3,
+ Name: "Some Facebook Guy",
+ })
}
func TestCallbackHandler_ExistingUser_NewProvider(t *testing.T) {
@@ -174,7 +212,12 @@ func TestCallbackHandler_ExistingUser_NewProvider(t *testing.T) {
Expect(user.Providers).HasLen(2)
Expect(code).Equals(http.StatusTemporaryRedirect)
- Expect(response.Header().Get("Location")).Equals("http://demo.test.fider.io?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyL2lkIjoxLCJ1c2VyL25hbWUiOiJKb24gU25vdyIsInVzZXIvZW1haWwiOiJqb24uc25vd0Bnb3QuY29tIn0.S7P8zTU0rVovmchNbwamBewYbO96GdJcOygn7tbsikw")
+
+ location, _ := url.Parse(response.Header().Get("Location"))
+ Expect(location.Host).Equals("demo.test.fider.io")
+ Expect(location.Scheme).Equals("http")
+ Expect(location.Path).Equals("")
+ ExpectFiderToken(location.Query().Get("token"), mock.JonSnow)
}
func TestCallbackHandler_NewUser_PrivateTenant(t *testing.T) {
@@ -192,7 +235,11 @@ func TestCallbackHandler_NewUser_PrivateTenant(t *testing.T) {
Expect(user).IsNil()
Expect(code).Equals(http.StatusTemporaryRedirect)
- Expect(response.Header().Get("Location")).Equals("http://ideas.theavengers.com/not-invited")
+ location, _ := url.Parse(response.Header().Get("Location"))
+ Expect(location.Host).Equals("ideas.theavengers.com")
+ Expect(location.Scheme).Equals("http")
+ Expect(location.Path).Equals("/not-invited")
+ Expect(location.Query().Get("token")).IsEmpty()
}
func TestSignInByEmailHandler_WithoutEmail(t *testing.T) {
@@ -276,15 +323,16 @@ func TestVerifySignInKeyHandler_CorrectKey_ExistingUser(t *testing.T) {
WithURL("http://demo.test.fider.io/signin/verify?k=1234567890").
Execute(handlers.VerifySignInKey(models.EmailVerificationKindSignIn))
- token, _ := jwt.Encode(models.FiderClaims{
- UserID: mock.JonSnow.ID,
- UserName: mock.JonSnow.Name,
- UserEmail: mock.JonSnow.Email,
- })
-
Expect(code).Equals(http.StatusTemporaryRedirect)
Expect(response.Header().Get("Location")).Equals("http://demo.test.fider.io")
- Expect(response.Header().Get("Set-Cookie")).ContainsSubstring(fmt.Sprintf("%s=%s;", web.CookieAuthName, token))
+
+ cookies := web.ParseCookies(response.Header().Get("Set-Cookie"))
+ Expect(cookies).HasLen(1)
+ Expect(cookies[0].Name).Equals(web.CookieAuthName)
+ ExpectFiderToken(cookies[0].Value, mock.JonSnow)
+ Expect(cookies[0].HttpOnly).IsTrue()
+ Expect(cookies[0].Path).Equals("/")
+ Expect(cookies[0].Expires).TemporarilySimilar(time.Now().Add(365*24*time.Hour), 5*time.Second)
}
func TestVerifySignInKeyHandler_CorrectKey_NewUser(t *testing.T) {
@@ -454,12 +502,13 @@ func TestCompleteSignInProfileHandler_CorrectKey(t *testing.T) {
Expect(user.Name).Equals("Hot Pie")
Expect(user.Email).Equals("[email protected]")
- token, _ := jwt.Encode(models.FiderClaims{
- UserID: user.ID,
- UserName: user.Name,
- UserEmail: user.Email,
- })
- Expect(response.Header().Get("Set-Cookie")).ContainsSubstring(fmt.Sprintf("%s=%s;", web.CookieAuthName, token))
+ cookies := web.ParseCookies(response.Header().Get("Set-Cookie"))
+ Expect(cookies).HasLen(1)
+ Expect(cookies[0].Name).Equals(web.CookieAuthName)
+ ExpectFiderToken(cookies[0].Value, user)
+ Expect(cookies[0].HttpOnly).IsTrue()
+ Expect(cookies[0].Path).Equals("/")
+ Expect(cookies[0].Expires).TemporarilySimilar(time.Now().Add(365*24*time.Hour), 5*time.Second)
request, err := services.Tenants.FindVerificationByKey(models.EmailVerificationKindSignIn, "1234567890")
Expect(err).IsNil()
@@ -506,3 +555,20 @@ func TestSignInPageHandler_PrivateTenant_UnauthenticatedUser(t *testing.T) {
Expect(code).Equals(http.StatusOK)
}
+
+func ExpectFiderToken(token string, expected *models.User) {
+ user, err := jwt.DecodeFiderClaims(token)
+ Expect(err).IsNil()
+ Expect(user.UserID).Equals(expected.ID)
+ Expect(user.UserName).Equals(expected.Name)
+ Expect(user.UserEmail).Equals(expected.Email)
+}
+
+func ExpectOAuthToken(token string, expected *jwt.OAuthClaims) {
+ user, err := jwt.DecodeOAuthClaims(token)
+ Expect(err).IsNil()
+ Expect(user.OAuthID).Equals(expected.OAuthID)
+ Expect(user.OAuthName).Equals(expected.OAuthName)
+ Expect(user.OAuthEmail).Equals(expected.OAuthEmail)
+ Expect(user.OAuthProvider).Equals(expected.OAuthProvider)
+}
diff --git a/app/handlers/signup_test.go b/app/handlers/signup_test.go
index 247aafaa0..57cf20fbb 100644
--- a/app/handlers/signup_test.go
+++ b/app/handlers/signup_test.go
@@ -86,7 +86,7 @@ func TestCreateTenantHandler_WithSocialAccount(t *testing.T) {
RegisterT(t)
server, services := mock.NewServer()
- token, _ := jwt.Encode(&models.OAuthClaims{
+ token, _ := jwt.Encode(&jwt.OAuthClaims{
OAuthID: "123",
OAuthName: "Jon Snow",
OAuthEmail: "[email protected]",
diff --git a/app/middlewares/jwt_test.go b/app/middlewares/jwt_test.go
index e24d8ee4a..0ef8dc079 100644
--- a/app/middlewares/jwt_test.go
+++ b/app/middlewares/jwt_test.go
@@ -5,7 +5,6 @@ import (
"testing"
"github.com/getfider/fider/app/middlewares"
- "github.com/getfider/fider/app/models"
. "github.com/getfider/fider/app/pkg/assert"
"github.com/getfider/fider/app/pkg/jwt"
"github.com/getfider/fider/app/pkg/mock"
@@ -32,7 +31,7 @@ func TestJwtGetter_WithCookie(t *testing.T) {
RegisterT(t)
server, _ := mock.NewServer()
- token, _ := jwt.Encode(&models.FiderClaims{
+ token, _ := jwt.Encode(&jwt.FiderClaims{
UserID: mock.JonSnow.ID,
UserName: mock.JonSnow.Name,
})
@@ -54,7 +53,7 @@ func TestJwtGetter_WithCookie_InvalidUser(t *testing.T) {
RegisterT(t)
server, _ := mock.NewServer()
- token, _ := jwt.Encode(&models.FiderClaims{
+ token, _ := jwt.Encode(&jwt.FiderClaims{
UserID: 999,
UserName: "Unknown",
})
@@ -78,7 +77,7 @@ func TestJwtGetter_WithCookie_DifferentTenant(t *testing.T) {
RegisterT(t)
server, _ := mock.NewServer()
- token, _ := jwt.Encode(&models.FiderClaims{
+ token, _ := jwt.Encode(&jwt.FiderClaims{
UserID: mock.JonSnow.ID,
UserName: mock.JonSnow.Name,
})
@@ -114,7 +113,7 @@ func TestJwtSetter_WithJwt_WithoutParameter(t *testing.T) {
RegisterT(t)
server, _ := mock.NewServer()
- token, _ := jwt.Encode(&models.FiderClaims{
+ token, _ := jwt.Encode(&jwt.FiderClaims{
UserName: mock.JonSnow.Name,
})
@@ -131,7 +130,7 @@ func TestJwtSetter_WithJwt_WithParameter(t *testing.T) {
RegisterT(t)
server, _ := mock.NewServer()
- token, _ := jwt.Encode(&models.FiderClaims{
+ token, _ := jwt.Encode(&jwt.FiderClaims{
UserName: mock.JonSnow.Name,
})
diff --git a/app/models/identity.go b/app/models/identity.go
index df014c3b8..c62b0909b 100644
--- a/app/models/identity.go
+++ b/app/models/identity.go
@@ -4,7 +4,7 @@ import (
"strings"
"time"
- jwt "github.com/dgrijalva/jwt-go"
+ "github.com/getfider/fider/app/pkg/jwt"
"github.com/getfider/fider/app/pkg/uuid"
)
@@ -106,23 +106,6 @@ type UserProvider struct {
UID string
}
-//FiderClaims represents what goes into JWT tokens
-type FiderClaims struct {
- UserID int `json:"user/id"`
- UserName string `json:"user/name"`
- UserEmail string `json:"user/email"`
- jwt.StandardClaims
-}
-
-//OAuthClaims represents what goes into temporary OAuth JWT tokens
-type OAuthClaims struct {
- OAuthID string `json:"oauth/id"`
- OAuthProvider string `json:"oauth/provider"`
- OAuthName string `json:"oauth/name"`
- OAuthEmail string `json:"oauth/email"`
- jwt.StandardClaims
-}
-
//CreateTenant is the input model used to create a tenant
type CreateTenant struct {
Token string `json:"token"`
@@ -132,7 +115,7 @@ type CreateTenant struct {
TenantName string `json:"tenantName"`
LegalAgreement bool `json:"legalAgreement"`
Subdomain string `json:"subdomain" format:"lower"`
- UserClaims *OAuthClaims
+ UserClaims *jwt.OAuthClaims
}
//GetEmail returns the email being verified
diff --git a/app/pkg/jwt/jwt.go b/app/pkg/jwt/jwt.go
index f3d7c57e5..dd0dd9094 100644
--- a/app/pkg/jwt/jwt.go
+++ b/app/pkg/jwt/jwt.go
@@ -2,14 +2,33 @@ package jwt
import (
jwtgo "github.com/dgrijalva/jwt-go"
- "github.com/getfider/fider/app/models"
"github.com/getfider/fider/app/pkg/env"
"github.com/getfider/fider/app/pkg/errors"
)
var jwtSecret = env.MustGet("JWT_SECRET")
-//Encode creates new JWT tokens with given claims
+// Metadata is the basic JWT information
+type Metadata = jwtgo.StandardClaims
+
+// FiderClaims represents what goes into JWT tokens
+type FiderClaims struct {
+ UserID int `json:"user/id"`
+ UserName string `json:"user/name"`
+ UserEmail string `json:"user/email"`
+ Metadata
+}
+
+// OAuthClaims represents what goes into temporary OAuth JWT tokens
+type OAuthClaims struct {
+ OAuthID string `json:"oauth/id"`
+ OAuthProvider string `json:"oauth/provider"`
+ OAuthName string `json:"oauth/name"`
+ OAuthEmail string `json:"oauth/email"`
+ Metadata
+}
+
+// Encode creates new JWT token with given claims
func Encode(claims jwtgo.Claims) (string, error) {
jwtToken := jwtgo.NewWithClaims(jwtgo.GetSigningMethod("HS256"), claims)
token, err := jwtToken.SignedString([]byte(jwtSecret))
@@ -19,9 +38,9 @@ func Encode(claims jwtgo.Claims) (string, error) {
return token, nil
}
-//DecodeFiderClaims extract claims from JWT tokens
-func DecodeFiderClaims(token string) (*models.FiderClaims, error) {
- claims := &models.FiderClaims{}
+// DecodeFiderClaims extract claims from JWT tokens
+func DecodeFiderClaims(token string) (*FiderClaims, error) {
+ claims := &FiderClaims{}
err := decode(token, claims)
if err != nil {
return nil, errors.Wrap(err, "failed to decode Fider claims")
@@ -29,9 +48,9 @@ func DecodeFiderClaims(token string) (*models.FiderClaims, error) {
return claims, nil
}
-//DecodeOAuthClaims extract OAuthClaims from given JWT token
-func DecodeOAuthClaims(token string) (*models.OAuthClaims, error) {
- claims := &models.OAuthClaims{}
+// DecodeOAuthClaims extract OAuthClaims from given JWT token
+func DecodeOAuthClaims(token string) (*OAuthClaims, error) {
+ claims := &OAuthClaims{}
err := decode(token, claims)
if err != nil {
return nil, errors.Wrap(err, "failed to decode OAuth claims")
@@ -44,8 +63,13 @@ func decode(token string, claims jwtgo.Claims) error {
return []byte(jwtSecret), nil
})
+ if err == nil {
+ err = claims.Valid()
+ }
+
if err == nil && jwtToken.Valid {
return nil
}
+
return err
}
diff --git a/app/pkg/jwt/jwt_test.go b/app/pkg/jwt/jwt_test.go
index bcf05e2b1..4372bfe82 100644
--- a/app/pkg/jwt/jwt_test.go
+++ b/app/pkg/jwt/jwt_test.go
@@ -2,8 +2,8 @@ package jwt_test
import (
"testing"
+ "time"
- "github.com/getfider/fider/app/models"
. "github.com/getfider/fider/app/pkg/assert"
"github.com/getfider/fider/app/pkg/jwt"
)
@@ -11,7 +11,7 @@ import (
func TestJWT_Encode(t *testing.T) {
RegisterT(t)
- claims := &models.FiderClaims{
+ claims := &jwt.FiderClaims{
UserID: 424,
UserName: "Jon Snow",
UserEmail: "[email protected]",
@@ -25,7 +25,7 @@ func TestJWT_Encode(t *testing.T) {
func TestJWT_Decode(t *testing.T) {
RegisterT(t)
- claims := &models.FiderClaims{
+ claims := &jwt.FiderClaims{
UserID: 424,
UserName: "Jon Snow",
UserEmail: "[email protected]",
@@ -40,10 +40,31 @@ func TestJWT_Decode(t *testing.T) {
Expect(decoded.UserEmail).Equals(claims.UserEmail)
}
+func TestJWT_DecodeExpired(t *testing.T) {
+ RegisterT(t)
+
+ claims := &jwt.FiderClaims{
+ UserID: 424,
+ UserName: "Jon Snow",
+ UserEmail: "[email protected]",
+ Metadata: jwt.Metadata{
+ ExpiresAt: time.Now().Unix(),
+ },
+ }
+
+ token, err := jwt.Encode(claims)
+ Expect(err).IsNil()
+ time.Sleep(1 * time.Second)
+
+ decoded, err := jwt.DecodeFiderClaims(token)
+ Expect(err).IsNotNil()
+ Expect(decoded).IsNil()
+}
+
func TestJWT_DecodeOAuthClaims(t *testing.T) {
RegisterT(t)
- claims := &models.OAuthClaims{
+ claims := &jwt.OAuthClaims{
OAuthID: "2",
OAuthEmail: "[email protected]",
OAuthName: "Jon Snow",
@@ -63,7 +84,7 @@ func TestJWT_DecodeOAuthClaims(t *testing.T) {
func TestJWT_DecodeChangedToken(t *testing.T) {
RegisterT(t)
- claims := &models.FiderClaims{
+ claims := &jwt.FiderClaims{
UserID: 424,
UserName: "Jon Snow",
UserEmail: "[email protected]",
@@ -75,3 +96,25 @@ func TestJWT_DecodeChangedToken(t *testing.T) {
Expect(err).IsNotNil()
Expect(decoded).IsNil()
}
+
+func TestJWT_DecodeOAuthClaimsExpired(t *testing.T) {
+ RegisterT(t)
+
+ claims := &jwt.OAuthClaims{
+ OAuthID: "2",
+ OAuthEmail: "[email protected]",
+ OAuthName: "Jon Snow",
+ OAuthProvider: "facebook",
+ Metadata: jwt.Metadata{
+ ExpiresAt: time.Now().Unix(),
+ },
+ }
+
+ token, err := jwt.Encode(claims)
+ Expect(err).IsNil()
+ time.Sleep(1 * time.Second)
+
+ decoded, err := jwt.DecodeOAuthClaims(token)
+ Expect(err).IsNotNil()
+ Expect(decoded).IsNil()
+}
diff --git a/app/pkg/web/context.go b/app/pkg/web/context.go
index eb7319319..73f3deab1 100644
--- a/app/pkg/web/context.go
+++ b/app/pkg/web/context.go
@@ -315,17 +315,21 @@ func (ctx *Context) Services() *app.Services {
//AddAuthCookie generates and adds a cookie
func (ctx *Context) AddAuthCookie(user *models.User) (string, error) {
- token, err := jwt.Encode(models.FiderClaims{
+ expiresAt := time.Now().Add(365 * 24 * time.Hour)
+ token, err := jwt.Encode(jwt.FiderClaims{
UserID: user.ID,
UserName: user.Name,
UserEmail: user.Email,
+ Metadata: jwt.Metadata{
+ ExpiresAt: expiresAt.Unix(),
+ },
})
if err != nil {
return token, errors.Wrap(err, "failed to add auth cookie")
}
- ctx.AddCookie(CookieAuthName, token, time.Now().Add(365*24*time.Hour))
+ ctx.AddCookie(CookieAuthName, token, expiresAt)
return token, nil
}
diff --git a/app/pkg/web/engine.go b/app/pkg/web/engine.go
index 47d70731d..d2d9cd506 100644
--- a/app/pkg/web/engine.go
+++ b/app/pkg/web/engine.go
@@ -264,3 +264,8 @@ func (g *Group) Static(prefix, root string) {
}
g.engine.mux.Handle("GET", prefix, g.handler(h))
}
+
+// ParseCookies return a list of cookie parsed from raw Set-Cookie
+func ParseCookies(s string) []*http.Cookie {
+ return (&http.Response{Header: http.Header{"Set-Cookie": {s}}}).Cookies()
+}