login: remember redirects
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -81,8 +82,8 @@ func (s *OAuthStore) DeleteSession(sessionID string) {
|
|||||||
s.mutex.Unlock()
|
s.mutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendToLoginPage(w http.ResponseWriter, r *http.Request) {
|
func sendToLoginPage(w http.ResponseWriter, r *http.Request, origin string) {
|
||||||
http.Redirect(w, r, "/oauth/login", http.StatusTemporaryRedirect)
|
http.Redirect(w, r, "/oauth/login?source="+url.QueryEscape(origin), http.StatusTemporaryRedirect)
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendToUnauthorized(w http.ResponseWriter, r *http.Request) {
|
func sendToUnauthorized(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -95,23 +96,12 @@ func generateRandomToken() string {
|
|||||||
return base64.StdEncoding.EncodeToString(b)
|
return base64.StdEncoding.EncodeToString(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func setRedirectCookie(w http.ResponseWriter, path string) {
|
func getRedirectFromCookie(r *http.Request) (string, error) {
|
||||||
http.SetCookie(w,
|
cookie, err := r.Cookie("redirect_origin")
|
||||||
&http.Cookie{
|
|
||||||
Name: "redirect_on_completion",
|
|
||||||
Value: path,
|
|
||||||
Path: "/",
|
|
||||||
HttpOnly: true,
|
|
||||||
SameSite: http.SameSiteStrictMode,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func getRedirectFromCookie(r *http.Request) string {
|
|
||||||
cookie, err := r.Cookie("redirect_on_completion")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "/"
|
return "", err
|
||||||
}
|
}
|
||||||
return cookie.Value
|
return cookie.Value, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//go:embed templates/LoginPage.html
|
//go:embed templates/LoginPage.html
|
||||||
@@ -147,6 +137,17 @@ func (s *OAuthStore) LoginPage() http.Handler {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if r.URL.Query().Has("source") {
|
||||||
|
http.SetCookie(w, &http.Cookie{
|
||||||
|
Name: "redirect_origin",
|
||||||
|
Value: r.URL.Query().Get("source"),
|
||||||
|
HttpOnly: true,
|
||||||
|
Secure: true,
|
||||||
|
SameSite: http.SameSiteLaxMode,
|
||||||
|
MaxAge: 60 * 10,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
url := s.oa2.AuthCodeURL(state)
|
url := s.oa2.AuthCodeURL(state)
|
||||||
|
|
||||||
provider := s.config.OAuthProvider.Kind
|
provider := s.config.OAuthProvider.Kind
|
||||||
@@ -173,13 +174,12 @@ func (s *OAuthStore) Protected(next http.Handler) http.Handler {
|
|||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
cookie, err := r.Cookie(SessionCookie)
|
cookie, err := r.Cookie(SessionCookie)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
sendToLoginPage(w, r)
|
sendToLoginPage(w, r, r.URL.Path)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
sess, exists := s.GetSession(cookie.Value)
|
sess, exists := s.GetSession(cookie.Value)
|
||||||
if !exists {
|
if !exists {
|
||||||
setRedirectCookie(w, r.URL.Path)
|
sendToLoginPage(w, r, r.URL.Path)
|
||||||
sendToLoginPage(w, r)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -232,7 +232,10 @@ func (s *OAuthStore) CallbackHandler() http.Handler {
|
|||||||
Path: "/",
|
Path: "/",
|
||||||
})
|
})
|
||||||
|
|
||||||
redirect := getRedirectFromCookie(r)
|
redirect, err := getRedirectFromCookie(r)
|
||||||
|
if err != nil {
|
||||||
|
redirect = "/"
|
||||||
|
}
|
||||||
|
|
||||||
// clear cookies
|
// clear cookies
|
||||||
http.SetCookie(w, &http.Cookie{
|
http.SetCookie(w, &http.Cookie{
|
||||||
@@ -246,7 +249,6 @@ func (s *OAuthStore) CallbackHandler() http.Handler {
|
|||||||
MaxAge: -1,
|
MaxAge: -1,
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO: remember what path the user was on and redirect them back there after doing the whole login process
|
|
||||||
http.Redirect(w, r, redirect, http.StatusTemporaryRedirect)
|
http.Redirect(w, r, redirect, http.StatusTemporaryRedirect)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user