easyauth
This commit is contained in:
parent
095d75f7ba
commit
969a4505ed
1 changed files with 16 additions and 6 deletions
20
easyauth.go
20
easyauth.go
|
@ -1,6 +1,7 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
|
@ -30,16 +31,15 @@ func (e EasyAuth) GetUser(w http.ResponseWriter, r *http.Request) (AuthentikUser
|
|||
}
|
||||
cookie, err := r.Cookie("jwt")
|
||||
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
|
||||
}
|
||||
|
||||
if cookie.Value == "" {
|
||||
http.Redirect(w, r, redirectURL, http.StatusTemporaryRedirect)
|
||||
e.redirectAuth(w, r)
|
||||
return AuthentikUser{}, false, nil
|
||||
}
|
||||
|
||||
|
@ -52,6 +52,16 @@ func (e EasyAuth) GetUser(w http.ResponseWriter, r *http.Request) (AuthentikUser
|
|||
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) {
|
||||
token, err := e.client.GetTokenFromCode(r.URL.Query().Get("code"))
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in a new issue