Skip to content

Commit

Permalink
Add "remember me" option for log in.
Browse files Browse the repository at this point in the history
Passes on whether the user has selected the "remember me" option by setting a cookie, and if so, the jwt cookie set at login is set to be valid for 6 months instead of one week.
Related: #1302
  • Loading branch information
YiranDuan721 committed Jan 14, 2024
1 parent 96f53da commit e9e6973
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion api/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (r usersRoutes) impersonateUser(c *gin.Context) {
})
return
}
tools.StartSession(c, &tools.SessionData{Userid: u.ID})
tools.StartSession(c, &tools.SessionData{Userid: u.ID}, false)
}

func (r usersRoutes) updateUser(c *gin.Context) {
Expand Down
15 changes: 10 additions & 5 deletions tools/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,26 @@ type SessionData struct {
SamlSubjectID *string
}

func StartSession(c *gin.Context, data *SessionData) {
token, err := createToken(data.Userid, data.SamlSubjectID)
func StartSession(c *gin.Context, data *SessionData, rememberMe bool) {
maxAgeInDays := 7 // by default, log-in status expires in one week
if rememberMe {
maxAgeInDays = 30 * 6 // if user chooses "remember me", let log-in status be valid for 6 months
}

token, err := createToken(data.Userid, data.SamlSubjectID, maxAgeInDays)
if err != nil {
logger.Error("Could not create token", "err", err)
return
}
c.SetCookie("jwt", token, 60*60*24*7, "/", "", CookieSecure, true)
c.SetCookie("jwt", token, 60*60*24*maxAgeInDays, "/", "", CookieSecure, true)
}

func createToken(user uint, samlSubjectID *string) (string, error) {
func createToken(user uint, samlSubjectID *string, maxAgeInDays int) (string, error) {
t := jwt.New(jwt.GetSigningMethod("RS256"))

t.Claims = &JWTClaims{
RegisteredClaims: &jwt.RegisteredClaims{
ExpiresAt: &jwt.NumericDate{Time: time.Now().Add(time.Hour * 24 * 7)}, // Token expires in one week
ExpiresAt: &jwt.NumericDate{Time: time.Now().Add(time.Hour * 24 * time.Duration(maxAgeInDays))}, // Token expires in one week
},
UserID: user,
SamlSubjectID: samlSubjectID,
Expand Down
14 changes: 14 additions & 0 deletions web/template/login.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@
<header>
<h1 class="font-bold text-3">Login</h1>
</header>

<div x-show="!resetPassword" class="flex">
<input type="checkbox" name="rememberMe" id="rememberMe">
<label for="rememberMe" class="text-5 text-sm px-2">Remember me for 6 months</label>
<script>
document.getElementById('rememberMe').addEventListener('change', function() {
document.cookie = "rememberMe=" + this.checked + "; path=/; max-age=600";
// The status whether "remember me" is checked is saved in a cookie, valid for 10 minutes
// This cookie will be deleted once logged in
});
</script>
</div>


{{if .UseSAML}}
<article class="text-center w-full">
<a href="/saml/out"
Expand Down
10 changes: 9 additions & 1 deletion web/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,15 @@ func (r mainRoutes) LoginHandler(c *gin.Context) {

// HandleValidLogin starts a session and redirects the user to the page they were trying to access.
func HandleValidLogin(c *gin.Context, data *tools.SessionData) {
tools.StartSession(c, data)
rememberMeCookie, err := c.Request.Cookie("rememberMe")
rememberMe := false
if err == nil && rememberMeCookie.Value == "true" {
rememberMe = true
}
// Delete cookie that was used for saving the status of "remember me"
c.SetCookie("rememberMe", "", -1, "/", "", tools.CookieSecure, true)

tools.StartSession(c, data, rememberMe)
redirURL, err := c.Cookie(redirCookieName)
if err != nil {
redirURL = "/" // Fallback in case no cookie is present: Redirect to index page
Expand Down

0 comments on commit e9e6973

Please sign in to comment.