Skip to content

Commit

Permalink
Merge branch 'master' into feat-pagebuilder-style
Browse files Browse the repository at this point in the history
  • Loading branch information
danni-cool committed Dec 13, 2024
2 parents 4518036 + 3211637 commit 1409f67
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/golang-jwt/jwt/v4 v4.5.1
github.com/google/go-cmp v0.6.0
github.com/google/uuid v1.6.0
github.com/huandu/go-clone v1.7.2
github.com/iancoleman/strcase v0.3.0
github.com/jinzhu/configor v1.2.2
github.com/jinzhu/inflection v1.0.0
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 h1:YBftPWNWd4WwGqtY2yeZL2ef8rH
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0/go.mod h1:YN5jB8ie0yfIUg6VvR9Kz84aCaG7AsGZnLjhHbUqwPg=
github.com/hashicorp/golang-lru v1.0.2 h1:dV3g9Z/unq5DpblPpw+Oqcv4dU/1omnb4Ok8iPY6p1c=
github.com/hashicorp/golang-lru v1.0.2/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/huandu/go-assert v1.1.5 h1:fjemmA7sSfYHJD7CUqs9qTwwfdNAx7/j2/ZlHXzNB3c=
github.com/huandu/go-assert v1.1.5/go.mod h1:yOLvuqZwmcHIC5rIzrBhT7D3Q9c3GFnd0JrPVhn/06U=
github.com/huandu/go-clone v1.7.2 h1:3+Aq0Ed8XK+zKkLjE2dfHg0XrpIfcohBE1K+c8Usxoo=
github.com/huandu/go-clone v1.7.2/go.mod h1:ReGivhG6op3GYr+UY3lS6mxjKp7MIGTknuU5TbTVaXE=
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
Expand Down Expand Up @@ -229,6 +233,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
Expand Down Expand Up @@ -342,6 +347,7 @@ google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHh
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
38 changes: 26 additions & 12 deletions login/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package login

import (
"context"
"errors"
"net/http"
"regexp"
"strconv"
"strings"
"time"

"github.com/huandu/go-clone"
)

type ContextUserKey int
Expand Down Expand Up @@ -38,6 +41,8 @@ func MockCurrentUser(user any) func(next http.Handler) http.Handler {
}
}

var ErrShouldNotExtend = errors.New("should not extend session")

func (b *Builder) Middleware(cfgs ...MiddlewareConfig) func(next http.Handler) http.Handler {
mustLogin := true
autoRedirectToHomePage := true
Expand Down Expand Up @@ -165,22 +170,31 @@ func (b *Builder) Middleware(cfgs ...MiddlewareConfig) func(next http.Handler) h
}

if b.autoExtendSession && time.Now().Sub(claims.IssuedAt.Time).Seconds() > float64(b.sessionMaxAge)/10 {
oldSessionToken := b.mustGetSessionToken(*claims)

claims.RegisteredClaims = b.genBaseSessionClaim(claims.UserID)
b.setAuthCookiesFromUserClaims(w, claims, secureSalt)

if b.afterExtendSessionHook != nil {
setCookieForRequest(r, &http.Cookie{Name: b.authCookieName, Value: b.mustGetSessionToken(*claims)})
if herr := b.wrapHook(b.afterExtendSessionHook)(r, user, oldSessionToken); herr != nil {
if !mustLogin {
next.ServeHTTP(w, r)
oldSessionToken := b.mustGetSessionToken(*claims)

newClaims := clone.Clone(claims).(*UserClaims)
newClaims.RegisteredClaims = b.genBaseSessionClaim(claims.UserID)
newSessionToken := b.mustGetSessionToken(*newClaims)
if herr := b.wrapHook(b.afterExtendSessionHook)(r, user, oldSessionToken, newSessionToken); herr != nil {
if !errors.Is(herr, ErrShouldNotExtend) {
if !mustLogin {
next.ServeHTTP(w, r)
return
}
setNoticeOrPanic(w, herr)
http.Redirect(w, r, b.LogoutURL, http.StatusFound)
return
}
setNoticeOrPanic(w, herr)
http.Redirect(w, r, b.LogoutURL, http.StatusFound)
return
} else {
claims = newClaims
b.setAuthCookiesFromUserClaims(w, claims, secureSalt)
setCookieForRequest(r, &http.Cookie{Name: b.authCookieName, Value: b.mustGetSessionToken(*claims)})
}
} else {
claims.RegisteredClaims = b.genBaseSessionClaim(claims.UserID)
b.setAuthCookiesFromUserClaims(w, claims, secureSalt)
setCookieForRequest(r, &http.Cookie{Name: b.authCookieName, Value: b.mustGetSessionToken(*claims)})
}
}

Expand Down

0 comments on commit 1409f67

Please sign in to comment.