mirror of
https://github.com/garethgeorge/backrest.git
synced 2025-12-15 18:15:37 +00:00
@@ -17,6 +17,17 @@ const UserContextKey contextKey = "user"
|
|||||||
|
|
||||||
func RequireAuthentication(h http.Handler, auth *Authenticator) http.Handler {
|
func RequireAuthentication(h http.Handler, auth *Authenticator) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
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"))
|
token, err := ParseBearerToken(r.Header.Get("Authorization"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Unauthorized (No Authorization Header)", http.StatusUnauthorized)
|
http.Error(w, "Unauthorized (No Authorization Header)", http.StatusUnauthorized)
|
||||||
|
|||||||
Reference in New Issue
Block a user