@@ -7,31 +7,43 @@ import (
77 "strings"
88
99 "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+
1013 "github.com/deploymenttheory/go-api-http-client/logger"
1114 "go.uber.org/zap"
1215)
1316
1417// HeaderHandler is responsible for managing and setting headers on HTTP requests.
1518type 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
2024}
2125
2226// 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 {
2428 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 ,
2933 }
3034}
3135
3236// 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
3547 if ! strings .HasPrefix (token , "Bearer " ) {
3648 token = "Bearer " + token
3749 }
@@ -103,7 +115,7 @@ func (h *HeaderHandler) SetRequestHeaders(endpoint string) {
103115 for header , value := range standardHeaders {
104116 if header == "Authorization" {
105117 // 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 "
107119 } else if value != "" {
108120 h .req .Header .Set (header , value )
109121 }
@@ -121,7 +133,7 @@ func (h *HeaderHandler) LogHeaders(hideSensitiveData bool) {
121133 // Redact sensitive values
122134 if len (values ) > 0 {
123135 // 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 ])
125137 redactedHeaders .Set (name , redactedValue )
126138 }
127139 }
@@ -157,19 +169,3 @@ func CheckDeprecationHeader(resp *http.Response, log logger.Logger) {
157169 )
158170 }
159171}
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