Skip to content

Commit c80ac15

Browse files
committed
Add helper function
1 parent 54a9e7e commit c80ac15

1 file changed

Lines changed: 14 additions & 9 deletions

File tree

crates/client-api/src/auth.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,19 @@ pub struct SpacetimeAuth {
8989
pub jwt_payload: String,
9090
}
9191

92+
impl SpacetimeAuth {
93+
pub fn new(creds: SpacetimeCreds, claims: SpacetimeIdentityClaims) -> Result<Self, anyhow::Error> {
94+
let payload = creds
95+
.extract_jwt_payload_string()
96+
.ok_or_else(|| anyhow!("Failed to extract JWT payload"))?;
97+
Ok(Self {
98+
creds,
99+
claims,
100+
jwt_payload: payload,
101+
})
102+
}
103+
}
104+
92105
impl From<SpacetimeAuth> for ConnectionAuthCtx {
93106
fn from(auth: SpacetimeAuth) -> Self {
94107
ConnectionAuthCtx {
@@ -176,15 +189,7 @@ impl SpacetimeAuth {
176189
let (claims, token) = claims.encode_and_sign(ctx.jwt_auth_provider()).map_err(log_and_500)?;
177190
let creds = SpacetimeCreds::from_signed_token(token);
178191
// Pulling out the payload should never fail, since we just made it.
179-
let payload = creds
180-
.extract_jwt_payload_string()
181-
.ok_or_else(|| log_and_500("internal error"))?;
182-
183-
Ok(Self {
184-
creds,
185-
claims,
186-
jwt_payload: payload,
187-
})
192+
Self::new(creds, claims).map_err(log_and_500)
188193
}
189194

190195
/// Get the auth credentials as headers to be returned from an endpoint.

0 commit comments

Comments
 (0)