1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2025-01-10 04:18:14 +02:00
oauth2-proxy/pkg/util/util.go

36 lines
864 B
Go
Raw Normal View History

package util
import (
"crypto/x509"
"fmt"
"io/ioutil"
2020-08-22 04:50:32 +02:00
"net/http"
)
func GetCertPool(paths []string) (*x509.CertPool, error) {
if len(paths) == 0 {
return nil, fmt.Errorf("invalid empty list of Root CAs file paths")
}
pool := x509.NewCertPool()
for _, path := range paths {
2020-07-21 03:49:45 +02:00
// Cert paths are a configurable option
data, err := ioutil.ReadFile(path) // #nosec G304
if err != nil {
return nil, fmt.Errorf("certificate authority file (%s) could not be read - %s", path, err)
}
if !pool.AppendCertsFromPEM(data) {
return nil, fmt.Errorf("loading certificate authority (%s) failed", path)
}
}
return pool, nil
}
2020-08-22 04:50:32 +02:00
// GetRequestHost return the request host header or X-Forwarded-Host if present
func GetRequestHost(req *http.Request) string {
host := req.Header.Get("X-Forwarded-Host")
if host == "" {
host = req.Host
}
return host
}