logo
pub struct DamlSandboxTokenBuilder { /* private fields */ }
Expand description

Build JWT tokens suitable for use in the Daml Sandbox.

The Daml Sandbox support the use JWT tokens for authentication. The following JSON structure represents the claims that may be supplied (see here for details):

{
  "https://daml.com/ledger-api": {
    "ledgerId": "my-ledger",
    "participantId": null,
    "applicationId": null,
    "admin": true,
    "actAs": ["Alice"],
    "readAs": ["Alice", "Bob"]
  },
  "exp": 1300819380,
}

All ledger API endpoints support passing a Bearer token in the authentication http header. This builder produces bearer token strings in HS256, RS256 & EC256 formats which are suitable for use by the Daml ledger API.

Note that test JWT tokens created with https://jwt.io/ will, by default, place the alg attribute ahead of the typ attribute in the header whereas the library used here will places them the opposite wa around. Whilst both produce valid tokens this can be confusing when trying to compare examples.

Examples

A HS256 (shared secret) bearer token matching the example above can be created as follows:

use daml_util::DamlSandboxTokenBuilder;

let token = DamlSandboxTokenBuilder::new_with_expiry(1300819380)
    .ledger_id("my-ledger")
    .admin(true)
    .act_as(vec!["Alice".to_owned()])
    .read_as(vec!["Alice".to_owned(), "Bob".to_owned()])
    .new_hs256_unsafe_token("some secret phrase")?;

The generated token can then supplied to the DamlGrpcClientBuilder via the with_auth method as follows:

use daml_grpc::DamlGrpcClientBuilder;
use daml_util::DamlSandboxTokenBuilder;

let token = DamlSandboxTokenBuilder::new_with_expiry(1300819380)
    .ledger_id("my-ledger")
    .admin(true)
    .act_as(vec!["Alice".to_owned()])
    .read_as(vec!["Alice".to_owned(), "Bob".to_owned()])
    .new_ec256_token("... EC256 key in bytes ...")?;

let ledger_client = DamlGrpcClientBuilder::uri("http://localhost:8080").with_auth(token).connect().await?;

Implementations

Create with an expiry relative to the current system time.

Create with an absolute expiry timestamp (unix).

DOCME

DOCME

DOCME

DOCME

DOCME

DOCME

Create a new HS256 JWT token based on a shared secret.

This approach is considered unsafe for production use and should be used for local testing only. Note that whilst the method name contains the word unsafe to highlight the above, the method does not contain any unsafe blocks or call any unsafe methods.

Create a new RS256 JWT token based on the supplied RSA key.

The key is expected to be in pem format.

Create a new EC256 JWT token based on the supplied RSA key.

The key is expected to be in pem format.

Render the token claims as a JSON string.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Wrap the input message T in a tonic::Request

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more