-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
84 lines (69 loc) · 2.32 KB
/
proxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package auth
import (
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"github.com/damiannolan/auth-proxy/openid"
"github.com/damiannolan/auth-proxy/realm"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
// ProxyService -
type ProxyService struct {
path string
providers map[string]*openid.AuthenticationProvider
upstream *httputil.ReverseProxy
}
// NewProxyService -
func NewProxyService() (*ProxyService, error) { // Maybe pass viper instance or config with fields - would be large config
rawURL := fmt.Sprintf("%s:%s", viper.GetString("services.upstream.host"), viper.GetString("services.upstream.port"))
url, err := url.Parse(rawURL)
if err != nil {
return nil, err
}
proxy := &ProxyService{
path: viper.GetString("oauth.url"),
providers: make(map[string]*openid.AuthenticationProvider),
upstream: httputil.NewSingleHostReverseProxy(url),
}
return proxy, nil
}
// Handler bootstraps package routes and their respective HTTP HandlerFuncs
// returning a standard http.Handler interface to be served independenly or mounted in a routing chain
func (svc *ProxyService) Handler() http.Handler {
mux := chi.NewMux()
mux.Use(middleware.DefaultLogger)
mux.Use(middleware.Recoverer)
mux.Use(middleware.RequestID)
mux.Use(realm.DiscoveryMiddleware())
// Change the below to be wrapped in a middleware func - mux.Use()
// mux.HandleFunc("/*", svc.upstream.ServeHTTP)
mux.Route(svc.Path(), func(r chi.Router) {
r.HandleFunc("/authorize", svc.authorize)
r.Get("/callback", svc.callback)
r.Get("/expired", svc.expired)
r.Get("/health", svc.health)
r.Post("/login", svc.login)
r.Get("/logout", svc.logout)
r.Get("/token", svc.token)
})
return mux
}
// ListenAndServe function
func (svc *ProxyService) ListenAndServe() error {
host := viper.GetString("services.auth-proxy.host")
port := viper.GetString(("services.auth-proxy.port"))
log.WithField("server", host).Info("listening on port ", port)
return http.ListenAndServe(port, svc.Handler())
}
// Path returns the configured path
func (svc *ProxyService) Path() string {
return svc.path
}
// RegisterProvider inserts the OpenID Connect Provider into the local cache
func (svc *ProxyService) RegisterProvider(key string, p *openid.AuthenticationProvider) {
svc.providers[key] = p
}