Skip to content

Instantly share code, notes, and snippets.

@LautaroJayat
Created May 10, 2021 15:52
Show Gist options
  • Save LautaroJayat/3251c7be1efb6825a3e2cd51c0017fd6 to your computer and use it in GitHub Desktop.
Save LautaroJayat/3251c7be1efb6825a3e2cd51c0017fd6 to your computer and use it in GitHub Desktop.
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