Skip to content

Commit

Permalink
Middleware: Add the logic to prolong the validity period in case that…
Browse files Browse the repository at this point in the history
… the user selected "remember me" when logging in
  • Loading branch information
YiranDuan721 committed Jan 21, 2024
1 parent 5f0f52b commit f7e8fa9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
29 changes: 29 additions & 0 deletions tools/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/url"
"strconv"
"strings"
"time"

"github.com/TUM-Dev/gocast/dao"
"github.com/TUM-Dev/gocast/model"
Expand All @@ -15,6 +16,11 @@ import (
"github.com/golang-jwt/jwt/v4"
)

const (
MaxTokenLifetimeWithRememberMeInDays = 180
MinUpdateIntervalInHours = 1
)

var templateExecutor TemplateExecutor

// SetTemplateExecutor sets the templates and template executor for the middlewares to execute error pages
Expand All @@ -25,6 +31,7 @@ func SetTemplateExecutor(e TemplateExecutor) {
// JWTClaims are the claims contained in a session
type JWTClaims struct {
*jwt.RegisteredClaims
UpdatedAt *jwt.NumericDate
UserID uint
SamlSubjectID *string // identifier of the SAML session (if any)
RememberMe bool
Expand Down Expand Up @@ -63,6 +70,28 @@ func InitContext(daoWrapper dao.DaoWrapper) gin.HandlerFunc {
return
}

claims := token.Claims.(*JWTClaims)

// in case when the user has selected "remember me" when logging in, prolong the validity of the token
// but only when the token has not been updated during the last 1 hour
if claims.RememberMe && time.Now().Sub(claims.UpdatedAt.Time).Hours() > MinUpdateIntervalInHours {

Check failure on line 77 in tools/middlewares.go

View workflow job for this annotation

GitHub Actions / lint (./...)

S1012: should use `time.Since` instead of `time.Now().Sub` (gosimple)

Check failure on line 77 in tools/middlewares.go

View workflow job for this annotation

GitHub Actions / lint (./worker)

S1012: should use `time.Since` instead of `time.Now().Sub` (gosimple)

Check failure on line 77 in tools/middlewares.go

View workflow job for this annotation

GitHub Actions / lint (./worker/edge)

S1012: should use `time.Since` instead of `time.Now().Sub` (gosimple)
// remove jwt cookie older than MaxTokenAgeWithRefreshInDays
expiresAt := &jwt.NumericDate{time.Now().Add(time.Hour * 24 * MaxTokenAgeInDays)}

Check failure on line 79 in tools/middlewares.go

View workflow job for this annotation

GitHub Actions / lint (./...)

composites: github.com/golang-jwt/jwt/v4.NumericDate struct literal uses unkeyed fields (govet)

Check failure on line 79 in tools/middlewares.go

View workflow job for this annotation

GitHub Actions / lint (./worker)

composites: github.com/golang-jwt/jwt/v4.NumericDate struct literal uses unkeyed fields (govet)

Check failure on line 79 in tools/middlewares.go

View workflow job for this annotation

GitHub Actions / lint (./worker/edge)

composites: github.com/golang-jwt/jwt/v4.NumericDate struct literal uses unkeyed fields (govet)
if expiresAt.Sub(claims.IssuedAt.Time).Hours() > MaxTokenLifetimeWithRememberMeInDays*24 {
c.SetCookie("jwt", "", -1, "/", "", false, true)
return
}
claims.ExpiresAt = expiresAt
claims.UpdatedAt = &jwt.NumericDate{time.Now()}

Check failure on line 85 in tools/middlewares.go

View workflow job for this annotation

GitHub Actions / lint (./...)

composites: github.com/golang-jwt/jwt/v4.NumericDate struct literal uses unkeyed fields (govet)

Check failure on line 85 in tools/middlewares.go

View workflow job for this annotation

GitHub Actions / lint (./worker)

composites: github.com/golang-jwt/jwt/v4.NumericDate struct literal uses unkeyed fields (govet)

Check failure on line 85 in tools/middlewares.go

View workflow job for this annotation

GitHub Actions / lint (./worker/edge)

composites: github.com/golang-jwt/jwt/v4.NumericDate struct literal uses unkeyed fields (govet)

token = jwt.NewWithClaims(token.Method, claims)
signedToken, err := token.SignedString(Cfg.GetJWTKey())
if err == nil {
c.SetCookie("jwt", signedToken, 60*60*24*MaxTokenAgeInDays, "/", "", CookieSecure, true)
}
logger.Error(signedToken)
}

user, err := daoWrapper.UsersDao.GetUserByID(c, token.Claims.(*JWTClaims).UserID)
if err != nil {
c.Set("TUMLiveContext", TUMLiveContext{})
Expand Down
2 changes: 2 additions & 0 deletions tools/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ func createToken(user uint, samlSubjectID *string, rememberMe bool) (string, err

t.Claims = &JWTClaims{
RegisteredClaims: &jwt.RegisteredClaims{
IssuedAt: &jwt.NumericDate{Time: time.Now()},
ExpiresAt: &jwt.NumericDate{Time: time.Now().Add(time.Hour * 24 * MaxTokenAgeInDays)}, // Token expires in one week
},
UpdatedAt: &jwt.NumericDate{Time: time.Now()},
UserID: user,
SamlSubjectID: samlSubjectID,
RememberMe: rememberMe,
Expand Down

0 comments on commit f7e8fa9

Please sign in to comment.