fix naming

This commit is contained in:
Milo Schwartz
2024-10-25 21:57:58 -04:00
parent 387ce901b9
commit 08a6336898
3 changed files with 3 additions and 8 deletions

View File

@@ -1,9 +1,9 @@
displayName: badger displayName: Fossorial Badger
type: middleware type: middleware
import: github.com/fosrl/badger import: github.com/fosrl/badger
summary: Middleware auth bouncer for Fossorial summary: Middleware auth bouncer for Pangolin
testData: testData:
apiBaseUrl: http://localhost:3001/api/v1 apiBaseUrl: http://localhost:3001/api/v1

View File

@@ -1,3 +1,3 @@
# badger # badger
Custom Traefik plugin middleware for auth Middleware auth bouncer for Pangolin

View File

@@ -35,26 +35,21 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
} }
func (p *Badger) ServeHTTP(rw http.ResponseWriter, req *http.Request) { func (p *Badger) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// Check if the session cookie exists
cookie, err := req.Cookie(SessionCookieName) cookie, err := req.Cookie(SessionCookieName)
if err != nil { if err != nil {
// No session cookie, redirect to login
originalRequestURL := url.QueryEscape(req.URL.String()) originalRequestURL := url.QueryEscape(req.URL.String())
http.Redirect(rw, req, fmt.Sprintf("%s/auth/login?redirect=%s", p.appBaseUrl, originalRequestURL), http.StatusFound) http.Redirect(rw, req, fmt.Sprintf("%s/auth/login?redirect=%s", p.appBaseUrl, originalRequestURL), http.StatusFound)
return return
} }
// Verify the user with the session ID
sessionID := cookie.Value sessionID := cookie.Value
verifyURL := fmt.Sprintf("%s/badger/verify-user?sessionId=%s", p.apiBaseUrl, sessionID) verifyURL := fmt.Sprintf("%s/badger/verify-user?sessionId=%s", p.apiBaseUrl, sessionID)
resp, err := http.Get(verifyURL) resp, err := http.Get(verifyURL)
if err != nil || resp.StatusCode != http.StatusOK { if err != nil || resp.StatusCode != http.StatusOK {
// If unauthorized (401), redirect to the homepage
if resp != nil && resp.StatusCode == http.StatusUnauthorized { if resp != nil && resp.StatusCode == http.StatusUnauthorized {
http.Redirect(rw, req, p.appBaseUrl, http.StatusFound) http.Redirect(rw, req, p.appBaseUrl, http.StatusFound)
} else { } else {
// Handle other errors, possibly log them (you can adjust the error handling here)
http.Error(rw, "Internal Server Error", http.StatusInternalServerError) http.Error(rw, "Internal Server Error", http.StatusInternalServerError)
} }
return return