git.iko.soy/typeable/oauth2-proxy
on 597ffeb help sign in
54 lines 1.4 kB history raw
1package util
3import (
4 "crypto/x509"
5 "fmt"
6 "io/ioutil"
7 "net/http"
8)
10func GetCertPool(paths []string) (*x509.CertPool, error) {
11 if len(paths) == 0 {
12 return nil, fmt.Errorf("invalid empty list of Root CAs file paths")
13 }
14 pool := x509.NewCertPool()
15 for _, path := range paths {
16 // Cert paths are a configurable option
17 data, err := ioutil.ReadFile(path) // #nosec G304
18 if err != nil {
19 return nil, fmt.Errorf("certificate authority file (%s) could not be read - %s", path, err)
20 }
21 if !pool.AppendCertsFromPEM(data) {
22 return nil, fmt.Errorf("loading certificate authority (%s) failed", path)
23 }
24 }
25 return pool, nil
28// GetRequestProto return the request host header or X-Forwarded-Proto if present
29func GetRequestProto(req *http.Request) string {
30 proto := req.Header.Get("X-Forwarded-Proto")
31 if proto == "" {
32 proto = req.URL.Scheme
33 }
34 return proto
37// GetRequestHost return the request host header or X-Forwarded-Host if present
38func GetRequestHost(req *http.Request) string {
39 host := req.Header.Get("X-Forwarded-Host")
40 if host == "" {
41 host = req.Host
42 }
43 return host
46// GetRequestURI return the request host header or X-Forwarded-Uri if present
47func GetRequestURI(req *http.Request) string {
48 uri := req.Header.Get("X-Forwarded-Uri")
49 if uri == "" {
50 // Use RequestURI to preserve ?query
51 uri = req.URL.RequestURI()
52 }
53 return uri