diff --git a/client/client.go b/client/client.go index 49b8818b..d907ae9b 100644 --- a/client/client.go +++ b/client/client.go @@ -10,6 +10,7 @@ import ( "github.com/spectrocloud/palette-api-go/apiutil/transport" clientV1 "github.com/spectrocloud/palette-api-go/client/v1" + "github.com/spectrocloud/palette-api-go/models" ) type V1Client struct { @@ -18,6 +19,8 @@ type V1Client struct { ctx context.Context apikey string jwt string + username string + password string hubbleUri string projectUid string schemes []string @@ -45,12 +48,24 @@ func WithAPIKey(apiKey string) func(*V1Client) { } } -func WithJWTToken(jwt string) func(*V1Client) { +func WithJWT(jwt string) func(*V1Client) { return func(v *V1Client) { v.jwt = jwt } } +func WithUsername(username string) func(*V1Client) { + return func(v *V1Client) { + v.username = username + } +} + +func WithPassword(password string) func(*V1Client) { + return func(v *V1Client) { + v.password = password + } +} + func WithHubbleURI(hubbleUri string) func(*V1Client) { return func(v *V1Client) { v.hubbleUri = hubbleUri @@ -118,7 +133,10 @@ func (h *V1Client) Clone() *V1Client { opts = append(opts, WithAPIKey(h.apikey)) } if h.jwt != "" { - opts = append(opts, WithJWTToken(h.jwt)) + opts = append(opts, WithJWT(h.jwt)) + } + if h.username != "" && h.password != "" { + opts = append(opts, WithUsername(h.username), WithPassword(h.password)) } if h.projectUid != "" { opts = append(opts, WithScopeProject(h.projectUid)) @@ -129,16 +147,20 @@ func (h *V1Client) Clone() *V1Client { return New(opts...) } -func (h *V1Client) getTransport() *transport.Runtime { - var httpTransport *transport.Runtime +func (h *V1Client) getTransport() (t *transport.Runtime) { + if h.username != "" && h.password != "" { + if err := h.authenticate(); err != nil { + return nil + } + } if h.apikey != "" { - httpTransport = h.apiKeyTransport() + t = h.apiKeyTransport() } else if h.jwt != "" { - httpTransport = h.jwtTransport() + t = h.jwtTransport() } else { - httpTransport = h.baseTransport() + t = h.baseTransport() } - return httpTransport + return } func (h *V1Client) apiKeyTransport() *transport.Runtime { @@ -153,6 +175,25 @@ func (h *V1Client) jwtTransport() *transport.Runtime { return httpTransport } +func (h *V1Client) authenticate() error { + httpTransport := h.baseTransport() + c := clientV1.New(httpTransport, strfmt.Default) + + params := &clientV1.V1AuthenticateParams{ + Body: &models.V1AuthLogin{ + EmailID: h.username, + Password: strfmt.Password(h.password), + }, + } + resp, err := c.V1Authenticate(params) + if err != nil { + return err + } + h.jwt = resp.Payload.Authorization + + return nil +} + func (h *V1Client) baseTransport() *transport.Runtime { httpTransport := transport.NewWithClient(h.hubbleUri, "", h.schemes, h.httpClient()) httpTransport.RetryAttempts = h.retryAttempts diff --git a/client/user.go b/client/user.go index 1dd654ac..07dd9b59 100644 --- a/client/user.go +++ b/client/user.go @@ -8,6 +8,16 @@ import ( "github.com/spectrocloud/palette-sdk-go/client/apiutil" ) +func (h *V1Client) Authenticate(body *models.V1AuthLogin) (*models.V1UserToken, error) { + params := clientV1.NewV1AuthenticateParams(). + WithBody(body) + resp, err := h.Client.V1Authenticate(params) + if err != nil { + return nil, err + } + return resp.Payload, nil +} + func (h *V1Client) GetMe() (*models.V1UserMe, error) { params := clientV1.NewV1UsersMeGetParamsWithContext(h.ctx) resp, err := h.Client.V1UsersMeGet(params)