tlsclient

package
v0.0.0-...-43a846b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package tlsclient with a TLS client helper supporting certificate, JWT or Basic authentication

Index

Constants

View Source
const DefaultClientTimeout = time.Second * 30

Variables

This section is empty.

Functions

func ConnectTLS

func ConnectTLS(serverURL string, clientCert *tls.Certificate, caCert *x509.Certificate) (
	*tls.Conn, error)

ConnectTLS creates a TLS connection to a server, optionally using a client certificate.

serverURL full URL:  tls/tcp/tcps://host:8883,  wss://host:9001
clientCert to login with. Nil to not use client certs
caCert of the server to setup to (recommended). Nil to not verify the server connection.

func NewHttp2TLSClient

func NewHttp2TLSClient(caCert *x509.Certificate, clientCert *tls.Certificate, timeout time.Duration) *http.Client

NewHttp2TLSClient creates a http client setup for http/2

func NewRequest

func NewRequest(method string, fullURL string, bearerToken string, body []byte) (*http.Request, error)

NewRequest creates a request object containing a bearer token if available

Types

type TLSClient

type TLSClient struct {
	// contains filtered or unexported fields
}

TLSClient is a simple TLS Client with authentication using certificates or JWT authentication with login/pw

func NewTLSClient

func NewTLSClient(hostPort string, clientCert *tls.Certificate, caCert *x509.Certificate,
	timeout time.Duration) *TLSClient

NewTLSClient creates a new TLS Client instance. Use setup/Remove to open and close connections

hostPort is the server hostname or IP address and port to setup to
clientCert is an option client certificate used to connect
caCert with the x509 CA certificate, nil if not available
timeout duration of the request or 0 for default

returns TLS client for submitting requests

func (*TLSClient) Certificate

func (cl *TLSClient) Certificate() *tls.Certificate

Certificate returns the client auth certificate or nil if none is used

func (*TLSClient) Close

func (cl *TLSClient) Close()

Close the connection with the server

func (*TLSClient) Delete

func (cl *TLSClient) Delete(path string) (resp []byte, httpStatus int, err error)

Delete sends a delete message Note that delete methods do not allow a body, or a 405 is returned.

path to invoke

func (*TLSClient) Get

func (cl *TLSClient) Get(path string) (resp []byte, httpStatus int, err error)

Get is a convenience function to send a request This returns the response data, the http status code and an error of delivery failed

path to invoke

func (*TLSClient) GetHttpClient

func (cl *TLSClient) GetHttpClient() *http.Client

GetHttpClient returns the underlying HTTP client

func (*TLSClient) Patch

func (cl *TLSClient) Patch(
	path string, body []byte) (resp []byte, statusCode int, err error)

Patch sends a patch message with json payload If msg is a string then it is considered to be already serialized. If msg is not a string then it will be json encoded.

path to invoke
body contains the serialized body

func (*TLSClient) Post

func (cl *TLSClient) Post(path string, body []byte) (
	resp []byte, statusCode int, err error)

Post a message. If msg is a string then it is considered to be already serialized. If msg is not a string then it will be json encoded.

path to invoke
body contains the serialized request body

func (*TLSClient) PostForm

func (cl *TLSClient) PostForm(path string, formData map[string]string) (
	resp []byte, statusCode int, err error)

PostForm posts a form message.

func (*TLSClient) Put

func (cl *TLSClient) Put(path string, body []byte) (
	resp []byte, statusCode int, err error)

Put a message with json payload If msg is a string then it is considered to be already serialized. If msg is not a string then it will be json encoded.

path to invoke
body contains the serialized request body
correlationID optional field to link async requests and responses

func (*TLSClient) Send

func (cl *TLSClient) Send(
	method string, requrl string, body []byte, contentType string, qParams map[string]string) (
	resp []byte, httpStatus int, headers http.Header, err error)

Send a HTTPS method and read response.

If a JWT authentication is enabled then add the bearer token to the header If msg is a string then it is considered to be already serialized. If msg is not a string then it will be json encoded.

method: GET, PUT, POST, ...
url: full URL to invoke
body contains the serialized request body
contentType: default is "application/json"
qParams: optional map with query parameters

This returns the serialized response data, a response message ID, return status code or an error

func (*TLSClient) SetAuthToken

func (cl *TLSClient) SetAuthToken(token string)

SetAuthToken Sets login ID and secret for bearer token authentication using a token obtained at login or elsewhere.

No error is returned as this just sets up the token for future use. No messages are send yet.

func (*TLSClient) SetHeader

func (cl *TLSClient) SetHeader(name string, val string)

SetHeader sets a header to include in each request use an empty value to remove the header

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL