Skip to content

Commit

Permalink
feat: check for basic auth (#110) (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
Janhouse authored Feb 27, 2024
1 parent e7b17e6 commit 871c54f
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions internal/auth/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ const UserContextKey contextKey = "user"

func RequireAuthentication(h http.Handler, auth *Authenticator) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

username, password, usesBasicAuth := r.BasicAuth()
if usesBasicAuth {
user, err := auth.Login(username, password)
if err == nil {
ctx := context.WithValue(r.Context(), UserContextKey, user)
h.ServeHTTP(w, r.WithContext(ctx))
return
}
}

token, err := ParseBearerToken(r.Header.Get("Authorization"))
if err != nil {
http.Error(w, "Unauthorized (No Authorization Header)", http.StatusUnauthorized)
Expand Down

0 comments on commit 871c54f

Please sign in to comment.