Struct Token

Source
pub struct Token<T: IntoNatsClaims> { /* private fields */ }
Expand description

JWT token builder.

§Example

let account_id = "ADP75IYE4WXG23T546A2R3LIRRVBSC52RTJQIHO44CJIQRXSOPLZ5WBJ";
let account_signing_key = KeyPair::from_seed("SAAHCUHEQJUYBGWQKN7XUBDCDXKB6C7IQVBZ74DH3T4M2ZNNQOGGECIBDE").unwrap();
let user_key = KeyPair::new_user();
Token::new_user(account_id, user_key.public_key())
    .name("My User")
    .max_subscriptions(10)
    .max_payload(1024 * 1024) // 1MiB
    .allow_publish("service.hello.world")
    .allow_subscribe("_INBOX.>")
    .sign(&account_signing_key);

Implementations§

Source§

impl<T: IntoNatsClaims> Token<T>

Source

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

Set the friendly name for the token, can be anything, defaults to the token subject

Source

pub fn max_subscriptions(self, max_subscriptions: i64) -> Self

Set the maximum number of subscriptions this token will allow

Source

pub fn max_payload(self, max_payload: i64) -> Self

Set the maximum payload size in bytes this token will allow

Source

pub fn max_data(self, max_data: i64) -> Self

Set the maximum data size in bytes this token will allow

Source

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

Allow a subject/pattern to be published to

Source

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

Deny a subject/pattern from being published to

Source

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

Allow a subject/pattern to be subcribe to

Source

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

Deny a subject/pattern from being subscribed to

Source

pub fn expires(self, expires: i64) -> Self

Set expiration

Source

pub fn sign(self, signing_key: &KeyPair) -> String

Sign the token with the given signing key, returning a JWT string.

If this is a User token, this should be the Account signing key. If this is an Account token, this should be the Operator key

§Panics
  • If system time is before UNIX epoch.
  • If the seconds from UNIX epoch cannot be represented in a i64.
Source§

impl Token<User>

Source

pub fn new_user( issuer_account_id: impl Into<String>, user_key_pub: impl Into<String>, ) -> Self

Start building a new user token.

issuer_account_id is the public key of the Account that will issue this token. This is not the signing key’s public key.

user_key_pub is the public key of the User for which the token is being issued.

Source

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

If true, the user isn’t challenged on connection. Typically used for websocket connections as the browser won’t have/want to have the user’s private key.

Source§

impl Token<Account>

Source

pub fn new_account(account_key_pub: impl Into<String>) -> Self

Start building a new account token.

account_key_pub is the public key of the Account for which the token is being issued.

Source

pub fn add_signing_key(self, signing_key: impl IntoPublicKey) -> Self

Add a signing key to the token. Takes anything that implements IntoPublicKey. This is implemented for String, &str, and &KeyPair

Source

pub fn max_imports(self, max_imports: i64) -> Self

Set the maximum number of imports this account can have.

Source

pub fn max_exports(self, max_exports: i64) -> Self

Set the maximum number of exports this account can have.

Source

pub fn max_connections(self, max_connections: i64) -> Self

Set the maximum number of connections this account can have.

Source

pub fn max_leaf_nodes(self, max_leaf_nodes: i64) -> Self

Set the maximum number of leaf nodes this account can have.

Source

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

Allow exports to contain wildcards

Trait Implementations§

Source§

impl<T: Clone + IntoNatsClaims> Clone for Token<T>

Source§

fn clone(&self) -> Token<T>

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<T: Debug + IntoNatsClaims> Debug for Token<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Token<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Token<T>
where T: RefUnwindSafe,

§

impl<T> Send for Token<T>
where T: Send,

§

impl<T> Sync for Token<T>
where T: Sync,

§

impl<T> Unpin for Token<T>
where T: Unpin,

§

impl<T> UnwindSafe for Token<T>
where T: UnwindSafe,

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, 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> Same for T

Source§

type Output = T

Should always be Self
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