Skip to content

Commit

Permalink
Resolves #378 - Add support for auth hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-r-west committed Oct 2, 2023
1 parent e09c5f1 commit cb5bd41
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions external/authentication/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -256,5 +278,6 @@ func fetchNewAuthenticationToken(values url.Values) (*ApiTokenResponse, error) {
}

log.Trace("Authentication successful")

return &authResponse, nil
}

0 comments on commit cb5bd41

Please sign in to comment.