Skip to content

Commit c58a7d1

Browse files
authored
Improve JwtClaims & AuthCtx docs (#3477)
# Description of Changes Adds a bit more docs to `JwtClaims` and `AuthCtx`. # API and ABI breaking changes None # Expected complexity level and risk 1 # Testing Just adds docs.
1 parent 39f9127 commit c58a7d1

2 files changed

Lines changed: 33 additions & 14 deletions

File tree

crates/bindings/src/lib.rs

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#![doc = include_str!("../README.md")]
22
// ^ if you are working on docs, go read the top comment of README.md please.
33

4+
use core::cell::{LazyCell, OnceCell, RefCell};
5+
use core::ops::Deref;
6+
use spacetimedb_lib::bsatn;
7+
48
#[cfg(feature = "unstable")]
59
mod client_visibility_filter;
610
pub mod log_stopwatch;
@@ -12,16 +16,11 @@ pub mod rt;
1216
#[doc(hidden)]
1317
pub mod table;
1418

19+
#[cfg(feature = "unstable")]
20+
pub use client_visibility_filter::Filter;
1521
pub use log;
1622
#[cfg(feature = "rand")]
1723
pub use rand08 as rand;
18-
use spacetimedb_lib::bsatn;
19-
use std::cell::LazyCell;
20-
use std::cell::{OnceCell, RefCell};
21-
use std::ops::Deref;
22-
23-
#[cfg(feature = "unstable")]
24-
pub use client_visibility_filter::Filter;
2524
#[cfg(feature = "rand08")]
2625
pub use rng::StdbRng;
2726
pub use sats::SpacetimeType;
@@ -1135,6 +1134,9 @@ impl DbContext for ReducerContext {
11351134
#[non_exhaustive]
11361135
pub struct Local {}
11371136

1137+
/// The [JWT] of an [`AuthCtx`].
1138+
///
1139+
/// [JWT]: https://en.wikipedia.org/wiki/JSON_Web_Token
11381140
#[non_exhaustive]
11391141
pub struct JwtClaims {
11401142
payload: String,
@@ -1145,7 +1147,8 @@ pub struct JwtClaims {
11451147
/// Authentication information for the caller of a reducer.
11461148
pub struct AuthCtx {
11471149
is_internal: bool,
1148-
// NOTE(jsdt): cannot directly use a LazyLock without making this struct generic.
1150+
// NOTE(jsdt): cannot directly use a `LazyCell` without making this struct generic,
1151+
// which would cause `ReducerContext` to become generic as well.
11491152
jwt: Box<dyn Deref<Target = Option<JwtClaims>>>,
11501153
}
11511154

@@ -1157,19 +1160,25 @@ impl AuthCtx {
11571160
}
11581161
}
11591162

1160-
/// Create an [`AuthCtx`] for an internal call, with no JWT.
1163+
/// Creates an [`AuthCtx`] for an internal call, with no [JWT].
11611164
/// This represents a scheduled reducer.
1165+
///
1166+
/// [JWT]: https://en.wikipedia.org/wiki/JSON_Web_Token
11621167
pub fn internal() -> AuthCtx {
11631168
Self::new(true, || None)
11641169
}
11651170

1166-
/// Creates an [`AuthCtx`] using the json claims from a JWT.
1171+
/// Creates an [`AuthCtx`] using the json claims from a [JWT].
11671172
/// This can be used to write unit tests.
1173+
///
1174+
/// [JWT]: https://en.wikipedia.org/wiki/JSON_Web_Token
11681175
pub fn from_jwt_payload(jwt_payload: String) -> AuthCtx {
11691176
Self::new(false, move || Some(JwtClaims::new(jwt_payload)))
11701177
}
11711178

1172-
/// Creates an [`AuthCtx`] that reads the JWT for the given connection id.
1179+
/// Creates an [`AuthCtx`] that reads the [JWT] for the given connection id.
1180+
///
1181+
/// [JWT]: https://en.wikipedia.org/wiki/JSON_Web_Token
11731182
fn from_connection_id(connection_id: ConnectionId) -> AuthCtx {
11741183
Self::new(false, move || rt::get_jwt(connection_id).map(JwtClaims::new))
11751184
}
@@ -1179,13 +1188,17 @@ impl AuthCtx {
11791188
self.is_internal
11801189
}
11811190

1182-
/// Check if there is a JWT without loading it.
1183-
/// If [`AuthCtx::is_internal`] is true, this will return false.
1191+
/// Checks if there is a [JWT] without loading it.
1192+
/// If [`AuthCtx::is_internal`] returns true, this will return false.
1193+
///
1194+
/// [JWT]: https://en.wikipedia.org/wiki/JSON_Web_Token
11841195
pub fn has_jwt(&self) -> bool {
11851196
self.jwt.is_some()
11861197
}
11871198

1188-
/// Load the jwt.
1199+
/// Loads the [JWT].
1200+
///
1201+
/// [JWT]: https://en.wikipedia.org/wiki/JSON_Web_Token
11891202
pub fn jwt(&self) -> Option<&JwtClaims> {
11901203
self.jwt.as_ref().deref().as_ref()
11911204
}
@@ -1240,6 +1253,9 @@ impl JwtClaims {
12401253
}
12411254

12421255
/// Get the whole JWT payload as a json string.
1256+
///
1257+
/// This method is intended for parsing custom claims,
1258+
/// beyond the methods offered by [`JwtClaims`].
12431259
pub fn raw_payload(&self) -> &str {
12441260
&self.payload
12451261
}

crates/lib/src/identity.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ impl Identity {
116116
Self::ZERO
117117
}
118118

119+
/// Derives an identity from a [JWT] `issuer` and a `subject`.
120+
///
121+
/// [JWT]: https://en.wikipedia.org/wiki/JSON_Web_Token
119122
pub fn from_claims(issuer: &str, subject: &str) -> Self {
120123
let input = format!("{issuer}|{subject}");
121124
let first_hash = blake3::hash(input.as_bytes());

0 commit comments

Comments
 (0)