Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

To support token based authentication #53

Merged
merged 19 commits into from
Aug 4, 2023
41 changes: 26 additions & 15 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,22 @@ const DefaultDateTimeFormat = "2006-01-02T15:04:05.000-0700"

// ClientOptions a client options
type ClientOptions struct {
UserAgent string
EndpointUrl string
Timeout time.Duration
ApiUser string
ApiPassword string
UserAgent string
EndpointUrl string
Timeout time.Duration
ApiUser string
ApiPassword string
AuthorizationHeader string
}

// Client a client for Camunda API
type Client struct {
httpClient *http.Client
endpointUrl string
userAgent string
apiUser string
apiPassword string
httpClient *http.Client
endpointUrl string
userAgent string
apiUser string
apiPassword string
authorizationHeader string

ExternalTask *ExternalTask
Deployment *Deployment
Expand Down Expand Up @@ -93,10 +95,11 @@ func NewClient(options ClientOptions) *Client {
httpClient: &http.Client{
Timeout: time.Second * DefaultTimeoutSec,
},
endpointUrl: DefaultEndpointUrl,
userAgent: DefaultUserAgent,
apiUser: options.ApiUser,
apiPassword: options.ApiPassword,
endpointUrl: DefaultEndpointUrl,
userAgent: DefaultUserAgent,
apiUser: options.ApiUser,
apiPassword: options.ApiPassword,
authorizationHeader: options.AuthorizationHeader,
}

if options.EndpointUrl != "" {
Expand All @@ -123,6 +126,10 @@ func NewClient(options ClientOptions) *Client {
return client
}

func (c *Client) SetAuthorizationHeader(bearerToken string) {
c.authorizationHeader = bearerToken
}

// SetCustomTransport set new custom transport
func (c *Client) SetCustomTransport(customHTTPTransport http.RoundTripper) {
if c.httpClient != nil {
Expand Down Expand Up @@ -180,7 +187,11 @@ func (c *Client) do(method, path string, query map[string]string, body io.Reader
req.Header.Set("Content-Type", contentType)
}

req.SetBasicAuth(c.apiUser, c.apiPassword)
if c.authorizationHeader != "" {
req.Header.Set("Authorization", c.authorizationHeader)
} else {
req.SetBasicAuth(c.apiUser, c.apiPassword)
}

res, err = c.httpClient.Do(req)
if err != nil {
Expand Down