Token Authentication Overview

Pulsar supports authenticating clients using security tokens that are based on JSON Web Tokens (RFC-7519).

Tokens are used to identify a Pulsar client and associate with some “principal” (or “role”) which will be then granted permissions to do some actions (eg: publish or consume from a topic).

A user will typically be given a token string by an administrator (or some automated service).

The compact representation of a signed JWT is a string that looks like:

  1. eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJKb2UifQ.ipevRNuRP6HflG8cFKnmUPtypruRC4fb1DWtoLL62SY
  2. ```
  3. Application will specify the token when creating the client instance. An alternative is to pass
  4. a "token supplier", that is to say a function that returns the token when the client library
  5. will need one.
  6. See [Token authentication admin](security-token-admin.md) for a reference on how to enable token
  7. authentication on a Pulsar cluster.
  8. ### CLI tools
  9. [Command-line tools](reference-cli-tools.md) like [`pulsar-admin`](reference-pulsar-admin.md), [`pulsar-perf`](reference-cli-tools.md#pulsar-perf), and [`pulsar-client`](reference-cli-tools.md#pulsar-client) use the `conf/client.conf` config file in a Pulsar installation.
  10. You'll need to add the following parameters to that file to use the token authentication with
  11. Pulsar's CLI tools:
  12. ```properties
  13. webServiceUrl=http://broker.example.com:8080/
  14. brokerServiceUrl=pulsar://broker.example.com:6650/
  15. authPlugin=org.apache.pulsar.client.impl.auth.AuthenticationToken
  16. authParams=token:eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJKb2UifQ.ipevRNuRP6HflG8cFKnmUPtypruRC4fb1DWtoLL62SY

The token string can also be read from a file, eg:

  1. authParams=file:///path/to/token/file

Java 客户端

  1. PulsarClient client = PulsarClient.builder()
  2. .serviceUrl("pulsar://broker.example.com:6650/")
  3. .authentication(
  4. AuthenticationFactory.token("eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJKb2UifQ.ipevRNuRP6HflG8cFKnmUPtypruRC4fb1DWtoLL62SY")
  5. .build();

Similarly, one can also pass a Supplier:

  1. PulsarClient client = PulsarClient.builder()
  2. .serviceUrl("pulsar://broker.example.com:6650/")
  3. .authentication(
  4. AuthenticationFactory.token(() -> {
  5. // Read token from custom source
  6. return readToken();
  7. })
  8. .build();

Python client

  1. from pulsar import Client, AuthenticationToken
  2. client = Client('pulsar://broker.example.com:6650/'
  3. authentication=AuthenticationToken('eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJKb2UifQ.ipevRNuRP6HflG8cFKnmUPtypruRC4fb1DWtoLL62SY'))

Alternatively, with a supplier:

  1. <br />def read_token():
  2. with open('/path/to/token.txt') as tf:
  3. return tf.read().strip()
  4. client = Client('pulsar://broker.example.com:6650/'
  5. authentication=AuthenticationToken(read_token))

Go client

  1. client, err := NewClient(ClientOptions{
  2. URL: "pulsar://localhost:6650",
  3. Authentication: NewAuthenticationToken("eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJKb2UifQ.ipevRNuRP6HflG8cFKnmUPtypruRC4fb1DWtoLL62SY"),
  4. })

Alternatively, with a supplier:

  1. client, err := NewClient(ClientOptions{
  2. URL: "pulsar://localhost:6650",
  3. Authentication: NewAuthenticationTokenSupplier(func () string {
  4. // Read token from custom source
  5. return readToken()
  6. }),
  7. })

C++ client

  1. #include <pulsar/Client.h>
  2. pulsar::ClientConfiguration config;
  3. config.setAuth(pulsar::AuthToken::createWithToken("eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJKb2UifQ.ipevRNuRP6HflG8cFKnmUPtypruRC4fb1DWtoLL62SY"));
  4. pulsar::Client client("pulsar://broker.example.com:6650/", config);