Skip to main content

ClerkAuthLayerConfig

Struct ClerkAuthLayerConfig 

Source
pub struct ClerkAuthLayerConfig { /* private fields */ }
Available on crate feature server only.
Expand description

Configuration for crate::server::ClerkAuthLayer.

§Security: default claim acceptance

By default (with no issuers, audiences, or authorized parties configured) verification accepts any RS256 JWT that is signed by your instance’s JWKS and passes standard exp/nbf validation. The iss, aud, and azp claims are not checked.

This is safe for the common single-instance case: the JWKS endpoint is scoped to your Clerk instance by the secret key, so a token that verifies against it was necessarily minted by your instance. It is not a bypass.

§Security: session tokens only, by default

Verification accepts only Clerk session tokens by default: a token must carry a sid (session id) claim. Clerk JWT-template tokens (minted with getToken({ template }) for third-party integrations) are signed by the same instance JWKS and carry a sub, but omit sid, so without this check a leaked template token could be replayed to authenticate as its user. Requiring sid rejects them while accepting every genuine session token. Opt out with allow_non_session_tokens only if you deliberately verify non-session Clerk JWTs here.

The claim checks below are further defense-in-depth for setups where the single-instance assumption is weaker: multiple apps or environments sharing an instance, satellite domains, or tokens minted for a different audience that you do not want one service to accept for another. For those, harden verification:

  • add_authorized_party: restrict the azp origins that may present tokens (Clerk’s recommended check; tokens without azp are still accepted, matching Clerk’s guidance).
  • add_issuer: pin the instance frontend origin.
  • add_audience: require a specific aud when you mint audience-scoped tokens.

Implementations§

Source§

impl ClerkAuthLayerConfig

Source

pub fn new(secret_key: impl Into<String>) -> Self

Creates a config using Clerk’s default Backend API base URL.

Source

pub fn from_env() -> Result<Self, ClerkError>

Creates a config from the conventional CLERK_SECRET_KEY environment variable.

Source

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

Sets the HTTPS Clerk Backend API base URL, including API version.

For example: https://api.clerk.com/v1. The JWKS endpoint is derived by appending /jwks to this base URL.

Source

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

Sets a local/test-only HTTP Backend API base URL.

This explicitly allows http:// URLs for local JWKS mocks. Do not use it in production; plain HTTP allows JWKS response tampering.

Source

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

Verifies against a fixed JWKS instead of fetching one from Clerk.

jwks_json is a JWKS document ({"keys": [...]}). When set, the Backend API base URL is never contacted: there is no HTTP client, no cache, and no refresh, so the secret key is unused and may be empty.

This is the offline path for tests — pair it with TestIssuer to verify locally minted tokens without standing up a JWKS mock server at all.

It is also usable in production to pin signing keys, but note that a fixed keyset does not rotate: when Clerk rotates its signing keys, every token signed by a new key is rejected until the configured JWKS is updated and the process restarts. Prefer the fetched default unless you have a specific reason to pin.

Source

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

Adds a single allowed azp authorized-party origin to the configured set.

Source

pub fn with_authorized_parties( self, parties: impl IntoIterator<Item = impl Into<String>>, ) -> Self

Sets allowed azp authorized-party origins.

When configured, a token with an azp claim must match one of these values. Tokens without azp continue to be accepted, matching Clerk’s manual verification guidance.

Source

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

Adds a single allowed JWT audience to the configured set.

Source

pub fn with_audiences( self, audiences: impl IntoIterator<Item = impl Into<String>>, ) -> Self

Sets allowed JWT audiences.

Source

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

Adds a single allowed JWT issuer to the configured set.

Clerk session tokens carry the instance frontend origin as iss (for example https://your-app.clerk.accounts.dev). When configured, tokens whose iss claim is missing or does not match are rejected.

Source

pub fn with_issuers( self, issuers: impl IntoIterator<Item = impl Into<String>>, ) -> Self

Sets allowed JWT issuers.

Source

pub fn with_clock_skew(self, clock_skew: Duration) -> Self

Sets accepted clock skew for JWT exp, nbf, and iat validation.

Source

pub fn allow_non_session_tokens(self) -> Self

Accept instance-signed JWTs that are not session tokens (tokens without a sid claim, such as Clerk JWT-template tokens).

By default verification requires a sid so a leaked JWT-template token cannot be replayed as a session (see the type-level security note). Call this only when you deliberately verify non-session Clerk JWTs here, and pair it with add_audience to keep tokens from one template being accepted for another.

Trait Implementations§

Source§

impl Clone for ClerkAuthLayerConfig

Source§

fn clone(&self) -> ClerkAuthLayerConfig

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for ClerkAuthLayerConfig

Source§

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

Formats the value using the given formatter. 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<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> InitializeFromFunction<T> for T

Source§

fn initialize_from_function(f: fn() -> T) -> T

Create an instance of this type from an initialization function
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<Ret> SpawnIfAsync<(), Ret> for Ret

Source§

fn spawn(self) -> Ret

Spawn the value into the dioxus runtime if it is an async block
Source§

impl<T, O> SuperFrom<T> for O
where O: From<T>,

Source§

fn super_from(input: T) -> O

Convert from a type to another type.
Source§

impl<T, O, M> SuperInto<O, M> for T
where O: SuperFrom<T, M>,

Source§

fn super_into(self) -> O

Convert from a type to another type.
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 = !

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