fix: add unauthorized instead of sending to a loop

This commit is contained in:
Lukas Werner
2025-08-31 09:57:32 -07:00
parent 3920b8913d
commit 7b7bebe701
3 changed files with 94 additions and 1 deletions
+16 -1
View File
@@ -84,6 +84,11 @@ func (s *OAuthStore) DeleteSession(sessionID string) {
func sendToLoginPage(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/oauth/login", http.StatusTemporaryRedirect)
}
func sendToUnauthorized(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/oauth/unauthorized", http.StatusTemporaryRedirect)
}
func generateRandomToken() string {
b := make([]byte, 32)
rand.Read(b)
@@ -93,6 +98,16 @@ func generateRandomToken() string {
//go:embed templates/LoginPage.html
var loginPageContent string
//go:embed templates/NotAuthorizedPage.html
var unauthorizedPageContent string
func (s *OAuthStore) UnauthorizedPage() http.Handler {
unauthorizedPageTemplate := template.Must(template.New("unauthorizedPageContent").Parse(unauthorizedPageContent))
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
unauthorizedPageTemplate.Execute(w, nil)
})
}
func (s *OAuthStore) LoginPage() http.Handler {
loginPageTemplate := template.Must(template.New("loginPageContent").Parse(loginPageContent))
@@ -156,7 +171,7 @@ func (s *OAuthStore) Protected(next http.Handler) http.Handler {
}
}
if !found {
sendToLoginPage(w, r)
sendToUnauthorized(w, r)
return
}