mirror of
https://github.com/fosrl/badger.git
synced 2026-03-06 10:46:43 +00:00
Feature: Allow Basic Auth challenge
This commit is contained in:
committed by
Owen Schwartz
parent
7d75628d86
commit
83e894f23f
71
main.go
71
main.go
@@ -38,12 +38,13 @@ type VerifyBody struct {
|
|||||||
|
|
||||||
type VerifyResponse struct {
|
type VerifyResponse struct {
|
||||||
Data struct {
|
Data struct {
|
||||||
Valid bool `json:"valid"`
|
HeaderAuthChallenged bool `json:"headerAuthChallenged"`
|
||||||
RedirectURL *string `json:"redirectUrl"`
|
Valid bool `json:"valid"`
|
||||||
Username *string `json:"username,omitempty"`
|
RedirectURL *string `json:"redirectUrl"`
|
||||||
Email *string `json:"email,omitempty"`
|
Username *string `json:"username,omitempty"`
|
||||||
Name *string `json:"name,omitempty"`
|
Email *string `json:"email,omitempty"`
|
||||||
ResponseHeaders map[string]string `json:"responseHeaders,omitempty"`
|
Name *string `json:"name,omitempty"`
|
||||||
|
ResponseHeaders map[string]string `json:"responseHeaders,omitempty"`
|
||||||
} `json:"data"`
|
} `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,6 +205,20 @@ func (p *Badger) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if result.Data.HeaderAuthChallenged {
|
||||||
|
fmt.Println("Badger: challenging client for header authentication")
|
||||||
|
rw.Header().Add("WWW-Authenticate", "Basic realm=\"pangolin\"")
|
||||||
|
|
||||||
|
if result.Data.RedirectURL != nil && *result.Data.RedirectURL != "" {
|
||||||
|
rw.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
|
rw.WriteHeader(http.StatusUnauthorized)
|
||||||
|
rw.Write([]byte(p.renderRedirectPage(*result.Data.RedirectURL)))
|
||||||
|
} else {
|
||||||
|
http.Error(rw, "Unauthorized", http.StatusUnauthorized)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if result.Data.RedirectURL != nil && *result.Data.RedirectURL != "" {
|
if result.Data.RedirectURL != nil && *result.Data.RedirectURL != "" {
|
||||||
fmt.Println("Badger: Redirecting to", *result.Data.RedirectURL)
|
fmt.Println("Badger: Redirecting to", *result.Data.RedirectURL)
|
||||||
http.Redirect(rw, req, *result.Data.RedirectURL, http.StatusFound)
|
http.Redirect(rw, req, *result.Data.RedirectURL, http.StatusFound)
|
||||||
@@ -254,3 +269,47 @@ func (p *Badger) getScheme(req *http.Request) string {
|
|||||||
}
|
}
|
||||||
return "http"
|
return "http"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Badger) renderRedirectPage(redirectURL string) string {
|
||||||
|
return fmt.Sprintf(`<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Redirecting...</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: Arial, sans-serif;
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
height: 100vh;
|
||||||
|
margin: 0;
|
||||||
|
background-color: #f5f5f5;
|
||||||
|
}
|
||||||
|
.container {
|
||||||
|
text-align: center;
|
||||||
|
padding: 2rem;
|
||||||
|
background: white;
|
||||||
|
border-radius: 8px;
|
||||||
|
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
||||||
|
}
|
||||||
|
a {
|
||||||
|
color: #0066cc;
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
a:hover {
|
||||||
|
text-decoration: underline;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<p>Redirecting...</p>
|
||||||
|
<p>If you are not redirected automatically, <a href="%s">click here</a>.</p>
|
||||||
|
</div>
|
||||||
|
<script>
|
||||||
|
window.location.href = "%s";
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>`, redirectURL, redirectURL)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user