Skip to content

Commit

Permalink
fix: nil error with cookie age
Browse files Browse the repository at this point in the history
  • Loading branch information
CrescentKohana committed Jan 22, 2024
1 parent 2d399c1 commit 242fe9f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 7 additions & 2 deletions pkg/api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func login(w http.ResponseWriter, r *http.Request) {
return
}

expiresIn := utils.ClampCookieAge(*credentials.ExpiresIn)
expiresIn := utils.ClampCookieAge(credentials.ExpiresIn)

if credentials.Username != "" && credentials.Password != "" {
access, userUUID, role := loginHelper(w, *credentials, db.Role(0))
Expand Down Expand Up @@ -128,7 +128,12 @@ func login(w http.ResponseWriter, r *http.Request) {
ExpiresIn: credentials.ExpiresIn,
}, r.URL.Path)
return
} else if credentials.Passphrase == config.Credentials.Passphrase {
} else if credentials.Passphrase != "" {
if config.Credentials.Passphrase != credentials.Passphrase {
errorHandler(w, http.StatusUnauthorized, "", r.URL.Path)
return
}

passphraseCookie := http.Cookie{
Name: "mtsu.jwt",
Value: "Passphrase " + credentials.Passphrase,
Expand Down
8 changes: 6 additions & 2 deletions pkg/utils/validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ const minCookieAge = 60
const maxCookieAge = 365 * 24 * 60 * 60 // year in seconds

// ClampCookieAge returns a valid cookie age in seconds.
func ClampCookieAge(seconds int64) int64 {
return Clamp(seconds, minCookieAge, maxCookieAge)
func ClampCookieAge(seconds *int64) int64 {
if seconds == nil {
return maxCookieAge
}

return Clamp(*seconds, minCookieAge, maxCookieAge)
}

// IsValidSessionName checks if the session name is valid.
Expand Down

0 comments on commit 242fe9f

Please sign in to comment.