-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove spacetimedb-core as a dep of cli
- Loading branch information
1 parent
1307a51
commit 621fba2
Showing
16 changed files
with
191 additions
and
73 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "spacetimedb-auth" | ||
version.workspace = true | ||
edition.workspace = true | ||
rust-version.workspace = true | ||
|
||
[dependencies] | ||
spacetimedb-lib.workspace = true | ||
|
||
anyhow.workspace = true | ||
serde.workspace = true | ||
serde_with.workspace = true | ||
jsonwebtoken.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
pub use jsonwebtoken::errors::Error as JwtError; | ||
pub use jsonwebtoken::errors::ErrorKind as JwtErrorKind; | ||
pub use jsonwebtoken::{DecodingKey, EncodingKey}; | ||
use serde::{Deserialize, Serialize}; | ||
use spacetimedb_lib::Identity; | ||
use std::time::SystemTime; | ||
|
||
// These are the claims that can be attached to a request/connection. | ||
#[serde_with::serde_as] | ||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct SpacetimeIdentityClaims { | ||
#[serde(rename = "hex_identity")] | ||
pub identity: Identity, | ||
#[serde(rename = "sub")] | ||
pub subject: String, | ||
#[serde(rename = "iss")] | ||
pub issuer: String, | ||
#[serde(rename = "aud")] | ||
pub audience: Vec<String>, | ||
|
||
/// The unix timestamp the token was issued at | ||
#[serde_as(as = "serde_with::TimestampSeconds")] | ||
pub iat: SystemTime, | ||
#[serde_as(as = "Option<serde_with::TimestampSeconds>")] | ||
pub exp: Option<SystemTime>, | ||
} | ||
|
||
// IncomingClaims are from the token we receive from the client. | ||
// The signature should be verified already, but further validation is needed to have a SpacetimeIdentityClaims2. | ||
#[serde_with::serde_as] | ||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct IncomingClaims { | ||
#[serde(rename = "hex_identity")] | ||
pub identity: Option<Identity>, | ||
#[serde(rename = "sub")] | ||
pub subject: String, | ||
#[serde(rename = "iss")] | ||
pub issuer: String, | ||
#[serde(rename = "aud", default)] | ||
pub audience: Vec<String>, | ||
|
||
/// The unix timestamp the token was issued at | ||
#[serde_as(as = "serde_with::TimestampSeconds")] | ||
pub iat: SystemTime, | ||
#[serde_as(as = "Option<serde_with::TimestampSeconds>")] | ||
pub exp: Option<SystemTime>, | ||
} | ||
|
||
impl TryInto<SpacetimeIdentityClaims> for IncomingClaims { | ||
type Error = anyhow::Error; | ||
|
||
fn try_into(self) -> anyhow::Result<SpacetimeIdentityClaims> { | ||
// The issuer and subject must be less than 128 bytes. | ||
if self.issuer.len() > 128 { | ||
return Err(anyhow::anyhow!("Issuer too long: {:?}", self.issuer)); | ||
} | ||
if self.subject.len() > 128 { | ||
return Err(anyhow::anyhow!("Subject too long: {:?}", self.subject)); | ||
} | ||
// The issuer and subject must be non-empty. | ||
if self.issuer.is_empty() { | ||
return Err(anyhow::anyhow!("Issuer empty")); | ||
} | ||
if self.subject.is_empty() { | ||
return Err(anyhow::anyhow!("Subject empty")); | ||
} | ||
|
||
let computed_identity = Identity::from_claims(&self.issuer, &self.subject); | ||
// If an identity is provided, it must match the computed identity. | ||
if let Some(token_identity) = self.identity { | ||
if token_identity != computed_identity { | ||
return Err(anyhow::anyhow!( | ||
"Identity mismatch: token identity {:?} does not match computed identity {:?}", | ||
token_identity, | ||
computed_identity, | ||
)); | ||
} | ||
} | ||
|
||
Ok(SpacetimeIdentityClaims { | ||
identity: computed_identity, | ||
subject: self.subject, | ||
issuer: self.issuer, | ||
audience: self.audience, | ||
iat: self.iat, | ||
exp: self.exp, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod identity; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.