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
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§
Source§impl Clone for DamlSandboxTokenBuilder
impl Clone for DamlSandboxTokenBuilder
Source§fn clone(&self) -> DamlSandboxTokenBuilder
fn clone(&self) -> DamlSandboxTokenBuilder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Default for DamlSandboxTokenBuilder
impl Default for DamlSandboxTokenBuilder
Source§fn default() -> DamlSandboxTokenBuilder
fn default() -> DamlSandboxTokenBuilder
Auto Trait Implementations§
impl Freeze for DamlSandboxTokenBuilder
impl RefUnwindSafe for DamlSandboxTokenBuilder
impl Send for DamlSandboxTokenBuilder
impl Sync for DamlSandboxTokenBuilder
impl Unpin for DamlSandboxTokenBuilder
impl UnwindSafe for DamlSandboxTokenBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request