From cb5bd41385c5b38d1885e2bd07dcd1423be06ed1 Mon Sep 17 00:00:00 2001 From: Steve Ramage Date: Mon, 2 Oct 2023 11:44:46 -0700 Subject: [PATCH] Resolves #378 - Add support for auth hooks --- external/authentication/auth.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/external/authentication/auth.go b/external/authentication/auth.go index ff3d7a7..6fbfc9c 100644 --- a/external/authentication/auth.go +++ b/external/authentication/auth.go @@ -37,6 +37,20 @@ var noTokenWarningMessageLogged = false var getTokenMutex = sync.Mutex{} +var postAuthErrorHook = []func(r *http.Request, e error){} +var postAuthHook = []func(r *http.Request, s *http.Response){} + +func AddPostAuthErrorHook(f func(r *http.Request, e error)) { + getTokenMutex.Lock() + defer getTokenMutex.Unlock() + postAuthErrorHook = append(postAuthErrorHook, f) +} + +func AddPostAuthHook(f func(r *http.Request, s *http.Response)) { + getTokenMutex.Lock() + defer getTokenMutex.Unlock() + postAuthHook = append(postAuthHook, f) +} func GetAuthenticationToken(useTokenFromProfileDir bool, valuesOverride *url.Values) (*ApiTokenResponse, error) { if useTokenFromProfileDir { @@ -189,11 +203,19 @@ func fetchNewAuthenticationToken(values url.Values) (*ApiTokenResponse, error) { resp, err := HttpClient.Do(req) if err != nil { + for _, f := range postAuthErrorHook { + f(req, err) + } + return nil, err } defer resp.Body.Close() + for _, f := range postAuthHook { + f(req, resp) + } + dumpRes, _ := httputil.DumpResponse(resp, true) profiles.LogRequestToDisk("POST", req.URL.Path, dumpReq, dumpRes, resp.StatusCode) @@ -256,5 +278,6 @@ func fetchNewAuthenticationToken(values url.Values) (*ApiTokenResponse, error) { } log.Trace("Authentication successful") + return &authResponse, nil }