|
4 | 4 | package auth |
5 | 5 |
|
6 | 6 | import ( |
7 | | - "bytes" |
8 | | - "crypto/tls" |
9 | | - "crypto/x509" |
10 | | - "encoding/json" |
| 7 | + "context" |
11 | 8 | "fmt" |
12 | | - "io" |
13 | | - "net/http" |
14 | | - "net/url" |
15 | | - "os" |
16 | 9 | "strings" |
17 | 10 | ) |
18 | 11 |
|
19 | | -// loadCACertPool loads the CA certificate from a file and returns a certificate pool. |
20 | | -func loadCACertPool(caCertPath string) (*x509.CertPool, error) { |
21 | | - caCert, err := os.ReadFile(caCertPath) |
22 | | - if err != nil { |
23 | | - return nil, fmt.Errorf("failed to read CA certificate: %w", err) |
24 | | - } |
25 | | - caCertPool := x509.NewCertPool() |
26 | | - if !caCertPool.AppendCertsFromPEM(caCert) { |
27 | | - return nil, fmt.Errorf("failed to append CA certificate") |
28 | | - } |
29 | | - return caCertPool, nil |
30 | | -} |
31 | | - |
32 | | -func fetchAccessToken(keycloakURL string, clientID string, clientSecret string, caCertPath string) (string, error) { |
33 | | - // Prepare the request data |
34 | | - data := url.Values{} |
35 | | - data.Set("grant_type", "client_credentials") |
36 | | - data.Set("client_id", clientID) |
37 | | - data.Set("client_secret", clientSecret) |
38 | | - reqBody := bytes.NewBufferString(data.Encode()) |
39 | | - |
40 | | - // Create the HTTP request |
41 | | - req, err := http.NewRequest("POST", "https://"+keycloakURL, reqBody) |
42 | | - if err != nil { |
43 | | - return "", err |
44 | | - } |
45 | | - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
46 | | - |
47 | | - // Configure TLS |
48 | | - var tlsConfig *tls.Config |
49 | | - if caCertPath != "" { |
50 | | - // Load the CA certificate from provided path |
51 | | - caCertPool, err := loadCACertPool(caCertPath) |
52 | | - if err != nil { |
53 | | - return "", fmt.Errorf("error loading CA certificate: %v", err) |
54 | | - } |
55 | | - tlsConfig = &tls.Config{ |
56 | | - RootCAs: caCertPool, |
57 | | - MinVersion: tls.VersionTLS12, |
58 | | - } |
59 | | - } else { |
60 | | - // Use system default CA certificates |
61 | | - tlsConfig = &tls.Config{ |
62 | | - MinVersion: tls.VersionTLS12, |
63 | | - } |
64 | | - } |
65 | | - |
66 | | - // Create an HTTP client with TLS configuration |
67 | | - client := &http.Client{ |
68 | | - Transport: &http.Transport{ |
69 | | - TLSClientConfig: tlsConfig, |
70 | | - }, |
71 | | - } |
72 | | - |
73 | | - // Perform the request |
74 | | - resp, err := client.Do(req) |
75 | | - if err != nil { |
76 | | - return "", err |
77 | | - } |
78 | | - defer resp.Body.Close() |
79 | | - |
80 | | - // Check for a successful response |
81 | | - if resp.StatusCode != http.StatusOK { |
82 | | - return "", fmt.Errorf("failed to get access token, status: %s", resp.Status) |
83 | | - } |
84 | | - |
85 | | - // Parse the JSON response |
86 | | - var result map[string]interface{} |
87 | | - if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { |
88 | | - return "", err |
89 | | - } |
90 | | - |
91 | | - // Extract the access token |
92 | | - token, ok := result["access_token"].(string) |
93 | | - if !ok || token == "" { |
94 | | - return "", fmt.Errorf("access token not found in response") |
95 | | - } |
96 | | - |
97 | | - return token, nil |
98 | | -} |
99 | | - |
100 | | -func fetchReleaseToken(releaseServerURL string, accessToken string, caCertPath string) (string, error) { |
101 | | - // Ensure the access token is not empty |
102 | | - if accessToken == "" { |
103 | | - return "", fmt.Errorf("access token is required") |
104 | | - } |
105 | | - |
106 | | - // Configure TLS |
107 | | - var tlsConfig *tls.Config |
108 | | - if caCertPath != "" { |
109 | | - // Load CA certificate from provided path |
110 | | - caCertPool, err := loadCACertPool(caCertPath) |
111 | | - if err != nil { |
112 | | - return "", fmt.Errorf("error loading CA certificate: %v", err) |
113 | | - } |
114 | | - tlsConfig = &tls.Config{ |
115 | | - RootCAs: caCertPool, |
116 | | - MinVersion: tls.VersionTLS12, |
117 | | - } |
118 | | - } else { |
119 | | - // Use system default CA certificates |
120 | | - tlsConfig = &tls.Config{ |
121 | | - MinVersion: tls.VersionTLS12, |
122 | | - } |
123 | | - } |
124 | | - |
125 | | - // Construct the HTTP request |
126 | | - req, err := http.NewRequest("GET", "https://"+releaseServerURL, nil) |
127 | | - if err != nil { |
128 | | - return "", fmt.Errorf("error creating request: %v", err) |
129 | | - } |
130 | | - |
131 | | - // Add the authorization header with the bearer token |
132 | | - req.Header.Set("Authorization", "Bearer "+accessToken) |
133 | | - |
134 | | - // Create an HTTP client with TLS configuration |
135 | | - client := &http.Client{ |
136 | | - Transport: &http.Transport{ |
137 | | - TLSClientConfig: tlsConfig, |
138 | | - }, |
139 | | - } |
140 | | - |
141 | | - // Send the request |
142 | | - resp, err := client.Do(req) |
143 | | - if err != nil { |
144 | | - return "", fmt.Errorf("error sending request: %v", err) |
145 | | - } |
146 | | - defer resp.Body.Close() |
147 | | - |
148 | | - // Check if the response status is 200 OK |
149 | | - if resp.StatusCode != http.StatusOK { |
150 | | - return "", fmt.Errorf("failed to get release token, status: %s", resp.Status) |
151 | | - } |
152 | | - |
153 | | - // Read the response body |
154 | | - body, err := io.ReadAll(resp.Body) |
155 | | - if err != nil { |
156 | | - return "", fmt.Errorf("error reading response body: %v", err) |
157 | | - } |
158 | | - |
159 | | - // Convert the response body to a string (the token) |
160 | | - token := string(body) |
161 | | - |
162 | | - // Validate the received token |
163 | | - if token == "null" || token == "" { |
164 | | - return "", fmt.Errorf("invalid token received") |
165 | | - } |
166 | | - |
167 | | - return token, nil |
168 | | -} |
169 | | - |
170 | | -// ClientAuth handles authentication and retrieves tokens. |
| 12 | +// ClientAuth handles authentication and retrieves tokens using client credentials. |
| 13 | +// This function is used in non-interactive mode after the device has been onboarded. |
171 | 14 | func ClientAuth(clientID string, clientSecret string, keycloakURL string, accessTokenURL string, releaseTokenURL string, caCertPath string) (idpAccessToken string, releaseToken string, err error) { |
172 | | - // Fetch JWT access token from Keycloak |
173 | | - idpAccessToken, err = fetchAccessToken(keycloakURL+accessTokenURL, clientID, clientSecret, caCertPath) |
| 15 | + ctx := context.Background() |
| 16 | + |
| 17 | + // Fetch JWT access token from Keycloak using client_credentials flow |
| 18 | + idpAccessToken, err = FetchClientCredentialsToken(ctx, ClientCredentialsParams{ |
| 19 | + KeycloakURL: keycloakURL, |
| 20 | + TokenPath: accessTokenURL, |
| 21 | + ClientID: clientID, |
| 22 | + ClientSecret: clientSecret, |
| 23 | + CACertPath: caCertPath, |
| 24 | + }) |
174 | 25 | if err != nil { |
175 | 26 | return "", "", fmt.Errorf("failed to get JWT access token from Keycloak: %v", err) |
176 | 27 | } |
177 | 28 |
|
178 | 29 | // Fetch release service token |
179 | | - releaseTokenURL = strings.Replace(keycloakURL, "keycloak", "release", 1) + releaseTokenURL |
180 | | - releaseToken, err = fetchReleaseToken(releaseTokenURL, idpAccessToken, caCertPath) |
| 30 | + releaseURL := strings.Replace(keycloakURL, "keycloak", "release", 1) + releaseTokenURL |
| 31 | + releaseToken, err = FetchReleaseToken(ctx, releaseURL, idpAccessToken, caCertPath) |
181 | 32 | if err != nil { |
182 | 33 | return "", "", fmt.Errorf("failed to get release service token: %v", err) |
183 | 34 | } |
|
0 commit comments