mirror of
https://github.com/harness/drone.git
synced 2025-05-06 06:41:35 +08:00
45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
// Copyright 2021 Harness Inc. All rights reserved.
|
|
// Use of this source code is governed by the Polyform Free Trial License
|
|
// that can be found in the LICENSE.md file for this repository.
|
|
|
|
package authn
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/harness/gitness/internal/api/render"
|
|
"github.com/harness/gitness/internal/api/request"
|
|
"github.com/harness/gitness/internal/auth/authn"
|
|
|
|
"github.com/rs/zerolog"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
/*
|
|
* Attempt returns an http.HandlerFunc middleware that authenticates
|
|
* the http.Request if authentication payload is available.
|
|
*/
|
|
func Attempt(authenticator authn.Authenticator) func(http.Handler) http.Handler {
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
user, err := authenticator.Authenticate(r)
|
|
if err != nil {
|
|
render.Unauthorized(w, err)
|
|
return
|
|
} else if user == nil {
|
|
next.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
|
|
ctx := r.Context()
|
|
log.Ctx(ctx).UpdateContext(func(c zerolog.Context) zerolog.Context {
|
|
return c.Str("session_email", user.Email).Bool("session_admin", user.Admin)
|
|
})
|
|
|
|
next.ServeHTTP(w, r.WithContext(
|
|
request.WithUser(ctx, user),
|
|
))
|
|
})
|
|
}
|
|
}
|