@@ -14,32 +14,59 @@ import (
1414 "github.com/xenitab/git-auth-proxy/pkg/auth"
1515)
1616
17- type Server struct {
18- srv * http. Server
17+ type GitProxy struct {
18+ authz * auth. Authorizer
1919}
2020
21- func NewServer (ctx context.Context , addr string , authz * auth.Authorizer ) * Server {
21+ func NewGitProxy (authz * auth.Authorizer ) * GitProxy {
22+ return & GitProxy {
23+ authz : authz ,
24+ }
25+ }
26+
27+ func (g * GitProxy ) Server (ctx context.Context , addr string ) * http.Server {
2228 cfg := pkggin .DefaultConfig ()
2329 cfg .LogConfig .Logger = logr .FromContextOrDiscard (ctx )
2430 cfg .MetricsConfig .HandlerID = "proxy"
2531 router := pkggin .NewEngine (cfg )
2632 router .GET ("/readyz" , readinessHandler )
2733 router .GET ("/healthz" , livenessHandler )
28- router .NoRoute (proxyHandler ( authz ) )
34+ router .NoRoute (g . proxyHandler )
2935 // The ReadTimeout is set to 5 min make sure that strange requests don't live forever
3036 // But in general the external request should set a good timeout value for it's request.
3137 srv := & http.Server {ReadTimeout : 5 * time .Minute , Addr : addr , Handler : router }
32- return & Server {
33- srv : srv ,
34- }
38+ return srv
3539}
3640
37- func (s * Server ) ListenAndServe () error {
38- return s .srv .ListenAndServe ()
39- }
41+ func (g * GitProxy ) proxyHandler (c * gin.Context ) {
42+ // Get the token from the request
43+ token , err := getTokenFromRequest (c .Request )
44+ if err != nil {
45+ c .Header ("WWW-Authenticate" , "Basic realm=\" Restricted\" " )
46+ c .String (http .StatusUnauthorized , "Missing basic authentication" )
47+ return
48+ }
49+ // Check basic auth with local auth configuration
50+ err = g .authz .IsPermitted (c .Request .URL .EscapedPath (), token )
51+ if err != nil {
52+ //nolint: errcheck //ignore
53+ c .Error (fmt .Errorf ("Received unauthorized request: %w" , err ))
54+ c .String (http .StatusForbidden , "User not permitted" )
55+ return
56+ }
57+ // Authenticate the request with the proper token
58+ req , url , err := g .authz .UpdateRequest (c .Request .Context (), c .Request , token )
59+ if err != nil {
60+ //nolint: errcheck //ignore
61+ c .Error (fmt .Errorf ("Could not authenticate request: %w" , err ))
62+ c .String (http .StatusInternalServerError , "Internal server error" )
63+ return
64+ }
4065
41- func (s * Server ) Shutdown (ctx context.Context ) error {
42- return s .srv .Shutdown (ctx )
66+ // TODO (Philip): Add caching of the proxy
67+ // Forward the request to the correct proxy
68+ proxy := httputil .NewSingleHostReverseProxy (url )
69+ proxy .ServeHTTP (c .Writer , req )
4370}
4471
4572func readinessHandler (c * gin.Context ) {
@@ -49,36 +76,3 @@ func readinessHandler(c *gin.Context) {
4976func livenessHandler (c * gin.Context ) {
5077 c .Status (http .StatusOK )
5178}
52-
53- func proxyHandler (authz * auth.Authorizer ) gin.HandlerFunc {
54- return func (c * gin.Context ) {
55- // Get the token from the request
56- token , err := getTokenFromRequest (c .Request )
57- if err != nil {
58- c .Header ("WWW-Authenticate" , "Basic realm=\" Restricted\" " )
59- c .String (http .StatusUnauthorized , "Missing basic authentication" )
60- return
61- }
62- // Check basic auth with local auth configuration
63- err = authz .IsPermitted (c .Request .URL .EscapedPath (), token )
64- if err != nil {
65- //nolint: errcheck //ignore
66- c .Error (fmt .Errorf ("Received unauthorized request: %w" , err ))
67- c .String (http .StatusForbidden , "User not permitted" )
68- return
69- }
70- // Authenticate the request with the proper token
71- req , url , err := authz .UpdateRequest (c .Request .Context (), c .Request , token )
72- if err != nil {
73- //nolint: errcheck //ignore
74- c .Error (fmt .Errorf ("Could not authenticate request: %w" , err ))
75- c .String (http .StatusInternalServerError , "Internal server error" )
76- return
77- }
78-
79- // TODO (Philip): Add caching of the proxy
80- // Forward the request to the correct proxy
81- proxy := httputil .NewSingleHostReverseProxy (url )
82- proxy .ServeHTTP (c .Writer , req )
83- }
84- }
0 commit comments