-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathuaa.go
More file actions
112 lines (94 loc) · 2.95 KB
/
Copy pathuaa.go
File metadata and controls
112 lines (94 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package pivnet
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
)
type AuthResp struct {
Token string `json:"access_token"`
}
type TokenFetcher struct {
Endpoint string
RefreshToken string
SkipSSLValidation bool
UserAgent string
ProxyAuthConfig ProxyAuthConfig
}
func NewTokenFetcher(endpoint, refreshToken string, skipSSLValidation bool, userAgent string, proxyAuthConfig ProxyAuthConfig) *TokenFetcher {
return &TokenFetcher{endpoint, refreshToken, skipSSLValidation, userAgent, proxyAuthConfig}
}
func (t TokenFetcher) GetToken() (string, error) {
var transport http.RoundTripper
var err error
// If proxy authentication is configured, use it; otherwise use standard transport
if t.ProxyAuthConfig.AuthType != "" {
// Create base transport
baseTransport := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: t.SkipSSLValidation,
},
}
// Parse proxy URL
if t.ProxyAuthConfig.ProxyURL == "" {
return "", fmt.Errorf("proxy URL is required when proxy authentication is specified")
}
proxyURL, err := url.Parse(t.ProxyAuthConfig.ProxyURL)
if err != nil {
return "", fmt.Errorf("failed to parse proxy URL: %w", err)
}
baseTransport.Proxy = http.ProxyURL(proxyURL)
// Create authenticator
authenticator, err := NewProxyAuthenticator(t.ProxyAuthConfig)
if err != nil {
return "", fmt.Errorf("failed to create proxy authenticator: %w", err)
}
// Wrap transport with proxy authentication
transport, err = NewProxyAuthTransport(baseTransport, authenticator)
if err != nil {
return "", fmt.Errorf("failed to initialize proxy authentication: %w", err)
}
} else {
// Use standard transport with environment proxy support
transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: t.SkipSSLValidation,
},
Proxy: http.ProxyFromEnvironment,
}
}
httpClient := &http.Client{
Timeout: 60 * time.Second,
Transport: transport,
}
body := AuthBody{RefreshToken: t.RefreshToken}
b, err := json.Marshal(body)
if err != nil {
return "", fmt.Errorf("failed to marshal API token request body: %s", err.Error())
}
req, err := http.NewRequest("POST", t.Endpoint+"/authentication/access_tokens", bytes.NewReader(b))
req.Header.Add("Content-Type", "application/json")
if t.UserAgent != "" {
req.Header.Add("User-Agent", t.UserAgent)
}
if err != nil {
return "", fmt.Errorf("failed to construct API token request: %s", err.Error())
}
resp, err := httpClient.Do(req)
if err != nil {
return "", fmt.Errorf("API token request failed: %s", err.Error())
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("failed to fetch API token - received status %v", resp.StatusCode)
}
var response AuthResp
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return "", fmt.Errorf("failed to decode API token response: %s", err.Error())
}
return response.Token, nil
}