You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by mm...@apache.org on 2019/12/19 16:57:31 UTC

[pulsar-client-go] branch master updated: Allow to specify a function that supply a token (#130)

This is an automated email from the ASF dual-hosted git repository.

mmerli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar-client-go.git


The following commit(s) were added to refs/heads/master by this push:
     new 322faca  Allow to specify a function that supply a token (#130)
322faca is described below

commit 322faca00f486f4cce5d94ed14b0a2918bd42f8b
Author: Matteo Merli <mm...@apache.org>
AuthorDate: Thu Dec 19 08:57:21 2019 -0800

    Allow to specify a function that supply a token (#130)
    
    * Allow to specify a function that supply a token
    
    * Fixed godocs
    
    * Fixed else block
---
 pulsar/client.go              |  8 ++++++++
 pulsar/client_impl_test.go    | 24 ++++++++++++++++++++++++
 pulsar/internal/auth/token.go | 12 +++++++++++-
 3 files changed, 43 insertions(+), 1 deletion(-)

diff --git a/pulsar/client.go b/pulsar/client.go
index 98a2189..31257ed 100644
--- a/pulsar/client.go
+++ b/pulsar/client.go
@@ -39,6 +39,14 @@ func NewAuthenticationToken(token string) Authentication {
 	return auth.NewAuthenticationToken(token)
 }
 
+// NewAuthenticationTokenFromSupplier returns a token auth provider that
+// gets the token data from a user supplied function. The function is
+// invoked each time the client library needs to use a token in talking
+// with Pulsar brokers
+func NewAuthenticationTokenFromSupplier(tokenSupplier func() (string, error)) Authentication {
+	return auth.NewAuthenticationTokenFromSupplier(tokenSupplier)
+}
+
 // Create new Authentication provider with specified auth token from a file
 func NewAuthenticationTokenFromFile(tokenFilePath string) Authentication {
 	return auth.NewAuthenticationTokenFromFile(tokenFilePath)
diff --git a/pulsar/client_impl_test.go b/pulsar/client_impl_test.go
index d9849bd..841069a 100644
--- a/pulsar/client_impl_test.go
+++ b/pulsar/client_impl_test.go
@@ -174,6 +174,30 @@ func TestTokenAuth(t *testing.T) {
 	client.Close()
 }
 
+func TestTokenAuthWithSupplier(t *testing.T) {
+	client, err := NewClient(ClientOptions{
+		URL:            serviceURL,
+		Authentication: NewAuthenticationTokenFromSupplier(func() (s string, err error) {
+			token, err := ioutil.ReadFile(tokenFilePath)
+			if err != nil {
+				return "", err
+			}
+
+			return string(token), nil
+		}),
+	})
+	assert.NoError(t, err)
+
+	producer, err := client.CreateProducer(ProducerOptions{
+		Topic: newAuthTopicName(),
+	})
+
+	assert.NoError(t, err)
+	assert.NotNil(t, producer)
+
+	client.Close()
+}
+
 func TestTokenAuthFromFile(t *testing.T) {
 	client, err := NewClient(ClientOptions{
 		URL:            serviceURL,
diff --git a/pulsar/internal/auth/token.go b/pulsar/internal/auth/token.go
index c89eeaf..e6a6d97 100644
--- a/pulsar/internal/auth/token.go
+++ b/pulsar/internal/auth/token.go
@@ -40,7 +40,8 @@ func NewAuthenticationTokenWithParams(params map[string]string) (Provider, error
 	}
 }
 
-// NewAuthenticationToken return a interface of Provider with a string token.
+// NewAuthenticationToken returns a token auth provider that will use the specified token to
+// talk with Pulsar brokers
 func NewAuthenticationToken(token string) Provider {
 	return &tokenAuthProvider{
 		tokenSupplier: func() (string, error) {
@@ -52,6 +53,15 @@ func NewAuthenticationToken(token string) Provider {
 	}
 }
 
+// NewAuthenticationTokenFromSupplier returns a token auth provider that get
+// the token data from a user supplied function. The function is invoked each
+// time the client library needs to use a token in talking with Pulsar brokers
+func NewAuthenticationTokenFromSupplier(tokenSupplier func() (string, error)) Provider {
+	return &tokenAuthProvider{
+		tokenSupplier: tokenSupplier,
+	}
+}
+
 // NewAuthenticationTokenFromFile return a interface of a Provider with a string token file path.
 func NewAuthenticationTokenFromFile(tokenFilePath string) Provider {
 	return &tokenAuthProvider{