From 28165497d703c18177f84344015ed10e4a0a35ee Mon Sep 17 00:00:00 2001 From: YR Chen Date: Thu, 29 Aug 2024 00:45:01 +0800 Subject: [PATCH] Fix setting headers on authentication API call --- auth.go | 4 +++- legacy_auth.go | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/auth.go b/auth.go index 843918c..5ffd496 100644 --- a/auth.go +++ b/auth.go @@ -100,7 +100,9 @@ func (auth *RESTfulAuthenticator) Auth(request AuthRequest, username string) (in if err != nil { return 0, nil, err } - req.Header = auth.Headers + req.Header = auth.Headers.Clone() + req.Header.Set("accept", "application/json") + req.Header.Set("content-type", "application/json") res, err := http.DefaultClient.Do(req) if err != nil { diff --git a/legacy_auth.go b/legacy_auth.go index 0d58af3..be8c736 100644 --- a/legacy_auth.go +++ b/legacy_auth.go @@ -52,9 +52,14 @@ type LegacyAuthenticator struct { Recovery RecoveryConfig UsernamePolicy UsernamePolicyConfig PasswordPolicy PasswordPolicyConfig + Headers http.Header } func makeLegacyAuthenticator(auth AuthConfig, recovery RecoveryConfig) LegacyAuthenticator { + headers := http.Header{} + for _, header := range auth.Headers { + headers.Add(header.Name, header.Value) + } return LegacyAuthenticator{ Endpoint: auth.Endpoint, Token: auth.Token, @@ -67,6 +72,7 @@ func makeLegacyAuthenticator(auth AuthConfig, recovery RecoveryConfig) LegacyAut AllUsernameNoPassword: auth.AllUsernameNoPassword, UsernamesNoPassword: auth.UsernamesNoPassword, }, + Headers: headers, } } @@ -154,7 +160,14 @@ func (auth LegacyAuthenticator) AuthUser(request any, username string) (*LegacyA if err := json.NewEncoder(payload).Encode(request); err != nil { return nil, err } - res, err := http.Post(auth.Endpoint, "application/json", payload) + req, err := http.NewRequest("POST", auth.Endpoint, payload) + if err != nil { + return nil, err + } + req.Header = auth.Headers.Clone() + req.Header.Set("accept", "application/json") + req.Header.Set("content-type", "application/json") + res, err := http.DefaultClient.Do(req) if err != nil { return nil, err }