Skip to content

Commit bfdbdae

Browse files
committed
fix TicketAuthProvider
1 parent 80f3102 commit bfdbdae

File tree

4 files changed

+63
-50
lines changed

4 files changed

+63
-50
lines changed

rest/access.go

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
package rest
22

33
import (
4-
"bytes"
54
"context"
6-
"encoding/json"
7-
"fmt"
8-
"net/http"
95

106
"github.com/sp-yduck/proxmox-go/api"
117
)
@@ -17,37 +13,3 @@ func (c *RESTClient) PostTicket(ctx context.Context, req TicketRequest) (*api.Se
1713
}
1814
return session, nil
1915
}
20-
21-
func retrieveSessionTokens(baseUrl string, req TicketRequest) (*api.Session, error) {
22-
endpoint := baseUrl + "/access/ticket"
23-
jsonReq, err := json.Marshal(req)
24-
if err != nil {
25-
return nil, err
26-
}
27-
body := bytes.NewReader(jsonReq)
28-
httpReq, err := http.NewRequestWithContext(context.Background(), http.MethodPost, endpoint, body)
29-
if err != nil {
30-
return nil, err
31-
}
32-
httpReq.Header.Add("Content-Type", "application/json")
33-
httpRsp, err := http.DefaultClient.Do(httpReq)
34-
if err != nil {
35-
return nil, err
36-
}
37-
defer httpRsp.Body.Close()
38-
39-
buf, err := checkResponse(httpRsp)
40-
if err != nil {
41-
return nil, fmt.Errorf("failed to retrieve session token: %v", err)
42-
}
43-
var datakey map[string]json.RawMessage
44-
if err := json.Unmarshal(buf, &datakey); err != nil {
45-
return nil, err
46-
}
47-
var session *api.Session
48-
data := datakey["data"]
49-
if err := json.Unmarshal(data, &session); err != nil {
50-
return nil, err
51-
}
52-
return session, nil
53-
}

rest/authentication.go

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package rest
22

33
import (
4+
"bytes"
5+
"context"
6+
"crypto/tls"
7+
"encoding/json"
48
"fmt"
59
"net/http"
610
"sync"
@@ -104,16 +108,20 @@ func (t *TokenProvider) addAuthHeader(header *http.Header) error {
104108

105109
// AuthProvider implementation
106110
type TicketProvider struct {
107-
baseUrl string
108-
session *api.Session
109-
expiry time.Time
110-
mu sync.Mutex
111+
baseUrl string
112+
username string
113+
password string
114+
session *api.Session
115+
expiry time.Time
116+
mu sync.Mutex
111117
}
112118

113119
func NewTicketProvider(baseUrl, username, password string) (*TicketProvider, error) {
114120
t := &TicketProvider{
115-
baseUrl: baseUrl,
116-
mu: sync.Mutex{},
121+
baseUrl: baseUrl,
122+
username: username,
123+
password: password,
124+
mu: sync.Mutex{},
117125
}
118126
req := TicketRequest{Username: username, Password: password}
119127
if err := t.startNewSession(req); err != nil {
@@ -136,7 +144,7 @@ func (t *TicketProvider) Session() (*api.Session, error) {
136144
t.mu.Lock()
137145
defer t.mu.Unlock()
138146
if t.ExpiryDelta() < 5*time.Minute {
139-
req := TicketRequest{Username: "dummy", Password: "dummy"}
147+
req := TicketRequest{Username: t.username, Password: t.password}
140148
if err := t.startNewSession(req); err != nil {
141149
return nil, err
142150
}
@@ -145,7 +153,7 @@ func (t *TicketProvider) Session() (*api.Session, error) {
145153
}
146154

147155
func (t *TicketProvider) startNewSession(req TicketRequest) error {
148-
session, err := retrieveSessionTokens(t.baseUrl, req)
156+
session, err := t.retrieveSessionTokens(req)
149157
if err != nil {
150158
return err
151159
}
@@ -157,6 +165,47 @@ func (t *TicketProvider) startNewSession(req TicketRequest) error {
157165
return nil
158166
}
159167

168+
func (t *TicketProvider) retrieveSessionTokens(req TicketRequest) (*api.Session, error) {
169+
endpoint := t.baseUrl + "/access/ticket"
170+
jsonReq, err := json.Marshal(req)
171+
if err != nil {
172+
return nil, err
173+
}
174+
body := bytes.NewReader(jsonReq)
175+
httpReq, err := http.NewRequestWithContext(context.Background(), http.MethodPost, endpoint, body)
176+
if err != nil {
177+
return nil, err
178+
}
179+
httpReq.Header.Add("Content-Type", "application/json")
180+
// to do : client should be inherited from RESTClient or so
181+
client := &http.Client{Transport: &http.Transport{
182+
TLSClientConfig: &tls.Config{
183+
InsecureSkipVerify: true,
184+
},
185+
},
186+
}
187+
httpRsp, err := client.Do(httpReq)
188+
if err != nil {
189+
return nil, err
190+
}
191+
defer httpRsp.Body.Close()
192+
193+
buf, err := checkResponse(httpRsp)
194+
if err != nil {
195+
return nil, fmt.Errorf("failed to retrieve session token: %v", err)
196+
}
197+
var datakey map[string]json.RawMessage
198+
if err := json.Unmarshal(buf, &datakey); err != nil {
199+
return nil, err
200+
}
201+
var session *api.Session
202+
data := datakey["data"]
203+
if err := json.Unmarshal(data, &session); err != nil {
204+
return nil, err
205+
}
206+
return session, nil
207+
}
208+
160209
func (t *TicketProvider) addAuthHeader(header *http.Header) error {
161210
session, err := t.Session()
162211
if err != nil {

rest/client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type RESTClient struct {
2525

2626
httpClient *http.Client
2727

28-
transport Transport
28+
transport *Transport
2929

3030
rateLimiter *rate.Limiter
3131

@@ -49,15 +49,15 @@ func NewRESTClient(baseUrl string, opts ...ClientOption) (*RESTClient, error) {
4949
client := &RESTClient{
5050
endpoint: complementURL(baseUrl),
5151
httpClient: &http.Client{},
52+
transport: &Transport{},
5253
rateLimiter: rate.NewLimiter(rate.Every(1*time.Second), defaultQPS),
5354
}
54-
5555
for _, option := range opts {
5656
if err := option(client); err != nil {
5757
return nil, err
5858
}
5959
}
60-
60+
client.httpClient = &http.Client{Transport: client.transport}
6161
return client, nil
6262
}
6363

rest/cluster_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package rest
22

3-
import "context"
3+
import (
4+
"context"
5+
)
46

57
func (s *TestSuite) TestGetNextID() {
68
nextid, err := s.restclient.GetNextID(context.TODO())

0 commit comments

Comments
 (0)