2021-01-17 21:29:08 +00:00
|
|
|
package httpd
|
|
|
|
|
|
|
|
import (
|
2021-02-03 07:55:28 +00:00
|
|
|
"errors"
|
2021-01-17 21:29:08 +00:00
|
|
|
"net/http"
|
2021-05-30 21:07:46 +00:00
|
|
|
"runtime/debug"
|
2021-01-17 21:29:08 +00:00
|
|
|
|
2021-05-30 21:07:46 +00:00
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
2021-03-05 17:50:45 +00:00
|
|
|
"github.com/go-chi/jwtauth/v5"
|
2021-01-17 21:29:08 +00:00
|
|
|
"github.com/lestrrat-go/jwx/jwt"
|
|
|
|
|
|
|
|
"github.com/drakkan/sftpgo/logger"
|
2021-02-02 08:14:10 +00:00
|
|
|
"github.com/drakkan/sftpgo/utils"
|
2021-01-17 21:29:08 +00:00
|
|
|
)
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
var (
|
2021-05-11 04:54:06 +00:00
|
|
|
forwardedProtoKey = &contextKey{"forwarded proto"}
|
|
|
|
errInvalidToken = errors.New("invalid JWT token")
|
2021-05-06 19:35:43 +00:00
|
|
|
)
|
2021-01-17 21:29:08 +00:00
|
|
|
|
2021-02-03 07:55:28 +00:00
|
|
|
type contextKey struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *contextKey) String() string {
|
|
|
|
return "context value " + k.name
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
func validateJWTToken(w http.ResponseWriter, r *http.Request, audience tokenAudience) error {
|
|
|
|
token, _, err := jwtauth.FromContext(r.Context())
|
|
|
|
|
|
|
|
var redirectPath string
|
|
|
|
if audience == tokenAudienceWebAdmin {
|
|
|
|
redirectPath = webLoginPath
|
|
|
|
} else {
|
|
|
|
redirectPath = webClientLoginPath
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
|
2021-06-05 14:07:09 +00:00
|
|
|
isAPIToken := (audience == tokenAudienceAPI || audience == tokenAudienceAPIUser)
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
if err != nil || token == nil {
|
|
|
|
logger.Debug(logSender, "", "error getting jwt token: %v", err)
|
2021-06-05 14:07:09 +00:00
|
|
|
if isAPIToken {
|
2021-01-17 21:29:08 +00:00
|
|
|
sendAPIResponse(w, r, err, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
2021-05-06 19:35:43 +00:00
|
|
|
} else {
|
|
|
|
http.Redirect(w, r, redirectPath, http.StatusFound)
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
return errInvalidToken
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
err = jwt.Validate(token)
|
|
|
|
if err != nil {
|
|
|
|
logger.Debug(logSender, "", "error validating jwt token: %v", err)
|
2021-06-05 14:07:09 +00:00
|
|
|
if isAPIToken {
|
2021-01-17 21:29:08 +00:00
|
|
|
sendAPIResponse(w, r, err, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
|
2021-05-06 19:35:43 +00:00
|
|
|
} else {
|
|
|
|
http.Redirect(w, r, redirectPath, http.StatusFound)
|
2021-01-17 21:29:08 +00:00
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
return errInvalidToken
|
|
|
|
}
|
|
|
|
if !utils.IsStringInSlice(audience, token.Audience()) {
|
|
|
|
logger.Debug(logSender, "", "the token is not valid for audience %#v", audience)
|
2021-06-05 14:07:09 +00:00
|
|
|
if isAPIToken {
|
2021-02-02 08:14:10 +00:00
|
|
|
sendAPIResponse(w, r, nil, "Your token audience is not valid", http.StatusUnauthorized)
|
2021-05-06 19:35:43 +00:00
|
|
|
} else {
|
|
|
|
http.Redirect(w, r, redirectPath, http.StatusFound)
|
2021-02-02 08:14:10 +00:00
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
return errInvalidToken
|
|
|
|
}
|
|
|
|
if isTokenInvalidated(r) {
|
|
|
|
logger.Debug(logSender, "", "the token has been invalidated")
|
2021-06-05 14:07:09 +00:00
|
|
|
if isAPIToken {
|
2021-01-26 21:35:36 +00:00
|
|
|
sendAPIResponse(w, r, nil, "Your token is no longer valid", http.StatusUnauthorized)
|
2021-05-06 19:35:43 +00:00
|
|
|
} else {
|
|
|
|
http.Redirect(w, r, redirectPath, http.StatusFound)
|
|
|
|
}
|
|
|
|
return errInvalidToken
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func jwtAuthenticatorAPI(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if err := validateJWTToken(w, r, tokenAudienceAPI); err != nil {
|
2021-01-26 21:35:36 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
|
|
|
|
// Token is authenticated, pass it through
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-05 14:07:09 +00:00
|
|
|
func jwtAuthenticatorAPIUser(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if err := validateJWTToken(w, r, tokenAudienceAPIUser); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Token is authenticated, pass it through
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
func jwtAuthenticatorWebAdmin(next http.Handler) http.Handler {
|
2021-01-17 21:29:08 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2021-05-06 19:35:43 +00:00
|
|
|
if err := validateJWTToken(w, r, tokenAudienceWebAdmin); err != nil {
|
2021-01-17 21:29:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
// Token is authenticated, pass it through
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func jwtAuthenticatorWebClient(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if err := validateJWTToken(w, r, tokenAudienceWebClient); err != nil {
|
2021-01-26 21:35:36 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
|
|
|
|
// Token is authenticated, pass it through
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-05 14:07:09 +00:00
|
|
|
//nolint:unparam
|
|
|
|
func checkHTTPUserPerm(perm string) func(next http.Handler) http.Handler {
|
2021-05-06 19:35:43 +00:00
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
_, claims, err := jwtauth.FromContext(r.Context())
|
|
|
|
if err != nil {
|
2021-06-05 14:07:09 +00:00
|
|
|
if isWebRequest(r) {
|
|
|
|
renderClientBadRequestPage(w, r, err)
|
|
|
|
} else {
|
|
|
|
sendAPIResponse(w, r, err, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
tokenClaims := jwtTokenClaims{}
|
|
|
|
tokenClaims.Decode(claims)
|
|
|
|
// for web client perms are negated and not granted
|
|
|
|
if tokenClaims.hasPerm(perm) {
|
2021-06-05 14:07:09 +00:00
|
|
|
if isWebRequest(r) {
|
|
|
|
renderClientForbiddenPage(w, r, "You don't have permission for this action")
|
|
|
|
} else {
|
|
|
|
sendAPIResponse(w, r, nil, http.StatusText(http.StatusForbidden), http.StatusForbidden)
|
|
|
|
}
|
2021-05-06 19:35:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
func checkPerm(perm string) func(next http.Handler) http.Handler {
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
_, claims, err := jwtauth.FromContext(r.Context())
|
|
|
|
if err != nil {
|
2021-05-06 19:35:43 +00:00
|
|
|
if isWebRequest(r) {
|
2021-01-17 21:29:08 +00:00
|
|
|
renderBadRequestPage(w, r, err)
|
|
|
|
} else {
|
|
|
|
sendAPIResponse(w, r, err, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tokenClaims := jwtTokenClaims{}
|
|
|
|
tokenClaims.Decode(claims)
|
|
|
|
|
|
|
|
if !tokenClaims.hasPerm(perm) {
|
2021-05-06 19:35:43 +00:00
|
|
|
if isWebRequest(r) {
|
2021-01-17 21:29:08 +00:00
|
|
|
renderForbiddenPage(w, r, "You don't have permission for this action")
|
|
|
|
} else {
|
|
|
|
sendAPIResponse(w, r, nil, http.StatusText(http.StatusForbidden), http.StatusForbidden)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-02-03 07:55:28 +00:00
|
|
|
|
|
|
|
func verifyCSRFHeader(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
tokenString := r.Header.Get(csrfHeaderToken)
|
|
|
|
token, err := jwtauth.VerifyToken(csrfTokenAuth, tokenString)
|
|
|
|
if err != nil || token == nil {
|
|
|
|
logger.Debug(logSender, "", "error validating CSRF header: %v", err)
|
|
|
|
sendAPIResponse(w, r, err, "Invalid token", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !utils.IsStringInSlice(tokenAudienceCSRF, token.Audience()) {
|
|
|
|
logger.Debug(logSender, "", "error validating CSRF header audience")
|
2021-03-21 18:15:47 +00:00
|
|
|
sendAPIResponse(w, r, errors.New("the token is not valid"), "", http.StatusForbidden)
|
2021-02-03 07:55:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
2021-05-30 21:07:46 +00:00
|
|
|
|
|
|
|
func recoverer(next http.Handler) http.Handler {
|
|
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer func() {
|
|
|
|
if rvr := recover(); rvr != nil {
|
|
|
|
if rvr == http.ErrAbortHandler {
|
|
|
|
panic(rvr)
|
|
|
|
}
|
|
|
|
|
|
|
|
logEntry := middleware.GetLogEntry(r)
|
|
|
|
if logEntry != nil {
|
|
|
|
logEntry.Panic(rvr, debug.Stack())
|
|
|
|
} else {
|
|
|
|
middleware.PrintPrettyStack(rvr)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.HandlerFunc(fn)
|
|
|
|
}
|