Skip to content

Commit

Permalink
http to https
Browse files Browse the repository at this point in the history
  • Loading branch information
Gwojda committed Aug 3, 2023
1 parent 5273356 commit 6ce3059
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,20 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
}

func (k *keycloakAuth) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if req.URL.Scheme == "http" {
req.URL.Scheme = "https"

host := req.Header.Get("X-Forwarded-Host")
originalURL := fmt.Sprintf("%s://%s%s", req.URL.Scheme, host, req.RequestURI)
scheme := req.Header.Get("X-Forwarded-Proto")
if scheme == "http" {
target := "https://" + req.Host + req.URL.Path
if len(req.URL.RawQuery) > 0 {
target += "?" + req.URL.RawQuery
}

http.Redirect(rw, req, originalURL, http.StatusFound)
http.Redirect(rw, req, target, http.StatusMovedPermanently)
return
} else if req.URL.Scheme != "https" {
http.Error(rw, req.URL.Scheme, http.StatusBadRequest)
} else if scheme != "https" {
http.Error(rw, "Invalid scheme: "+scheme, http.StatusBadRequest)
return
}

cookie, err := req.Cookie("Authorization")
if err == nil && strings.HasPrefix(cookie.Value, "Bearer ") {
token := strings.TrimPrefix(cookie.Value, "Bearer ")
Expand Down

0 comments on commit 6ce3059

Please sign in to comment.