Skip to content

Commit

Permalink
Handle err
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy5189 committed Nov 23, 2023
1 parent 8214b1c commit ca1756d
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions internal/session_cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,25 @@ func sessionCookieEndPoint(c *gin.Context, config *Config) error {
*/
clientIp := c.Request.Header.Get("X-Client-IP")
dsc, err := c.Cookie(SessionCookieName)
urlDecodedDsc, _ := url.QueryUnescape(dsc)
urlDecodedDsc, decodeErr := url.QueryUnescape(dsc)

// if fail to decode, use the original dsc
if decodeErr != nil {
log.Printf("DSC: fail to urldecode cookie %s, use the original one\n", dsc)
urlDecodedDsc = dsc
}

if err == nil {
// cookie exists, validate it
validateErr := validateSessionCookie(urlDecodedDsc, config.SessionCookieHmacSecret, time.Now(), clientIp)
if validateErr == nil {
// cookie is valid, do not attach cookie but only report dsc_new=false
// fmt.Printf("DSC: [%s] cookie %s is valid, report dsc_new=false\n", clientIp, dsc)
// log.Printf("DSC: [%s] cookie %s is valid, report dsc_new=false\n", clientIp, urlDecodedDsc)
attachSessionCookie(c, config, urlDecodedDsc, false)
} else {
// cookie is invalid, create a new one
newDsc := newSessionCookie(config.SessionCookieHmacSecret, config.SessionCookieTtlSeconds, clientIp)
log.Printf("DSC: [%s] cookie %s is not valid, issue new: %s\n", clientIp, dsc, newDsc)
log.Printf("DSC: [%s] cookie %s is not valid, issue new: %s\n", clientIp, urlDecodedDsc, newDsc)
attachSessionCookie(c, config, newDsc, true)
}
return nil
Expand Down

0 comments on commit ca1756d

Please sign in to comment.