Created
May 10, 2021 15:52
-
-
Save LautaroJayat/3251c7be1efb6825a3e2cd51c0017fd6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package strategy | |
import ( | |
"io" | |
"net/http" | |
"net/http/httputil" | |
"os" | |
"github.com/julienschmidt/httprouter" | |
) | |
func CheckAuthHeader(rp *httputil.ReverseProxy) httprouter.Handle{ | |
//Getting envs | |
secret := os.Getenv("SECRET") | |
hardCodedAuth := os.Getenv("AUTH") | |
//Returning our handler | |
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params){ | |
if (secret!= ""){ | |
r.Header.Add("SECRET", secret) | |
} | |
auth := r.Header.Get("Auth") | |
if auth != hardCodedAuth { | |
w.WriteHeader(http.StatusForbidden) | |
io.WriteString(w, "Auth needed\n") | |
return | |
} | |
rp.ServeHTTP(w, r) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment