Redirect http-requests to https (#29)

This commit is contained in:
Ilja Neumann authored and GitHub committed 2020-03-27 10:57:32 +01:00
1 parent e3bcc0d5aa
commit 976c13cf62
3 files changed
+26

No files matched your search

@@ -0,0 +1,4 @@
Change: Insecure http-requests are now redirected to https
https://github.com/owncloud/ocis-proxy/pull/29
+3
View File
@@ -236,6 +236,9 @@ func Server(cfg *config.Config) *cli.Command {
func loadMiddlewares(cfg *config.Config, l log.Logger) []func(handler http.Handler) http.Handler {
var configuredMiddlewares = make([]func(handler http.Handler) http.Handler, 0)
configuredMiddlewares = append(configuredMiddlewares, middleware.RedirectToHTTPS)
if cfg.OIDC != nil {
l.Info().Msg("Loading OIDC-Middleware")
l.Debug().Interface("oidc_config", cfg.OIDC).Msg("OIDC-Config")
+19
View File
@@ -0,0 +1,19 @@
package middleware
import (
"fmt"
"net/http"
)
// RedirectToHTTPS redirects insecure requests to https
func RedirectToHTTPS(next http.Handler) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
proto := req.Header.Get("x-forwarded-proto")
if proto == "http" || proto == "HTTP" {
http.Redirect(res, req, fmt.Sprintf("https://%s%s", req.Host, req.URL), http.StatusPermanentRedirect)
return
}
next.ServeHTTP(res, req)
})
}