11package rest
22
33import (
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
106110type 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
113119func 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
147155func (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+
160209func (t * TicketProvider ) addAuthHeader (header * http.Header ) error {
161210 session , err := t .Session ()
162211 if err != nil {
0 commit comments