@@ -120,9 +120,20 @@ func (rw *HTTPReaderWriter) reset(pw *io.PipeWriter) io.WriteCloser {
120120}
121121
122122// applyOptionsToRequest applies the client Options (such as auth, headers, client info) to the given http.Request
123- func applyOptionsToRequest (req * http.Request , opt * Options ) {
124- if opt .TLS != nil && len (opt .Auth .JWT ) > 0 {
125- req .Header .Set ("Authorization" , "Bearer " + opt .Auth .JWT )
123+ func applyOptionsToRequest (ctx context.Context , req * http.Request , opt * Options ) error {
124+ jwt := queryOptionsJWT (ctx )
125+ useJWT := jwt != "" || useJWTAuth (opt )
126+
127+ if opt .TLS != nil && useJWT {
128+ if jwt == "" {
129+ var err error
130+ jwt , err = opt .GetJWT (ctx )
131+ if err != nil {
132+ return fmt .Errorf ("failed to get JWT: %w" , err )
133+ }
134+ }
135+
136+ req .Header .Set ("Authorization" , "Bearer " + jwt )
126137 } else if opt .TLS != nil && len (opt .Auth .Username ) > 0 {
127138 req .Header .Set ("X-ClickHouse-User" , opt .Auth .Username )
128139 if len (opt .Auth .Password ) > 0 {
@@ -145,6 +156,8 @@ func applyOptionsToRequest(req *http.Request, opt *Options) {
145156 for k , v := range opt .HttpHeaders {
146157 req .Header .Set (k , v )
147158 }
159+
160+ return nil
148161}
149162
150163func dialHttp (ctx context.Context , addr string , num int , opt * Options ) (* httpConnect , error ) {
@@ -462,7 +475,11 @@ func (h *httpConnect) createRequest(ctx context.Context, requestUrl string, read
462475 return nil , err
463476 }
464477
465- applyOptionsToRequest (req , h .opt )
478+ err = applyOptionsToRequest (ctx , req , h .opt )
479+ if err != nil {
480+ return nil , err
481+ }
482+
466483 for k , v := range headers {
467484 req .Header .Add (k , v )
468485 }
0 commit comments