1- // Copyright 2024 Keyfactor
1+ // Copyright 2026 Keyfactor
22//
33// Licensed under the Apache License, Version 2.0 (the "License");
44// you may not use this file except in compliance with the License.
@@ -16,9 +16,11 @@ package auth_providers_test
1616
1717import (
1818 "crypto/tls"
19+ "encoding/json"
1920 "encoding/pem"
2021 "fmt"
2122 "net/http"
23+ "net/http/httptest"
2224 "net/url"
2325 "os"
2426 "path/filepath"
@@ -568,3 +570,48 @@ func DownloadCertificate(input string, outputPath string) error {
568570 fmt .Printf ("Certificate chain saved to: %s\n " , outputFile )
569571 return nil
570572}
573+
574+ func TestCommandConfigOauth_TokenSourceIsReused (t * testing.T ) {
575+ tokenRequestCount := 0
576+
577+ // Fake IdP token endpoint
578+ tokenServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
579+ tokenRequestCount ++
580+ w .Header ().Set ("Content-Type" , "application/json" )
581+ json .NewEncoder (w ).Encode (map [string ]interface {}{
582+ "access_token" : "shared-test-token" ,
583+ "token_type" : "Bearer" ,
584+ "expires_in" : 3600 ,
585+ })
586+ }))
587+ defer tokenServer .Close ()
588+
589+ // Fake API endpoint (just needs to accept requests)
590+ apiServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
591+ w .WriteHeader (http .StatusOK )
592+ }))
593+ defer apiServer .Close ()
594+
595+ config := & auth_providers.CommandConfigOauth {
596+ ClientID : "test-client-id" ,
597+ ClientSecret : "test-client-secret" ,
598+ TokenURL : tokenServer .URL + "/token" ,
599+ }
600+
601+ // Get multiple clients from the same config
602+ const numClients = 3
603+ for i := 0 ; i < numClients ; i ++ {
604+ client , err := config .GetHttpClient ()
605+ if err != nil {
606+ t .Fatalf ("GetHttpClient() call %d failed: %v" , i + 1 , err )
607+ }
608+ _ , err = client .Get (apiServer .URL )
609+ if err != nil {
610+ t .Fatalf ("request %d failed: %v" , i + 1 , err )
611+ }
612+ }
613+
614+ if tokenRequestCount != 1 {
615+ t .Errorf ("expected token endpoint to be called once, got %d — token source is not being reused across GetHttpClient() calls" , tokenRequestCount )
616+ }
617+ }
0 commit comments