MCPcopy Index your code
hub / github.com/tinyauthapp/tinyauth / RefreshSessionCookie

Method RefreshSessionCookie

internal/service/auth_service.go:335–385  ·  view source on GitHub ↗
(c *gin.Context)

Source from the content-addressed store, hash-verified

333}
334
335func (auth *AuthService) RefreshSessionCookie(c *gin.Context) error {
336 cookie, err := c.Cookie(auth.config.SessionCookieName)
337
338 if err != nil {
339 return err
340 }
341
342 session, err := auth.queries.GetSession(c, cookie)
343
344 if err != nil {
345 return err
346 }
347
348 currentTime := time.Now().Unix()
349
350 var refreshThreshold int64
351
352 if auth.config.SessionExpiry <= int(time.Hour.Seconds()) {
353 refreshThreshold = int64(auth.config.SessionExpiry / 2)
354 } else {
355 refreshThreshold = int64(time.Hour.Seconds())
356 }
357
358 if session.Expiry-currentTime > refreshThreshold {
359 return nil
360 }
361
362 newExpiry := session.Expiry + refreshThreshold
363
364 _, err = auth.queries.UpdateSession(c, repository.UpdateSessionParams{
365 Username: session.Username,
366 Email: session.Email,
367 Name: session.Name,
368 Provider: session.Provider,
369 TotpPending: session.TotpPending,
370 OAuthGroups: session.OAuthGroups,
371 Expiry: newExpiry,
372 OAuthName: session.OAuthName,
373 OAuthSub: session.OAuthSub,
374 UUID: session.UUID,
375 })
376
377 if err != nil {
378 return err
379 }
380
381 c.SetCookie(auth.config.SessionCookieName, cookie, int(newExpiry-currentTime), "/", fmt.Sprintf(".%s", auth.config.CookieDomain), auth.config.SecureCookie, true)
382 tlog.App.Trace().Str("username", session.Username).Msg("Session cookie refreshed")
383
384 return nil
385}
386
387func (auth *AuthService) DeleteSessionCookie(c *gin.Context) error {
388 cookie, err := c.Cookie(auth.config.SessionCookieName)

Callers 1

MiddlewareMethod · 0.80

Calls 2

GetSessionMethod · 0.80
UpdateSessionMethod · 0.80

Tested by

no test coverage detected