Skip to content

Commit

Permalink
To support token based authentication (#53)
Browse files Browse the repository at this point in the history
* To support Token based authentication

---------

Co-authored-by: Srinivas Gadisetti <[email protected]>
Co-authored-by: sashankgadisetti <[email protected]>
  • Loading branch information
3 people committed Aug 4, 2023
1 parent 0c428cc commit e389c70
Showing 1 changed file with 26 additions and 15 deletions.
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

0 comments on commit e389c70

Please sign in to comment.