diff --git a/easyauth.go b/easyauth.go index 65db334..af543fe 100644 --- a/easyauth.go +++ b/easyauth.go @@ -1,10 +1,13 @@ package auth import ( + "context" "errors" + "fmt" "net/http" "time" + session "github.com/go-session/session/v3" "github.com/golang-jwt/jwt/v5" ) @@ -26,21 +29,25 @@ func NewEasyAuth(client Auth) (EasyAuth, error) { } func (e EasyAuth) GetUser(w http.ResponseWriter, r *http.Request) (AuthentikUser, bool, error) { - if r.Method == http.MethodGet { - setCookie(w, "url", r.URL.String(), time.Now().Add(1*time.Minute)) - } - - cookieValue, err := getCookie(w, r, "jwt") + store, err := session.Start(context.Background(), w, r) if err != nil { return AuthentikUser{}, false, err } - if cookieValue == "" { + if r.Method == http.MethodGet { + store.Set("url", r.URL.String()) + } + + jwtData, ok := store.Get("jwt") + + if !ok { e.redirectAuth(w, r) return AuthentikUser{}, false, nil } - parsedAccessToken, _ := jwt.ParseWithClaims(cookieValue, &UserClaims{}, func(token *jwt.Token) (interface{}, error) { + jwtString := fmt.Sprintf("%s", jwtData) + + parsedAccessToken, _ := jwt.ParseWithClaims(jwtString, &UserClaims{}, func(token *jwt.Token) (interface{}, error) { return e.jwtSecret, nil }) @@ -87,18 +94,21 @@ func (e EasyAuth) AuthHTTPHandler(w http.ResponseWriter, r *http.Request) { panic(err) // TODO: Deal with error } - setCookie(w, "jwt", jwtString, expired) - - redirectURL, err := getCookie(w, r, "url") + store, err := session.Start(context.Background(), w, r) if err != nil { panic(err) // TODO: Deal with error } - if redirectURL == "" { - redirectURL = "/" + store.Set("jwt", jwtString) + + redirectURL, ok := store.Get("url") + redirectUrlString := "/" + + if ok { + redirectUrlString = fmt.Sprintf("%s", redirectURL) } - http.Redirect(w, r, redirectURL, http.StatusTemporaryRedirect) + http.Redirect(w, r, redirectUrlString, http.StatusTemporaryRedirect) } func getCookie(w http.ResponseWriter, r *http.Request, name string) (string, error) { diff --git a/go.mod b/go.mod index 3e9a163..827900c 100644 --- a/go.mod +++ b/go.mod @@ -3,11 +3,13 @@ module git.keks.cloud/kekskurse/auth go 1.23.1 require ( + github.com/go-session/session/v3 v3.2.1 github.com/golang-jwt/jwt/v5 v5.2.1 github.com/stretchr/testify v1.9.0 ) require ( + github.com/bytedance/gopkg v0.0.0-20221122125632-68358b8ecec6 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 61da7a6..de6a3d3 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,30 @@ +github.com/bytedance/gopkg v0.0.0-20221122125632-68358b8ecec6 h1:FCLDGi1EmB7JzjVVYNZiqc/zAJj2BQ5M0lfkVOxbfs8= +github.com/bytedance/gopkg v0.0.0-20221122125632-68358b8ecec6/go.mod h1:5FoAH5xUHHCMDvQPy1rnj8moqLkLHFaDVBjHhcFwEi0= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-session/session/v3 v3.2.1 h1:APQf5JFW84+bhbqRjEZO8J+IppSgT1jMQTFI/XVyIFY= +github.com/go-session/session/v3 v3.2.1/go.mod h1:RftEBbyuzqkNCAxIrCLJe+rfBqB/4G11qxq9KYKrx4M= github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk= github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= +github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 h1:l5lAOZEym3oK3SQ2HBHWsJUfbNBiTXJDeW2QDxw9AQ0= +github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= +github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/smartystreets/assertions v1.1.0 h1:MkTeG1DMwsrdH7QtLXy5W+fUxWq+vmb6cLmyJ7aRtF0= +github.com/smartystreets/assertions v1.1.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= +github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= +github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +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=