DamlSandboxTokenBuilder

Struct DamlSandboxTokenBuilder 

Source
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§

Source§

impl DamlSandboxTokenBuilder

Source

pub fn new_with_duration_secs(secs: i64) -> Self

Create with an expiry relative to the current system time.

Source

pub fn new_with_expiry(timestamp: i64) -> Self

Create with an absolute expiry timestamp (unix).

Source

pub fn ledger_id(self, ledger_id: impl Into<String>) -> Self

DOCME

Source

pub fn participant_id(self, participant_id: impl Into<String>) -> Self

DOCME

Source

pub fn application_id(self, application_id: impl Into<String>) -> Self

DOCME

Source

pub fn admin(self, admin: bool) -> Self

DOCME

Source

pub fn act_as(self, act_as: Vec<String>) -> Self

DOCME

Source

pub fn read_as(self, read_as: Vec<String>) -> Self

DOCME

Source

pub fn new_hs256_unsafe_token( self, secret: impl AsRef<[u8]>, ) -> DamlSandboxAuthResult<String>

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.

Source

pub fn new_rs256_token( self, rsa_pem: impl AsRef<[u8]>, ) -> DamlSandboxAuthResult<String>

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

The key is expected to be in pem format.

Source

pub fn new_ec256_token( self, ec_pem: impl AsRef<[u8]>, ) -> DamlSandboxAuthResult<String>

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

The key is expected to be in pem format.

Source

pub fn claims_json(&self) -> DamlSandboxAuthResult<String>

Render the token claims as a JSON string.

Trait Implementations§

Source§

impl Clone for DamlSandboxTokenBuilder

Source§

fn clone(&self) -> DamlSandboxTokenBuilder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for DamlSandboxTokenBuilder

Source§

fn default() -> DamlSandboxTokenBuilder

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

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

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

fn in_current_span(self) -> Instrumented<Self>

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

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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