Struct daml_util::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
sourceimpl DamlSandboxTokenBuilder
impl DamlSandboxTokenBuilder
sourcepub fn new_with_duration_secs(secs: i64) -> Self
pub fn new_with_duration_secs(secs: i64) -> Self
Create with an expiry relative to the current system time.
sourcepub fn new_with_expiry(timestamp: i64) -> Self
pub fn new_with_expiry(timestamp: i64) -> Self
Create with an absolute expiry timestamp (unix).
sourcepub fn participant_id(self, participant_id: impl Into<String>) -> Self
pub fn participant_id(self, participant_id: impl Into<String>) -> Self
DOCME
sourcepub fn application_id(self, application_id: impl Into<String>) -> Self
pub fn application_id(self, application_id: impl Into<String>) -> Self
DOCME
sourcepub fn new_hs256_unsafe_token(
self,
secret: impl AsRef<[u8]>
) -> DamlSandboxAuthResult<String>
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.
sourcepub fn new_rs256_token(
self,
rsa_pem: impl AsRef<[u8]>
) -> DamlSandboxAuthResult<String>
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.
sourcepub fn new_ec256_token(
self,
ec_pem: impl AsRef<[u8]>
) -> DamlSandboxAuthResult<String>
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.
sourcepub fn claims_json(&self) -> DamlSandboxAuthResult<String>
pub fn claims_json(&self) -> DamlSandboxAuthResult<String>
Render the token claims as a JSON string.
Trait Implementations
sourceimpl Clone for DamlSandboxTokenBuilder
impl Clone for DamlSandboxTokenBuilder
sourcefn clone(&self) -> DamlSandboxTokenBuilder
fn clone(&self) -> DamlSandboxTokenBuilder
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source. Read more
sourceimpl Default for DamlSandboxTokenBuilder
impl Default for DamlSandboxTokenBuilder
sourcefn default() -> DamlSandboxTokenBuilder
fn default() -> DamlSandboxTokenBuilder
Returns the “default value” for a type. Read more
Auto Trait Implementations
impl RefUnwindSafe for DamlSandboxTokenBuilder
impl Send for DamlSandboxTokenBuilder
impl Sync for DamlSandboxTokenBuilder
impl Unpin for DamlSandboxTokenBuilder
impl UnwindSafe for DamlSandboxTokenBuilder
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
sourcefn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message T in a tonic::Request
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into)Uses borrowed data to replace owned data, usually by cloning. Read more
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
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
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber to this type, returning a
WithDispatch wrapper. Read more