Skip to content

Commit

Permalink
Fix setting headers on authentication API call
Browse files Browse the repository at this point in the history
  • Loading branch information
stevapple committed Aug 28, 2024
1 parent 2cec205 commit 2816549
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 3 additions & 1 deletion auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 14 additions & 1 deletion legacy_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -67,6 +72,7 @@ func makeLegacyAuthenticator(auth AuthConfig, recovery RecoveryConfig) LegacyAut
AllUsernameNoPassword: auth.AllUsernameNoPassword,
UsernamesNoPassword: auth.UsernamesNoPassword,
},
Headers: headers,
}
}

Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 2816549

Please sign in to comment.