@@ -7,31 +7,43 @@ import (
7
7
"strings"
8
8
9
9
"github.com/deploymenttheory/go-api-http-client/apiintegrations/apihandler"
10
+ "github.com/deploymenttheory/go-api-http-client/authenticationhandler"
11
+ "github.com/deploymenttheory/go-api-http-client/headers/redact"
12
+
10
13
"github.com/deploymenttheory/go-api-http-client/logger"
11
14
"go.uber.org/zap"
12
15
)
13
16
14
17
// HeaderHandler is responsible for managing and setting headers on HTTP requests.
15
18
type HeaderHandler struct {
16
- req * http.Request // The http.Request for which headers are being managed
17
- log logger.Logger // The logger to use for logging headers
18
- apiHandler apihandler.APIHandler // The APIHandler to use for retrieving standard headers
19
- token string // The token to use for setting the Authorization header
19
+ req * http.Request // The http.Request for which headers are being managed
20
+ log logger.Logger // The logger to use for logging headers
21
+ apiHandler apihandler.APIHandler // The APIHandler to use for retrieving standard headers
22
+ token string // The token to use for setting the Authorization header
23
+ authTokenHandler * authenticationhandler.AuthTokenHandler
20
24
}
21
25
22
26
// NewHeaderHandler creates a new instance of HeaderHandler for a given http.Request, logger, and APIHandler.
23
- func NewHeaderHandler (req * http.Request , log logger.Logger , apiHandler apihandler.APIHandler , token string ) * HeaderHandler {
27
+ func NewHeaderHandler (req * http.Request , log logger.Logger , apiHandler apihandler.APIHandler , authTokenHandler * authenticationhandler. AuthTokenHandler ) * HeaderHandler {
24
28
return & HeaderHandler {
25
- req : req ,
26
- log : log ,
27
- apiHandler : apiHandler ,
28
- token : token ,
29
+ req : req ,
30
+ log : log ,
31
+ apiHandler : apiHandler ,
32
+ authTokenHandler : authTokenHandler ,
29
33
}
30
34
}
31
35
32
36
// SetAuthorization sets the Authorization header for the request.
33
- func (h * HeaderHandler ) SetAuthorization (token string ) {
34
- // Ensure the token is prefixed with "Bearer " only once
37
+ // func (h *HeaderHandler) SetAuthorization(token string) {
38
+ // // Ensure the token is prefixed with "Bearer " only once
39
+ // if !strings.HasPrefix(token, "Bearer ") {
40
+ // token = "Bearer " + token
41
+ // }
42
+ // h.req.Header.Set("Authorization", token)
43
+ // }
44
+
45
+ func (h * HeaderHandler ) SetAuthorization () {
46
+ token := h .authTokenHandler .Token
35
47
if ! strings .HasPrefix (token , "Bearer " ) {
36
48
token = "Bearer " + token
37
49
}
@@ -103,7 +115,7 @@ func (h *HeaderHandler) SetRequestHeaders(endpoint string) {
103
115
for header , value := range standardHeaders {
104
116
if header == "Authorization" {
105
117
// Set the Authorization header using the token
106
- h .SetAuthorization (h . token ) // Ensure the token is correctly prefixed with "Bearer "
118
+ h .SetAuthorization () // Ensure the token is correctly prefixed with "Bearer "
107
119
} else if value != "" {
108
120
h .req .Header .Set (header , value )
109
121
}
@@ -121,7 +133,7 @@ func (h *HeaderHandler) LogHeaders(hideSensitiveData bool) {
121
133
// Redact sensitive values
122
134
if len (values ) > 0 {
123
135
// Use the first value for simplicity; adjust if multiple values per header are expected
124
- redactedValue := RedactSensitiveHeaderData (hideSensitiveData , name , values [0 ])
136
+ redactedValue := redact . RedactSensitiveHeaderData (hideSensitiveData , name , values [0 ])
125
137
redactedHeaders .Set (name , redactedValue )
126
138
}
127
139
}
@@ -157,19 +169,3 @@ func CheckDeprecationHeader(resp *http.Response, log logger.Logger) {
157
169
)
158
170
}
159
171
}
160
-
161
- // RedactSensitiveHeaderData redacts sensitive data based on the hideSensitiveData flag.
162
- func RedactSensitiveHeaderData (hideSensitiveData bool , key , value string ) string {
163
- if hideSensitiveData {
164
- // Define sensitive data keys that should be redacted.
165
- sensitiveKeys := map [string ]bool {
166
- "AccessToken" : true ,
167
- "Authorization" : true ,
168
- }
169
-
170
- if _ , found := sensitiveKeys [key ]; found {
171
- return "REDACTED"
172
- }
173
- }
174
- return value
175
- }
0 commit comments