Skip to content

Commit

Permalink
at least it works now
Browse files Browse the repository at this point in the history
  • Loading branch information
1lann committed Jan 5, 2024
1 parent 36cb78f commit 7208bca
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 28 deletions.
6 changes: 3 additions & 3 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (t *Tmpauth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)

t.DebugLog(fmt.Sprintf("proxying request to mini server: %s", u.String()))

resp, err := t.miniClient.Do(req)
resp, err := t.miniClient(req)
if err != nil {
return http.StatusInternalServerError, err
}
Expand Down Expand Up @@ -282,7 +282,7 @@ func (t *Tmpauth) StartAuth(w http.ResponseWriter, r *http.Request) (int, error)
req.Header.Set(HostHeader, r.Host)
req.Header.Set("Content-Type", "application/jwt")

resp, err := t.miniClient.Do(req)
resp, err := t.miniClient(req)
if err != nil {
return http.StatusInternalServerError, fmt.Errorf("StartAuth on mini server: %w", err)
}
Expand Down Expand Up @@ -400,7 +400,7 @@ func (t *Tmpauth) Whomst() (map[string]json.RawMessage, error) {

req.Header.Set(ConfigIDHeader, t.miniConfigID)

resp, respErr = t.miniClient.Do(req)
resp, respErr = t.miniClient(req)
} else {
resp, respErr = t.HttpClient.Get("https://" + TmpAuthHost + "/whomst")
}
Expand Down
24 changes: 1 addition & 23 deletions mini.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,6 @@ type RemoteConfig struct {
Secret []byte
}

// the registry and TransportWorkaround are used to work around a bug in yaegi.
var transportRegistry = make(map[string]func(req *http.Request) (*http.Response, error))

type TransportWorkaround struct {
configID string
}

func (t TransportWorkaround) RoundTrip(req *http.Request) (*http.Response, error) {
return transportRegistry[t.configID](req)
}

func DoTransportWorkaround(a interface{}) http.RoundTripper {
return a.(http.RoundTripper)
}

func NewMini(config MiniConfig, next CaddyHandleFunc) (*Tmpauth, error) {
var lastErr error
var remoteConfig RemoteConfig
Expand Down Expand Up @@ -138,14 +123,7 @@ func NewMini(config MiniConfig, next CaddyHandleFunc) (*Tmpauth, error) {
tmpauth: t,
}

transportRegistry[remoteConfig.ConfigID] = transport.RoundTrip

t.miniClient = &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
Transport: DoTransportWorkaround(TransportWorkaround{configID: remoteConfig.ConfigID}),
}
t.miniClient = transport.RoundTrip

return t, nil
}
Expand Down
2 changes: 1 addition & 1 deletion setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type Tmpauth struct {
miniServerHost string
miniConfigID string
miniConfigJSON []byte
miniClient *http.Client
miniClient func(req *http.Request) (*http.Response, error)

done chan struct{}
doneOnce sync.Once
Expand Down
2 changes: 1 addition & 1 deletion token.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (t *Tmpauth) ParseWrappedAuthJWT(tokenStr string) (*CachedToken, error) {
req.Header.Set(ConfigIDHeader, t.miniConfigID)
req.Header.Set("Content-Type", "application/jwt")

resp, err := t.miniClient.Do(req)
resp, err := t.miniClient(req)
if err != nil {
return nil, fmt.Errorf("ParseWrappedAuthJWT on mini server: %w", err)
}
Expand Down

0 comments on commit 7208bca

Please sign in to comment.