This commit is contained in:
kekskurse 2024-09-26 11:20:49 +02:00
parent 095d75f7ba
commit 969a4505ed

View file

@ -1,6 +1,7 @@
package auth package auth
import ( import (
"errors"
"net/http" "net/http"
"time" "time"
@ -30,16 +31,15 @@ func (e EasyAuth) GetUser(w http.ResponseWriter, r *http.Request) (AuthentikUser
} }
cookie, err := r.Cookie("jwt") cookie, err := r.Cookie("jwt")
if err != nil { if err != nil {
return AuthentikUser{}, false, err if errors.Is(err, http.ErrNoCookie) {
} e.redirectAuth(w, r)
return AuthentikUser{}, false, nil
redirectURL, err := e.client.GetAuthorizationURL("") }
if err != nil {
return AuthentikUser{}, false, err return AuthentikUser{}, false, err
} }
if cookie.Value == "" { if cookie.Value == "" {
http.Redirect(w, r, redirectURL, http.StatusTemporaryRedirect) e.redirectAuth(w, r)
return AuthentikUser{}, false, nil return AuthentikUser{}, false, nil
} }
@ -52,6 +52,16 @@ func (e EasyAuth) GetUser(w http.ResponseWriter, r *http.Request) (AuthentikUser
return claims.User, true, nil return claims.User, true, nil
} }
func (e EasyAuth) redirectAuth(w http.ResponseWriter, r *http.Request) error {
redirectURL, err := e.client.GetAuthorizationURL("")
if err != nil {
return err
}
http.Redirect(w, r, redirectURL, http.StatusTemporaryRedirect)
return nil
}
func (e EasyAuth) AuthHTTPHandler(w http.ResponseWriter, r *http.Request) { func (e EasyAuth) AuthHTTPHandler(w http.ResponseWriter, r *http.Request) {
token, err := e.client.GetTokenFromCode(r.URL.Query().Get("code")) token, err := e.client.GetTokenFromCode(r.URL.Query().Get("code"))
if err != nil { if err != nil {