Skip to content

Commit a24c213

Browse files
Add proxy authentication support to UAA token fetching
1 parent 9ebd81c commit a24c213

8 files changed

Lines changed: 264 additions & 19 deletions

File tree

auth_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ var _ = Describe("PivnetClient - Auth", func() {
3838
Host: apiAddress,
3939
UserAgent: userAgent,
4040
}
41-
accessTokenService := pivnet.NewAccessTokenOrLegacyToken(token, apiAddress, false)
41+
accessTokenService := pivnet.NewAccessTokenOrLegacyToken(token, apiAddress, false, pivnet.ProxyAuthConfig{})
4242
client = pivnet.NewClient(accessTokenService, newClientConfig, fakeLogger)
4343
})
4444

example/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func main() {
1616
SkipSSLValidation: true,
1717
}
1818

19-
accessTokenService := pivnet.NewAccessTokenOrLegacyToken("token-from-pivnet", config.Host, config.SkipSSLValidation)
19+
accessTokenService := pivnet.NewAccessTokenOrLegacyToken("token-from-pivnet", config.Host, config.SkipSSLValidation, pivnet.ProxyAuthConfig{})
2020

2121
stdoutLogger := log.New(os.Stdout, "", log.LstdFlags)
2222
stderrLogger := log.New(os.Stderr, "", log.LstdFlags)

example/proxy_basic_auth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ func main() {
5252
},
5353
}
5454

55-
// Create access token
56-
token := pivnet.NewAccessTokenOrLegacyToken(apiToken, config.Host, config.SkipSSLValidation)
55+
// Create access token with proxy auth config
56+
token := pivnet.NewAccessTokenOrLegacyToken(apiToken, config.Host, config.SkipSSLValidation, config.ProxyAuthConfig)
5757

5858
// Create the client with proxy support
5959
client, err := pivnet.NewClientWithProxy(token, config, logger)

example/proxy_spnego_auth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ func main() {
5656
},
5757
}
5858

59-
// Create access token
60-
token := pivnet.NewAccessTokenOrLegacyToken(apiToken, config.Host, config.SkipSSLValidation)
59+
// Create access token with proxy auth config
60+
token := pivnet.NewAccessTokenOrLegacyToken(apiToken, config.Host, config.SkipSSLValidation, config.ProxyAuthConfig)
6161

6262
// Create the client with proxy support
6363
fmt.Println("Initializing client with SPNEGO proxy authentication...")

integration/init_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var _ = BeforeSuite(func() {
4242
sanitizedWriter := sanitizer.NewSanitizer(sanitized, GinkgoWriter)
4343
GinkgoWriter = sanitizedWriter
4444

45-
accessTokenService := pivnet.NewAccessTokenOrLegacyToken(APIToken, Host, false)
45+
accessTokenService := pivnet.NewAccessTokenOrLegacyToken(APIToken, Host, false, pivnet.ProxyAuthConfig{})
4646

4747
config := pivnet.ClientConfig{
4848
Host: Host,

pivnet.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type AccessTokenOrLegacyToken struct {
5757
refreshToken string
5858
skipSSLValidation bool
5959
userAgent string
60+
proxyAuthConfig ProxyAuthConfig
6061
}
6162

6263
type QueryParameter struct {
@@ -68,7 +69,7 @@ func (o AccessTokenOrLegacyToken) AccessToken() (string, error) {
6869
const legacyAPITokenLength = 20
6970
if len(o.refreshToken) > legacyAPITokenLength {
7071
baseURL := fmt.Sprintf("%s%s", o.host, apiVersion)
71-
tokenFetcher := NewTokenFetcher(baseURL, o.refreshToken, o.skipSSLValidation, o.userAgent)
72+
tokenFetcher := NewTokenFetcher(baseURL, o.refreshToken, o.skipSSLValidation, o.userAgent, o.proxyAuthConfig)
7273

7374
accessToken, err := tokenFetcher.GetToken()
7475
if err != nil {
@@ -111,7 +112,7 @@ type AccessTokenService interface {
111112
AccessToken() (string, error)
112113
}
113114

114-
func NewAccessTokenOrLegacyToken(token string, host string, skipSSLValidation bool, userAgentOptional ...string) AccessTokenOrLegacyToken {
115+
func NewAccessTokenOrLegacyToken(token string, host string, skipSSLValidation bool, proxyAuthConfig ProxyAuthConfig, userAgentOptional ...string) AccessTokenOrLegacyToken {
115116
var userAgent = ""
116117
if len(userAgentOptional) > 0 {
117118
userAgent = userAgentOptional[0]
@@ -121,9 +122,25 @@ func NewAccessTokenOrLegacyToken(token string, host string, skipSSLValidation bo
121122
host: host,
122123
skipSSLValidation: skipSSLValidation,
123124
userAgent: userAgent,
125+
proxyAuthConfig: proxyAuthConfig,
124126
}
125127
}
126128

129+
//// NewAccessTokenOrLegacyTokenWithProxy creates an AccessTokenOrLegacyToken with proxy authentication support
130+
//func NewAccessTokenOrLegacyTokenWithProxy(token string, host string, skipSSLValidation bool, proxyAuthConfig ProxyAuthConfig, userAgentOptional ...string) AccessTokenOrLegacyToken {
131+
// var userAgent = ""
132+
// if len(userAgentOptional) > 0 {
133+
// userAgent = userAgentOptional[0]
134+
// }
135+
// return AccessTokenOrLegacyToken{
136+
// refreshToken: token,
137+
// host: host,
138+
// skipSSLValidation: skipSSLValidation,
139+
// userAgent: userAgent,
140+
// proxyAuthConfig: proxyAuthConfig,
141+
// }
142+
//}
143+
127144
// createProxyAuthTransport creates an HTTP transport with proxy authentication
128145
func createProxyAuthTransport(config ClientConfig) (http.RoundTripper, error) {
129146
// Validate required fields for proxy authentication

uaa.go

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"fmt"
88
"net/http"
9+
"net/url"
910
"time"
1011
)
1112

@@ -18,22 +19,62 @@ type TokenFetcher struct {
1819
RefreshToken string
1920
SkipSSLValidation bool
2021
UserAgent string
22+
ProxyAuthConfig ProxyAuthConfig
2123
}
2224

23-
func NewTokenFetcher(endpoint, refreshToken string, skipSSLValidation bool, userAgent string) *TokenFetcher {
24-
return &TokenFetcher{endpoint, refreshToken, skipSSLValidation, userAgent }
25+
func NewTokenFetcher(endpoint, refreshToken string, skipSSLValidation bool, userAgent string, proxyAuthConfig ProxyAuthConfig) *TokenFetcher {
26+
return &TokenFetcher{endpoint, refreshToken, skipSSLValidation, userAgent, proxyAuthConfig}
2527
}
2628

2729
func (t TokenFetcher) GetToken() (string, error) {
28-
httpClient := &http.Client{
29-
Timeout: 60 * time.Second,
30-
Transport: &http.Transport{
30+
var transport http.RoundTripper
31+
var err error
32+
33+
// If proxy authentication is configured, use it; otherwise use standard transport
34+
if t.ProxyAuthConfig.AuthType != "" {
35+
// Create base transport
36+
baseTransport := &http.Transport{
37+
TLSClientConfig: &tls.Config{
38+
InsecureSkipVerify: t.SkipSSLValidation,
39+
},
40+
}
41+
42+
// Parse proxy URL
43+
if t.ProxyAuthConfig.ProxyURL == "" {
44+
return "", fmt.Errorf("proxy URL is required when proxy authentication is specified")
45+
}
46+
proxyURL, err := url.Parse(t.ProxyAuthConfig.ProxyURL)
47+
if err != nil {
48+
return "", fmt.Errorf("failed to parse proxy URL: %w", err)
49+
}
50+
baseTransport.Proxy = http.ProxyURL(proxyURL)
51+
52+
// Create authenticator
53+
authenticator, err := NewProxyAuthenticator(t.ProxyAuthConfig)
54+
if err != nil {
55+
return "", fmt.Errorf("failed to create proxy authenticator: %w", err)
56+
}
57+
58+
// Wrap transport with proxy authentication
59+
transport, err = NewProxyAuthTransport(baseTransport, authenticator)
60+
if err != nil {
61+
return "", fmt.Errorf("failed to initialize proxy authentication: %w", err)
62+
}
63+
} else {
64+
// Use standard transport with environment proxy support
65+
transport = &http.Transport{
3166
TLSClientConfig: &tls.Config{
3267
InsecureSkipVerify: t.SkipSSLValidation,
3368
},
3469
Proxy: http.ProxyFromEnvironment,
35-
},
70+
}
71+
}
72+
73+
httpClient := &http.Client{
74+
Timeout: 60 * time.Second,
75+
Transport: transport,
3676
}
77+
3778
body := AuthBody{RefreshToken: t.RefreshToken}
3879
b, err := json.Marshal(body)
3980
if err != nil {

0 commit comments

Comments
 (0)